python - 通过循环 Python 将值列表传递给 poisson.pmf
问题描述
我对 python 和编码的世界相当陌生,所以如果我遗漏了一些明显的东西或找不到解决方案的信息,请告诉我,提前谢谢!
我的脚本使用泊松分布来计算足球比赛中主队和客队可能进球的概率百分比。这需要 4 个参数(列表),如下所述。
results = poisson.pmf(predict_home_goals,predict_home_xG) * poisson.pmf(predict_away_goals,predict_away_xG) * 100
结果是特定结果的概率,例如 12.2343 % 对于每场比赛(在本例中为 9 场比赛)我想计算概率。每个结果并将其作为列/行添加到 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
)
推荐阅读
- python - 用 PYTHON 以“形式”同时显示 IMAGE 和 TEXTBOX
- css - 如何减少字段集/表单组中过多的空白
- c++ - Detours Hook FindNextFileW Hang
- magento - magento 1.7 缺少左侧客户帐户导航
- machine-learning - MxNet 无法保存网络的所有参数
- javascript - 如何从数据数组(JS)中删除特定列
- spring-boot-admin - Spring Boot Admin Client 未注册应用程序
- python-3.x - 熊猫如何在月底前获得n天的日期
- java - 在 ArrayList INSIDE Stack 中搜索元素
- c# - 使用 C# .NET 将 PDF 转换为 Excel