Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy直方图索引_Python_Numpy_Scipy - Fatal编程技术网

Python numpy直方图索引

Python numpy直方图索引,python,numpy,scipy,Python,Numpy,Scipy,考虑到我有一个3D直方图,或者为了简单起见,我有一个3D numpy形状数组(X,Y,Z) 使用numpy或scipy获取满足球体条件的数组值索引的最佳方法是什么 (index_x**2 + index_y**2 + index_z**2) <= radius**2 (index\ux**2+index\uy**2+index\uz**2)您可以首先使用ogrid()高效地获取索引,然后使用非零()获取满足条件的索引 使用()可以获得索引,如下所示: 或者,对于输入数据数组的任意形状:

考虑到我有一个3D直方图,或者为了简单起见,我有一个3D numpy形状数组(X,Y,Z)

使用numpy或scipy获取满足球体条件的数组值索引的最佳方法是什么

(index_x**2 + index_y**2 + index_z**2) <= radius**2

(index\ux**2+index\uy**2+index\uz**2)您可以首先使用
ogrid()
高效地获取索引,然后使用
非零()
获取满足条件的索引

使用()可以获得索引,如下所示:

或者,对于输入
数据
数组的任意形状:

x, y, z = ogrid[tuple(slice(None, dim) for dim in data.shape)]

为了使@EOL nice方法更通用,可以在数组的形状中定义一个中心

array = np.random.random((100,100,100))
center = (30,10,25)
radius = 5.0
x, y, z = np.ogrid[-center[0]:array.shape[0]-center[0],-center[1] :array.shape[1]-center[1], -center[2]:array.shape[2]-center[2]]
indexes = numpy.transpose((x**2+y**2+z**2 <= radius**2).nonzero())
array=np.random.random((100100))
中心=(30,10,25)
半径=5.0
x、 y,z=np.ogrid[-center[0]:array.shape[0]-center[0],-center[1]:array.shape[1]-center[1],-center[2]:array.shape[2]-center[2]]

index=numpy.transpose((x**2+y**2+z**2)太棒了……我曾想过使用ogrid,但不知道它可以在多维空间工作,非常感谢
indexes = numpy.transpose((x**2+y**2+z**2 <= radius**2).nonzero())  # transpose() might be unnecessary: it depends on your needs
x, y, z = numpy.ogrid[:100, :100, :100]
x, y, z = ogrid[tuple(slice(None, dim) for dim in data.shape)]
array = np.random.random((100,100,100))
center = (30,10,25)
radius = 5.0
x, y, z = np.ogrid[-center[0]:array.shape[0]-center[0],-center[1] :array.shape[1]-center[1], -center[2]:array.shape[2]-center[2]]
indexes = numpy.transpose((x**2+y**2+z**2 <= radius**2).nonzero())