5.6.2. Ndarray 对象¶
NumPy的ndarray:一种多维数组对象:
In [3]: import numpy as np
...: data = np.random.randn(2, 3)
...: print(data)
...:
[[ 0.35775698 -1.78119191 -0.80584732]
[ 2.15299456 0.04187989 -0.18685547]]
#所有元素都乘以10
In [4]: print('data * 10: \n', data * 10)
data * 10:
[[ 3.57756982 -17.81191913 -8.05847316]
[ 21.52994562 0.41879885 -1.86855466]]
In [5]: print('data shape:', data.shape)
...: print('data dtype:', data.dtype)
data shape: (2, 3)
data dtype: float64
In [11]: print(np.zeros(10))
...: print(np.zeros((3, 6)))
...: print(np.empty((2, 3, 2)))
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
[[[-2.00000000e+000 1.49457942e-154]
[ 2.10077583e-312 6.79038654e-313]
[ 2.22809558e-312 2.14321575e-312]]
[[ 2.35541533e-312 6.79038654e-313]
[ 2.22809558e-312 2.14321575e-312]
[ 2.46151512e-312 2.41907520e-312]]]
In [12]: print(np.arange(15))
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
ndarray的数据类型:
In [13]: arr1 = np.array([1, 2, 3], dtype=np.float64)
...: arr2 = np.array([1, 2, 3], dtype=np.int32)
...:
...: print(arr1.dtype)
...: print(arr2.dtype)
float64
int32
备注
索引和切片与内置list相同