scipy percentileofscore的加权版本 [英] Weighted version of scipy percentileofscore

查看:240
本文介绍了scipy percentileofscore的加权版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to pass weights to scipy.stats.percentileofscore. For example:

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

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

Returns 75, as 75% of the values in a lie at or below the val 3.

我想添加权重,例如:

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

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

Should return 70, since (2 + 2 + 3) / (2 + 2 + 3 + 3) = 7 / 10 of the weights fall at or below 3.

这也应该适用于十进制权重和大权重,因此仅扩展数组是不理想的.

This should also work for decimal weights and large weights, so just expanding the arrays isn't ideal.

使用numpy加权的百分位数是相关的,但是可以计算百分位数(例如,要求第10个百分位数值),而不是值的特定百分位数.

Weighted percentile using numpy is relevant, but calculates percentiles (e.g. asking for the 10th percentile value) rather than the specific percentile for a value.

推荐答案

这应该可以完成工作.

This should do the job.

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.

在实践中,您要做的是看到分数的总权重小于或等于阈值-除以权重的总和和百分数.

In practice, what you want to do is see the overall weight of the scores less or equal than your threshold score - divided by the whole sum of weights and in percent.

要获取每个值的相应加权百分比作为数组:

To get each value's corresponding weighted percentile as an array:

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

这篇关于scipy percentileofscore的加权版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆