Numpy开关从列编号到行

时间:2015-09-04 22:13:35

标签: python numpy

我需要更改矩阵的编号方案。说,

import numpy as np
a = np.arange(6).reshape(3,2)
array([[0, 1],
       [2, 3],
       [4, 5]])

我想将其切换为

b = np.array([[0,3],[1,4],[2,5]])
array([[0, 3],
       [1, 4],
       [2, 5]])

所以基本上我首先通过行对矩阵进行编号。我确信在numpy

中有一个很好的方法

2 个答案:

答案 0 :(得分:5)

>>> import numpy as np
>>> np.arange(6).reshape(3,2, order = 'F')
>>> array([[0, 3],
   [1, 4],
   [2, 5]])

来自doc:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

使用order =' F'指定Fortran传统表示和顺序=' C' (默认值)使用传统的C表示。

答案 1 :(得分:2)

要使用新形状创建相同数据的视图,您可以使用$ a=atext $ b=btext $ var1="$a\n$b" # Assign with literal "\" and "n" characters $ printf "$var1" # Here, printf changes the "\n" into the newline atext btext $ printf '%s' "$var1" # ...but this form shows that the "\n" are really there atext\nbtext $ var2="$a"$'\n'"$b" # now, we put a single newline in the string $ printf '%s' "$var2" # and now even accurate use of printf shows that newline atext btext

a.T.reshape(3, 2, order='F')

通过更改In [35]: a = np.arange(6).reshape(3,2) In [36]: a Out[36]: array([[0, 1], [2, 3], [4, 5]]) In [37]: b = a.T.reshape(3, 2, order='F') In [38]: b Out[38]: array([[0, 3], [1, 4], [2, 5]]) 并检查a来验证ba是相同数据的观看次数:

b