使用Seaborn生成CDF图

时间:2018-10-22 15:58:07

标签: pandas simulation seaborn

我正在尝试使用Seaborn为我的代码绘制CDF图,但无法使其正常工作。

具体地说,我想为sum_MDA,sum_CLA,sum_BIA和grand_total生成CDF图。在我对整个代码进行了1000次模拟之后。我的代码如下(对于长度,我们先表示歉意)。

def sim():

    df['RAND'] = np.random.uniform(0,1, size=df.index.size)
    dfRAND = list(df['RAND'])

    def L():
        result = []
        conditions = [df.RAND >= (1 - 0.8062), (df.RAND < (1 - 0.8062)) & (df.RAND >= 0.1),
                              (df.RAND < 0.1) & (df.RAND >= 0.05), (df.RAND < 0.05) & 
                              (df.RAND >= 0.025), (df.RAND < 0.025) & (df.RAND >= 0.0125), 
                              (df.RAND < 0.0125)]
        choices = ['L0', 'L1', 'L2', 'L3', 'L4', 'L5']
        df['L'] = np.select(conditions, choices)
        result = df['L'].values
        return result
    L()
    #print(L())
    #print(df.pivot_table(index='L', aggfunc=len, fill_value=0))

    def MD():
        result = []
        conditions = [L() == 'L0', L() == 'L1', L() == 'L2', L() == 'L3', 
                  L() == 'L4', L() == 'L5']
        choices = [(df['P_MD'].apply(lambda x: x * 0.02)), (df['P_MD'].apply(lambda x: x * 0.15)),
               (df['P_MD'].apply(lambda x: x * 0.20)), (df['P_MD'].apply(lambda x: x * 0.50)),
               (df['P_MD'].apply(lambda x: x * 1.0)), (df['P_MD'].apply(lambda x: x * 1.0))]
        df['MDL'] = np.select(conditions, choices)
        #result = print(df['MDL'].values)
        return result
    MD()

    def CL():
        result = []
        conditions = [L() == 'L0', L() == 'L1', L() == 'L2', L() == 'L3', L() == 'L4', 
                  L() == 'L5']
        choices = [1600, 3200, 9600, 48000, 48000, 48000]
        df['CL'] = np.select(conditions, choices)
        #result = print(df['CL'].values)
        return result
    CL()

    def BI():
        result = []
        conditions = [L() == 'L0', L() == 'L1', L() == 'L2', L() == 'L3', 
                  L() == 'L4', L() == 'L5']
        choices = [(df['P_BI'].apply(lambda x: (x / 548) * 1)),
               (df['P_BI'].apply(lambda x: (x / 548) * 2)),
               (df['P_BI'].apply(lambda x: (x / 548) * 14)),
               (df['P_BI'].apply(lambda x: (x / 548) * 60)),
               (df['P_BI'].apply(lambda x: (x / 548) * 180)),
               (df['P_BI'].apply(lambda x: (x / 548) * 365))]
        df['BIL'] = np.select(conditions, choices)
        #result = print(df['BIL'].values)
        return result
    BI()

    sum_MDA = int(np.sum(df['MDL']))
    sum_CLA = int(np.sum(df['CL']))
    sum_BIA = int(np.sum(df['BIL']))
    grand_total = int(sum_MDA + sum_CLA + sum_BIA)

    result = sum_MDA, sum_CLA, sum_BIA, grand_total
    return result

sim()

for i in range(1000):
    print(sim())

#sns.distplot(sim(), bins=100,
     #kde_kws=dict(cumulative=True), axlabel='(£)',  color='purple', 
     #).set_title('Simulation (N=1000)')

感谢您的帮助。非常感谢。

2 个答案:

答案 0 :(得分:1)

此内容在版本0.11.0中被添加为sns.ecdfplothttps://seaborn.pydata.org/generated/seaborn.ecdfplot.html

答案 1 :(得分:0)

如上所述,您正在将整个数据帧传递给Seaborn。您想传递特定的列,例如game_data <- data.frame( player = c(1,1,1,1,2,2,2,2), dateday = c("2015-04-08","2015-05-08","2015-05-10","2015-06-28","2015-09-01","2015-09-02","2015-09-03","2015-10-11"), points = c(20,80,140,230,40,60,98,102), stringsAsFactors = FALSE)

请参阅有关我的问题here的示例。

sim['MDL']