{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom scipy import stats\nfrom scipy.stats import norm\n\nsns.set_style('whitegrid')\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '../input/nfl-playing-surface-analytics/'\npList = pd.read_csv(path + 'PlayList.csv')\niRecord = pd.read_csv(path + 'InjuryRecord.csv')\n# pTrack = pd.read_csv(path + 'PlayerTrackData.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"pList.sample(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('The information of the PlayList.csv'.center(50, '-'))\nprint('The rows and columns of the data are {}'.format(pList.shape))\nTypes = pList.dtypes\nTotal = pList.isnull().sum().sort_values(ascending = False)\nPercentage = Total / pList.shape[0]\npd.concat([Total, Percentage, Types], axis = 1, keys = ['Total', 'Percentage', 'Types']).sort_values(by = ['Total'], ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"iRecord.sample(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('The information of the InjuryRecord.csv'.center(50, '-'))\nprint('The rows and columns of the data are {}'.format(iRecord.shape))\nTypes = iRecord.dtypes\nTotal = iRecord.isnull().sum().sort_values(ascending = False)\nPercentage = Total / iRecord.shape[0]\npd.concat([Total, Percentage, Types], axis = 1, keys = ['Total', 'Percentage', 'Types']).sort_values(by = ['Total'], ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# pTrack.sample(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print('The information of the PlayerTrackData.csv'.center(50, '-'))\n# print('The rows and columns of the data are {}'.format(pTrack.shape))\n# Types = pTrack.dtypes\n# Total = pTrack.isnull().sum().sort_values(ascending = False)\n# Percentage = Total / pTrack.shape[0]\n# pd.concat([Total, Percentage, Types], axis = 1, keys = ['Total', 'Percentage', 'Types']).sort_values(by = ['Total'], ascending = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Injury Rcord Data Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"iRecord.sample(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"iRecord.groupby(['BodyPart', 'Surface'])['DM_M1','DM_M7', 'DM_M28', 'DM_M42'].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12, 10))\nax1 = iRecord.groupby(['BodyPart', 'Surface'])['DM_M1','DM_M7', 'DM_M28', 'DM_M42'].sum().plot.bar()\nplt.title('The Corr of Body Part and Surface')\nplt.xlabel('Body Part , Surface')\nplt.xticks(rotation = 45)\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12, 10))\n\nsns.set_style('whitegrid')\nax2 = sns.catplot(x = 'Surface', hue = 'BodyPart', kind = 'count', data = iRecord, palette = 'mako')\nax2.set(title = 'Correlation between Surface and Body Part ', xlabel = 'Surface Types', ylabel = 'Count')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### PlayList Data Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(pList['PlayerKey'].nunique())\npList['PlayerKey'].unique().tolist()[: 10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12, 10))\n\nsns.set_style('whitegrid')\nax3 = sns.catplot(x = 'FieldType', hue = 'StadiumType', kind = 'count', data = pList, palette = 'mako')\nax3.set(title = 'Correlation between FieldType and StadiumType ', xlabel = 'FieldType', ylabel = 'Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(55, 80))\nsns.set_style('whitegrid')\nax4 = sns.catplot(y = 'Weather', kind = 'count', data = pList, palette = 'mako')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(pList['StadiumType'].nunique())\npList['StadiumType'].unique().tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def groupingStadiumType(data):\n    \"\"\"\n    grouped the stadiumType to the indoor or outdoor.\n    Params: data\n    Return: indoor and outdoor\n    \"\"\"\n    if data in ['Outdoor', 'Oudoor', 'Outdoors', 'Open', 'Outdoor Retr Roof-Open', 'Ourdoor', 'Outddors', 'Retr. Roof-Open', 'Open Roof', 'Domed, Open', 'Domed, open', 'Heinz Field',\n 'Cloudy', 'Retr. Roof - Open', 'Outdor', 'Outside']:\n        value = 'outdoor'\n    else:\n        value = 'indoor'\n    return value","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pList['groupedStadiumType'] = pList['StadiumType'].apply(groupingStadiumType)\npList['groupedStadiumType'].value_counts().to_frame()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (15, 10))\nsns.set_style('whitegrid')\nax5 = sns.catplot(x = 'groupedStadiumType', kind = 'count', data = pList, palette = 'mako')\nax5.set(title = 'The grouped StadiumType data', xlabel = 'grouped StadiumType', ylabel = 'Count')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (12, 10))\n\nsns.set_style('whitegrid')\n\nax6 = sns.violinplot(x = 'FieldType', y = 'Temperature', hue = 'groupedStadiumType', split = True, data = pList.loc[pList['Temperature'] > -500], palette = 'mako')\nax6.set(title = 'The tempeature of the Field Type and StadiumType')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('The information of the playerlist dataset'.center(50, '-'))\nprint('The number of player list dataset {}'.format(pList.shape))\npList.sample(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(pList['PlayType'].nunique())\npList['RosterPosition'].value_counts().sort_values(ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataToPlot = pList.groupby(['RosterPosition', 'PlayType', 'PlayerKey'])['PlayerDay'].count().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataToPlot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (42, 10))\n\nsns.set_style('whitegrid')\nax7 = sns.catplot(x = 'RosterPosition', hue = 'PlayType', data = dataToPlot, kind = 'count', palette = 'mako')\nax7.set(title = 'The corr between the Play Type and Roster Position', xlabel = 'Roster Position', ylabel = 'Count')\nplt.xticks(rotation = 45)\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}