[英]Appending multi-dimensional arrays of different shapes in Python
Arrays C1
and C2
have shapes (1, 2, 2)
and (1, 1, 2)
respectively.数组
C1
和C2
分别具有形状(1, 2, 2)
和(1, 1, 2)
。 I want to append these arrays into a new array C3
.我想将这些数组附加到一个新数组
C3
中。 But I am getting an error.但我收到一个错误。 The desired output is attached.
附加了所需的输出。
import numpy as np
arC1 = []
arC2 = []
C1=np.array([[[0, 1],[0,2]]])
arC1.append(C1)
C2=np.array([[[1,1]]])
C2.shape
arC2.append(C2)
C3=np.append(C1,C2,axis=0)
The error is错误是
<module>
C3=np.append(C1,C2,axis=0)
File "<__array_function__ internals>", line 5, in append
File "C:\Users\USER\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 4745, in append
return concatenate((arr, values), axis=axis)
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 1
The desired output is所需的输出是
C3[0]=np.array([[[0, 1], [0, 2]]])
C3[1]=np.array([[2,3]])
C1 = np.array([[[0, 1],[0,2]]])
print(C1.shape) # (1, 2, 2)
C2 = np.array([[[1,1]]])
print(C2.shape) # (1, 1, 2)
If you run如果你跑
C3 = np.append(C1,C2,axis=1)
print(C2.shape) # (1, 3, 2)
It means like this.意思是这样的。 (1, 2 , 2) + (1, 1 , 2) = (1, 3 , 2)
(1, 2 , 2) + (1, 1 , 2) = (1, 3 , 2)
If you run如果你跑
C3 = np.append(C1,C2,axis=0)
It means like this.意思是这样的。 ( 1 , 2, 2) + ( 1 , 1, 2) = ( 2 , ?? , 2)
( 1 , 2, 2) + ( 1 , 1, 2) = ( 2 , ?? , 2)
Since numpy is a matrix, it can't be added because the lower dimensions are different.由于numpy是一个矩阵,所以不能相加,因为低维不同。
Simply, when you set axis parameter value at append function for append two arrays, the values of all dimensions shall be the same except for the values of the dimensions corresponding to the axis简单来说,当你在append函数中设置axis参数值来追加两个数组时,除了轴对应的维度的值之外,所有维度的值都应该相同
C1 = np.array([[[[[0, 1]]],[[[0, 2]]], [[[0, 2]]]]])
C2 = np.array([[[[[1]]],[[[2]]], [[[3]]]]])
C3 = np.append(C1, C2, axis=4)
and if the dimensions are as follows,如果尺寸如下,
(1, 3, 1, 1, 2) # C1
(1, 3, 1, 1, 1) # C2
-->
(1, 3, 1, 1, 3) # C3
Only axis 4 dimmension is different so you can do np.append(C1, C2, axis=4)只有轴 4 尺寸不同,所以你可以做np.append(C1, C2, axis=4)
C1 = np.array([[[[[0, 1]]],[[[0, 2]]], [[[0, 2]]]]])
C2 = np.array([[[[[1, 1]]],[[[2, 2]]], [[[3, 3]]]], [[[[1, 1]]],[[[2, 2]]], [[[3, 3]]]]])
C3 = np.append(C1, C2, axis=0)
and if the dimensions are as follows,如果尺寸如下,
(1, 3, 1, 1, 2) # C1
(2, 3, 1, 1, 2) # C2
-->
(3, 3, 1, 1, 2) # C3
Only axis 0 dimmension is different so you can do np.append(C1, C2, axis=0)只有轴 0 尺寸不同,所以你可以做np.append(C1, C2, axis=0)
C1 = np.array([[[[[0, 1]]],[[[0, 2]]], [[[0, 2]]]]])
C2 = np.array([[[[[1]]],[[[2]]], [[[3]]]], [[[[1]]],[[[2]]], [[[3]]]]])
and if the dimensions are as follows,如果尺寸如下,
(1, 3, 1, 1, 2) # C1
(2, 3, 1, 1, 1) # C2
-->
ERROR
Except for the Axis value you specify, at least one dimension is more different.除了您指定的 Axis 值之外,至少有一个维度更加不同。 So it's impossible to set axis parameter with these arrays.
所以不可能用这些数组设置轴参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.