Home > Enterprise >  Pass list of values to poisson.pmf by looping Python
Pass list of values to poisson.pmf by looping Python

Time:11-20

I am fairly new to the world of python and coding so if i am missing something obvious or information to find a solution please let me know, thanks in advance!

My script is using the poisson distribution to calculate the prob % of possible goals scored for home team and away team in a football match. This requires 4 arguments (lists) as stated below.

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

The result is the probability for a specific outcome for example 12.2343 % For each game (in this case 9 games) i would like to calculate the prob. per outcome and add this as column/rows to a DataFrame and eventually sum prob% per outcome. The problem is i can't seem to "loop" this calculation but i have to code each outcome manually, so the piece of code now looks like this:

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)

The output is as i want it to be:

 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

But my code is getting kind of unreadable like this. When i try to use the lists as arguments i get the following valueerror:

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

i really hope someone can help me with automating the process as stated above. Any help is appreciated, thanks in advance!

CodePudding user response:

I don't know what's in xG_fixtures but a good starting point would be to use a dynamic structure instead of variable names. e.g. filling a list or dataframe with your results. Also look at what changes (the goals) and what stays constant and find a way to iterate over the changing part. Be it with for loops or vectorization.

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
        )
  • Related