根据Python中大量值计算累积分布函数

时间:2020-06-30 14:52:36

标签: python cumulative-sum

我想为该列表中的每个值在Python中计算累积分布函数;

y = 1000, 1012, 1014, 1015, 1016, 1017, 1018, 1019...

以下功能;

F =(yi +先前的y值)/ y值之和

示例:F代表1014

 F = (1014 + 1012 + 1000) / 8111 = 0.3730736

我想知道如何在Python中将此函数应用于大量y值。

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试一下:

<div id="logo-area" style="background-image: url(vendors/img/background.jpg); background-repeat: no-repeat;">

结果是:

import numpy as np

y =  [1000, 1012, 1014, 1015, 1016, 1017, 1018, 1019]

np.cumsum(y)/np.sum(y)

或者:

array([0.12328936, 0.24805819, 0.3730736 , 0.4982123 , 0.62347429,
       0.74885957, 0.87436814, 1.        ])

结果现在为import pandas as pd y = pd.Series([1000, 1012, 1014, 1015, 1016, 1017, 1018, 1019]) y.cumsum()/y.sum() 类型:

pd.Series

答案 1 :(得分:0)

您可以定义如下函数:

y =  [1000, 1012, 1014, 1015, 1016, 1017, 1018, 1019]

def cumululative(list_of_values):
    sum_values = sum(list_of_values)
    F = []  
    temp = 0
    for i in list_of_values:
        temp += i
        F.append(temp/sum_values)
    return F

print(cumululative(y))

输出:

[0.12328936012822093, 0.2480581925779805, 0.37307360374799653, 0.4982123042781408, 0.6234742941684133, 0.7488595734188139, 0.8743681420293429, 1.0]