是否可以通过 python 中的确切值获得分位数?
Is it possible to get quantile by the exact value in python?
我得到标准正态分布的分位数(例如 0.9 值)。我用 scipy.stats.norm.ppf(0.9, 0, 1)
作为它。
我需要在我的自定义数据中找到这个值对应的分位数。所以我必须四舍五入我从 scipy.stats.norm.ppf(0.9, 0, 1)
得到的值,然后找到它的分位数。有没有专门的封装功能?
我想你在找 numpy.quantile
:
import numpy as np
from scipy import stats
mean = 0
std = 1
N = 1000
quantile = 0.9
dist = stats.norm(mean, std)
x = dist.rvs(size = N)
data_quantile = np.quantile(x, quantile)
dist_quantile = dist.ppf(quantile)
print(f'The 0.9th quantile of the dataset is {data_quantile}')
#The 0.9th quantile of the dataset is 1.2580295186126398
print(f'The 0.9th quantile of the actual distribution is {dist_quantile}')
#The 0.9th quantile of the actual distribution is 1.2815515655446004
编辑
但是,我可能误解了 re-reading 我想知道你是否真的想这样做:
def get_effective_quantile(dataset, distribution, quantile):
dist_quantile = distribution.ppf(quantile)
effective_quantile = sum(dataset <= dist_quantile) / len(dataset)
return(effective_quantile)
print(f'The effective quantile of {dist_quantile} in the dataset is {get_effective_quantile(x, dist, quantile)}')
#The effective quantile of 1.2815515655446004 in the dataset is 0.904
我不知道执行此操作的程序包或函数,但上述函数非常简单,并且根据您的描述看起来比您目前正在做的更简单、更可靠。
我得到标准正态分布的分位数(例如 0.9 值)。我用 scipy.stats.norm.ppf(0.9, 0, 1)
作为它。
我需要在我的自定义数据中找到这个值对应的分位数。所以我必须四舍五入我从 scipy.stats.norm.ppf(0.9, 0, 1)
得到的值,然后找到它的分位数。有没有专门的封装功能?
我想你在找 numpy.quantile
:
import numpy as np
from scipy import stats
mean = 0
std = 1
N = 1000
quantile = 0.9
dist = stats.norm(mean, std)
x = dist.rvs(size = N)
data_quantile = np.quantile(x, quantile)
dist_quantile = dist.ppf(quantile)
print(f'The 0.9th quantile of the dataset is {data_quantile}')
#The 0.9th quantile of the dataset is 1.2580295186126398
print(f'The 0.9th quantile of the actual distribution is {dist_quantile}')
#The 0.9th quantile of the actual distribution is 1.2815515655446004
编辑
但是,我可能误解了 re-reading 我想知道你是否真的想这样做:
def get_effective_quantile(dataset, distribution, quantile):
dist_quantile = distribution.ppf(quantile)
effective_quantile = sum(dataset <= dist_quantile) / len(dataset)
return(effective_quantile)
print(f'The effective quantile of {dist_quantile} in the dataset is {get_effective_quantile(x, dist, quantile)}')
#The effective quantile of 1.2815515655446004 in the dataset is 0.904
我不知道执行此操作的程序包或函数,但上述函数非常简单,并且根据您的描述看起来比您目前正在做的更简单、更可靠。