如何迭代地将新列添加到预先存在的numpy数组

时间:2019-12-20 17:14:53

标签: arrays numpy

我有一组在x,y坐标中成对的n点。我想根据一些排除标准(与哪一个无关)将一对选定的x,y点存储到一个新数组中。

点存储在一个数组中,该数组由两行(x和y)和多列(点束)组成。我首先使用numpy.empty([2,1])初始化一个空数组(我们称其为“存储”),该数组具有2行和1列。接下来,我开始在列数+1的范围内进行迭代,将x分别设为[0,n],将y分别设为[1,n]-因此x始终是第n列和第一行的元素而y是第n列和第二行的元素。然后是排除标准(此时无关紧要)。之后,我定义了一个新的数组(称为“成员”),其中包含一列和两行(x,y)。然后,我用numpy.append(store,member,1)(1表示我可以按列追加。)定义了一个新数组(我们称其为“ new”)。代码如下所示:

points=np.array([ [1,2,3], [4,5,6] ])
store=np.empty([2,1])

这是我的点集和目标数组存储。

for i in range(n):
  x=points[0,i]
  y=points[1,i]
  member=np.array([ [x], [y] ])
  new=np.append(store,member,1)

而且效果不佳。有什么更好的方法吗?

3 个答案:

答案 0 :(得分:0)

是的,有更好的方法,您可以避免for循环并使用索引。

import numpy as np

points=np.array([ [1,2,3], [2,3,4] ])
value = 2 

indx = points <= value #criterion
indx = np.logical_and(indx[0,:], indx[1,:]) #if the criterion applies to both x and y
store = points[:,indx] #get each column that matches the criterion

答案 1 :(得分:0)

In [1]: points=np.array([ [1,2,3], [4,5,6] ]) 
   ...: store=np.empty([2,1])                                                   
In [2]: store                                                                   
Out[2]: 
array([[7.74860419e-304],
       [7.74860419e-304]])

np.empty产生的数组中的值是任意的。

带有轴值的

np.append只是np.concatenate的一个变体。我们可以轻松加入storepoints

In [3]: np.concatenate([store, points], axis=1)                                 
Out[3]: 
array([[7.74860419e-304, 1.00000000e+000, 2.00000000e+000,
        3.00000000e+000],
       [7.74860419e-304, 4.00000000e+000, 5.00000000e+000,
        6.00000000e+000]])

您的member计算:

In [4]: i=0                                                                     
In [5]:   x=points[0,i] 
   ...:   y=points[1,i] 
   ...:   member=np.array([ [x], [y] ])                                         
In [6]: member                                                                  
Out[6]: 
array([[1],
       [4]])
In [7]: points[:,[i]]        # same thing, the ith column of points                                                   
Out[7]: 
array([[1],
       [4]])

您可以将points的列迭代地添加到store数组中:

In [8]: store = np.ones([2,1],int)                                              
In [9]: store                                                                   
Out[9]: 
array([[1],
       [1]])
In [10]: store = np.concatenate([store, points[:,[0]]],1)                       
In [11]: store                                                                  
Out[11]: 
array([[1, 1],
       [1, 4]])
In [12]: store = np.concatenate([store, points[:,[1]]],1)                       
In [13]: store                                                                  
Out[13]: 
array([[1, 1, 2],
       [1, 4, 5]])
In [14]: store = np.concatenate([store, points[:,[2]]],1)                       
In [15]: store                                                                  
Out[15]: 
array([[1, 1, 2, 3],
       [1, 4, 5, 6]])

但是一般来说,使用numpy进行迭代工作很慢。更好地使用列表。

In [16]: store=[]            # empty list, no relation to np.empty                                                         
In [17]: for i in range(3): 
    ...:     store.append(points[:,i]) 
    ...:                                                                        
In [18]: store                                                                  
Out[18]: [array([1, 4]), array([2, 5]), array([3, 6])]
In [19]: np.array(store)                                                        
Out[19]: 
array([[1, 4],
       [2, 5],
       [3, 6]])
In [20]: np.array(store).T                                                      
Out[20]: 
array([[1, 2, 3],
       [4, 5, 6]])

答案 2 :(得分:0)

也许您正在寻找这样的东西:

points=np.array([ [1,2,3], [4,5,6] ])
store=np.empty([2,1])

store=np.hstack((store, points))