前言
这节我们学的是数据分析中numpy中两个函数百分位数percentile()和中位数median()
首先,我们要理解什么是百分位数,什么是中位数
1.percentile()百分位数
理解:百分位数就是在百分之几位置的数组的值
例如:
- 数组[1,2,3],在50%位置上就是2
- 数组[1,2,3,4],在50%位置上就是2与3的中间值,就是2.5
代码格式:
percentile(a,q[,axis])
a:数组或可以转化成数组的对象
q:[0,100]范围的浮点数
axis:指定沿着某个轴来计算百分位数,axis=0 表示案列,axis=1 表示按行,默认值 None
代码示例:
import numpy as np
arr=np.arange(12).reshape(3,4)
print(arr)
#[[ 0 1 2 3]
#[ 4 5 6 7]
#[ 8 9 10 11]]
#使用percentile求百分位数
a=np.percentile(arr,50)
#求arr数组垂直方向的百分位数
b=np.percentile(arr,50,axis=0)
#求arr数组横向方向的百分位数
c=np.percentile(arr,50,axis=1)
print(a,b,c) #5.5 [4. 5. 6. 7.] [1.5 5.5 9.5]
2.median()中位数
理解:中位数就是在中间位置上的数意思跟 percentile函数中数组[1,2,3,...],在50%位置一个意思
代码格式:
numpy.median(a[,axis])
代码示例:
import numpy as np
arr=np.arange(12).reshape(3,4)
print(arr)
#[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
d=np.median(arr)
e=np.median(arr,axis=0)
f=np.median(arr,axis=1)
print(d,e,f) #5.5 [4. 5. 6. 7.] [1.5 5.5 9.5]
这篇文章就到这里了,我会持续更新,多谢大家的点赞关注支持,谢谢大家!