Numpy 有一个强大之处在于可以很方便的修改生成的N维数组的形状。
数组具有由沿着每个轴的元素数量给出的形状:
a = np.floor(10*np.random.random((3,4)))
a
Out[181]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
a.shape
Out[182]: (3, 4)
上面生成了一个 3x4 的数组,现在对它进行形状的改变。
a.ravel()
Out[184]: array([6., 0., 2., 1., 5., 2., 8., 2., 8., 4., 8., 4.])
a.reshape(2,6)
Out[185]:
array([[6., 0., 2., 1., 5., 2.],
[8., 2., 8., 4., 8., 4.]])
a.T
Out[186]:
array([[6., 5., 8.],
[0., 2., 4.],
[2., 8., 8.],
[1., 2., 4.]])
a.shape
Out[187]: (3, 4)
无论是ravel、reshape、T,它们都不会更改原有的数组形状,都是返回一个新的数组。
使用 resize 方法可以直接修改数组本身:
a
Out[188]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
a.resize(2,6)
a
Out[190]:
array([[6., 0., 2., 1., 5., 2.],
[8., 2., 8., 4., 8., 4.]])
技巧:在使用 reshape 时,可以将其中的一个维度指定为 -1,Numpy 会自动计算出它的真实值
a.reshape(3, -1)
Out[191]:
array([[6., 0., 2., 1.],
[5., 2., 8., 2.],
[8., 4., 8., 4.]])
除了可以对单个数组的形状进行转换外,还可以把多个数据进行堆叠。
a = np.floor(10*np.random.random((2,2)))
a
Out[192]:
array([[0., 3.],
[1., 9.]])
b = np.floor(10*np.random.random((2,2)))
b
Out[193]:
array([[2., 8.],
[9., 7.]])
np.hstack((a,b))
Out[194]:
array([[0., 3., 2., 8.],
[1., 9., 9., 7.]])
对于2D数组来说,使用hstack和column_stack 效果一样,对于1D数组来说,column_stack 会将1D数组作为列堆叠到2D数组中:
from numpy import newaxis
np.column_stack((a,b))
Out[195]:
array([[0., 3., 2., 8.],
[1., 9., 9., 7.]])
a = np.array([4.,2.])
b = np.array([3.,8.])
np.column_stack((a,b))
Out[196]:
array([[4., 3.],
[2., 8.]])
np.hstack((a,b)) # 一维数组的情况下,column_stack和hstack结果不一样
Out[197]: array([4., 2., 3., 8.])
a[:,newaxis]
Out[198]:
array([[4.],
[2.]])
np.column_stack((a[:,newaxis],b[:,newaxis]))
Out[199]:
array([[4., 3.],
[2., 8.]])
np.hstack((a[:,newaxis],b[:,newaxis])) # 二维数组的情况下,column_stack和hstack结果一样
Out[200]:
array([[4., 3.],
[2., 8.]])
另一方面,对于任何输入数组,函数row_stack等效于vstack。一般来说,对于具有两个以上维度的数组,hstack沿第二轴堆叠,vstack沿第一轴堆叠,concatenate允许一个可选参数,给出串接应该发生的轴。
既然可以将多个数组进行对堆叠,自然也可以将一个数组拆分成多个小数组。
使用hsplit,可以沿其水平轴拆分数组,通过指定要返回的均匀划分的数组数量,或通过指定要在其后进行划分的列:
from pprint import pprint
a = np.floor(10*np.random.random((2,12)))
a
Out[201]:
array([[8., 5., 5., 7., 3., 5., 8., 6., 2., 8., 9., 0.],
[5., 8., 0., 0., 9., 0., 7., 5., 3., 9., 4., 8.]])
pprint(np.hsplit(a,3)) #水平切成三等分
[array([[8., 5., 5., 7.],
[5., 8., 0., 0.]]),
array([[3., 5., 8., 6.],
[9., 0., 7., 5.]]),
array([[2., 8., 9., 0.],
[3., 9., 4., 8.]])]
vsplit沿垂直轴分割,array_split允许指定沿哪个轴分割。
原文:https://www.cnblogs.com/zhangyafei/p/10567323.html