在 numpy 中,resize 是用来改变数组的元素个数,而 reshap 不改变元素个数,只是改变维度,比如说从一维变成二维。

import numpy as np
X=np.array([[1,2,3,4],
              [5,6,7,8],
              [9,10,11,12]])

X_new=np.resize(X,(3,3)) # do not change the original X
print("X:\n",X)  #original X
print("X_new:\n",X_new) # new X

>>
X:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
X_new:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
import numpy as np
X=np.array([1,2,3,4,5,6,7,8])

X_2=X.reshape((2,4)) #retuen a 2*4 2-dim array
X_3=X.reshape((2,2,2)) # retuen a 2*2*2 3-dim array

print("X:\n",X)
print("X_2:\n",X_2)
print("X_3:\n",X_3)

>>
X:
 [1 2 3 4 5 6 7 8]
X_2:
 [[1 2 3 4]
 [5 6 7 8]]
X_3:
 [[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

参考: https://blog.csdn.net/qq_24193303/article/details/80965274
https://blog.csdn.net/DocStorm/article/details/58593682

标签: python

添加新评论