1

That's my first post here. I'm doing a project in Python about Football Scores statistics and prediction. I got the ideas from this project and I was trying to recreate it, but it gives me an error like this

I'm re-writing the code for my needs, but even if I do a copy and paste of the original one, it gives me the same error, while in the original post it seems to go all good.

That's the part of code incriminated:

ax1.bar(chel_home.index-0.4,chel_home.values,width=0.4,color="#034694",label="Chelsea")

And it just says that "only size-1 arrays can be converted to Python scalars", but I don't really know where the problem could be because that's one of my first approaches with Python.

The full code is this:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn
from scipy.stats import poisson,skellam

epl_1617 = pd.read_csv("http://www.football-data.co.uk/mmz4281/1617/E0.csv")
epl_1617 = epl_1617[['HomeTeam','AwayTeam','FTHG','FTAG']]
epl_1617 = epl_1617.rename(columns={'FTHG': 'HomeGoals', 'FTAG': 'AwayGoals'})
epl_1617.head()

epl_1617 = epl_1617[:-10]
epl_1617.mean()

    # construct Poisson  for each mean goals value
poisson_pred = np.column_stack([[poisson.pmf(i, epl_1617.mean()[j]) for i in range(8)] for j in range(2)])

# plot histogram of actual goals
plt.hist(epl_1617[['HomeGoals', 'AwayGoals']].values, range(9), 
         alpha=0.7, label=['Home', 'Away'],normed=True, color=["#FFA07A", "#20B2AA"])

# add lines for the Poisson distributions
pois1, = plt.plot([i-0.5 for i in range(1,9)], poisson_pred[:,0],
                  linestyle='-', marker='o',label="Home", color = '#CD5C5C')
pois2, = plt.plot([i-0.5 for i in range(1,9)], poisson_pred[:,1],
                  linestyle='-', marker='o',label="Away", color = '#006400')

leg=plt.legend(loc='upper right', fontsize=13, ncol=2)
leg.set_title("Poisson           Actual        ", prop = {'size':'14', 

'weight':'bold'})
    
    plt.xticks([i-0.5 for i in range(1,9)],[i for i in range(9)])
    plt.xlabel("Goals per Match",size=13)
    plt.ylabel("Proportion of Matches",size=13)
    plt.title("Number of Goals per Match (EPL 2016/17 Season)",size=14,fontweight='bold')
    plt.ylim([-0.004, 0.4])
    plt.tight_layout()
    plt.show()

    # probability of draw between home and away team
    skellam.pmf(0.0,  epl_1617.mean()[0],  epl_1617.mean()[1])

    # probability of home team winning by one goal
    skellam.pmf(1,  epl_1617.mean()[0],  epl_1617.mean()[1])

    skellam_pred = [skellam.pmf(i,  epl_1617.mean()[0],  epl_1617.mean()[1]) for i in range(-6,8)]

plt.hist(epl_1617[['HomeGoals']].values - epl_1617[['AwayGoals']].values, range(-6,8), 
         alpha=0.7, label='Actual',normed=True)
plt.plot([i+0.5 for i in range(-6,8)], skellam_pred,
                  linestyle='-', marker='o',label="Skellam", color = '#CD5C5C')
plt.legend(loc='upper right', fontsize=13)
plt.xticks([i+0.5 for i in range(-6,8)],[i for i in range(-6,8)])
plt.xlabel("Home Goals - Away Goals",size=13)
plt.ylabel("Proportion of Matches",size=13)
plt.title("Difference in Goals Scored (Home Team vs Away Team)",size=14,fontweight='bold')
plt.ylim([-0.004, 0.26])
plt.tight_layout()
plt.show()

It works perfectly until this point, then there's the part that is giving me that error:

fig,(ax1,ax2) = plt.subplots(2, 1)


chel_home = epl_1617[epl_1617['HomeTeam']=='Chelsea'][['HomeGoals']].apply(pd.value_counts,normalize=True)
chel_home_pois = [poisson.pmf(i,np.sum(np.multiply(chel_home.values.T,chel_home.index.T),axis=1)[0]) for i in range(8)]
sun_home = epl_1617[epl_1617['HomeTeam']=='Sunderland'][['HomeGoals']].apply(pd.value_counts,normalize=True)
sun_home_pois = [poisson.pmf(i,np.sum(np.multiply(sun_home.values.T,sun_home.index.T),axis=1)[0]) for i in range(8)]

chel_away = epl_1617[epl_1617['AwayTeam']=='Chelsea'][['AwayGoals']].apply(pd.value_counts,normalize=True)
chel_away_pois = [poisson.pmf(i,np.sum(np.multiply(chel_away.values.T,chel_away.index.T),axis=1)[0]) for i in range(8)]
sun_away = epl_1617[epl_1617['AwayTeam']=='Sunderland'][['AwayGoals']].apply(pd.value_counts,normalize=True)
sun_away_pois = [poisson.pmf(i,np.sum(np.multiply(sun_away.values.T,sun_away.index.T),axis=1)[0]) for i in range(8)]

ax1.bar(chel_home.index-0.4,chel_home.values,width=0.4,color="#034694",label="Chelsea")
ax1.bar(sun_home.index,sun_home.values,width=0.4,color="#EB172B",label="Sunderland")
pois1, = ax1.plot([i for i in range(8)], chel_home_pois,
                  linestyle='-', marker='o',label="Chelsea", color = "#0a7bff")
pois1, = ax1.plot([i for i in range(8)], sun_home_pois,
                  linestyle='-', marker='o',label="Sunderland", color = "#ff7c89")
leg=ax1.legend(loc='upper right', fontsize=12, ncol=2)
leg.set_title("Poisson                 Actual                ", prop = {'size':'14', 'weight':'bold'})
ax1.set_xlim([-0.5,7.5])
ax1.set_ylim([-0.01,0.65])
ax1.set_xticklabels([])
# mimicing the facet plots in ggplot2 with a bit of a hack
ax1.text(7.65, 0.585, '                Home                ', rotation=-90,
        bbox={'facecolor':'#ffbcf6', 'alpha':0.5, 'pad':5})
ax2.text(7.65, 0.585, '                Away                ', rotation=-90,
        bbox={'facecolor':'#ffbcf6', 'alpha':0.5, 'pad':5})
 
ax2.bar(chel_away.index-0.4,chel_away.values,width=0.4,color="#034694",label="Chelsea")
ax2.bar(sun_away.index,sun_away.values,width=0.4,color="#EB172B",label="Sunderland")
pois1, = ax2.plot([i for i in range(8)], chel_away_pois,
                  linestyle='-', marker='o',label="Chelsea", color = "#0a7bff")
pois1, = ax2.plot([i for i in range(8)], sun_away_pois,
                  linestyle='-', marker='o',label="Sunderland", color = "#ff7c89")
ax2.set_xlim([-0.5,7.5])
ax2.set_ylim([-0.01,0.65])
ax1.set_title("Number of Goals per Match (EPL 2016/17 Season)",size=14,fontweight='bold')
ax2.set_xlabel("Goals per Match",size=13)
ax2.text(-1.15, 0.9, 'Proportion of Matches', rotation=90, size=13)
plt.tight_layout()
plt.show()

Here another graph should appear, but instead it just says: "only size-1 arrays can be converted to Python scalars".

I don't really know what to do and I'm starting to go crazy, so I really hope that you can help me. Thank you in advance and have a nice day everybody!

8
  • Your code works perfectly fine for me on matplotlib 2.2.2, pandas '0.23.0', and python 3.6.5. I see that you are using normed=True which is depreciated now and should be replaced by density=True. What versions are you using? Commented Jan 10, 2019 at 14:49
  • I'm using Python 3.6.0, what do you think i should do? Commented Jan 10, 2019 at 14:50
  • Yes, but what about matplotlib? Commented Jan 10, 2019 at 14:51
  • How can I see which version i'm using? Commented Jan 10, 2019 at 14:52
  • import matplotlib and then print (matplotlib.__version__). The same for pandas after import pandas Commented Jan 10, 2019 at 14:53

1 Answer 1

1

The problem is that your arrays for the bar plot are 2d arrays and you have to flatten them. This can be easily done using .flatten() which converts the 2d arrays in your code into 1-d arrays. If you look at chel_home.values, it looks like

array([[0.33333333],
       [0.22222222],
       [0.22222222],
       [0.16666667],
       [0.05555556]])

whereas what you need is

array([0.33333333, 0.22222222, 0.22222222, 0.16666667, 0.05555556])

Just replace the plotting commands in your code with the following lines

ax1.bar(chel_home.index-0.4,chel_home.values.flatten(),width=0.4,color="#034694",label="Chelsea")
ax1.bar(sun_home.index, sun_home.values.flatten(),width=0.4,color="#EB172B",label="Sunderland")

ax2.bar(chel_away.index-0.4,chel_away.values.flatten(),width=0.4,color="#034694",label="Chelsea")
ax2.bar(sun_away.index,sun_away.values.flatten(),width=0.4,color="#EB172B",label="Sunderland")

You can also use .ravel() instead of .flatten()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.