使用小型 2 数组:
In [404]: x = np.arange(4).reshape(2,2)
reshape
阶 F 是 MATLAB(:)
索引的最直接等价物,产生一个 (n,1) 数组。(是x(:).'
(1,n) 矩阵的语法吗?)
In [405]: x1 = x.reshape((4,1),order='F')
In [406]: x
Out[406]:
array([[0, 1],
[2, 3]])
In [407]: x1
Out[407]:
array([[0],
[2],
[1],
[3]])
加入两个这样的“列向量”很容易:
In [408]: np.concatenate((x1,x1), axis=1)
Out[408]:
array([[0, 0],
[2, 2],
[1, 1],
[3, 3]])
np.stack
是一个concatenate
创建新维度并加入其中的版本。和axis=0
它一样np.array((x,x))
In [409]: np.stack((x,x), axis=2)
Out[409]:
array([[[0, 0],
[1, 1]],
[[2, 2],
[3, 3]]])
一个 order F reshape 像以前一样创建 2 列数组:
In [411]: np.stack((x,x), axis=2).reshape((-1,2),order='F')
Out[411]:
array([[0, 0],
[2, 2],
[1, 1],
[3, 3]])
或使用默认顺序:
In [412]: np.stack((x,x), axis=2).reshape((-1,2))
Out[412]:
array([[0, 0],
[1, 1],
[2, 2],
[3, 3]])
numpy
是一个 Python 包,使用函数、索引和方法。它不会改变或添加到基本的 Python 语法。