<jupyter_start><jupyter_text>2. Exploring data regarding NHL games The is a very interesting dataset regarding [NHL game data](https://www.kaggle.com/martinellis/nhl-game-data) with a lot of amazing details. This data is unfortunately too big to store it together with this notebook in Github.---**To be able to execute this notebook, please download your own local version of the data from https://www.kaggle.com/martinellis/nhl-game-data and store it into the folder "data/nhl/nhl_stats/"**---<jupyter_code>import pandas as pd
import datetime as dt
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from IPython.display import Image

sns.set()<jupyter_output><empty_output><jupyter_text>Dataset structure<jupyter_code>Image(filename='data/images/table_relationships.JPG', width=800)<jupyter_output><empty_output><jupyter_text>Reading and exploring data about skating players in the statististics<jupyter_code>df = pd.read_csv('data/nhl/nhl_stats/game_skater_stats.csv')
df.head()
df.columns<jupyter_output><empty_output><jupyter_text>---Reading data about players. This data contains similar fields for each player like the [salaries dataset](https://www.kaggle.com/camnugent/predict-nhl-player-salaries). Possible merge later if needed.<jupyter_code>df = pd.read_csv('data/nhl/nhl_stats/player_info.csv')
df.head()
df.info()
df['birthDateDate']= pd.to_datetime(df['birthDate'])
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 3925 entries, 0 to 3924
Data columns (total 13 columns):
 #   Column              Non-Null Count  Dtype         
---  ------              --------------  -----         
 0   player_id           3925 non-null   int64         
 1   firstName           3925 non-null   object        
 2   lastName            3925 non-null   object        
 3   nationality         3917 non-null   object        
 4   birthCity           3920 non-null   object        
 5   primaryPosition     3925 non-null   object        
 6   birthDate           3925 non-null   object        
 7   birthStateProvince  2802 non-null   object        
 8   height              3922 non-null   object        
 9   height_cm           3922 non-null   float64       
 10  weight              3922 non-null   float64       
 11  shootsCatches       3908 non-null   object        
 12  birthDateDate       3925 non-null   datetime64[ns]
dtypes: datetime64[ns](1), float64(2), int64(1), obje[...]<jupyter_text>---Now let's have a quick look at the game if there is something worth to be mentioned<jupyter_code>df = pd.read_csv('data/nhl/nhl_stats/game.csv')
df.head()
df = pd.read_csv('data/nhl/nhl_stats/game_plays.csv')
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>This is a very interesting table we can extract a lot of details from. There are more than 5 million entries describing the progress of several NHL games. How many *events* per game do we have in average (median):<jupyter_code># how many 'events' per game?
df.groupby('game_id').size().median()<jupyter_output><empty_output><jupyter_text>Besides others, there is a very interesting insight in the data regarding the shots on goal. One can figure out, what shots were made, broken down by the type of the shot and the location. This is very interesting. We also can see if the shot lead to a goal. One could immediately try to figure out, what is the probability of the goal based on the position (and possibly some other parameters). This is something I could approach later. Let's have a closer look at the shots data first. Focusing to goals first.<jupyter_code>df_goals = df[df.event=='Goal'][['x', 'y', 'secondaryType']]
df_goals.dropna()<jupyter_output><empty_output><jupyter_text>Interesting to see what types of shot leads to a goal<jupyter_code>df_goals.secondaryType.value_counts()<jupyter_output><empty_output><jupyter_text>*Wrist shot* is apparently a good way to make a goal. I'll investigate it much closer later in this project.<jupyter_code>df_goal_counts = df_goals.secondaryType.value_counts()[:20].reset_index().rename(columns={'secondaryType':'cnt', 'index':'type'}).sort_values('cnt')

plt.barh(df_goal_counts.type, df_goal_counts.cnt);
plt.xticks(rotation=90);
plt.title('Goals by shot type');<jupyter_output><empty_output><jupyter_text>And now, let's visualize these shots and their locations on a real ice hockey rink.<jupyter_code># example of slap shot locations
plt.figure(figsize=(10, 6), dpi=80)
img=plt.imread('data/images/rink_allstars.png')
plt.grid()
plt.axis("off")
plt.imshow(img, extent=[-105, 105, -48, 48])
#plt.scatter(df_goals.x, df_goals.y, s=.2, c='green');
plt.scatter(df_goals[df_goals.secondaryType=='Backhand'].x, df_goals[df_goals.secondaryType=='Backhand'].y, s=.2, c='green');
plt.scatter(df_goals[df_goals.secondaryType=='Tip-In'].x, df_goals[df_goals.secondaryType=='Tip-In'].y, s=.2, c='brown');
plt.scatter(df_goals[df_goals.secondaryType=='Wrist Shot'].x, df_goals[df_goals.secondaryType=='Wrist Shot'].y, s=.2, c='orange');
plt.scatter(df_goals[df_goals.secondaryType=='Snap Shot'].x, df_goals[df_goals.secondaryType=='Snap Shot'].y, s=.2, c='yellow');
plt.scatter(df_goals[df_goals.secondaryType=='Slap Shot'].x, df_goals[df_goals.secondaryType=='Slap Shot'].y, s=.2, c='lightblue');
plt.scatter(df_goals[df_goals.secondaryType=='Deflected'].x, df_goals[df_goals.secondaryType=='Deflected'].y, s=.2, c='black');
plt.scatter(df_goals[df_goals.secondaryType=='Wrap-around'].x, df_goals[df_goals.secondaryType=='Wrap-around'].y, s=.2, c='red');
plt.title('Goals by shot type and location');<jupyter_output><empty_output><jupyter_text>---Let's see the time frame of the dataset:<jupyter_code>df = pd.read_csv('data/nhl/nhl_stats/game.csv')
df.head()
print(f'Games documented from {df.date_time_GMT.min()} to {df.date_time_GMT.max()}!')
df = pd.read_csv('data/nhl/nhl_stats/team_info.csv')
df.head(15)<jupyter_output><empty_output><jupyter_text>Checking random sample: Based on the data, there was a game (id=2016020045) on date_time_GMT=2016-10-19 between teams home_team_id=16 (Chicago Blackhawks) and away_team_id=4 (Philadelphia Flyers), with the result home_goals=7 and away_goals=4.---This is really true. There was a game like this according to Google:<jupyter_code>Image(filename='data/images/game_2016020045.jpg', width=600)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12381703470031545, 'fraction_numerical': 0.026182965299684544, 'mean_word_length': 4.616474756421612, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '622735', 'license': 'MIT', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2113, 'n_words': 547, 'path': '2_nhl_explore_games.ipynb', 'repo_name': 'rmnng/dsnanocapstone', 'size': '6340'}