import numpy as np
# 生成指定维度的随机多维数据
data = np.random.rand(2, 3)
print(data)
print(type(data))
[[ 0.40252658 0.37509588 0.286451 ]
[ 0.89310065 0.03391813 0.45979816]]
<class 'numpy.ndarray'>
print('维度个数', data.ndim)
print('各维度大小: ', data.shape)
print('数据类型: ', data.dtype)
维度个数 2
各维度大小: (2, 3)
数据类型: float64
# list转换为 ndarray
l = range(10)
data = np.array(l)
print(data)
print(data.shape)
print(data.ndim)
[0 1 2 3 4 5 6 7 8 9]
(10,)
1
# 嵌套序列转换为ndarray
l2 = [range(10), range(10)]
data = np.array(l2)
print(data)
print(data.shape)
[[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]]
(2, 10)
# np.zeros, np.ones 和 np.empty
# np.zeros
zeros_arr = np.zeros((3, 4))
# np.ones
ones_arr = np.ones((2, 3))
# np.empty
empty_arr = np.empty((3, 3))
# np.empty 指定数据类型
empty_int_arr = np.empty((3, 3), int)
print(zeros_arr)
print('-------------')
print(ones_arr)
print('-------------')
print(empty_arr)
print('-------------')
print(empty_int_arr)
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
-------------
[[ 1. 1. 1.]
[ 1. 1. 1.]]
-------------
[[ 4.24399158e-314 8.48798317e-314 1.27319747e-313]
[ 1.69759663e-313 2.12199580e-314 6.36598737e-314]
[ 1.06099790e-313 1.48539705e-313 1.90979621e-313]]
-------------
[[1 2 3]
[4 5 6]
[7 8 9]]
# np.arange()
print(np.arange(10))
[0 1 2 3 4 5 6 7 8 9]
zeros_float_arr = np.zeros((3, 4), dtype=np.float64)
print(zeros_float_arr)
print(zeros_float_arr.dtype)
# astype转换数据类型
zeros_int_arr = zeros_float_arr.astype(np.int32)
print(zeros_int_arr)
print(zeros_int_arr.dtype)
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
float64
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
int32
# 矢量与矢量运算
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("元素相乘:")
print(arr * arr)
print("矩阵相加:")
print(arr + arr)
元素相乘:
[[ 1 4 9]
[16 25 36]]
矩阵相加:
[[ 2 4 6]
[ 8 10 12]]
# 矢量与标量运算
print(1. / arr)
print(2. * arr)
[[ 1. 0.5 0.33333333]
[ 0.25 0.2 0.16666667]]
[[ 2. 4. 6.]
[ 8. 10. 12.]]
# 一维数组
arr1 = np.arange(10)
print(arr1)
print(arr1[2:5])
[0 1 2 3 4 5 6 7 8 9]
[2 3 4]
# 多维数组
arr2 = np.arange(12).reshape(3,4)
print(arr2)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
print(arr2[1])
print(arr2[0:2, 2:])
print(arr2[:, 1:3])
[4 5 6 7]
[[2 3]
[6 7]]
[[ 1 2]
[ 5 6]
[ 9 10]]
# 条件索引
# 找出 data_arr 中 2015年后的数据
data_arr = np.random.rand(3,3)
print(data_arr)
year_arr = np.array([[2000, 2001, 2000],
[2005, 2002, 2009],
[2001, 2003, 2010]])
#is_year_after_2005 = year_arr >= 2005
#print is_year_after_2005, is_year_after_2005.dtype
#filtered_arr = data_arr[is_year_after_2005]
filtered_arr = data_arr[year_arr >= 2005]
print(filtered_arr)
[[ 0.90915374 0.1663667 0.33398738]
[ 0.80025907 0.34861433 0.73339721]
[ 0.6319773 0.81760336 0.63986709]]
[ 0.80025907 0.73339721 0.63986709]
# 多个条件
filtered_arr = data_arr[(year_arr <= 2005) & (year_arr % 2 == 0)]
print(filtered_arr)
[ 0.90915374 0.33398738 0.34861433]
arr = np.random.rand(2,3)
print(arr)
print(arr.transpose())
[[ 0.79356285 0.6653338 0.07126922]
[ 0.49996323 0.93060841 0.16805786]]
[[ 0.79356285 0.49996323]
[ 0.6653338 0.93060841]
[ 0.07126922 0.16805786]]
arr3d = np.random.rand(2,3,4)
print(arr3d)
print('----------------------')
print(arr3d.transpose((1,0,2))) # 3x2x4
[[[ 0.07533279 0.33241798 0.27172617 0.11609248]
[ 0.76681846 0.52051066 0.6896428 0.67371356]
[ 0.00783419 0.10007337 0.2129019 0.797555 ]]
[[ 0.090838 0.87627376 0.40256159 0.93520448]
[ 0.91975347 0.32649431 0.03785318 0.23180578]
[ 0.38099443 0.36612974 0.37354004 0.04117499]]]
----------------------
[[[ 0.07533279 0.33241798 0.27172617 0.11609248]
[ 0.090838 0.87627376 0.40256159 0.93520448]]
[[ 0.76681846 0.52051066 0.6896428 0.67371356]
[ 0.91975347 0.32649431 0.03785318 0.23180578]]
[[ 0.00783419 0.10007337 0.2129019 0.797555 ]
[ 0.38099443 0.36612974 0.37354004 0.04117499]]]
arr = np.random.randn(2,3)
print(arr)
print(np.ceil(arr))
print(np.floor(arr))
print(np.rint(arr))
print(np.isnan(arr))
[[ 0.80286024 -0.25494811 0.82233072]
[-0.54124198 -1.27729749 -0.80643226]]
[[ 1. -0. 1.]
[-0. -1. -0.]]
[[ 0. -1. 0.]
[-1. -2. -1.]]
[[ 1. -0. 1.]
[-1. -1. -1.]]
[[False False False]
[False False False]]
arr = np.random.randn(3,4)
print(arr)
np.where(arr > 0, 1, -1)
[[-1.239156 0.48230589 0.01207341 0.030358 ]
[-0.47182634 0.3205272 0.31465978 -1.02769886]
[ 0.2938293 1.03855079 -0.51851337 0.16275925]]
array([[-1, 1, 1, 1],
[-1, 1, 1, -1],
[ 1, 1, -1, 1]])
arr = np.arange(10).reshape(5,2)
print(arr)
print(np.sum(arr))
print(np.sum(arr, axis=0))
print(np.sum(arr, axis=1))
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
45
[20 25]
[ 1 5 9 13 17]
arr = np.random.randn(2,3)
print(arr)
print(np.any(arr > 0))
print(np.all(arr > 0))
[[-0.58810594 -1.39092028 -0.9455846 ]
[ 2.07338185 0.89882792 -0.15339059]]
True
False
arr = np.array([[1, 2, 1], [2, 3, 4]])
print(arr)
print(np.unique(arr))
[[1 2 1]
[2 3 4]]
[1 2 3 4]