通过循环 Python 将值列表传递给 poisson.pmf

Pass list of values to poisson.pmf by looping Python

我对 python 和编码的世界还很陌生,所以如果我遗漏了一些明显的东西或找不到解决方案的信息,请告诉我,在此先感谢!

我的脚本使用泊松分布来计算足球比赛中主队和客队可能进球的概率百分比。这需要 4 个参数(列表),如下所述。

results = poisson.pmf(predict_home_goals,predict_home_xG) * poisson.pmf(predict_away_goals,predict_away_xG) * 100

结果是特定结果的概率,例如 12.2343 % 对于每场比赛(在本例中为 9 场比赛),我想计算概率。每个结果并将其作为 column/rows 添加到 DataFrame 中,并最终对每个结果求和 prob%。问题是我似乎无法“循环”这个计算,但我必须手动对每个结果进行编码,所以这段代码现在看起来像这样:

predict_home_goals = [0,1,2,3,4,5] #home team goals scored
predict_away_goals = [0,1,2,3,4,5] #away team goals scored

predict_home_xG = [np.clip((xG_fixtures['home_xG']),1.0,None)] 
#when print(predict_home_xG) output is:
#                   [0    2.105436
#                    1    4.012993
#                    2    1.234767
#                    3    1.329749
#                    4    1.000000
#                    5    2.849462
#                    6    3.704301
#                    7    1.266428
#                    8    1.646356
#                    Name: home_xG, dtype:float64]

predict_away_xG = [np.clip((xG_fixtures['away_xG']),1.0,None)]
print(predict_away_xG)
#when print(predict_away_xG) output is:
#                   [0    1.607959
#                   1    1.879433
#                   2    2.067376
#                   3    1.052482
#                   4    1.503546
#                   5    1.002364
#                   6    1.000000
#                   7    2.255319
#                   8    1.378251
#                   Name: away_xG, dtype: float64]

## draws
d_home0_away0 = poisson.pmf(predict_home_goals[0],predict_home_xG[0]) * poisson.pmf(predict_away_goals[0],predict_away_xG[0]) * 100
d_home1_away1 = poisson.pmf(predict_home_goals[1],predict_home_xG[0]) * poisson.pmf(predict_away_goals[1],predict_away_xG[0]) * 100
d_home2_away2 = poisson.pmf(predict_home_goals[2],predict_home_xG[0]) * poisson.pmf(predict_away_goals[2],predict_away_xG[0]) * 100
d_home3_away3 = poisson.pmf(predict_home_goals[3],predict_home_xG[0]) * poisson.pmf(predict_away_goals[3],predict_away_xG[0]) * 100

## home wins
h_home1_away0 = poisson.pmf(predict_home_goals[1],predict_home_xG[0]) * poisson.pmf(predict_away_goals[0],predict_away_xG[0]) * 100

h_home2_away0 = poisson.pmf(predict_home_goals[2],predict_home_xG[0]) * poisson.pmf(predict_away_goals[0],predict_away_xG[0]) * 100
h_home2_away1 = poisson.pmf(predict_home_goals[2],predict_home_xG[0]) * poisson.pmf(predict_away_goals[1],predict_away_xG[0]) * 100

h_home3_away0 = poisson.pmf(predict_home_goals[3],predict_home_xG[0]) * poisson.pmf(predict_away_goals[1],predict_away_xG[0]) * 100
h_home3_away1 = poisson.pmf(predict_home_goals[3],predict_home_xG[0]) * poisson.pmf(predict_away_goals[1],predict_away_xG[0]) * 100
h_home3_away2 = poisson.pmf(predict_home_goals[3],predict_home_xG[0]) * poisson.pmf(predict_away_goals[2],predict_away_xG[0]) * 100

## away wins
a_home0_away1 = poisson.pmf(predict_home_goals[0],predict_home_xG[0]) * poisson.pmf(predict_away_goals[1],predict_away_xG[0]) * 100

a_home0_away2 = poisson.pmf(predict_home_goals[0],predict_home_xG[0]) * poisson.pmf(predict_away_goals[2],predict_away_xG[0]) * 100
a_home1_away2 = poisson.pmf(predict_home_goals[1],predict_home_xG[0]) * poisson.pmf(predict_away_goals[2],predict_away_xG[0]) * 100

a_home0_away3 = poisson.pmf(predict_home_goals[0],predict_home_xG[0]) * poisson.pmf(predict_away_goals[3],predict_away_xG[0]) * 100
a_home1_away3 = poisson.pmf(predict_home_goals[1],predict_home_xG[0]) * poisson.pmf(predict_away_goals[3],predict_away_xG[0]) * 100
a_home2_away3 = poisson.pmf(predict_home_goals[2],predict_home_xG[0]) * poisson.pmf(predict_away_goals[3],predict_away_xG[0]) * 100

## add probability for draws to df
predict_outcome = pd.DataFrame(xG_fixtures[['fixture.id','teams.home.name','teams.away.name']])
predict_outcome['draw 0 - 0'] = d_home0_away0.tolist()
predict_outcome['draw 1 - 1'] = d_home1_away1.tolist()
predict_outcome['draw 2 - 2'] = d_home2_away2.tolist()
predict_outcome['draw 3 - 3'] = d_home3_away3.tolist()

## add probability for home wins to df
predict_outcome['home 1 - 0'] = h_home1_away0.tolist()

predict_outcome['home 2 - 0'] = h_home2_away0.tolist()
predict_outcome['home 2 - 1'] = h_home2_away1.tolist()

predict_outcome['home 3 - 0'] = h_home3_away0.tolist()
predict_outcome['home 3 - 1'] = h_home3_away1.tolist()
predict_outcome['home 3 - 2'] = h_home3_away2.tolist()

## add probability for away wins to df
predict_outcome['away 0 - 1'] = a_home0_away1.tolist()

predict_outcome['away 0 - 2'] = a_home0_away2.tolist()
predict_outcome['away 1 - 2'] = a_home1_away2.tolist()

predict_outcome['away 0 - 3'] = a_home0_away3.tolist()
predict_outcome['away 1 - 3'] = a_home1_away3.tolist()
predict_outcome['away 2 - 3'] = a_home2_away3.tolist()

# sum probabilities for home/draw/away %
col_list_home = list(predict_outcome.columns.str.startswith('home'))
predict_outcome['home %'] = predict_outcome.loc[:,col_list_home].sum(axis=1)

col_list_draw = list(predict_outcome.columns.str.startswith('draw'))
predict_outcome['draw %'] = predict_outcome.loc[:,col_list_draw].sum(axis=1)

col_list_away = list(predict_outcome.columns.str.startswith('away'))
predict_outcome['away %'] = predict_outcome.loc[:,col_list_away].sum(axis=1)
                                                        
print(predict_outcome)

输出如我所愿:

 fixture.id   teams.home.name  teams.away.name  draw 0 - 0  draw 1 - 1 and so on..
 812312       PSV Eindhoven    Vitesse Arnhem   2.43894     8.258669

但我的代码变得像这样难以阅读。 当我尝试将列表用作参数时,出现以下值错误:

ValueError: operands could not be broadcast together with shapes (1,9) (6,)

我真的希望有人能帮助我实现上述过程的自动化。 感谢任何帮助,提前致谢!

我不知道 xG_fixtures 中有什么,但一个好的起点是使用动态结构而不是变量名。例如用您的结果填充列表或数据框。还要查看哪些变化(目标)和哪些保持不变,并找到一种方法来迭代变化的部分。无论是 for 循环还是矢量化。

score_prob_df = pd.DataFrame(columns=["home", "away", "prob"])

for home_goals in range(4):
    for away_goals in range(4):
        score_prob_df = score_prob_df.append(
            {
                "home": home_goals,
                "away": away_goals,
                "prob": poisson.pmf(predict_home_goals[home_goals],predict_home_xG[0]) * poisson.pmf(predict_away_goals[away_goals],predict_away_xG[0]) * 100
            }, ignore_index = True
        )