Python数据分析-图解数组计算模块NumPy
1. 简介
NumPy为数据分析三剑客之一,主要用于数组计算、矩阵运算和科学计算。
它是Python数组计算、矩阵运算和科学计算的核心库,NumPy这个词来源于Numerical和Python两个单词。
NumPy提供了一个高性能的数组对象,让我们轻松创建一维数组、二维数组和多维数组,以及大量的函数和方法,帮助我们轻松地进行数组计算,从而广泛地应用于数据分析、机器学习、图像处理和计算机图形学、数学任务等领域当中。
2. 创建简单的数组
NumPy创建简单的数组主要使用array()函数,语法如下:
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
参数说明:
- object:array_like
An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence. If object is a scalar, a 0-dimensional array containing object is returned.
- dtype:data-type, optional
The desired data-type for the array. If not given, NumPy will try to use a default dtype that can represent the values (by applying promotion rules when necessary.)
- copy:bool, optional
If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.).
- order:{‘K’, ‘A’, ‘C’, ‘F’}, optional
Specify the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless ‘F’ is specified, in which case it will be in Fortran order (column major). If object is an array the following holds.
|
order |
no copy |
copy=True |
|
‘K’ |
unchanged |
F & C order preserved, otherwise most similar order |
|
‘A’ |
unchanged |
F order if input is F and not C, otherwise C order |
|
‘C’ |
C order |
C order |
|
‘F’ |
F order |
F order |
When copy=False and a copy is made for other reasons, the result is the same as if copy=True, with some exceptions for ‘A’, see the Notes section. The default order is ‘K’.
- subok:bool, optional
If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
- ndmin:int, optional
Specifies the minimum number of dimensions that the resulting array should have. Ones will be prepended to the shape as needed to meet this requirement.
- like:array_like, optional
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
CodeView:
1 import numpy as np # 导入numpy模块 2 3 n1 = np.array([1, 2, 3], ndmin=2) 4 n2 = np.array([1, 2, 3], dtype=complex) 5 n3 = np.array([(1, 2), (3, 4)], dtype=[('a', '<i4'), ('b', '<i4')]) 6 n4 = np.array(np.mat('1 2; 3 4')) 7 n5 = np.array(np.mat('1 2; 3 4'), subok=True) 8 9 print('n1: \n{}'.format(n1)) 10 print('n2: \n{}'.format(n2)) 11 print('n3: \n{}'.format(n3)) 12 print('n4: \n{}'.format(n4)) 13 print('n5: \n{}'.format(n5)) 14 15 # 结果 16 # n1: 17 # [[1 2 3]] 18 # n2: 19 # [1.+0.j 2.+0.j 3.+0.j] 20 # n3: 21 # [(1, 2) (3, 4)] 22 # n4: 23 # [[1 2] 24 # [3 4]] 25 # n5: 26 # [[1 2] 27 # [3 4]]
3. 数组的基本操作
3.1. 数据类型
|
数 据 类 型 |
描述 |
|
bool_ |
存储一个字节的布尔值(真或假) |
|
int_ |
默认整数,相当于C的long,通常为int32 |
|
intc |
相当于C的int,通常为int32 |
|
intp |
用于索引的整数,相当于C的size_t,通常为int64 |
|
int8 |
字节(-128~127) |
|
int16 |
16位整数(-32768~32767) |
|
int32 |
32位整数(-2147483648~2147483647) |
|
int64 |
64位整数(-9223372036854775808~9223372036854775807) |
|
uint8 |
8位无符号整数(0~255) |
|
uint16 |
16位无符号整数(0~65535) |
|
uint32 |
32位无符号整数(0~4294967295) |
|
uint64 |
64位无符号整数(0~18446744073709551615) |
|
float |
_float64的简写 |
|
float16 |
半精度浮点:1个符号位,5位指数,10位尾数 |
|
float32 |
单精度浮点:1个符号位,8位指数,23位尾数 |
|
float64 |
双精度浮点:1个符号位,11位指数,52位尾数 |
|
complex_ |
complex128类型的简写 |
|
omplex64 |
复数,由两个32位浮点表示(实部和虚部) |
|
complex128 |
复数,由两个64位浮点表示(实部和虚部) |
|
datatime64 |
日期时间类型 |
|
timedelta64 |
两个时间之间的间隔 |
3.2. 数组运算
数组的减法、乘法、除法和幂运算
1 import numpy as np 2 3 n1 = np.array([1, 2]) # 创建一维数组 4 n2 = np.array([3, 4]) 5 print(n1 + n2) # 加法运算 6 print(n1 - n2) # 减法运算 7 print(n1 * n2) # 乘法运算 8 print(n1 / n2) # 除法运算 9 print(n1**n2) # 幂运算 10 11 # 结果 12 # [4 6] 13 # [-2 -2] 14 # [3 8] 15 # [0.33333333 0.5 ] 16 # [ 1 16]
比较运算
1 import numpy as np 2 3 n1 = np.array([1, 2]) # 创建一维数组 4 n2 = np.array([3, 4]) 5 print(n1 >= n2) # 大于等于 6 print(n1 == n2) # 等于 7 print(n1 <= n2) # 小于等于 8 print(n1 != n2) # 不等于 9 10 # 结果 11 # [False False] 12 # [False False] 13 # [ True True] 14 # [ True True]
创建等差数列数组
1 import numpy as np 2 n1 = np.linspace(7500,10000,6,dtype='int') #创建等差数列数组 3 print(n1) 4 print(n1/1000) #米转换为公里 5 6 # 结果 7 # [ 7500 8000 8500 9000 9500 10000] 8 # [ 7.5 8. 8.5 9. 9.5 10. ]
3.3. 数组的索引和切片
1 import numpy as np 2 3 n = np.arange(10) # 使用arange函数创建一维数组 4 print(n) 5 print(n[:3]) # 0 1 2 6 print(n[3:6]) # 3 4 5 7 print(n[6:]) # 6 7 8 9 8 print(n[::]) # 0 1 2 3 4 5 6 7 8 9 9 print(n[:]) # 0 1 2 3 4 5 6 7 8 9 10 print(n[::2]) # 0 2 4 6 8 11 print(n[1::5]) # 1 6 12 print(n[2::6]) # 2 8 13 # start、stop、step为负数时 14 print(n[::-1]) # 9 8 7 6 5 4 3 2 1 0 15 print(n[:-3:-1]) # 9 8 16 print(n[-3:-5:-1]) # 7 6 17 print(n[-5::-1]) # 5 4 3 2 1 0
3.4. 数组重塑
1 import numpy as np 2 3 n = np.arange(6) # 创建一维数组 4 # print(n) 5 n1 = n.reshape(2, 3) # 将数组重塑为2行3列的二维数组 6 # print(n1) 7 8 n = np.array(['床', '前', '明', '月', '光', '疑', '是', '地', '上', '霜', '举', '头', '望', '明', '月', '低', '头', '思', '故', '乡']) 9 n1 = n.reshape(4, 5) 10 print(n1) 11 12 # 结果 13 # [['床' '前' '明' '月' '光'] 14 # ['疑' '是' '地' '上' '霜'] 15 # ['举' '头' '望' '明' '月'] 16 # ['低' '头' '思' '故' '乡']] 17 import numpy as np 18 19 n = np.array([[0, 1, 2], [3, 4, 5]]) # 创建二维数组 20 print(n) 21 n1 = n.reshape(3, 2) # 将数组重塑为3行2列的二维数组 22 print(n1) 23 24 # 结果 25 # [[0 1] 26 # [2 3] 27 # [4 5]]
3.5. T属性行列转置
1 import numpy as np 2 3 n = np.array([['A', 100], ['B', 200], ['C', 300], ['D', 400], ['E', 500]]) 4 print(n) 5 print(n.T) # T属性行列转置 6 7 print(n.transpose()) # transpose()函数行列转置 8 9 # 结果 10 # [['A' '100'] 11 # ['B' '200'] 12 # ['C' '300'] 13 # ['D' '400'] 14 # ['E' '500']] 15 # [['A' 'B' 'C' 'D' 'E'] 16 # ['100' '200' '300' '400' '500']] 17 # [['A' 'B' 'C' 'D' 'E'] 18 # ['100' '200' '300' '400' '500']]
3.6. 数组的增、删、改、查
增加
1 import numpy as np 2 3 # 创建二维数组 4 n1 = np.array([[1, 2], [3, 4], [5, 6]]) 5 n2 = np.array([[10, 20], [30, 40], [50, 60]]) 6 print(np.hstack((n1, n2))) # 水平方向增加数据 7 print(np.vstack((n1, n2))) # 垂直方向增加数据 8 9 # 结果 10 # [[ 1 2 10 20] 11 # [ 3 4 30 40] 12 # [ 5 6 50 60]] 13 # [[ 1 2] 14 # [ 3 4] 15 # [ 5 6] 16 # [10 20] 17 # [30 40] 18 # [50 60]]
删除
1 import numpy as np 2 3 # 创建二维数组 4 n1 = np.array([[1, 2], [3, 4], [5, 6]]) 5 print(n1) 6 n2 = np.delete(n1, 2, axis=0) # 删除第3行 7 n3 = np.delete(n1, 0, axis=1) # 删除第1列 8 n4 = np.delete(n1, (1, 2), 0) # 删除第2行和第3行 9 print('删除第3行后的数组:', '\n', n2) 10 print('删除第1列后的数组:', '\n', n3) 11 print('删除第2行和第3行后的数组:', '\n', n4) 12 13 # 结果 14 # [[1 2] 15 # [3 4] 16 # [5 6]] 17 # 删除第3行后的数组: 18 # [[1 2] 19 # [3 4]] 20 # 删除第1列后的数组: 21 # [[2] 22 # [4] 23 # [6]] 24 # 删除第2行和第3行后的数组: 25 # [[1 2]]
修改
import numpy as np # 创建二维数组 n1 = np.array([[1, 2], [3, 4], [5, 6]]) print(n1) n1[1] = [30, 40] # 修改第2行数组[3,4]为[30,40] n1[2][1] = 88 # 修改第3行第3个元素6为88 print('修改后的数组:', '\n', n1) # 结果 # [[1 2] # [3 4] # [5 6]] # 修改后的数组: # [[ 1 2] # [30 40] # [ 5 88]]
查找
1 import numpy as np 2 3 n1 = np.arange(10) # 创建一个一维数组 4 print(n1) 5 print(np.where(n1 > 5, 2, 0)) # 大于5输出1,不大于5输出0 6 n2 = n1[np.where(n1 > 5)] 7 print(n2) 8 9 # 结果 10 # [0 1 2 3 4 5 6 7 8 9] 11 # [0 0 0 0 0 0 2 2 2 2] 12 # [6 7 8 9]
4. NumPy矩阵的基本操作
在数学中经常会看到矩阵,而在程序中常用的是数组,可以简单地理解为,矩阵是数学的概念,而数组是计算机程序设计领域的概念。在NumPy中,矩阵是数组的分支,数组和矩阵有些时候是通用的,二维数组也称矩阵。
4.1. 创建矩阵
1 import numpy as np 2 3 # 创建一个3*3的零矩阵 4 data1 = np.mat(np.zeros((3, 3))) 5 print('3*3的零矩阵:') 6 print(data1) 7 # 创建一个2*4的1矩阵 8 data1 = np.mat(np.ones((2, 4))) 9 print('2*4的1矩阵:') 10 print(data1) 11 12 # 使用rand()函数创建一个3*3在0~1之间随机产生的二维数组 13 data1 = np.mat(np.random.rand(3, 3)) 14 print('3*3在0~1之间随机产生的二维数组:') 15 print(data1) 16 17 # 创建一个1~8之间的随机整数矩阵 18 data1 = np.mat(np.random.randint(1, 8, size=(3, 5))) 19 print('1~8之间的随机整数矩阵:') 20 print(data1) 21 22 # 创建对角矩阵 23 print('对角矩阵:') 24 data1 = np.mat(np.eye(2, 2, dtype=int)) # 2*2对角矩阵 25 print(data1) 26 data1 = np.mat(np.eye(4, 4, dtype=int)) # 4*4对角矩阵 27 print(data1) 28 29 # 创建对角线矩阵 30 print('对角线矩阵:') 31 a = [1, 2, 3] 32 data1 = np.mat(np.diag(a)) # 对角线1、2、3矩阵 33 print(data1) 34 b = [4, 5, 6] 35 data1 = np.mat(np.diag(b)) # 对角线4、5、6矩阵 36 print(data1) 37 38 # 结果 39 # 3*3的零矩阵: 40 # [[0. 0. 0.] 41 # [0. 0. 0.] 42 # [0. 0. 0.]] 43 # 2*4的1矩阵: 44 # [[1. 1. 1. 1.] 45 # [1. 1. 1. 1.]] 46 # 3*3在0~1之间随机产生的二维数组: 47 # [[0.35556201 0.2446428 0.07246242] 48 # [0.80509442 0.00989151 0.2586037 ] 49 # [0.79639634 0.7166461 0.03040685]] 50 # 1~8之间的随机整数矩阵: 51 # [[4 1 4 5 2] 52 # [5 7 6 5 3] 53 # [3 5 7 3 1]] 54 # 对角矩阵: 55 # [[1 0] 56 # [0 1]] 57 # [[1 0 0 0] 58 # [0 1 0 0] 59 # [0 0 1 0] 60 # [0 0 0 1]] 61 # 对角线矩阵: 62 # [[1 0 0] 63 # [0 2 0] 64 # [0 0 3]] 65 # [[4 0 0] 66 # [0 5 0] 67 # [0 0 6]]
4.2. 矩阵运算
四则运算
1 import numpy as np 2 3 # 矩阵加法运算 4 data1 = np.mat([[1, 2], [3, 4], [5, 6]]) 5 data2 = np.mat([1, 2]) 6 print(data1 + data2) 7 8 # 结果 9 # [[2 4] 10 # [4 6] 11 # [6 8]] 12 13 ################################################ 14 # 矩阵减法、除法运算 15 data1 = np.mat([[1, 2], [3, 4], [5, 6]]) 16 data2 = np.mat([1, 2]) 17 print(data1 - data2) 18 print(data1 / data2) 19 20 # 结果 21 # [[0 0] 22 # [2 2] 23 # [4 4]] 24 # [[1. 1.] 25 # [3. 2.] 26 # [5. 3.]] 27 28 ################################################ 29 # 矩阵乘法运算 30 data1 = np.mat([[1, 2], [3, 4], [5, 6]]) 31 data2 = np.mat([[1, 2], [3, 4]]) 32 print(data1 * data2) 33 34 # 结果 35 # [[ 7 10] 36 # [15 22] 37 # [23 34]]
乘法与点乘运算
1 import numpy as np 2 3 # 创建数组 4 n1 = np.array([1, 2, 3]) 5 n2 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) 6 print('数组相乘结果为:', '\n', n1 * n2) # 数组相乘 7 print('数组点乘结果为:', '\n', np.dot(n1, n2)) # 数组点乘 8 print(np.multiply(n1, n2)) 9 10 # 结果 11 # 数组相乘结果为: 12 # [[1 4 9] 13 # [1 4 9] 14 # [1 4 9]] 15 # 数组点乘结果为: 16 # [ 6 12 18] 17 # [[1 4 9] 18 # [1 4 9] 19 # [1 4 9]]
4.3. 矩阵转换
矩阵转置
1 import numpy as np 2 3 n1 = np.mat('1 3 3;4 5 6;7 12 9') # 创建矩阵,使用分号隔开数据 4 print('矩阵转置结果为:\n', n1.T) # 矩阵转置 5 6 # 结果 7 # 矩阵转置结果为: 8 # [[ 1 4 7] 9 # [ 3 5 12] 10 # [ 3 6 9]]
逆矩阵
1 import numpy as np 2 3 n1 = np.mat('1 3 3;4 5 6;7 12 9') # 创建矩阵,使用分号隔开数据 4 print('矩阵的逆矩阵结果为:\n', n1.I) # 逆矩阵 5 6 # 结果 7 # 矩阵的逆矩阵结果为: 8 # [[-0.9 0.3 0.1 ] 9 # [ 0.2 -0.4 0.2 ] 10 # [ 0.43333333 0.3 -0.23333333]]
5. NumPy常用统计分析函数
5.1. 数学运算函数
|
函数 |
说明 |
|
add()、subtract()、multiply()、divide() |
简单的数组加、减、乘、除运算 |
|
abs() |
取数组中各元素的绝对值 |
|
sqrt() |
计算数组中各元素的平方根 |
|
square() |
计算数组中各元素的平方 |
|
log()、log10()、log2() |
计算数组中各元素的自然对数和分别以10、2为底的对数 |
|
reciprocal() |
计算数组中各元素的倒数 |
|
power() |
第一个数组中的元素作为底数,计算它与第二个数组中相应元素的幂 |
|
mod() |
计算数组之间相应元素相除后的余数 |
|
around() |
计算数组中各元素指定小数位数的四舍五入值 |
|
ceil()、floor() |
计算数组中各元素向上取整和向下取整 |
|
sin()、cos()、tan() |
三角函数。计算数组中角度的正弦值、余弦值和正切值 |
|
modf() |
将数组各元素的小数和整数部分分割为两个独立的数组 |
|
exp() |
计算数组中各元素的指数值 |
|
sign() |
计算数组中各元素的符号值1(+),0,-1(-) |
|
maximum()、fmax() |
计算数组元素的最大值 |
|
minimum()、fmin() |
计算数组元素的最小值 |
|
copysign(a,b) |
将数组b中各元素的符号赋值给数组a对应的元素 |
代码示例:
1 import numpy as np 2 3 n1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 创建数组 4 n2 = np.array([10, 10, 10]) 5 print('两个数组相加:') 6 print(np.add(n1, n2)) 7 print('两个数组相减:') 8 print(np.subtract(n1, n2)) 9 print('两个数组相乘:') 10 print(np.multiply(n1, n2)) 11 print('两个数组相除:') 12 print(np.divide(n1, n2)) 13 14 # 结果 15 # 两个数组相加: 16 # [[11 12 13] 17 # [14 15 16] 18 # [17 18 19]] 19 # 两个数组相减: 20 # [[-9 -8 -7] 21 # [-6 -5 -4] 22 # [-3 -2 -1]] 23 # 两个数组相乘: 24 # [[10 20 30] 25 # [40 50 60] 26 # [70 80 90]] 27 # 两个数组相除: 28 # [[0.1 0.2 0.3] 29 # [0.4 0.5 0.6] 30 # [0.7 0.8 0.9]] 31 32 ##################################################### 33 a = np.array([0.25, 1.75, 2, 100]) 34 print(np.reciprocal(a)) 35 36 # 结果 37 # [4. 0.57142857 0.5 0.01 ] 38 39 ##################################################### 40 n1 = np.array([10, 100, 1000]) 41 print(np.power(n1, 3)) 42 n2 = np.array([1, 2, 3]) 43 print(np.power(n1, n2)) 44 45 # 结果 46 # [ 1000 1000000 1000000000] 47 # [ 10 10000 1000000000] 48 49 ##################################################### 50 n = np.array([0, 30, 45, 60, 90]) 51 print('不同角度的正弦值:') 52 # 通过乘 pi/180 转化为弧度 53 print(np.sin(n * np.pi / 180)) 54 print('数组中角度的余弦值:') 55 print(np.cos(n * np.pi / 180)) 56 print('数组中角度的正切值:') 57 print(np.tan(n * np.pi / 180)) 58 59 # 结果 60 # 不同角度的正弦值: 61 # [0. 0.5 0.70710678 0.8660254 1. ] 62 # 数组中角度的余弦值: 63 # [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 64 # 6.12323400e-17] 65 # 数组中角度的正切值: 66 # [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 67 # 1.63312394e+16]
5.2. 统计分析函数
|
函数 |
说明 |
|
sum() |
对数组中元素或某行某列的元素求和 |
|
cumsum() |
所有数组元素累计求和 |
|
cumprod() |
所有数组元素累计求积 |
|
mean() |
计算平均值 |
|
min()、max() |
计算数组的最小值和最大值 |
|
average() |
计算加权平均值 |
|
median() |
计算数组中元素的中位数(中值) |
|
var() |
计算方差 |
|
std() |
计算标准差 |
|
eg() |
对数组的第二维度的数据进行求平均 |
|
argmin()、argmax() |
计算数组最小值和最大值的下标(注:是一维的下标) |
|
unravel_index() |
根据数组形状将一维下标转成多维下标 |
|
ptp() |
计算数组最大值和最小值的差 |
代码示例:
1 import numpy as np 2 3 n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 4 print('对数组元素求和:') 5 print(n.sum()) 6 print('对数组元素按行求和:') 7 print(n.sum(axis=0)) 8 print('对数组元素按列求和:') 9 print(n.sum(axis=1)) 10 11 # 结果 12 # 对数组元素求和: 13 # 45 14 # 对数组元素按行求和: 15 # [12 15 18] 16 # 对数组元素按列求和: 17 # [ 6 15 24] 18 19 #################################################### 20 n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 21 print('对数组元素求平均值:') 22 print(n.mean()) 23 print('对数组元素按行求平均值:') 24 print(n.mean(axis=0)) 25 print('对数组元素按列求平均值:') 26 print(n.mean(axis=1)) 27 28 # 结果 29 # 对数组元素求平均值: 30 # 5.0 31 # 对数组元素按行求平均值: 32 # [4. 5. 6.] 33 # 对数组元素按列求平均值: 34 # [2. 5. 8.] 35 36 #################################################### 37 n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 38 print('数组元素最大值:') 39 print(n.max()) 40 print('数组中每一行的最大值:') 41 print(n.max(axis=0)) 42 print('数组中每一列的最大值:') 43 print(n.max(axis=1)) 44 print('数组元素最小值:') 45 print(n.min()) 46 print('数组中每一行的最小值:') 47 print(n.min(axis=0)) 48 print('数组中每一列的最小值:') 49 print(n.min(axis=1)) 50 51 # 结果 52 # 数组元素最大值: 53 # 9 54 # 数组中每一行的最大值: 55 # [7 8 9] 56 # 数组中每一列的最大值: 57 # [3 6 9] 58 # 数组元素最小值: 59 # 1 60 # 数组中每一行的最小值: 61 # [1 2 3] 62 # 数组中每一列的最小值: 63 # [1 4 7] 64 65 #################################################### 66 price = np.array([34.5, 36, 37.8, 39, 39.8, 33.6]) # 创建“单价”数组 67 number = np.array([900, 580, 230, 150, 120, 1800]) # 创建“销售数量”数组 68 print('加权平均价:') 69 print(np.average(price, weights=number)) 70 71 # 结果 72 # 加权平均价: 73 # 34.84920634920635
5.3. 数组的排序
代码示例:
1 import numpy as np 2 3 n = np.array([[4, 7, 3], [2, 8, 5], [9, 1, 6]]) 4 print('数组排序:') 5 print(np.sort(n)) 6 print('按行排序:') 7 print(np.sort(n, axis=0)) 8 print('按列排序:') 9 print(np.sort(n, axis=1)) 10 11 # 结果 12 # 数组排序: 13 # [[3 4 7] 14 # [2 5 8] 15 # [1 6 9]] 16 # 按行排序: 17 # [[2 1 3] 18 # [4 7 5] 19 # [9 8 6]] 20 # 按列排序: 21 # [[3 4 7] 22 # [2 5 8] 23 # [1 6 9]] 24 25 ############################################# 26 x = np.array([4, 7, 3, 2, 8, 5, 1, 9, 6]) 27 print('升序排序后的索引值:') 28 y = np.argsort(x) 29 print(y) 30 print('排序后的顺序重构原数组:') 31 print(x[y]) 32 33 # 结果 34 # 升序排序后的索引值: 35 # [6 3 2 0 5 8 1 4 7] 36 # 排序后的顺序重构原数组: 37 # [1 2 3 4 5 6 7 8 9] 38 39 ############################################## 40 math = np.array([101, 109, 115, 108, 118, 118]) 41 en = np.array([117, 105, 118, 108, 98, 109]) 42 total = np.array([621, 623, 620, 620, 615, 615]) 43 sort_total = np.lexsort((en, math, total)) 44 print('排序后的索引值') 45 print(sort_total) 46 print('通过排序后的索引获取排序后的数组:') 47 print(np.array([[en[i], math[i], total[i]] for i in sort_total])) 48 49 # 结果 50 # 排序后的索引值 51 # [4 5 3 2 0 1] 52 # 通过排序后的索引获取排序后的数组: 53 # [[ 98 118 615] 54 # [109 118 615] 55 # [108 108 620] 56 # [118 115 620] 57 # [117 101 621] 58 # [105 109 623]]
时间:2024年4月24日

Python数据分析-图解数组计算模块NumPy
浙公网安备 33010602011771号