Numpy中的shape和reshape()

shape是查看数据有多少行多少列
reshape()是数组array中的方法,做用是将数据从新组织数组

1.shapespa

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

2.reshape()  是数组对象中的方法,用于改变数组的形状。
在这里插入图片描述code

形状变化是基于数组元素不能改变的,变成的新形状中所包含的元素个数必须符合原来元素个数。若是数组元素发生变化的时候,就会报错:对象

reshape新生成数组和原数组公用一个内存,无论改变哪一个都会互相影响。
在这里插入图片描述blog

相关文章
相关标签/搜索