【问题标题】:Different results when calculating quantile in pandas (Python) and R在熊猫(Python)和R中计算分位数时的不同结果
【发布时间】:2018-11-19 05:08:22
【问题描述】:

您能否告诉我,为什么在 pandas (Python) 和 R 中计算分位数时结果不同?

熊猫代码:

 print('p_new:   {:>5}   {:>5}     {:>5}'.format(
        round(self.pandas_data_frame['pending_new'].quantile(0.50), 2),
        round(self.pandas_data_frame['pending_new'].quantile(0.95), 2),
        round(self.pandas_data_frame['pending_new'].quantile(0.99), 2),
    ))

    print('new:     {:>5}   {:>5}   {:>5}'.format(
        round(self.pandas_data_frame['new'].quantile(0.50), 2),
        round(self.pandas_data_frame['new'].quantile(0.95), 2),
        round(self.pandas_data_frame['new'].quantile(0.99), 2),
    ))

结果:

name     |   .50|    .95|    .99| 
p_new:     2.0    12.0      20.0
new:      52.0    78.0   106.06

R 代码:

dd = read.csv(“stats.csv”)
quantile(dd$pending_new, c(.50, .95, .99))
quantile(dd$new, c(.50, .95, .99))

结果:

> quantile(dd$pending_new, c(.50, .95, .99))                                                                                                                                               
50%  95%  99% 
2.0 13.1 34.0 
> quantile(dd$new, c(.50, .95, .99))                                                                                                                                                       
50%    95%    99% 
52.00  81.00 129.26 

【问题讨论】:

  • 使用来源(pandasr),卢克!
  • 有许多不同的ways 来估计分位数。 R 默认使用一种方式,pandas 使用另一种方式。

标签: python r python-3.x pandas


【解决方案1】:

在 Python 中执行此函数时,np.percentile() 系列的所有函数都有一个可选的参数插值。将此参数设置为“中点”,您的结果与 R 中的结果相匹配。 您还可以在此处阅读有关 python 函数的更多信息: How to calculate 1st and 3rd quartiles?

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2017-08-18
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多