Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于2x2x2矩阵,为什么整形(4,2)与整形(2,-1).T不同_Python_Numpy - Fatal编程技术网

Python 对于2x2x2矩阵,为什么整形(4,2)与整形(2,-1).T不同

Python 对于2x2x2矩阵,为什么整形(4,2)与整形(2,-1).T不同,python,numpy,Python,Numpy,我尝试了以下代码片段 a = np.array([[[1, 2], [3, 4]],[[5, 6], [7, 8]]]) b = a.reshape(2,-1).T c = a.reshape(4,2) 我以为b和c是一样的,因为a被重塑成4x2矩阵。但事实并非如此。这是b和c [[1 5] [2 6] [3 7] [4 8]] [[1 2] [3 4] [5 6] [7 8]] 为什么安排会改变?这与您正在执行的操作有关 >>> a = np.array([

我尝试了以下代码片段

a = np.array([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])
b = a.reshape(2,-1).T
c = a.reshape(4,2)
我以为b和c是一样的,因为a被重塑成4x2矩阵。但事实并非如此。这是b和c

[[1 5]
 [2 6]
 [3 7]
 [4 8]]

[[1 2]
 [3 4]
 [5 6]
 [7 8]]

为什么安排会改变?

这与您正在执行的操作有关

>>> a = np.array([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])
>>> a
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
这将得到您想要的4,2形状

>>> a.reshape(4,2)
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])
这将得到2行4列

>>> a.reshape(2,-1)
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
应用.T运算符可将行切换为列:

>>> a.reshape(2,-1).T
array([[1, 5],
       [2, 6],
       [3, 7],
       [4, 8]])
若要使用-1将尺寸重塑为4,2阵列,需要将其置于第一个位置以实现4,2阵列:

>>> a.reshape(-1,2)
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

b
中,它被塑造成一个2x4矩阵,这与4x2矩阵不同Transpose改变了有效的内存布局,而重塑则没有。