白云岛资源网 Design By www.pvray.com

numpy库对多维数组有非常灵巧的处理方式,主要的处理方法有:

.reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变

.resize(shape) : 与.reshape()功能一致,但修改原数组

In [22]: a = np.arange(20)
#原数组不变
In [23]: a.reshape([4,5])
Out[23]:
array([[ 0, 1, 2, 3, 4],
    [ 5, 6, 7, 8, 9],
    [10, 11, 12, 13, 14],
    [15, 16, 17, 18, 19]])

In [24]: a
Out[24]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19])

#修改原数组
In [25]: a.resize([4,5])

In [26]: a
Out[26]:
array([[ 0, 1, 2, 3, 4],
    [ 5, 6, 7, 8, 9],
    [10, 11, 12, 13, 14],
    [15, 16, 17, 18, 19]])

.swapaxes(ax1,ax2) : 将数组n个维度中两个维度进行调换,不改变原数组

In [27]: a.swapaxes(1,0)
Out[27]:
array([[ 0, 5, 10, 15],
    [ 1, 6, 11, 16],
    [ 2, 7, 12, 17],
    [ 3, 8, 13, 18],
    [ 4, 9, 14, 19]])

.flatten() : 对数组进行降维,返回折叠后的一维数组,原数组不变

In [29]: a.flatten()
Out[29]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19])
白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com