<jupyter_start><jupyter_text>8 classical football metrics  (A) number of passes<jupyter_code>df_pass = pd.read_csv('./2020_Problem_D_DATA/passingevents.csv')
pass_huskies = df_pass[df_pass['TeamID'] == 'Huskies']
huskies_pass = pass_huskies.groupby('MatchID')['TeamID'].count().values

pass_opponent = df_pass[df_pass['TeamID'] != 'Huskies']
opponent_pass = pass_opponent.groupby('MatchID')['TeamID'].count().values

df_matches['Huskies_pass'] = huskies_pass
df_matches['Opponent_pass'] = opponent_pass

df_match_pass = df_matches.groupby('OpponentID')[['Huskies_pass', 'Opponent_pass']].sum()
df_match_pass.to_csv('Passes.csv')
df_match_pass[:3]<jupyter_output><empty_output><jupyter_text>(B) number of shots<jupyter_code>df = pd.read_csv('./2020_Problem_D_DATA/fullevents.csv')
df_huskies = df[df['TeamID'] == 'Huskies']
df_opponents = df[df['TeamID'] != 'Huskies']

# Huskies队在所有场的比赛中射门次数
huskies_shot = df_huskies[df_huskies['EventType'] == 'Shot'].groupby('MatchID')['TeamID'].count().values

# Opponent某队在所有场的比赛中射门次数（每场的队不一样）
opponent_shot = df_opponents[df_opponents['EventType'] == 'Shot'].groupby('MatchID')['TeamID'].count().values

df_matches['Huskies_shot'] = huskies_shot
df_matches['Opponent_shot'] = opponent_shot

df_shots = df_matches.groupby('OpponentID')[['Huskies_shot', 'Opponent_shot']].sum()
df_shots.to_csv('./Shots.csv')
df_shots[:3]<jupyter_output><empty_output><jupyter_text>(C) number of goals<jupyter_code>df_goals = df_matches.groupby('OpponentID')[['OwnScore','OpponentScore']].sum()
df_goals = df_goals.rename(columns={"OwnScore": "Huskies_score", "OpponentScore": "Opponent_score"})
df_goals.to_csv('./Goals.csv')
df_goals[:3]<jupyter_output><empty_output><jupyter_text>(D) number of points  (E) average of X<jupyter_code>def cal_mean_position(direction):
    huskies_coor = df_pass[df_pass['TeamID'] == 'Huskies']
    opponent_coor = df_pass[df_pass['TeamID'] != 'Huskies']

    huskies_coor_match = huskies_coor.groupby('MatchID')['EventOrigin_{}'.format(direction)].mean()
    opponent_coor_match = opponent_coor.groupby('MatchID')['EventOrigin_{}'.format(direction)].mean()

    df_matches['Huskies_{}'.format(direction)] = huskies_coor_match.values # 注意这里要用.values赋值，否则第零行是空的
    df_matches['Opponent_{}'.format(direction)] = opponent_coor_match.values

    huskies_coor_id = df_matches.groupby('OpponentID')['Huskies_{}'.format(direction)].mean()
    opponent_coor_id = df_matches.groupby('OpponentID')['Opponent_{}'.format(direction)].mean()
    
    return huskies_coor_id, opponent_coor_id

huskies_x_id, opponent_x_id = cal_mean_position(direction='x')
df_coor_x = pd.DataFrame([huskies_x_id, opponent_x_id]).T
df_coor_x.to_csv('Coorx.csv')
df_coor_x[:3]<jupyter_output><empty_output><jupyter_text>(F) average of Y<jupyter_code>huskies_y_id, opponent_y_id = cal_mean_position(direction='y')

df_coor_y = pd.DataFrame([huskies_y_id, opponent_y_id]).T
df_coor_y.to_csv('Coory.csv')
df_coor_y[:3]<jupyter_output><empty_output><jupyter_text>(G) dispersion<jupyter_code>h_coor_match = pd.merge(pass_huskies, df_matches, how='left', on='MatchID')
h_coor_match['Distance'] = np.sqrt(np.square(h_coor_match['EventOrigin_x'] - h_coor_match['Huskies_x']) + \
                                   np.square(h_coor_match['EventOrigin_y'] - h_coor_match['Huskies_y']))

h_dispersion_match = h_coor_match.groupby('MatchID')['Distance'].std(ddof=1).values
df_matches['Huskies_dispersion'] = h_dispersion_match
h_dispersion_id = df_matches.groupby('OpponentID')['Huskies_dispersion'].mean()
o_coor_match = pd.merge(pass_opponent, df_matches, how='left', on='MatchID')
o_coor_match['Distance'] = np.sqrt(np.square(o_coor_match['EventOrigin_x'] - o_coor_match['Opponent_x']) + \
                                   np.square(o_coor_match['EventOrigin_y'] - o_coor_match['Opponent_y']))

o_dispersion_match = o_coor_match.groupby('MatchID')['Distance'].std(ddof=1).values
df_matches['Opponent_dispersion'] = o_dispersion_match
o_dispersion_id = df_matches.groupby('OpponentID')['Opponent_dispersion'].mean()
df_dispersion = pd.DataFrame([h_dispersion_id, o_dispersion_id]).T
df_dispersion.to_csv('Dispersion.csv')
df_dispersion[:3]<jupyter_output><empty_output><jupyter_text>(H) advance ratio<jupyter_code>h_coor_match['Delta_x'] = np.abs(h_coor_match['EventDestination_x'] - h_coor_match['EventOrigin_x'])
h_coor_match['Delta_y'] = np.abs(h_coor_match['EventDestination_y'] - h_coor_match['EventOrigin_y'])
h_dx_match = h_coor_match.groupby('MatchID')['Delta_x'].sum().values
h_dy_match = h_coor_match.groupby('MatchID')['Delta_y'].sum().values
df_matches['Huskies_ratio'] = h_dy_match / h_dx_match
o_coor_match['Delta_x'] = np.abs(o_coor_match['EventDestination_x'] - o_coor_match['EventOrigin_x'])
o_coor_match['Delta_y'] = np.abs(o_coor_match['EventDestination_y'] - o_coor_match['EventOrigin_y'])
o_dx_match = o_coor_match.groupby('MatchID')['Delta_x'].sum().values
o_dy_match = o_coor_match.groupby('MatchID')['Delta_y'].sum().values
df_matches['Opponent_ratio'] = o_dy_match / o_dx_match
h_ratio_id = df_matches.groupby('OpponentID')['Huskies_ratio'].mean()
o_ratio_id = df_matches.groupby('OpponentID')['Opponent_ratio'].mean()
Ratio = pd.merge(h_ratio_id, o_ratio_id, how='left', on='OpponentID')
Ratio.to_csv('AdvanceRatio.csv')
Ratio<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code>def calc_mean_std(x):
    return (np.mean(x), np.std(x, ddof=1) / np.sqrt(len(x)))
### overall
from scipy import stats
fig, axes = plt.subplots(1, 3, figsize=(15, 4))

file_set = ['Passes', 'Shots', 'Goals']
column_names = ['_pass', '_shot', '_score']
ylabel_set = ['Passes', 'Shots', 'Goals']
color_dict = {True: 'orange', False: 'dodgerblue'}

tag_set = ['A', 'B', 'C', 'D', 'E']

for ind in range(3):
    ax = axes[ind % 3]
    
    df = pd.read_csv(file_set[ind] + '.csv')
    column = column_names[ind]
    
    huskie = calc_mean_std(df['Huskies' + column]) 
    oppo = calc_mean_std(df['Opponent' + column])
    
    levene = stats.levene(df['Huskies' + column].values, df['Opponent' + column].values)
    ttest = stats.ttest_ind(df['Huskies' + column].values, 
                            df['Opponent' + column].values, 
                            equal_var=(levene.pvalue > 0.05))
    pvalue = ttest.pvalue
    
    x_label = ['Huskies', 'Opponents']
    x_pos = [1.5, 3.0]
    
    ax.bar(x_pos, [huskie[0], oppo[0]], 
           yerr=[huskie[1], oppo[1]], 
           error_kw={'capsize': 3}, 
           width=1, color=color_dict[pvalue < 0.05], 
           edgecolor='gray',
           alpha=0.6)
    ylim = ax.get_ylim()
    if ind == 1:
        ax.text(0.5, ylim[1] - (ylim[1] - ylim[0]) * 0.11, r'$p={:.4f}$'.format(pvalue))
    else:
        ax.text(0.5, ylim[1] - (ylim[1] - ylim[0]) * 0.11, r'$p={:.3f}$'.format(pvalue))
    ax.set_xlim(0, 4.5)
    ax.set_ylim(ylim[0], ylim[0] + 1.1 * (ylim[1] - ylim[0]))
    ax.set_xticks(x_pos)
    ax.set_xticklabels(x_label)
    ax.set_ylabel(ylabel_set[ind])
    ax.tick_params(direction='in')
    
    ax.text(3.9, ylim[1] - (ylim[1] - ylim[0]) * 0.11, 
            r'$\textbf{' + tag_set[ind] + '}$', 
            fontsize=17,
            fontweight='bold',
            horizontalalignment='center', 
            verticalalignment='center')
    ax.set_ylim(ylim)

    
plt.subplots_adjust(wspace=0.25)

plt.savefig('./pass_shot_goal.png', dpi=200, bbox_inches='tight')
plt.savefig('./pass_shot_goal.pdf', dpi=200, bbox_inches='tight')
### Structual
from scipy import stats
fig, axes = plt.subplots(1, 4, figsize=(20, 4))

file_set = ['Coorx', 'Coory', 'Dispersion', 'AdvanceRatio']
column_names = ['_x', '_y', '_dispersion', '_ratio']
ylabel_set = [r'$\langle X \rangle$', r'$\langle Y \rangle$', 'Dispersion', 
              r'$\langle \Delta Y \rangle / \langle \Delta X \rangle$']
color_dict = {True: 'orange', False: 'dodgerblue'}

tag_set = ['A', 'B', 'C', 'D', 'E']

for ind in range(4):
    ax = axes[ind % 4]
    
    df = pd.read_csv(file_set[ind] + '.csv')
    column = column_names[ind]
    
    huskie = calc_mean_std(df['Huskies' + column]) 
    oppo = calc_mean_std(df['Opponent' + column])
    
    levene = stats.levene(df['Huskies' + column].values, df['Opponent' + column].values)
    ttest = stats.ttest_ind(df['Huskies' + column].values, 
                            df['Opponent' + column].values, 
                            equal_var=(levene.pvalue > 0.05))
    pvalue = ttest.pvalue
    
    x_label = ['Huskies', 'Opponents']
    x_pos = [1.5, 3.0]
    
    ax.bar(x_pos, [huskie[0], oppo[0]], 
           yerr=[huskie[1], oppo[1]], 
           error_kw={'capsize': 3}, 
           width=1, color=color_dict[pvalue < 0.05], 
           edgecolor='gray',
           alpha=0.6)
    ylim = ax.get_ylim()
    ax.text(0.5, ylim[1] - (ylim[1] - ylim[0]) * 0.02, r'$p={:.3f}$'.format(pvalue))
    ax.set_xlim(0, 4.5)
    ax.set_ylim(ylim[0], ylim[0] + 1.1 * (ylim[1] - ylim[0]))
    ax.set_xticks(x_pos)
    ax.set_xticklabels(x_label)
    ax.set_ylabel(ylabel_set[ind])
    ax.tick_params(direction='in')
    
    ax.text(3.9, ylim[1] - (ylim[1] - ylim[0]) * 0.02, 
            r'$\textbf{' + tag_set[ind] + '}$', 
            fontsize=17,
            fontweight='bold',
            horizontalalignment='center', 
            verticalalignment='center')
    
plt.subplots_adjust(wspace=0.25)

plt.savefig('./Draft/spatial_structure.png', dpi=200, bbox_inches='tight')
plt.savefig('./Draft/spatial_structure.pdf', dpi=200, bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17770512550094916, 'fraction_numerical': 0.016030373338958025, 'mean_word_length': 4.768248175182482, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 35, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '93686', 'license': 'MIT', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3816, 'n_words': 462, 'path': 'Classical-football-metrics.ipynb', 'repo_name': 'AstroJacobLi/FootballTeamStrategy', 'size': '9482'}