numpy.reshape()

numpy.reshape()

import numpy as np

if __name__ == '__main__':
    a = [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
    print('把最后一维改为6个元素一组')
    b = np.reshape(a, (-1, 6))
    print(b)
    print('把最后一维改为5个元素一组')
    b = np.reshape(a, (-1, 5))
    print(b)
    print('把他改成3*10')
    b = np.reshape(a, (3, 10))
    print(b)
    print('把他改成3*5*2')
    b = np.reshape(a, (3, 5, 2))
    print(b)

ans:

把最后一维改为6个元素一组
[[0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]]
把最后一维改为5个元素一组
[[0 1 2 3 4]
 [5 0 1 2 3]
 [4 5 0 1 2]
 [3 4 5 0 1]
 [2 3 4 5 0]
 [1 2 3 4 5]]
把他改成3*10
[[0 1 2 3 4 5 0 1 2 3]
 [4 5 0 1 2 3 4 5 0 1]
 [2 3 4 5 0 1 2 3 4 5]]
把他改成3*5*2
[[[0 1]
  [2 3]
  [4 5]
  [0 1]
  [2 3]]

 [[4 5]
  [0 1]
  [2 3]
  [4 5]
  [0 1]]

 [[2 3]
  [4 5]
  [0 1]
  [2 3]
  [4 5]]]
文章目录