简体   繁体   English

使用 Numpy 在空数组中插入行

[英]Inserting rows in an empty array using Numpy

Hello im currently trying to convert this array你好,我目前正在尝试转换这个数组

test=np.array([[0,0],[0,1],[1,1],[3,0]])

and using Manhattan distance to convert this array into this shape并使用曼哈顿距离将此数组转换为此形状

[0., 1., 2., 3.] 
[1., 0., 1., 4.]
[2., 1., 0., 3.,
[3., 4., 3., 0.]

the code goes like this代码是这样的

list_x=[]
newarray=np.array([])
length=len(test)
for i in range(length):
    for j in range(length):
        print('i=',i)
        print('j=',j)
        var=sum(abs(a-b) for a,b in zip(test[i],test[j]))
        list_x.append(var)
    newarray= np.append(newarray,list_x,axis = 0) 
    list_x=[]
    

but the outcome of the code keeps giving me this:但代码的结果不断给我这个:

array([0., 1., 2., 3., 1., 0., 1., 4., 2., 1., 0., 3., 3., 4., 3., 0.])

is there a problem in my np.append() that prevent to convert it to 4*4 shap array ?我的 np.append() 中是否存在阻止将其转换为 4*4 shap 数组的问题?

You could go with scikit-learn's manhattan_distances to obtain all pairwise manhattan distances simplifying the above to a single function call:您可以使用 scikit-learn 的manhattan_distances来获取所有成对的曼哈顿距离,将上述简化为单个函数调用:

from sklearn.metrics.pairwise import manhattan_distances

manhattan_distances(test)
array([[0., 1., 2., 3.],
       [1., 0., 1., 4.],
       [2., 1., 0., 3.],
       [3., 4., 3., 0.]])

If you wanted to obtain the distance with a for loop, I'd suggest you to use python lists instead.如果您想使用 for 循环获取距离,我建议您改用 python 列表。 In order to end up with a nested list, generate an inner list with the distances of a row to the others, and append it to an outer list on each iteration:为了得到一个嵌套列表,生成一个内部列表,其中包含一行与其他行的距离,并在每次迭代时将其附加到外部列表:

out=[]
for i in range(length):
    new_row = []
    for j in range(length):
        var=sum(abs(a-b) for a,b in zip(test[i],test[j]))
        new_row.append(var)
    out.append(new_row)

print(out)
# [[0, 1, 2, 3], [1, 0, 1, 4], [2, 1, 0, 3], [3, 4, 3, 0]]

Another way of doing it is using Scipy:另一种方法是使用 Scipy:

from scipy.spatial.distance import cdist
cdist(test,test,'cityblock')

output:输出:

[[0. 1. 2. 3.]
 [1. 0. 1. 4.]
 [2. 1. 0. 3.]
 [3. 4. 3. 0.]]

Comparison :比较

#@ehsan's solution
def m1(test):
  return cdist(test,test,'cityblock')

#@yatu's solution
def m2(test):
  return manhattan_distances(test)

in_ = [np.random.randint(0,10,(n,2)) for n in [10,100,1000,10000]]

For large arrays they seem to have similar performance, but for smaller array (around 1000 rows) m1 seems faster.对于大型阵列,它们似乎具有相似的性能,但对于较小的阵列(大约 1000 行), m1似乎更快。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM