<jupyter_start><jupyter_text>Data Frame<jupyter_code>matches_df.head(2)<jupyter_output><empty_output><jupyter_text>RQ1<jupyter_code>point_data = list()
for i in range(len(matches_df)):
    gameweek = matches_df.iloc[i].gameweek# iloc is used for the data in a data fram
    label = matches_df.iloc[i].label
    [[home_team, away_team], [home_score, away_score]] = [[o.strip() for o in s.split('-')] for s in label.split(',')]# here we have converted the label column into a nested list, od home team/away team . HOme score and Away score 
    home_score = int(home_score)
    away_score = int(away_score)
    if home_score > away_score:# we have identified the winner /looser and Draw
        home_point = 3
        away_point = 0
    if away_score > home_score:
        away_point = 3
        home_point = 0
    if away_score == home_score:
        home_point = 1
        away_point = 1
    point_data.append([gameweek, home_team, home_point]) #here we have summarize the team with week and point all together
    point_data.append([gameweek, away_team, away_point])
point_df = pd.DataFrame(point_data, columns=['gameweek', 'team', 'point']) #setup a datafram<jupyter_output><empty_output><jupyter_text>Summarize Dataframe<jupyter_code>point_df.head(5)<jupyter_output><empty_output><jupyter_text>Final Plotting of Data<jupyter_code>import matplotlib.pyplot as plt
colormap = plt.cm.gist_ncar

team_table = point_df.pivot(index= 'gameweek', columns='team', values=['point']).cumsum().fillna(method = 'backfill').fillna(method='ffill')
plt.figure(figsize=[20,12])
color = [colormap(i) for i in np.linspace(0, 0.9, len(team_table.columns))]
[plt.plot(team_table.iloc[:,i], color = color[i]) for i in range(len(team_table.columns))]
plt.legend([team_table.columns[i][1] for i in range(len(team_table.columns))], fontsize=12)
plt.xticks(team_table.index)
plt.xlabel("Weeks", fontsize=16)
plt.ylabel("Points", fontsize=16)<jupyter_output><empty_output><jupyter_text>RQ2 null hypothesis : if a team is playing in Home or away it will not effect their performance ; pvalue ranges upto 95 %alternative Hypo: if tram is playing in their home it effects below 5 %<jupyter_code>point_data = list()
result = {1 : "draw", 0 : "lost", 3: "win"}
for i in range(len(matches_df)):
    gameweek = matches_df.iloc[i].gameweek
    label = matches_df.iloc[i].label
    [[home_team, away_team], [home_score, away_score]] = [[o.strip() for o in s.split('-')] for s in label.split(',')]
    home_score = int(home_score)
    away_score = int(away_score)
    if home_score > away_score:## setting up the Scoring Mechanism
        home_point = 3
        away_point = 0
    if away_score > home_score:
        away_point = 3
        home_point = 0
    if away_score == home_score:
        home_point = 1
        away_point = 1
    point_data.append([gameweek, home_team, home_point, 'home', result[home_point]])
    point_data.append([gameweek, away_team, away_point, 'away', result[away_point]])
point_df = pd.DataFrame(point_data, columns=['gameweek', 'team', 'point', 'home_away', 'result'])
point_df.head(5)<jupyter_output><empty_output><jupyter_text>Final Dataset:Filtered and Selected 5 Teams<jupyter_code>teams = ['Arsenal', 'Chelsea', 'Liverpool', 'Manchester United', 'Manchester City']
point_df_selected = point_df[[t in teams for t in point_df['team']]]
tab = pd.crosstab(index=[point_df_selected['team'],point_df_selected['home_away']], columns=point_df_selected['result'])<jupyter_output><empty_output><jupyter_text>final Dataset<jupyter_code>tab<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>from scipy.stats import chi2_contingency
chi2_contingency(tab.iloc[:2,:].values)
tab.iloc[:2,:]## the result table
chi2_contingency(tab.iloc[:2,:].values) ##
tab.iloc[2:4,:]
chi2_contingency(tab.iloc[2:4,:].values)
tab.iloc[4:6,:]
chi2_contingency(tab.iloc[4:6,:].values)
tab.iloc[6:8,:]
chi2_contingency(tab.iloc[6:8,:].values)
tab.iloc[8:10,:]
chi2_contingency(tab.iloc[8:10,:].values)<jupyter_output><empty_output><jupyter_text>RQ3  Import Teams Data<jupyter_code>teams_df = pd.read_json('soccer data/teams.json', orient = 'values')
teams_df.head()<jupyter_output><empty_output><jupyter_text>Filtering the Teams Data for RQ3<jupyter_code>teams_df = teams_df.loc[[str(t).find('England') != -1 for t in teams_df.area]]# fileted only 'England' from AREA for Premiere league
teams_df = teams_df.loc[[str(t).find('club') != -1 for t in teams_df.type]]# filtered for TYPE 'club' Only
teams_df.head()<jupyter_output><empty_output><jupyter_text>Import Coach Data<jupyter_code>coaches_df = pd.read_json('soccer data/coaches.json', orient = 'values')
coaches_df.head()<jupyter_output><empty_output><jupyter_text>Combining The two Data with a key<jupyter_code>coaches_teams_df = pd.merge(left=teams_df, right=coaches_df,
                            left_on='wyId', right_on='currentTeamId',
                            how='inner')[['name', 'birthDate', 'shortName']].groupby('name').agg('max', on = 'birthDate').sort_values(by='birthDate', ascending = False)
now = pd.Timestamp('now')## for current time
age = (now - pd.to_datetime(coaches_teams_df['birthDate'], yearfirst=True)).astype('<m8[Y]')## to calculate the age of coaches
coaches_teams_df['age'] = age<jupyter_output><empty_output><jupyter_text>Result 1 : The Ranking<jupyter_code>print(coaches_teams_df.head(10))<jupyter_output>birthDate       shortName   age
name                                               
AFC Bournemouth    1977-11-29         <NAME>  41.0
Everton            1977-07-12     <NAME>  42.0
Newcastle United   1973-02-19  <NAME>  46.0
Tottenham Hotspur  1972-03-02   <NAME>  47.0
Arsenal            1971-11-03      Unai Emery  47.0
Burnley            1971-06-28        <NAME>  48.0
Manchester City    1971-01-18       Guardiola  48.0
Watford            1970-05-01     <NAME>  49.0
Southampton        1967-08-09   <NAME>  52.0
Liverpool          1967-06-16        <NAME>  52.0<jupyter_text>Result 2: The distribution (Boxplot)<jupyter_code>plt.boxplot(age)#, density = True, edgecolor='black', linewidth=1.2)
plt.ylabel('Age(year)', fontsize=16)
plt.xticks([1], [''])
plt.title('Boxplot of Coaches Ages', fontsize=18)<jupyter_output><empty_output><jupyter_text>RQ4  Importing the Event File<jupyter_code>with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    with Z.open('events_England.json') as f:
        events_df = pd.read_json(f)
events_df.head()<jupyter_output><empty_output><jupyter_text>Driving the Desired Column<jupyter_code>passes_df = events_df[['playerId', 'matchId', 'eventName', 'tags']]
passes_df.head()<jupyter_output><empty_output><jupyter_text>Passes Assesment<jupyter_code>passes_df = passes_df.loc[passes_df.eventName == 'Pass']
passes_df['pass_success'] = [str(t).find('1801') != -1 for t in passes_df.tags]
passes_df.drop(columns=['tags','eventName'], inplace = True)
passes_df.head()<jupyter_output><empty_output><jupyter_text>Summarizing the Count of Passes<jupyter_code>passes_df = passes_df.groupby(['playerId', 'matchId'], as_index = False, group_keys = False).agg(['sum','count'] , on='pass_success').reset_index()
passes_df.columns = ['playerId', 'matchId', 'successive_pass', 'total_pass']
passes_df = passes_df.loc[passes_df['playerId'] != 0]
passes_df.head()<jupyter_output><empty_output><jupyter_text>Identifying relation for Threshold<jupyter_code>plt.hist(passes_df['total_pass'])<jupyter_output><empty_output><jupyter_text>Assumption is that a player in a match less than 40 Passes , so he has not played enough time in a game.<jupyter_code>passes_df = passes_df.loc[passes_df['total_pass'] > 40]
passes_df.head()
passes_df.drop(columns = ['matchId'], inplace = True)<jupyter_output><empty_output><jupyter_text>Creating an Success rate<jupyter_code>passes_df = passes_df.groupby('playerId').agg('sum', level = 0, on = ['Successive_pass', 'total_pass']).reset_index()
passes_df.head()
passes_df['pass_success_rate %'] = passes_df['successive_pass']/passes_df['total_pass']*100
passes_df.head()<jupyter_output><empty_output><jupyter_text>outcome<jupyter_code>passes_top10 = passes_df.sort_values('pass_success_rate %', ascending=False).head(10)
passes_top10<jupyter_output><empty_output><jupyter_text>Removing the Goal Keeper from Players Data<jupyter_code>players_df = pd.read_json(r"soccer data/players.json")
players_df = players_df.loc[[str(t).find('GK') == -1 for t in players_df.role]]
players_df.head()<jupyter_output><empty_output><jupyter_text>combining names<jupyter_code>players_name = players_df[['firstName','middleName','lastName', 'wyId']].copy()
players_name['fullName'] = players_name['firstName'] + ' ' + players_name['middleName'] + ' ' + players_name['lastName']
players_name.head()<jupyter_output><empty_output><jupyter_text>Dropping extra columns<jupyter_code>players_name.drop(columns = ['firstName', 'middleName', 'lastName'], inplace = True)
players_name.head()<jupyter_output><empty_output><jupyter_text>Joining Names over IDs<jupyter_code>passes_top10 = pd.merge(left=passes_top10, right=players_name, left_on='playerId', right_on='wyId', how='inner').head(10)<jupyter_output><empty_output><jupyter_text>Final Results<jupyter_code>passes_top10[['fullName','pass_success_rate %']]<jupyter_output><empty_output><jupyter_text>RQ 5<jupyter_code>airduels_df = events_df[['playerId', 'matchId', 'eventName', 'subEventName', 'tags']] # already Imported data
airduels_df.head()<jupyter_output><empty_output><jupyter_text>Assessing Dual Success<jupyter_code>airduels_df = airduels_df.loc[airduels_df.subEventName == 'Air duel']
airduels_df = airduels_df.loc[airduels_df.eventName == 'Duel']
airduels_df['duel_success'] = [str(t).find('1801') != -1 for t in airduels_df.tags]
airduels_df.drop(columns=['tags','eventName', 'subEventName'], inplace = True)
airduels_df.head()<jupyter_output><empty_output><jupyter_text>Summarize Player and Match wise Air Duels<jupyter_code>airduels_df = airduels_df.groupby(['playerId', 'matchId'], as_index = False, group_keys = False).agg(['sum','count'] , on='duel_success').reset_index()
airduels_df.columns = ['playerId', 'matchId', 'success_duels', 'total_duels']
airduels_df = airduels_df.loc[airduels_df['playerId'] != 0]
airduels_df.head()<jupyter_output><empty_output><jupyter_text>Assumption Considering players in each game made greater than 5 Air duels, defines that player has played enough in game<jupyter_code>airduels_df = airduels_df.loc[airduels_df['total_duels'] > 5]
airduels_df.head()<jupyter_output><empty_output><jupyter_text>Success Rate<jupyter_code>airduels_df = airduels_df.groupby(airduels_df["playerId"]).sum(on = ['success_duels' , 'total_duels']).reset_index()[['playerId','success_duels','total_duels']]
airduels_df['success_ratio'] = airduels_df['success_duels']/airduels_df['total_duels']*100
airduels_df.head()<jupyter_output><empty_output><jupyter_text>Reffering Player ID with Height<jupyter_code>players_height = players_df[['height', 'wyId']].copy()
players_height.head()<jupyter_output><empty_output><jupyter_text>Joining Height with Player ID<jupyter_code>airduels_height = pd.merge(left=airduels_df, right=players_height, left_on='playerId', right_on='wyId', how='inner')[['height', 'success_ratio']]
airduels_height.head()<jupyter_output><empty_output><jupyter_text>Creating Range of Heights for a enhanced Visualization of result<jupyter_code>airduels_height['height_range'] = pd.cut(airduels_height["height"], np.arange(165, 210, 5))
airduels_height.head()<jupyter_output><empty_output><jupyter_text>Result<jupyter_code>plt.figure(figsize=(15,7))
ranges = pd.unique(airduels_height['height_range']).sort_values()
for i,r in enumerate(ranges):
    temp_df = airduels_height.loc[airduels_height['height_range'] == r]
    plt.scatter(x = temp_df['height'], y = temp_df['success_ratio'])
plt.ylabel('Success Rate', fontsize = 16)
plt.xlabel('Height', fontsize = 16)<jupyter_output><empty_output><jupyter_text>RQ6  CRQ1<jupyter_code>events_df.head()
goals_df = events_df[['playerId', 'eventSec','teamId','tags','eventName', 'matchPeriod']]
goals_df.head()
tags101 = [str(t).find(' 101') != -1 for t in goals_df['tags']]
goals_df = goals_df.loc[tags101]
goals_df.head()
goals_df = goals_df.loc[goals_df['eventName'] != 'Save attempt']
goals_df['eventMin'] = goals_df['eventSec']//60 + 1
goals_df.head()
time_slots = [str(t) for t in pd.cut(goals_df['eventMin'], np.arange(0, 60, 9))]
goals_df['time_slot'] = time_slots
goals_df.head()
res = goals_df.groupby(['matchPeriod', 'time_slot']).count()[['playerId']]
res
res_plot = res.plot(kind='bar', legend=False)
res1 = goals_df.groupby(['teamId', 'time_slot', 'matchPeriod']).count()[['playerId']].reset_index()
res1.columns = ['teamId','time_slot','matchPeriod','scores']
res2 = res1.loc[res1['time_slot'] == '(36, 45]']
res3 = res2.loc[[str(t).find('2H') != -1 for t in res2['matchPeriod']]]
asd = pd.merge(left = res3, right=teams_df, left_on='teamId', right_on='wyId')[['time_slot','matchPeriod','scores','officialName']]
asd.max()
goals_df.head()
r0 = goals_df.groupby(['time_slot','playerId']).count().reset_index()[['time_slot','playerId','tags']]
r0.columns = ['time_slot','playerId','scores']
r0.head()
r1 = r0.groupby('playerId').count().reset_index()[['playerId','time_slot']]
r1.columns = ['playerId', 'nslot_covered']
r1.sort_values(by = 'nslot_covered', ascending=False)
events_df.head()
pd.unique(events_df['eventName'])
## RCQ2
with zipfile.ZipFile("Soccer Data\matches.zip") as Z:
    with Z.open('matches_Spain.json') as f:
            matches_df = pd.read_json(f)
with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    with Z.open('events_Spain.json') as f:
            events_spain_df = pd.read_json(f)
events_spain_df.iloc[594533,:]
barcelona_mardrid_id = 2565907 #Barcelona - Real Madrid
CR7_id = 3359  #CR7
LM_id = 3322 #Messi
def event_coordinate(coordinate):
    [[_,y_start],[_,x_start],[_,y_end],[_,x_end]] = [i.split(': ') for i in str(coordinate).replace('[','').replace(']','').replace('{','').replace('}','').split(',')]
    return int(x_start)/100*130, int(y_start)/100*90, int(x_end)/100*130, int(y_end)/100*90
barcelona_madrid_df = events_spain_df[['eventName','matchId','positions','playerId']].loc[
    events_spain_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_spain_df['matchId'].isin([barcelona_mardrid_id]) &
    events_spain_df['playerId'].isin([CR7_id])]
xy_CR7 = barcelona_madrid_df['positions'].apply(event_coordinate)
xy_CR7 = xy_CR7.loc[[i[2] != 0 and i[3] != 0 for i in xy_CR7]]
barcelona_madrid_df = events_spain_df[['eventName','matchId','positions','playerId']].loc[
    events_spain_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_spain_df['matchId'].isin([barcelona_mardrid_id]) &
    events_spain_df['playerId'].isin([LM_id])]
xy_LM = barcelona_madrid_df['positions'].apply(event_coordinate)
xy_LM = xy_CR7.loc[[i[2] != 0 and i[3] != 0 for i in xy_LM]]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import seaborn as sns
#Create figure
def plot_pitch():
    fig=plt.figure()
    fig.set_size_inches(7, 5)
    ax=fig.add_subplot(1,1,1)

    #Pitch Outline & Centre Line
    plt.plot([0,0],[0,90], color="black")
    plt.plot([0,130],[90,90], color="black")
    plt.plot([130,130],[90,0], color="black")
    plt.plot([130,0],[0,0], color="black")
    plt.plot([65,65],[0,90], color="black")

    #Left Penalty Area
    plt.plot([16.5,16.5],[65,25],color="black")
    plt.plot([0,16.5],[65,65],color="black")
    plt.plot([16.5,0],[25,25],color="black")

    #Right Penalty Area
    plt.plot([130,113.5],[65,65],color="black")
    plt.plot([113.5,113.5],[65,25],color="black")
    plt.plot([113.5,130],[25,25],color="black")

    #Left 6-yard Box
    plt.plot([0,5.5],[54,54],color="black")
    plt.plot([5.5,5.5],[54,36],color="black")
    plt.plot([5.5,0.5],[36,36],color="black")

    #Right 6-yard Box
    plt.plot([130,124.5],[54,54],color="black")
    plt.plot([124.5,124.5],[54,36],color="black")
    plt.plot([124.5,130],[36,36],color="black")

    #Prepare Circles
    centreCircle = plt.Circle((65,45),9.15,color="black",fill=False)
    centreSpot = plt.Circle((65,45),0.8,color="black")
    leftPenSpot = plt.Circle((11,45),0.8,color="black")
    rightPenSpot = plt.Circle((119,45),0.8,color="black")

    #Draw Circles
    ax.add_patch(centreCircle)
    ax.add_patch(centreSpot)
    ax.add_patch(leftPenSpot)
    ax.add_patch(rightPenSpot)

    #Prepare Arcs
    leftArc = Arc((11,45),height=18.3,width=18.3,angle=0,theta1=310,theta2=50,color="black")
    rightArc = Arc((119,45),height=18.3,width=18.3,angle=0,theta1=130,theta2=230,color="black")

    #Draw Arcs
    ax.add_patch(leftArc)
    ax.add_patch(rightArc)

    #Tidy Axes
    plt.axis('off')
plot_pitch()
x_coord = [i[0] for i in xy_CR7]
y_coord = [i[1] for i in xy_CR7]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
#plt.title('asdasd')
plt.show()
plot_pitch()
x_coord = [i[0] for i in xy_LM]
y_coord = [i[1] for i in xy_LM]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
plt.show()
with zipfile.ZipFile("Soccer Data\matches.zip") as Z:
    with Z.open('matches_Italy.json') as f:
            matches_df = pd.read_json(f)
with zipfile.ZipFile("Soccer Data\events.zip") as Z:
    with Z.open('events_Italy.json') as f:
            events_italy_df = pd.read_json(f)
juventus_napoli_id = 2576295 #Barcelona - Real Madrid
Jorg_id = 21315  # Jorginho
Pjan_id = 20443 # <NAME>
juventus_napoli_df = events_italy_df[['eventName','matchId','positions','playerId']].loc[
    events_italy_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_italy_df['matchId'].isin([juventus_napoli_id]) &
    events_italy_df['playerId'].isin([Jorg_id])]
xy_Jorg = juventus_napoli_df['positions'].apply(event_coordinate)
xy_Jorg = xy_Jorg.loc[[i[2] != 0 and i[3] != 0 for i in xy_Jorg]]
plot_pitch()
x_coord = [i[0] for i in xy_Jorg]
y_coord = [i[1] for i in xy_Jorg]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
for xy in xy_Jorg:
    plt.annotate(xy = [xy[2],xy[3]], arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color = "blue"),s ='',
                 xytext = [xy[0],xy[1]])
juventus_napoli_df = events_italy_df[['eventName','matchId','positions','playerId']].loc[
    events_italy_df['eventName'].isin(['Pass', 'Duel','Free Kick','Shot']) & 
    events_italy_df['matchId'].isin([juventus_napoli_id]) &
    events_italy_df['playerId'].isin([Pjan_id])]
xy_Pjan = juventus_napoli_df['positions'].apply(event_coordinate)
xy_Pjan = xy_Jorg.loc[[i[2] != 0 and i[3] != 0 for i in xy_Pjan]]
plot_pitch()
#plt.title('asdasd')
x_coord = [i[0] for i in xy_Pjan]
y_coord = [i[1] for i in xy_Pjan]
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30, shade_lowest = False)
for xy in xy_Pjan:
    plt.annotate(xy = [xy[2],xy[3]], arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color = "blue"),s ='',
                 xytext = [xy[0],xy[1]])
#events_italy_df
#events_italy_df.loc[events_italy_df['eventId'] == 2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16809828726117965, 'fraction_numerical': 0.03258889062418658, 'mean_word_length': 6.346080305927342, 'pattern_counts': {'":': 0, '<': 158, '<?xml version=': 0, '>': 165, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '37', 'dir': 'jupyter-structured-clean-dedup', 'id': '138066', 'license': 'MIT', 'n_tokens_mistral': 7835, 'n_tokens_neox': 7361, 'n_words': 1192, 'path': '.ipynb_checkpoints/HW03-checkpoint.ipynb', 'repo_name': 'MosesDastmard/soccer_analytics', 'size': '19254'}