简体   繁体   English

np.concatenate 用于可变维度数组

[英]np.concatenate for variable dimension array

c = np.array([[2,2],[2]])
d = np.array([[3,3],[3]])
res=np.concatenate((c,d),axis=1)

I tried concatenating c and d using np.concatenate but it gives me an error due to variable dimensions.我尝试使用 np.concatenate 连接 c 和 d 但由于尺寸可变,它给了我一个错误。

numpy.AxisError: axis 1 is out of bounds for array of dimension 1

I want to concatenate c and d to give:我想连接 c 和 d 给:

res=np.array([[2,3],[2,3]],[[2,3]])

How can I get this result using numpy library functions?如何使用 numpy 库函数获得此结果? Thanks in Advance:)提前致谢:)

Code:代码:

[np.dstack((res[i], res[i+2]))[0] for i in range(len(np.concatenate((c,d))[:2]))]

Output: Output:

 [array([[2, 3],
        [2, 3]]),
 array([[2, 3]])]

Did you get the warning when you created the arrays?您在创建 arrays 时收到警告了吗? Did you look at the arrays?你看过 arrays 吗?

In [183]: c = np.array([[2,2],[2]])
     ...: d = np.array([[3,3],[3]])
C:\Users\paul\AppData\Local\Temp\ipykernel_6304\2700245180.py:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  c = np.array([[2,2],[2]])
C:\Users\paul\AppData\Local\Temp\ipykernel_6304\2700245180.py:2: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  d = np.array([[3,3],[3]])

In [184]: c
Out[184]: array([list([2, 2]), list([2])], dtype=object)

In [185]: c.shape
Out[185]: (2,)

That is 1d, so of course np.concatenate will complain if you specify axis 1!那是 1d,所以如果您指定轴 1, np.concatenate当然会抱怨! It can only use axis 0它只能使用轴 0

In [186]: np.concatenate((c,d))
Out[186]: array([list([2, 2]), list([2]), list([3, 3]), list([3])], dtype=object)

making a (4,) shape array.制作 (4,) 形状数组。

stack is a variant that can join arrays on a new axis: stack是一种变体,可以在新轴上加入 arrays:

In [188]: np.stack((c,d))
Out[188]: 
array([[list([2, 2]), list([2])],
       [list([3, 3]), list([3])]], dtype=object)

In [189]: np.stack((c,d),axis=1)
Out[189]: 
array([[list([2, 2]), list([3, 3])],
       [list([2]), list([3])]], dtype=object)

Look at your desired result看看你想要的结果

In [191]: res=np.array(([[2,3],[2,3]],[[2,3]]),object)
In [192]: res
Out[192]: array([list([[2, 3], [2, 3]]), list([[2, 3]])], dtype=object)

(this too is (2,) shape; note it handles the 1 element lists different from the 2 element ones). (这也是 (2,) 形状;注意它处理与 2 元素列表不同的 1 元素列表)。

Compare that to what we get with a plain list "transpose":将其与我们使用普通列表“转置”得到的结果进行比较:

In [193]: list(zip(c,d))
Out[193]: [([2, 2], [3, 3]), ([2], [3])]

Wrapped in np.array , that makes a (2,2) object dtype.包裹在np.array中,生成 (2,2) object dtype。

Redefining c to contain a list and a number:重新定义c以包含一个列表和一个数字:

c1 = np.array([[2,2],2],object)

In [212]: np.stack((c1,d1),axis=1)
Out[212]: 
array([[list([2, 2]), list([3, 3])],
       [2, 3]], dtype=object)

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

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