Python中np.percentile和df.quantile分位数详解
作者:小小喽啰 时间:2021-11-25 17:26:00
np.percentile
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
参数:
a : array,用来算分位数的对象,可以是多维的数组
q : array_like of float,介于0-100的float,用来计算是几分位的参数,如四分之一位就是25,如要算两个位置的数就(25,75)
axis : 坐标轴的方向,一维的就不用考虑了,多维的就用这个调整计算的维度方向,取值范围0/1,默认值为沿着数组的展平版本计算百分位数
out : 输出数据的存放对象,参数要与预期输出有相同的形状和缓冲区长度
overwrite_input : bool,默认False,为True时及计算直接在数组内存计算,计算后原数组无法保存
interpolation : 取值范围{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
默认liner,比如取中位数,但是中位数有两个数字6和7,选不同参数来调整输出
keepdims : bool,默认False,为真时取中位数的那个轴将保留在结果中
a = np.array([[10, 7, 4], [3, 2, 1]])
a
'''
array([[10, 7, 4],
[ 3, 2, 1]])
'''
np.percentile(a, 50)
#3.5
np.percentile(a, 50, axis=0)
#array([[ 6.5, 4.5, 2.5]])
np.percentile(a, 50, axis=1)
#array([ 7., 2.])
np.percentile(a, 50, axis=1, keepdims=True)
'''
array([[ 7.],
[ 2.]])
'''
pandas.DataFrame.quantile
DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation='linear')
参数:
q:float or array-like, default 0.5 (50% quantile),0 <= q <= 1之间的值,即要计算的分位数
axis:{0, 1, ‘index’, ‘columns’}, default 0,对于行,等于0或“索引”,对于列,等于1或“列”
numeric_only:bool, default True,如果为False,则还将计算日期时间和时间增量数据的分位数
interpolation:{‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’},当所需分位数位于两个数据点i和j之间时,此可选参数指定要使用的插值方法
返回
Series or DataFrame
如果
q
是数组,则将返回DataFrame,其中index为q
,列为self的列,值为分位数。如果
q
为float,则在index是self的列,值是分位数
df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]),
columns=['a', 'b'])
df.quantile(.1)
'''
a 1.3
b 3.7
Name: 0.1, dtype: float64
'''
df.quantile([.1, .5])
'''
a b
0.1 1.3 3.7
0.5 2.5 55.0
'''
总结
来源:https://www.cnblogs.com/cgmcoding/p/13713171.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp中把数据表映射成ajax可调用的json格式的方法
springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项
Python打印特殊符号及对应编码解析
![](https://img.aspxhome.com/file/2023/8/67388_0s.png)
Django如何实现上传图片功能
![](https://img.aspxhome.com/file/2023/8/90608_0s.png)
Vue 项目中遇到的跨域问题及解决方法(后台php)
![](https://img.aspxhome.com/file/2023/0/100540_0s.png)
瞎扯之Web导航
![](https://img.aspxhome.com/file/UploadPic/20093/18/01-85s.jpg)
pycharm远程连接服务器并配置python interpreter的方法
![](https://img.aspxhome.com/file/2023/7/82897_0s.png)
http通过StreamingHttpResponse完成连续的数据传输长链接方式
如何实现优惠打折?
PHP解析xml格式数据工具类示例
OpenCV中resize函数插值算法的实现过程(五种)
![](https://img.aspxhome.com/file/2023/5/93815_0s.png)
DreamweaverMX 2004打造细线表格
python之excel文件(.xls文件)处理方式
![](https://img.aspxhome.com/file/2023/3/75973_0s.png)
MYSQL教程:检查数据表和修复数据表
分享PyCharm最新激活码(真永久激活方法)不用每月找安装参数或最新激活码了
![](https://img.aspxhome.com/file/2023/7/89227_0s.png)
处理Python中的URLError异常的方法
关于Ajax在浏览器中产生前进后退的实现方法
使用python检查yaml配置文件是否符合要求
python中列表的切片与修改知识点总结
![](https://img.aspxhome.com/file/2023/4/84094_0s.jpg)