numpy
高效的数组操作:NumPy的数组操作非常高效,因为它是用C语言编写的,可以直接操作计算机的内存。这使得NumPy在大规模数据处理和科学计算中非常强大。
广泛的数学函数:NumPy提供了大量的数学函数,包括基本的数学运算、统计分析、线性代数等。这些函数使得科学计算和数据分析变得更加容易。
广泛的数据类型支持:NumPy支持多种数据类型,包括整数、浮点数、复数等,可以满足不同应用的需求。
广泛的数据输入和输出支持:NumPy可以轻松地与各种文件格式进行数据交互,包括文本文件、CSV文件、HDF5文件等。这使得数据的导入和导出非常方便。
python
import numpy as np
python
a = [1,2.2,99]
python
a_np = np.array(a)
a_np
array([ 1. , 2.2, 99. ])
python
a_np.dtype
dtype('float64')
创建全 1 的numpy
python
np.ones((3,4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
创建全 0 的数组
python
np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
创建多维数组,自定义填充值
np.full(shape,fill_value)
python
np.full((3,4),fill_value=5)
array([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]])
单位矩阵
python
np.eye(3,4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
生成等差数列
python
np.linspace(0,10,20)
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316,
2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211,
5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105,
7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ])
根据开始值,结束值,步长 生成数组
python
np.arange(0,10,1)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
在数值范围内随机生成指定shape的多维数组
python
np.random.randint(0,100,(3,4))
array([[29, 61, 27, 53],
[67, 35, 82, 24],
[84, 57, 17, 18]])
生成标准正态分布
python
np.random.randn(2,4)
array([[ 0.88892813, 0.72167577, -1.65972133, 0.35827695],
[ 0.6485041 , 1.09440367, -0.40121106, 0.65053935]])
生成普通正态分布
python
np.random.normal(170,5,(3,4))
array([[181.2757465 , 174.13997894, 178.4883532 , 167.11768902],
[170.09477859, 168.32151363, 171.23464558, 166.28698326],
[175.0587546 , 171.14062693, 178.34369189, 170.53161937]])
生成0-1的随机数
python
np.random.random((3,4))
array([[0.96068526, 0.48792948, 0.42863906, 0.05561108],
[0.01916122, 0.9598553 , 0.279491 , 0.55950602],
[0.24036869, 0.01171435, 0.39917078, 0.25462912]])
生成一组随机索引
python
np.random.permutation(20)
array([ 7, 1, 17, 18, 12, 5, 4, 3, 14, 13, 16, 2, 6, 19, 0, 8, 15,
11, 9, 10])
ndarray的属性
ndim:维度
shape:形状
size:总大小
dtype:元素类型
python
arr = np.random.randint(0,100,(3,4,5))
arr
array([[[66, 0, 85, 79, 48],
[94, 10, 9, 28, 2],
[23, 78, 51, 50, 21],
[ 3, 93, 25, 77, 48]],
[[33, 62, 99, 51, 92],
[24, 68, 25, 10, 58],
[45, 92, 77, 4, 35],
[31, 94, 94, 0, 86]],
[[26, 88, 66, 13, 33],
[15, 16, 9, 94, 86],
[62, 67, 18, 55, 34],
[83, 77, 5, 76, 73]]])
python
arr.ndim
3
python
arr.shape
(3, 4, 5)
python
arr.size
60
python
arr.dtype
dtype('int64')
ndarray的基本操作
1、索引
python
arr[1,3,4]
86
使用列表作为索引
python
arr[[0,1],[1,2]]
array([[94, 10, 9, 28, 2],
[45, 92, 77, 4, 35]])
使用bool 列表作为索引,对应为true的值返回
python
arr[arr>20]
array([66, 85, 79, 48, 94, 28, 23, 78, 51, 50, 21, 93, 25, 77, 48, 33, 62,
99, 51, 92, 24, 68, 25, 58, 45, 92, 77, 35, 31, 94, 94, 86, 26, 88,
66, 33, 94, 86, 62, 67, 55, 34, 83, 77, 76, 73])
2、切片
切片都是开区间(左闭右开),与列表一样
python
arr2 = np.random.randint(0,100,(5,6))
arr2
array([[ 1, 23, 54, 33, 81, 57],
[60, 3, 10, 9, 79, 5],
[71, 38, 15, 4, 46, 10],
[99, 24, 4, 6, 82, 28],
[31, 18, 66, 83, 24, 26]])
python
#行切片
arr2[1:3]
array([[60, 3, 10, 9, 79, 5],
[71, 38, 15, 4, 46, 10]])
python
#列切片,不论多少维度,每一维都用:进行分割
arr2[:,0:2]
array([[ 1, 23],
[60, 3],
[71, 38],
[99, 24],
[31, 18]])
python
# :: 表示反转
arr2[:,::-1]
array([[57, 81, 33, 54, 23, 1],
[ 5, 79, 9, 10, 3, 60],
[10, 46, 4, 15, 38, 71],
[28, 82, 6, 4, 24, 99],
[26, 24, 83, 66, 18, 31]])
3、变形
使用reshape,参数是一个元组(tuple)
python
arr2.reshape((6,5))
array([[ 1, 23, 54, 33, 81],
[57, 60, 3, 10, 9],
[79, 5, 71, 38, 15],
[ 4, 46, 10, 99, 24],
[ 4, 6, 82, 28, 31],
[18, 66, 83, 24, 26]])
4、级联(拼接)
python
a1 = np.random.randint(0,10,(3,4))
a2 = np.random.uniform(10,20,(3,4))
display(a1,a2)
array([[0, 2, 8, 9],
[7, 8, 3, 9],
[9, 1, 4, 5]])
array([[12.70255101, 13.93713091, 15.21392137, 19.09076818],
[15.13001599, 15.70935654, 13.69569491, 19.59468276],
[12.00687179, 10.94521978, 17.60840831, 15.91710952]])
python
#axis = 0 表示0维方向进行拼接
np.concatenate((a1,a2),axis=0)
array([[ 0. , 2. , 8. , 9. ],
[ 7. , 8. , 3. , 9. ],
[ 9. , 1. , 4. , 5. ],
[12.70255101, 13.93713091, 15.21392137, 19.09076818],
[15.13001599, 15.70935654, 13.69569491, 19.59468276],
[12.00687179, 10.94521978, 17.60840831, 15.91710952]])
python
np.concatenate((a1,a2),axis=1)
array([[ 0. , 2. , 8. , 9. , 12.70255101,
13.93713091, 15.21392137, 19.09076818],
[ 7. , 8. , 3. , 9. , 15.13001599,
15.70935654, 13.69569491, 19.59468276],
[ 9. , 1. , 4. , 5. , 12.00687179,
10.94521978, 17.60840831, 15.91710952]])
5、切分
python
a3 = np.random.randint(10,20,(6,6))
a3
array([[10, 15, 17, 16, 16, 15],
[11, 16, 11, 14, 10, 18],
[19, 15, 17, 10, 12, 11],
[17, 11, 12, 16, 14, 19],
[14, 12, 17, 11, 13, 10],
[17, 12, 18, 17, 15, 10]])
python
#indices_or_sections 表示切分的份数,axis 表示切分方向(即维度)
part1,part2 = np.split(a3, indices_or_sections=2)
display(part1,part2)
array([[10, 15, 17, 16, 16, 15],
[11, 16, 11, 14, 10, 18],
[19, 15, 17, 10, 12, 11]])
array([[17, 11, 12, 16, 14, 19],
[14, 12, 17, 11, 13, 10],
[17, 12, 18, 17, 15, 10]])
python
part1,part2 = np.split(a3, indices_or_sections=2,axis = 1)
display(part1,part2)
array([[10, 15, 17],
[11, 16, 11],
[19, 15, 17],
[17, 11, 12],
[14, 12, 17],
[17, 12, 18]])
array([[16, 16, 15],
[14, 10, 18],
[10, 12, 11],
[16, 14, 19],
[11, 13, 10],
[17, 15, 10]])
python
6、副本
所有的赋值运算都不会产生副本,对赋值后的对象生效,也对原来对象生效
使用 copy 生成副本
python
a4 = a3.copy()
a4
array([[10, 15, 17, 16, 16, 15],
[11, 16, 11, 14, 10, 18],
[19, 15, 17, 10, 12, 11],
[17, 11, 12, 16, 14, 19],
[14, 12, 17, 11, 13, 10],
[17, 12, 18, 17, 15, 10]])
python
a4[0,0] = 11111
display(a3,a4)
array([[10, 15, 17, 16, 16, 15],
[11, 16, 11, 14, 10, 18],
[19, 15, 17, 10, 12, 11],
[17, 11, 12, 16, 14, 19],
[14, 12, 17, 11, 13, 10],
[17, 12, 18, 17, 15, 10]])
array([[11111, 15, 17, 16, 16, 15],
[ 11, 16, 11, 14, 10, 18],
[ 19, 15, 17, 10, 12, 11],
[ 17, 11, 12, 16, 14, 19],
[ 14, 12, 17, 11, 13, 10],
[ 17, 12, 18, 17, 15, 10]])
聚合操作
python
arr = np.array([1, 2, 3, 4, 5])
total = np.sum(arr) # 计算数组元素的总和
mean_value = np.mean(arr) # 计算数组元素的平均值
max_value = np.max(arr) # 找到数组中的最大值
min_value = np.min(arr) # 找到数组中的最小值
max_index = np.argmax(arr)
display(total,mean_value,max_value,min_value,max_index)
15
3.0
5
1
4
python
# 标准差
arr.std()
1.4142135623730951
python
# 方差
arr.var()
2.0
python
# 中位数
np.median(arr)
3.0
python
#any 检测bool 列表中,是否存在一个true
#all 检测bool 列表中,是否全是true
广播
python
arr = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
# 使用广播将标量加到数组的每个元素
result = arr + scalar # 结果是一个与arr相同形状的数组,每个元素加10
result
array([[11, 12, 13],
[14, 15, 16]])
矩阵乘法
python
# 矩阵乘法
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
np.dot(matrix1,matrix2)
array([[19, 22],
[43, 50]])
文件保存
python
arr = np.array([1, 2, 3, 4, 5])
# 将数组保存到文件
np.save("my_array.npy", arr)
# 从文件中读取数组
loaded_arr = np.load("my_array.npy")