ndarray.ndim
- 数组的维度:
import numpy as np # 创建一个一维数组 arr_1d = np.array([1, 2, 3]) print("数组:", arr_1d) print("数组的维度:", arr_1d.ndim) 数组: [1 2 3] 数组的维度: 1
ndarray.shape
- 数组的形状(维度大小):
import numpy as np # 创建一个二维数组 arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(arr_2d) print("数组的形状:", arr_2d.shape) [[1 2 3] [4 5 6]] 数组的形状: (2, 3)
ndarray.size
- 数组元素的总个数:
import numpy as np # 创建一个三维数组 arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr_3d) print("数组元素的总个数:", arr_3d.size) [[[1 2] [3 4]] [[5 6] [7 8]]] 数组元素的总个数: 8
ndarray.dtype
- 数组元素的数据类型:
import numpy as np arr_int = np.array([1, 2, 3], dtype=int) arr_float = np.array([1.1, 2.2, 3.3], dtype=float)
改变数据类型
import numpy as np arr = np.array([1, 2, 3]) arr_float = arr.astype(float)
标签:arr,numpy,print,普通,数组,np,array,方法 From: https://www.cnblogs.com/mxleader/p/17864463.html