bbe*_*t36 7 python numpy scipy percentile pandas
Example DataFrame Values -
0 78
1 38
2 42
3 48
4 31
5 89
6 94
7 102
8 122
9 122
stats.percentileofscore(temp['INCOME'].values, 38, kind='mean')
15.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='strict')
10.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='weak')
20.0
stats.percentileofscore(temp['INCOME'].values, 38, kind='rank')
20.0
temp['INCOME'].rank(pct=True)
1 0.20 (Only showing the 38 value index)
temp['INCOME'].quantile(0.11)
37.93
temp['INCOME'].quantile(0.12)
38.31999999999999
Based on the results above, you can see none of the methods are consistent
with the pd.quantiles() method.
Run Code Online (Sandbox Code Playgroud)
我需要为数据帧中的每一行(255M行)获取一列的百分位数,但找不到任何返回他们在&中使用的"线性插值"方法的函数/方法. pd.quantilenp.percentile
我尝试了以下方法/功能 -
.rank(pct=True)
Run Code Online (Sandbox Code Playgroud)
此方法仅返回按顺序排列的值,而不是使用我正在寻找的百分位方法.不一致pd.quantiles
scipy.stats.percentileofscore
Run Code Online (Sandbox Code Playgroud)
这种方法几乎与我正在寻找的方法更接近,但由于某种原因仍与100%的"线性插值"方法不一致. 这个问题的相关问题没有真正的答案
我查看了与此问题相关的每个SO答案,但没有一个使用我需要使用的相同插值方法,所以请不要将其标记为重复,除非您可以验证它们使用相同的方法.
在这一点上,我的最后一个选择是找到所有100百分位数的bin截止值并以这种方式应用它或者自己计算线性插值但这看起来非常低效并且将永远适用于255M记录.
还有其他建议吗?
谢谢!
igr*_*nis 10
使用
sz = temp['INCOME'].size-1
temp['PCNT_LIN'] = temp['INCOME'].rank(method='max').apply(lambda x: 100.0*(x-1)/sz)
INCOME PCNT_LIN
0 78 44.444444
1 38 11.111111
2 42 22.222222
3 48 33.333333
4 31 0.000000
5 89 55.555556
6 94 66.666667
7 102 77.777778
8 122 100.000000
9 122 100.000000
Run Code Online (Sandbox Code Playgroud)
一旦你了解了机制,它实际上非常简单.当您在寻找分数的百分位数时,您已经在每一行中获得了分数.剩下的唯一步骤是了解您需要的数字百分位数小于或等于所选值.这正是参数样="弱"的scipy.stats.percentileofscore()和方法="平均"的DataFrame.rank()事.为了反转它,Series.quantile()使用interpolation ='lower'运行.
所以,行为scipy.stats.percentileofscore(),Series.rank()并且Series.quantile() 是一致的,见下文:
In[]:
temp = pd.DataFrame([ 78, 38, 42, 48, 31, 89, 94, 102, 122, 122], columns=['INCOME'])
temp['PCNT_RANK']=temp['INCOME'].rank(method='max', pct=True)
temp['POF'] = temp['INCOME'].apply(lambda x: scipy.stats.percentileofscore(temp['INCOME'], x, kind='weak'))
temp['QUANTILE_VALUE'] = temp['PCNT_RANK'].apply(lambda x: temp['INCOME'].quantile(x, 'lower'))
temp['RANK']=temp['INCOME'].rank(method='max')
sz = temp['RANK'].size - 1
temp['PCNT_LIN'] = temp['RANK'].apply(lambda x: (x-1)/sz)
temp['CHK'] = temp['PCNT_LIN'].apply(lambda x: temp['INCOME'].quantile(x))
temp
Out[]:
INCOME PCNT_RANK POF QUANTILE_VALUE RANK PCNT_LIN CHK
0 78 0.5 50.0 78 5.0 0.444444 78.0
1 38 0.2 20.0 38 2.0 0.111111 38.0
2 42 0.3 30.0 42 3.0 0.222222 42.0
3 48 0.4 40.0 48 4.0 0.333333 48.0
4 31 0.1 10.0 31 1.0 0.000000 31.0
5 89 0.6 60.0 89 6.0 0.555556 89.0
6 94 0.7 70.0 94 7.0 0.666667 94.0
7 102 0.8 80.0 102 8.0 0.777778 102.0
8 122 1.0 100.0 122 10.0 1.000000 122.0
9 122 1.0 100.0 122 10.0 1.000000 122.0
Run Code Online (Sandbox Code Playgroud)
现在,在列中,PCNT_RANK您可以获得小于或等于列中值的比率INCOME.但是如果你想要"插值"比率,它就在列中PCNT_LIN.当你Series.rank()用于计算时,它非常快,并且会在几秒钟内压缩你的255M数字.
在这里,我将解释如何使用获得的价值quantile()与linear插补:
temp['INCOME'].quantile(0.11)
37.93
Run Code Online (Sandbox Code Playgroud)
我们的数据temp['INCOME']只有十个值.根据您链接到Wiki的公式,第11百分位的排名是
rank = 11*(10-1)/100 + 1 = 1.99
Run Code Online (Sandbox Code Playgroud)
等级的截断部分是1,其对应于值31,并且具有等级2(即,下一个仓)的值是38.值fraction是等级的小数部分.这导致了结果:
31 + (38-31)*(0.99) = 37.93
Run Code Online (Sandbox Code Playgroud)
对于值本身,该fraction部分必须为零,因此很容易进行逆计算以获得百分位数:
p = (rank - 1)*100/(10 - 1)
Run Code Online (Sandbox Code Playgroud)
我希望我说得更清楚.
这似乎有效:
A = np.sort(temp['INCOME'].values)
np.interp(sample, A, np.linspace(0, 1, len(A)))
Run Code Online (Sandbox Code Playgroud)
例如:
>>> temp.INCOME.quantile(np.interp([37.5, 38, 122, 121], A, np.linspace(0, 1, len(A))))
0.103175 37.5
0.111111 38.0
1.000000 122.0
0.883333 121.0
Name: INCOME, dtype: float64
Run Code Online (Sandbox Code Playgroud)
请注意,只有当您想要查询足够多的值时,此策略才有意义。否则排序成本太高。
| 归档时间: |
|
| 查看次数: |
5415 次 |
| 最近记录: |