numpy提供了丰富的函数,主要分为以下几类:
- 字符串函数
- 数学函数
- 算术函数
- 统计函数
- 排序及筛选函数
13 字符串函数
numpy中dtype 为 numpy.string_ 或 numpy.unicode_ 的数组可以执行向量化字符串操作,这些字符串函数基于python内置库中的标准字符串函数,定义在numpy字符数组类(numpy.char)中。
函数 | 描述 |
| 对两个数组的逐个字符串元素进行连接 |
| 返回按元素多重连接后的字符串 |
| 居中字符串 |
| 将字符串第一个字母转换为大写 |
| 将字符串的每个单词的第一个字母转换为大写 |
| 数组元素转换为小写 |
| 数组元素转换为大写 |
| 指定分隔符对字符串进行分割,并返回数组列表 |
| 返回元素中的行列表,以换行符分割 |
| 移除元素开头或者结尾处的特定字符 |
| 通过指定分隔符来连接数组中的元素 |
| 使用新字符串替换字符串中的所有子字符串 |
| 数组元素依次调用 |
| 数组元素依次调用 |
13.1 numpy.char.add()
依次对两个数组的元素进行字符串连接
import numpy as np
print ('连接两个字符串:')
print (np.char.add(['hello'],[' xyz']))
print ('\n')
print ('连接示例:')
print (np.char.add(['hello', 'hi'],[' abc', ' xyz']))
'''
输出结果
连接两个字符串:
['hello xyz']
连接示例:
['hello abc' 'hi xyz']
'''
13.2 numpy.char.multiply()
执行多重连接
import numpy as np
print (np.char.multiply('numpy ',3))
'''
输出结果
numpy numpy numpy
'''
13.3 numpy.char.center()
将字符串居中,并使用指定字符在左侧和右侧进行填充
np.char.center(str , width,fillchar)
- str: 字符串,
- width: 长度,
- fillchar: 填充字符
import numpy as np
print (np.char.center('numpy', 20,fillchar = '*'))
'''
输出结果
*******numpy********
'''
13.4 numpy.char.capitalize()
将字符串的第一个字母转换为大写
import numpy as np
print (np.char.capitalize('numpy'))
'''
输出结果
Numpy
'''
13.5 numpy.char.title()
将字符串的每个单词的第一个字母转换为大写
import numpy as np
print (np.char.title('i like python'))
'''
输出结果
I Like Python
'''
13.6 numpy.char.lower()
对数组的每个元素转换为小写。它对每个元素调用 str.lower
import numpy as np
#操作数组
print (np.char.lower(['PYTHON','BAIDU']))
# 操作字符串
print (np.char.lower('NUMPY'))
'''
输出结果
['python' 'baidu']
numpy
'''
13.7 numpy.char.upper()
对数组的每个元素转换为大写。它对每个元素调用 str.upper
import numpy as np
#操作数组
print (np.char.upper(['python','numpy']))
# 操作字符串
print (np.char.upper('numpy'))
'''
输出结果
['PYTHON' 'NUMPY']
NUMPY
'''
13.8 numpy.char.split()
以换行符作为分隔符来分割字符串,并返回数组
import numpy as np
# 换行符 \n
print (np.char.splitlines('i\nlike python?'))
print (np.char.splitlines('i\rlike\n python?'))
'''
输出结果
['i', 'like python?']
['i', 'like', ' python?']
'''
13.9 numpy.char.strip()
用于移除开头或结尾处的特定字符
import numpy as np
# 移除字符串头尾的 a 字符
print (np.char.strip('abook aisa pythona','a'))
# 移除数组元素头尾的 a 字符
print (np.char.strip(['abeijinga','admin','java'],'a'))
'''
输出结果
book aisa python
['beijing' 'dmin' 'jav']
'''
13.10 numpy.char.join()
过指定分隔符来连接数组中的元素或字符串
import numpy as np
# 操作字符串
print (np.char.join(':','YMDHMS'))
# 指定多个分隔符操作数组元素
print (np.char.join([':','-'],['YMDHMS','YMDHMS']))
'''
输出结果
Y:M:D:H:M:S
['Y:M:D:H:M:S' 'Y-M-D-H-M-S']
'''
13.11 numpy.char.replace()
使用新字符串替换字符串中的所有子字符串
import numpy as np
print (np.char.replace ('i like python', 'py', 'cj'))
'''
输出结果
i like cjthon
'''
13.12 numpy.char.encode()
数组中的每个元素调用 str.encode
函数。 默认编码是 utf-8,可以使用标准 Python 库中的编解码器。
(function) def encode(
a: _ArrayLikeStr_co,
encoding: str | None = ...,
errors: str | None = ...
) -> NDArray[bytes_]
encoding常用编码类型
- ASCII:仅支持英文字符和数字
- CP500:简体中文编码
- GBK:扩展中文编码,包含更多中文字符
- UTF-8:默认编码,支持多国语言字符
import numpy as np
a = np.char.encode('numpy', 'cp500')
print (a)
b = np.char.encode('numpy', 'UTF-8')
print (b)
'''
输出结果
b'\x95\xa4\x94\x97\xa8'
b'numpy'
'''
13.13 numpy.char.decode()
对编码的元素进行 str.decode()
解码。
import numpy as np
a = np.char.encode('python', 'cp500')
print (a)
print (np.char.decode(a,'cp500'))
'''
输出结果
b'\x97\xa8\xa3\x88\x96\x95'
python
'''
14 NumPy 数学函数
NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等
14.1 三角函数
求正弦 余弦 正切
import numpy as np
a = np.array([0,30,45,60,90])
print ('不同角度的正弦值:')
# 通过乘 pi/180 转化为弧度
print (np.sin(a*np.pi/180))
print ('\n')
print ('数组中角度的余弦值:')
print (np.cos(a*np.pi/180))
print ('\n')
print ('数组中角度的正切值:')
print (np.tan(a*np.pi/180))
'''
输出结果
不同角度的正弦值:
[0. 0.5 0.70710678 0.8660254 1. ]
数组中角度的余弦值:
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
数组中角度的正切值:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
'''
arcsin,arccos,和 arctan 函数返回给定角度的 sin,cos 和 tan 的反三角函数。
14.2 numpy.around()舍入函数
指定数字的四舍五入值
numpy.around(a,decimals)
- a: 数组
- decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置
import numpy as np
a = np.array([10.0,7.55, 220, 0.189, 25.799])
print ('原数组:')
print (a)
print ('\n')
print ('舍入后:')
print (np.around(a))
print (np.around(a, decimals = 1))
print (np.around(a, decimals = -1))
'''
输出结果
原数组:
[1.0000e+01 7.5500e+00 2.2000e+02 1.8900e-01 2.5799e+01]
舍入后:
[ 10. 8. 220. 0. 26.]
[1.00e+01 7.60e+00 2.20e+02 2.00e-01 2.58e+01]
[ 10. 10. 220. 0. 30.]
'''
14.3 numpy.floor()
返回小于或者等于指定表达式的最大整数,即向下取整
import numpy as np
a = np.array([-1.9, 1.4, -1.2, 0.6, 210])
print ('提供的数组:')
print (a)
print ('\n')
print ('修改后的数组:')
print (np.floor(a))
'''
输出结果
提供的数组:
[ -1.9 1.4 -1.2 0.6 210. ]
修改后的数组:
[ -2. 1. -2. 0. 210.]
'''
14.4 numpy.ceil()
返回大于或者等于指定表达式的最小整数,即向上取整
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print ('提供的数组:')
print (a)
print ('\n')
print ('修改后的数组:')
print (np.ceil(a))
'''
输出结果
提供的数组:
[-1.7 1.5 -0.2 0.6 10. ]
修改后的数组:
[-1. 2. -0. 1. 10.]
'''
15 算术函数
15.1 加减乘除
数组必须具有相同的形状或符合数组广播规则
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print ('第一个数组:')
print (a)
print ('\n')
print ('第二个数组:')
b = np.array([10,10,10])
print (b)
print ('\n')
print ('两个数组相加:')
print (np.add(a,b))
print ('\n')
print ('两个数组相减:')
print (np.subtract(a,b))
print ('\n')
print ('两个数组相乘:')
print (np.multiply(a,b))
print ('\n')
print ('两个数组相除:')
print (np.divide(a,b))
'''
输出结果
第一个数组:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
第二个数组:
[10 10 10]
两个数组相加:
[[10. 11. 12.]
[13. 14. 15.]
[16. 17. 18.]]
两个数组相减:
[[-10. -9. -8.]
[ -7. -6. -5.]
[ -4. -3. -2.]]
两个数组相乘:
[[ 0. 10. 20.]
[30. 40. 50.]
[60. 70. 80.]]
两个数组相除:
[[0. 0.1 0.2]
[0.3 0.4 0.5]
[0.6 0.7 0.8]]
'''
15.2 numpy.reciprocal()
返回参数逐元素的倒数。如 1/4 倒数为 4/1。
import numpy as np
a = np.array([0.25, 1.33, 1, 10])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 reciprocal 函数:')
print (np.reciprocal(a))
'''
输出结果
我们的数组是:
[ 0.25 1.33 1. 10. ]
调用 reciprocal 函数:
[4. 0.7518797 1. 0.1 ]
'''
15.3 numpy.power()
将第一个输入数组中的元素作为底数,计算它与第二个输入数组中相应元素的幂。
import numpy as np
a = np.array([10,100,1000])
print ('我们的数组是;')
print (a)
print ('\n')
print ('调用 power 函数:')
print (np.power(a,2))
print ('\n')
print ('第二个数组:')
b = np.array([1,2,3])
print (b)
print ('\n')
print ('再次调用 power 函数:')
print (np.power(a,b))
'''
输出结果
我们的数组是;
[ 10 100 1000]
调用 power 函数:
[ 100 10000 1000000]
第二个数组:
[1 2 3]
再次调用 power 函数:
[ 10 10000 1000000000]
'''
15.4 numpy.mod()/numpy.remainder()
算输入数组中相应元素的相除后的余数。
import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])
print ('第一个数组:')
print (a)
print ('\n')
print ('第二个数组:')
print (b)
print ('\n')
print ('调用 mod() 函数:')
print (np.mod(a,b))
print ('\n')
print ('调用 remainder() 函数:')
print (np.remainder(a,b))
'''
输出结果
第一个数组:
[10 20 30]
第二个数组:
[3 5 7]
调用 mod() 函数:
[1 0 2]
调用 remainder() 函数:
[1 0 2]
'''
16 NumPy 统计函数
16.1 numpy.amin() 和 numpy.amax()
amin()
计算数组中的元素沿指定轴的最小值
numpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
a
: 输入的数组,可以是一个NumPy数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算最小值。如果不提供此参数,则返回整个数组的最小值。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。out
: 可选参数,用于指定结果的存储位置。keepdims
: 可选参数,如果为True,将保持结果数组的维度数目与输入数组相同。如果为False(默认值),则会去除计算后维度为1的轴。initial
: 可选参数,用于指定一个初始值,然后在数组的元素上计算最小值。where
: 可选参数,一个布尔数组,用于指定仅考虑满足条件的元素。
amax()
计算数组中的元素沿指定轴的最大值
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
a
: 输入的数组,可以是一个NumPy数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算最大值。如果不提供此参数,则返回整个数组的最大值。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。out
: 可选参数,用于指定结果的存储位置。keepdims
: 可选参数,如果为True,将保持结果数组的维度数目与输入数组相同。如果为False(默认值),则会去除计算后维度为1的轴。initial
: 可选参数,用于指定一个初始值,然后在数组的元素上计算最大值。where
: 可选参数,一个布尔数组,用于指定仅考虑满足条件的元素。
import numpy as np
a = np.array([[6,9,5],[8,7,3],[4,0,1]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 amin() 函数:')
print (np.amin(a,1)) # 按行中最小
print ('\n')
print ('再次调用 amin() 函数:')
print (np.amin(a,0)) # 按列中最小
print ('\n')
print ('调用 amax() 函数:')
print (np.amax(a))
print ('\n')
print ('再次调用 amax() 函数:')
print (np.amax(a, axis = 0)) # 按列中最大
'''
输出结果
我们的数组是:
[[6 9 5]
[8 7 3]
[4 0 1]]
调用 amin() 函数:
[5 3 0]
再次调用 amin() 函数:
[4 0 1]
调用 amax() 函数:
9
再次调用 amax() 函数:
[8 9 5]
'''
16.2 numpy.ptp()
计算数组中元素最大值与最小值的差(最大值 - 最小值)
numpy.ptp(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
a
: 输入的数组,可以是一个 NumPy 数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算峰-峰值。如果不提供此参数,则返回整个数组的峰-峰值。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。out
: 可选参数,用于指定结果的存储位置。keepdims
: 可选参数,如果为 True,将保持结果数组的维度数目与输入数组相同。如果为 False(默认值),则会去除计算后维度为1的轴。initial
: 可选参数,用于指定一个初始值,然后在数组的元素上计算峰-峰值。where
: 可选参数,一个布尔数组,用于指定仅考虑满足条件的元素。
import numpy as np
a = np.array([[6,9,5],[8,7,3],[4,4,1]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 ptp() 函数:')
print (np.ptp(a))
print ('\n')
print ('沿轴 1 调用 ptp() 函数:')
print (np.ptp(a, axis = 1))
print ('\n')
print ('沿轴 0 调用 ptp() 函数:')
print (np.ptp(a, axis = 0))
'''
输出结果
我们的数组是:
[[6 9 5]
[8 7 3]
[4 4 1]]
调用 ptp() 函数:
8
沿轴 1 调用 ptp() 函数:
[4 5 3]
沿轴 0 调用 ptp() 函数:
[4 5 4]
'''
16.3 numpy.percentile()
百分位数是统计中使用的度量,表示小于这个值的观察值的百分比.即第 p 个百分位数是这样一个值,它使得至少有 p% 的数据项小于或等于这个值,且至少有 (100-p)% 的数据项大于或等于这个值。
numpy.percentile(a, q, axis)
- a: 输入数组
- q: 要计算的百分位数,在 0 ~ 100 之间
- axis: 沿着它计算百分位数的轴
import numpy as np
a = np.array([[10, 8,6], [3, 2, 1]])
print ('我们的数组是:')
print (a)
print ('调用 percentile() 函数:')
# 50% 的分位数,就是 a 里排序之后的中位数
print (np.percentile(a, 50))
# axis 为 0,在纵列上求
print(f'在纵列上求')
print (np.percentile(a, 50, axis=0))
# axis 为 1,在横行上求
print(f'在横行上求')
print (np.percentile(a, 50, axis=1))
# 保持维度不变
print (np.percentile(a, 50, axis=1, keepdims=True))
'''
输出结果
我们的数组是:
[[10 8 6]
[ 3 2 1]]
调用 percentile() 函数:
4.5
在纵列上求
[6.5 5. 3.5]
在横行上求
[8. 2.]
[[8.]
[2.]]
'''
16.4 numpy.median()
计算数组 a 中元素的中位数(中值)
numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=<no value>)
a
: 输入的数组,可以是一个 NumPy 数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算中位数。如果不提供此参数,则计算整个数组的中位数。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。out
: 可选参数,用于指定结果的存储位置。overwrite_input
: 可选参数,如果为True,则允许在计算中使用输入数组的内存。这可能会在某些情况下提高性能,但可能会修改输入数组的内容。keepdims
: 可选参数,如果为True,将保持结果数组的维度数目与输入数组相同。如果为False(默认值),则会去除计算后维度为1的轴。
import numpy as np
a = np.array([[30,65,70],[80,95,10],[50,55,60]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 median() 函数:')
print (np.median(a))
print ('\n')
print ('沿轴 0 调用 median() 函数:')
print (np.median(a, axis = 0))
print ('\n')
print ('沿轴 1 调用 median() 函数:')
print (np.median(a, axis = 1))
'''
输出结果
我们的数组是:
[[30 65 70]
[80 95 10]
[50 55 60]]
调用 median() 函数:
60.0
沿轴 0 调用 median() 函数:
[50. 65. 60.]
沿轴 1 调用 median() 函数:
[65. 80. 55.]
'''
16.5 numpy.mean()
返回数组中元素的算术平均值,如果提供了轴,则沿其计算
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)
a
: 输入的数组,可以是一个 NumPy 数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算平均值。如果不提供此参数,则计算整个数组的平均值。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。dtype
: 可选参数,用于指定输出的数据类型。如果不提供,则根据输入数据的类型选择合适的数据类型。out
: 可选参数,用于指定结果的存储位置。keepdims
: 可选参数,如果为True,将保持结果数组的维度数目与输入数组相同。如果为False(默认值),则会去除计算后维度为1的轴。
import numpy as np
a = np.array([[15,22,39],[31,21,59],[45,35,46],[50,60,45]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 mean() 函数:')
print (np.mean(a))
print ('\n')
print ('沿轴 0 调用 mean() 函数:') # 操作沿着行方向进行。
print (np.mean(a, axis = 0))
print ('\n')
print ('沿轴 1 调用 mean() 函数:') # 操作沿着列方向进行。
print (np.mean(a, axis = 1))
'''
输出结果
我们的数组是:
[[15 22 39]
[31 21 59]
[45 35 46]
[50 60 45]]
调用 mean() 函数:
39.0
沿轴 0 调用 mean() 函数:
[35.25 34.5 47.25]
沿轴 1 调用 mean() 函数:
[25.33333333 37. 42. 51.66666667]
'''
16.6 numpy.average()
根据在另一个数组中给出的各自的权重计算数组中元素的加权平均值。
- 该函数可以接受一个轴参数。 如果没有指定轴,则数组会被展开
- 加权平均值即将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数
numpy.average(a, axis=None, weights=None, returned=False)
a
: 输入的数组,可以是一个 NumPy 数组或类似数组的对象。axis
: 可选参数,用于指定在哪个轴上计算加权平均值。如果不提供此参数,则计算整个数组的加权平均值。可以是一个整数表示轴的索引,也可以是一个元组表示多个轴。weights
: 可选参数,用于指定对应数据点的权重。如果不提供权重数组,则默认为等权重。returned
: 可选参数,如果为True,将同时返回加权平均值和权重总和。
import numpy as np
a = np.array([1,2,3,4])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 average() 函数:')
print (np.average(a))
print ('\n')
# 不指定权重时相当于 mean 函数
wts = np.array([4,3,2,1])
print ('再次调用 average() 函数:')
print (np.average(a,weights = wts))
print ('\n')
# 如果 returned 参数设为 true,则返回权重的和
print ('权重的和:')
print (np.average([1,2,3, 4],weights = [4,3,2,1], returned = True))
print ('\n')
print ('在多维数组中,可以指定用于计算的轴')
a = np.arange(6).reshape(3,2)
print ('我们的数组是:')
print (a)
print ('\n')
print ('修改后的数组:')
wt = np.array([3,5])
print (np.average(a, axis = 1, weights = wt))
print ('\n')
print ('修改后的数组:')
print (np.average(a, axis = 1, weights = wt, returned = True))
'''
axis=0:操作沿着行方向进行。
axis=1:操作沿着列方向进行。
输出结果
我们的数组是:
[1 2 3 4]
调用 average() 函数:
2.5
再次调用 average() 函数:
2.0
权重的和:
(2.0, 10.0)
在多维数组中,可以指定用于计算的轴
我们的数组是:
[[0 1]
[2 3]
[4 5]]
修改后的数组:
[0.625 2.625 4.625]
修改后的数组:
(array([0.625, 2.625, 4.625]), array([8., 8., 8.]))
'''
16.7 标准差
标准差是一组数据平均值分散程度的一种度量。
标准差是方差的算术平方根
std = sqrt(mean((x - x.mean())**2))
如果数组arr是 [4,6,5,9],则arr平均数为6,差的平方s [4,0,1,9],再求s的平均值为14/4,最后求标准差为√14/4,即1.8708286933869707
import numpy as np
print (np.std([4,6,5,9]))
'''
输出结果
1.8708286933869707
'''
16.8 方差
方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数,即 mean((x - x.mean())** 2)。可以看出,标准差是方差的平方根
import numpy as np
print (np.var([4,6,5,9]))
'''
输出结果
3.5
'''
17 NumPy 排序、条件筛选函数
17.1 numpy.sort()
返回输入数组的排序副本
numpy.sort(a, axis, kind, order)
- a: 要排序的数组
- axis: 沿着它排序数组的轴,如果没有数组会被展开,沿着最后的轴排序, axis=0 按列排序,axis=1 按行排序
- kind: 默认为'quicksort'(快速排序)
- order: 如果数组包含字段,则是要排序的字段
import numpy as np
a = np.array([[3,7],[9,1]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 sort() 函数:')
print (np.sort(a))
print ('\n')
print ('按列排序:')
print (np.sort(a, axis = 0))
print ('\n')
# 在 sort 函数中排序字段
dt = np.dtype([('name', 'S10'),('age', int)])
a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)
print ('我们的数组是:')
print (a)
print ('\n')
print ('按 name 排序:')
print (np.sort(a, order = 'name'))
'''
输出结果
我们的数组是:
[[3 7]
[9 1]]
调用 sort() 函数:
[[3 7]
[1 9]]
按列排序:
[[3 1]
[9 7]]
我们的数组是:
[(b'raju', 21) (b'anil', 25) (b'ravi', 17) (b'amar', 27)]
按 name 排序:
[(b'amar', 27) (b'anil', 25) (b'raju', 21) (b'ravi', 17)]
'''
17.2 numpy.argsort()
函数返回的是数组值从小到大的索引值
import numpy as np
x = np.array([3, 1, 2])
print ('我们的数组是:')
print (x)
print ('\n')
print ('对 x 调用 argsort() 函数:')
y = np.argsort(x)
print (y)
print ('\n')
print ('以排序后的顺序重构原数组:')
print (x[y])
print ('\n')
print ('使用循环重构原数组:')
for i in y:
print (x[i], end=" ")
'''
输出结果
我们的数组是:
[3 1 2]
对 x 调用 argsort() 函数:
[1 2 0]
以排序后的顺序重构原数组:
[1 2 3]
使用循环重构原数组
1 2 3
'''
17.3 numpy.lexsort()
于对多个序列进行排序。把它想象成对电子表格进行排序,每一列代表一个序列,排序时优先照顾靠后的列。例如,通过ABCDE五项技能选拔人才时,录取规则是:综合得分高者优先录取,综合得分相同时,A技能得分高的优先录取,若再出现相同,则B技能得分高的优先录取,以此类推,这样做EXCEL表格时,各项的排列顺序如下表
E | D | C | B | A | 综合得分 |
import numpy as np
a = [[100, 2, 34], [12, 45, 2], [45, 90, 21]]
a = np.array(a)
print(f'原始的数组为')
print(a)
print(f'按照第一列排序')
b = a[:, 0] # [100 12 45]
index = np.lexsort((b,)) # [1 2 0]
print(a[index])
print('\n')
print(f'按照第一行排序')
b = a[0, :] # [100 2 34]
index = np.lexsort((b,)) # [1 2 0]
print(a.T[index])
print('\n')
print(a.T[index].T)
print('\n')
print(a[index])
'''
输出结果
原始的数组为
[[100 2 34]
[ 12 45 2]
[ 45 90 21]]
按照第一列排序
[[ 12 45 2]
[ 45 90 21]
[100 2 34]]
按照第一行排序
[[ 2 45 90]
[ 34 2 21]
[100 12 45]]
[[ 2 34 100]
[ 45 2 12]
[ 90 21 45]]
[[ 12 45 2]
[ 45 90 21]
[100 2 34]]
'''
17.4 numpy.argmax() 和 numpy.argmin()
沿给定轴返回最大和最小元素的索引
import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print ('我们的数组是:')
print (a)
print ('\n')
print ('调用 argmax() 函数:')
print (np.argmax(a))
print ('\n')
print ('展开数组:')
print (a.flatten())
print ('\n')
print ('沿轴 0 的最大值索引:')
maxindex = np.argmax(a, axis = 0)
print (maxindex)
print ('\n')
print ('沿轴 1 的最大值索引:')
maxindex = np.argmax(a, axis = 1)
print (maxindex)
print ('\n')
print ('调用 argmin() 函数:')
minindex = np.argmin(a)
print (minindex)
print ('\n')
print ('展开数组中的最小值:')
print (a.flatten()[minindex])
print ('\n')
print ('沿轴 0 的最小值索引:')
minindex = np.argmin(a, axis = 0)
print (minindex)
print ('\n')
print ('沿轴 1 的最小值索引:')
minindex = np.argmin(a, axis = 1)
print (minindex)
'''
输出结果
我们的数组是:
[[30 40 70]
[80 20 10]
[50 90 60]]
调用 argmax() 函数:
7
展开数组:
[30 40 70 80 20 10 50 90 60]
沿轴 0 的最大值索引:
[1 2 0]
沿轴 1 的最大值索引:
[2 0 1]
调用 argmin() 函数:
5
...
沿轴 1 的最小值索引:
[0 2 0]
'''
17.5 numpy.extract()
按照某个条件从数组中抽取元素,返回满条件的元素。
import numpy as np
x = np.arange(9.).reshape(3, 3)
print ('我们的数组是:')
print (x)
# 定义条件, 选择偶数元素
condition = np.mod(x,2) == 0
print ('按元素的条件值:')
print (condition)
print ('使用条件提取元素:')
print (np.extract(condition, x))
'''
输出结果
我们的数组是:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
按元素的条件值:
[[ True False True]
[False True False]
[ True False True]]
使用条件提取元素:
[0. 2. 4. 6. 8.]
'''