在 Matplotlib 中使用 “for i in range(Y.shape[0])” 时,.shape[] 有什么作用?
shape 属性通常用于获取数组的当前形状,但也可以通过为其分配数组维数元组来就地重塑数组。
步骤
使用 np.array 方法获取数组 Y.
Y.shape 将返回一个元组 (4, ).
Y.shape[0] 方法将返回 4,即元组的第一个元素。
示例
import numpy as np
Y = np.array([1, 2, 3, 4])
print("Output of .show method would be: ", Y.shape, " for ", Y)
print("Output of .show[0] method would be: ", Y.shape[0], " for ", Y)
print("Output for i in range(Y.shape[0]): ", end=" ")
for i in range(Y.shape[0]):
print(Y[i], end=" ")输出
Output of .show method would be: (4,) for [1 2 3 4] Output of .show[0] method would be: 4 for [1 2 3 4] Output for i in range(Y.shape[0]): 1 2 3 4
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP