我对python和编码的世界相当陌生,所以如果我遗漏了一些明显的东西或寻找解决方案的信息,请让我知道,提前谢谢!
我的脚本使用泊松分布来计算主队和客队在一场足球比赛中可能进球的概率百分比。这需要4个参数(列表),如下所述。
results = poisson.pmf(predict_home_goals,predict_home_xG) * poisson.pmf(predict_away_goals,predict_away_xG) * 100结果是一个特定结果的概率,例如每个游戏(在这个例子中是9个游戏)的12.2343 %,我想计算这个概率。每个结果,并将其作为列/行添加到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但是我的代码就像这样变得不可读了。当我尝试使用列表作为参数时,我得到以下值lists:
ValueError: operands could not be broadcast together with shapes (1,9) (6,)我真的希望有人能帮助我自动化的过程如上所述。如有任何帮助,我们将非常感谢!
发布于 2021-11-16 14:24:20
我不知道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
)https://stackoverflow.com/questions/69990690
复制相似问题