scipy percentileofscore的加权版本

时间:2018-01-14 17:33:54

标签: python numpy scipy

我想将权重传递给scipy.stats.percentileofscore。例如:

from scipy import stats
a = [1, 2, 3, 4]
val = 3
stats.percentileofscore(a, val)

返回75,因为a中75%的值位于val 3或以下。

我想添加权重,例如:

weights = [2, 2, 3, 3]
weightedpercentileofscore(a, val, weights)

应该返回70,因为(2 + 2 + 3)/(2 + 2 + 3 + 3)= 7/10的权重下降到或低于3.

这也适用于十进制权重和大权重,因此仅扩展数组并不理想。

Weighted percentile using numpy是相关的,但计算百分位数(例如,要求第10百分位值)而不是值的特定百分位数。

1 个答案:

答案 0 :(得分:2)

这应该可以胜任。

import numpy as np

def weighted_percentile_of_score(a, weights, score, kind='weak'):
    npa = np.array(a)
    npw = np.array(weights)

    if kind == 'rank':  # Equivalent to 'weak' since we have weights.
        kind = 'weak'

    if kind in ['strict', 'mean']:
        indx = npa < score
        strict = 100 * sum(npw[indx]) / sum(weights)
    if kind == 'strict':
        return strict

    if kind in ['weak', 'mean']:    
        indx = npa <= score
        weak = 100 * sum(npw[indx]) / sum(weights)
    if kind == 'weak':
        return weak

    if kind == 'mean':
        return (strict + weak) / 2


a = [1, 2, 3, 4]
weights = [2, 2, 3, 3]
print(weighted_percentile_of_score(a, weights, 3))  # 70.0 as desired.

在实践中,你想要做的是看分数的总重量是否小于或等于你的门槛得分 - 除以重量的总和和百分比。

将每个值的相应加权百分位数作为数组:

[weighted_percentile_of_score(a, weights, val) for val in a]
# [20.0, 40.0, 70.0, 100.0]