下面是一个使用percentileofscore
函数进行分组,并使用expanding
方法的示例代码:
import numpy as np
from scipy.stats import percentileofscore
# 创建一个随机数组
np.random.seed(0)
data = np.random.randint(0, 10, size=100)
# 按照percentileofscore进行分组
groups = []
for score in range(0, 11, 2):
group = (score, percentileofscore(data, score))
groups.append(group)
# 打印分组结果
for group in groups:
print("Score: {} - Percentile: {:.2f}%".format(group[0], group[1]))
# 使用expanding方法计算累积分位数
cum_percentiles = np.expanding_apply(data, percentileofscore)
# 打印累积分位数结果
print("\nCumulative Percentiles:")
print(cum_percentiles)
这段代码首先使用numpy.random.randint
函数创建一个随机数组data
。然后,通过循环遍历0到10之间的整数,使用percentileofscore
函数计算每个整数对应的分位数,并将结果存储在groups
列表中。接下来,通过遍历groups
列表,打印出每个整数对应的分位数。
最后,使用numpy.expanding_apply
函数和percentileofscore
函数计算data
数组的累积分位数,并将结果存储在cum_percentiles
数组中。最后,打印出cum_percentiles
数组的结果。