我需要更改矩阵的编号方案。说,
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
中有一个很好的方法答案 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
来验证b
和a
是相同数据的观看次数:
b