shape和reshape的应用

在numpy中,shape和reshape()函数很常用。二者的功能都是对于数组的形状进行操作。
shape函数可以了解数组的结构;reshape()函数可以对数组的结构进行改变。

 import numpy as np
  b = np.array([[1,2],[2,3],[3,4]])
  print(b.shape)   
  print(b.shape[0])   
  print(b.shape[1])
  print(b[1][0])

运行结果如下:
(3, 2) 其维度为3组,每组2个数
3 shape这个数组的第一个元素
2 shape这个数组的第二个元素
2 b数组第二个元素的第一个

a=np.array([1,2,3,4,5,6,7,8])
print(a.reshape(2,4))
print(a.reshape(4,2))

运行结果如下:
[[1 2 3 4] 变成了两行,每行四个
[5 6 7 8]]
[[1 2] 变成了四行,每行2个
[3 4]
[5 6]
[7 8]]

发布了40 篇原创文章 · 获赞 1 · 访问量 2064

猜你喜欢

转载自blog.csdn.net/qq_40660825/article/details/100160827