shape和reshape

import numpy as np
a = np.array([1,2,3,4,5,6,7,8])  #一维数组
print(a.shape[0])  #值为8,因为有8个数据
print(a.shape[1])  #IndexError: tuple index out of range

a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
print(a.shape[1])  #值为4,内层矩阵有4个元素。
print(a.shape[2])  #IndexError: tuple index out of range

a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]) #一维数组
print(a.reshape(2,8))#建立二维数组,两行八列
print(a.reshape((2,2,2,2)))#建立四维数组
print(a.reshape((2,2,2,2)))#建立四维数组
print(a.reshape((2,4,2)))#建立三维数组



 

图像重建数组

image = np.array([[[ 0.67826139, 0.29380381],
[ 0.90714982, 0.52835647],
[ 0.4215251 , 0.45017551]],

[[ 0.92814219, 0.96677647],
[ 0.85304703, 0.52351845],
[ 0.19981397, 0.27417313]],

[[ 0.60659855, 0.00533165],
[ 0.10820313, 0.49978937],
[ 0.34144279, 0.94630077]]])

三维数组相当于(3,3,2)

image.shape[0]=3最外层

image.shape[1]=3次外层

image.shape[2]=2最里层

3*3*2=16

经过 v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)   

变成 16行,1列

image2vector(image) = 
[[0.67826139] [0.29380381] [0.90714982] [0.52835647] [0.4215251 ] [0.45017551] [0.92814219] [0.96677647] [0.85304703] [0.52351845] [0.19981397] [0.27417313] [0.60659855] [0.00533165] [0.10820313] [0.49978937] [0.34144279] [0.94630077]]

原文地址:https://www.cnblogs.com/cmybky/p/11918699.html