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_Python 3.x_Numpy - Fatal编程技术网

Python 跨numpy数组的多个索引执行相同的自定义操作

Python 跨numpy数组的多个索引执行相同的自定义操作,python,python-3.x,numpy,Python,Python 3.x,Numpy,以上是具有代表性的例子;实际点集可能有1000个点。因此,我想“矢量化”最后2行(用于循环) 是否有一种方法可以做到这一点,而不必在最后使用for循环,同时并行处理每个索引i 注意:由于x的邻域可以在每个i处具有不同数量的索引,因此该数组是dtypeobject,因此我无法直接使用它来选择y的元素,因此将x的邻域作为数组没有任何好处;它也可以是一个列表。除非你能想出一种方法来给每个x[i,:]相同数量的邻域,否则你只能一点一点地进行计算。为了将其矢量化,第一步可能是使x的邻域成为齐次ndarra

以上是具有代表性的例子;实际点集可能有1000个点。因此,我想“矢量化”最后2行(用于循环)

是否有一种方法可以做到这一点,而不必在最后使用for循环,同时并行处理每个索引
i


注意:由于x的
邻域可以在每个
i
处具有不同数量的索引,因此该数组是dtype
object
,因此我无法直接使用它来选择
y

的元素,因此将x的
邻域作为数组没有任何好处;它也可以是一个列表。除非你能想出一种方法来给每个
x[i,:]
相同数量的邻域,否则你只能一点一点地进行计算。为了将其矢量化,第一步可能是使
x的邻域成为齐次
ndarray
。您可以使用此解决方案,因此将
邻域\u of_x
设置为数组没有任何好处;它也可以是一个列表。除非你能想出一种方法来给每个
x[i,:]
相同数量的邻域,否则你只能一点一点地进行计算。为了将其矢量化,第一步可能是使
x的邻域成为齐次
ndarray
。你可以使用这个解决方案
import numpy as np
x = np.random.random((3,3)) #point-set 1
y = np.random.random((3,3)) #point-set 2

neighbors_of_x = np.array([[0,1],[0],[0]])
#^^ contains indices of neighbors of points in x, in y.
#each point can have different number of neighbors in y.

#Below, for every point in x, subtract from its neighbor in y
for i in range(x.shape[0]):
  print(x[i,:] - y[neighbors_of_x[i],:])