我有一个形状为(30,480,640)的numpy ndarray,第1和第2轴代表位置(纬度和长度),第0轴包含实际数据点.我想在每个位置沿第0轴使用最频繁的值,是构造一个形状为(1,480,640).ie的新数组:
>>> data
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[40, 40, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
(perform calculation)
>>> new_data
array([[[ 0, 1, 2, 3, 4],
[ 5, …
Run Code Online (Sandbox Code Playgroud) 是否有另一种方法在numpy中实现scipy.stats.mode函数以获取沿轴的ndarrays中最常见的值?(不导入其他模块)即
import numpy as np
from scipy.stats import mode
a = np.array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[40, 40, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
mode= mode(data, axis=0)
mode = …
Run Code Online (Sandbox Code Playgroud) 如何将多个numpy数组写入多个列中的一个csv文件?
import numpy
import csv
arrA = numpy.array(file.root.a)
arrB = numpy.array(file.root.b)
arrC = numpy.array(file.root.c)
for i in range (480):
for j in range (640):
(write arrA[i,j] into column1,write arrB[i,j] into column2,write arrC[i,j] into column3)
Run Code Online (Sandbox Code Playgroud)
非常感谢!
我有三个阵列:经度(400,600),纬度(400,600),数据(30,400,60); 我想要做的是根据它的位置(纬度和经度)提取数据数组中的值.
这是我的代码:
import numpy
import tables
hdf = "data.hdf5"
h5file = tables.openFile(hdf, mode = "r")
lon = numpy.array(h5file.root.Lonitude)
lat = numpy.array(h5file.root.Latitude)
arr = numpy.array(h5file.root.data)
lon = numpy.array(lon.flat)
lat = numpy.array(lat.flat)
arr = numpy.array(arr.flat)
lonlist=[]
latlist=[]
layer=[]
fre=[]
for i in range(0,len(lon)):
for j in range(0,30):
longi = lon[j]
lati = lat[j]
layers=[j]
frequency= arr[i]
lonlist.append(longi)
latlist.append(lati)
layer.append(layers)
fre.append(frequency)
output = numpy.column_stack((lonlist,latlist,layer,fre))
Run Code Online (Sandbox Code Playgroud)
问题是"频率"不是我想要的.我希望数据数组沿着零轴变平,因此"频率"将是一个位置的30个值.这样的函数在numpy中沿着特定的轴展平ndarray?