gpt4 book ai didi

python - scipypercentileofscore 的加权版本

转载 作者:行者123 更新时间:2023-11-30 22:22:39 25 4
gpt4 key购买 nike

我想将权重传递给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 个百分位数值)而不是值的特定百分位数。

最佳答案

这应该可以完成工作。

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]

关于python - scipypercentileofscore 的加权版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48252282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com