您的位置:首页 > 编程语言 > Python开发

numpy.reshape

2016-03-04 16:00 465 查看
numpy.reshape(a, newshape, order=’C’)

官方解释:Gives a new shape to an array without changing its data.

看几个例子

[code]l=range(120)
import numpy as np
arr=np.array(l)
arr=arr.reshape(2,4,5,3)


arr为:

[[[[ 0, 1, 2],

[ 3, 4, 5],

[ 6, 7, 8],

[ 9, 10, 11],

[ 12, 13, 14]],

[code]    [[ 15,  16,  17],
     [ 18,  19,  20],
     [ 21,  22,  23],
     [ 24,  25,  26],
     [ 27,  28,  29]],

    [[ 30,  31,  32],
     [ 33,  34,  35],
     [ 36,  37,  38],
     [ 39,  40,  41],
     [ 42,  43,  44]],

    [[ 45,  46,  47],
     [ 48,  49,  50],
     [ 51,  52,  53],
     [ 54,  55,  56],
     [ 57,  58,  59]]],

   [[[ 60,  61,  62],
     [ 63,  64,  65],
     [ 66,  67,  68],
     [ 69,  70,  71],
     [ 72,  73,  74]],

    [[ 75,  76,  77],
     [ 78,  79,  80],
     [ 81,  82,  83],
     [ 84,  85,  86],
     [ 87,  88,  89]],

    [[ 90,  91,  92],
     [ 93,  94,  95],
     [ 96,  97,  98],
     [ 99, 100, 101],
     [102, 103, 104]],

    [[105, 106, 107],
     [108, 109, 110],
     [111, 112, 113],
     [114, 115, 116],
     [117, 118, 119]]]]


[code]arr_r = arr.reshape(2,4,3,5)


arr_r为:

[[[[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[ 10, 11, 12, 13, 14]],

[code]    [[ 15,  16,  17,  18,  19],
     [ 20,  21,  22,  23,  24],
     [ 25,  26,  27,  28,  29]],

    [[ 30,  31,  32,  33,  34],
     [ 35,  36,  37,  38,  39],
     [ 40,  41,  42,  43,  44]],

    [[ 45,  46,  47,  48,  49],
     [ 50,  51,  52,  53,  54],
     [ 55,  56,  57,  58,  59]]],

   [[[ 60,  61,  62,  63,  64],
     [ 65,  66,  67,  68,  69],
     [ 70,  71,  72,  73,  74]],

    [[ 75,  76,  77,  78,  79],
     [ 80,  81,  82,  83,  84],
     [ 85,  86,  87,  88,  89]],

    [[ 90,  91,  92,  93,  94],
     [ 95,  96,  97,  98,  99],
     [100, 101, 102, 103, 104]],

    [[105, 106, 107, 108, 109],
     [110, 111, 112, 113, 114],
     [115, 116, 117, 118, 119]]]]


可见在跑一趟好哦你中,具有和C++一样的RowMajor性质。

注意arr.transpose()与arr.reshape()的不同:transpose只是转动一下坐标轴,每个轴多长并不变,只是换了个方向;而reshape()会将元素以RowMajor准则对元素进行重新排列。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: