组合两个不同维度的numpy数组

2024-04-27 00:23:09 发布

您现在位置:Python中文网/ 问答频道 /正文

当我阅读numpy教程时,我给自己一些挑战来建立我的理解。我在通读教程点.com的numpy资源,当我在上一个示例中看到他们的最终产品修改的数组不是真正的数组时。你知道吗

页面底部,广播迭代示例: https://www.tutorialspoint.com/numpy/numpy_iterating_over_array.htm

所以我决定尝试创建与数组相同的最终产品将是一个很好的挑战。我成功了,但我不能使用np.nditer公司我也不能利用广播,尽管我确信一定有办法利用其中一个/两个。你知道吗

这是我的密码:

a = np.arange(0,60,5) 
a = a.reshape(12,1)
b = np.arange(1,5)
arr = np.zeros((12,2))

counter = 0
for i in range(arr.shape[0]):
    if counter < 4:
        arr[i,:] = np.array([a[i],b[counter]])
        counter += 1
    else:
        counter = 0
        arr[i,:] = np.array([a[i],b[counter]])

print arr

我怎样才能更有效地做到这一点?你知道吗


Tags: httpsnumpycom利用示例产品npcounter
1条回答
网友
1楼 · 发布于 2024-04-27 00:23:09

我以前没有看过那种特别的nditer教程。
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.nditer.html

是我用过的那个。我一直告诉人们,nditer,使用这个Python接口,效率不高。这个页面作为在C代码中使用nditer的垫脚石非常有用,如上一个cython示例所示。你知道吗

使用np.nditer(在Python代码中)的numpy函数并不多。np.ndindex是为数不多的。值得一读它的代码。np.einsum使用这个迭代器,但是在编译代码中。你知道吗

稍后我将花时间阅读并评论这个例子。学会使用广播比使用nditer更重要。你知道吗

In [212]: a=np.arange(0,60,5).reshape(3,4)
In [213]: a
Out[213]: 
array([[ 0,  5, 10, 15],
       [20, 25, 30, 35],
       [40, 45, 50, 55]])
In [214]: b=np.arange(1,5)
In [215]: b
Out[215]: array([1, 2, 3, 4])

In [225]: for x,y in np.nditer([a,b]):
     ...:     print("%d:%d"%(x,y), end=' ')
     ...: print()
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4 

等价的纯Python迭代:

In [231]: for row in a:
     ...:     for x,y in zip(row,b):
     ...:         print("%d:%d"%(x,y), end=' ')
     ...: print()
     ...: 
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4 

np.broadcast将用(4,)广播(3,4)数组:

In [234]: np.broadcast(a,b)
Out[234]: <numpy.broadcast at 0x9c2a7f8>
In [235]: list(_)
Out[235]: 
[(0, 1),
 (5, 2),
 (10, 3),
 (15, 4),
 (20, 1),
 (25, 2),
 (30, 3),
 (35, 4),
 (40, 1),
 (45, 2),
 (50, 3),
 (55, 4)]

使用np.array(list(np.broadcast(a,b)))生成(12,2)数组。你知道吗

或使用相同的打印:

In [237]: for x,y in np.broadcast(a,b):
     ...:     print("%d:%d"%(x,y), end=' ')
     ...: print()
     ...: 
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4

您的迭代:

In [251]: arr = np.zeros((12,2),dtype=int)
     ...: counter = 0
     ...: for i in range(arr.shape[0]):
     ...:     if counter < 4:
     ...:         arr[i,:] = np.array([a.flat[i],b[counter]])
     ...:         counter += 1
     ...:     else:
     ...:         counter = 0
     ...:         arr[i,:] = np.array([a.flat[i],b[counter]])
     ...:         
In [252]: arr
Out[252]: 
array([[ 0,  1],
       [ 5,  2],
       [10,  3],
       [15,  4],
       [20,  1],
       [25,  1],
       [30,  2],
       [35,  3],
       [40,  4],
       [45,  1],
       [50,  1],
       [55,  2]])

哦,如果您希望第二列是一个重复的b,那么看起来有点不对劲。你知道吗

有许多方法可以将ab组合到这种数组中。你知道吗

这将2d a转换为1d;用tile复制b,并用stack连接它们(column_stack也可以):

In [264]: np.stack((a.flat, np.tile(b,3)),1)
Out[264]: 
array([[ 0,  1],
       [ 5,  2],
       [10,  3],
       [15,  4],
       [20,  1],
       [25,  2],
       [30,  3],
       [35,  4],
       [40,  1],
       [45,  2],
       [50,  3],
       [55,  4]])

相关问题 更多 >