{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Descriptiond of the Competition\n\n#### There's a reason why it's called March Madness®. Upsets happen, underdogs become \"cinderellas,\" and games that analysts expected to be blowouts become nail-biters through the final seconds. A team's competitiveness is what keeps games exciting and the tournament truly \"mad.\"\n\n#### In addition to the predictive modeling competitions we typically host (NCAA Men's and Women’s), we are hosting a separate competition using Kaggle Notebooks that challenges you to present an exploratory analysis of the “Madness.” Can you quantify competitiveness? Can you explain \"cinderella…ness\"?\n\n#### Or perhaps, can you determine what dictates the ability of a team to “stay in the game” and increase their chance to win late in the contest? This may or may not be a scalar metric. It might be a clustering of types of competitiveness and then a rating within each. Does this metric have predictive power? The interpretation is up to you.\n\n### Your challenge is to tell a data story about college basketball through a combination of both narrative text and data exploration. A “story” could be defined any number of ways, and that’s deliberate. You are to deeply explore (through data) the mania of the Men’s and Women’s NCAA College Basketball tournaments. That story can be examined in the macro (for example: How does “competitiveness” differ from the regular season to their decisions in the tournament?) or the micro (for example: Does effectively neutralizing an opponent’s star players increase their ability to “stay in the game”?)."},{"metadata":{},"cell_type":"markdown","source":"### Load Necessary Library for the competetion"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport matplotlib as mpl\nfrom matplotlib.patches import Circle, Rectangle, Arc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nplt.style.use('seaborn-dark-palette')\nmypal = plt.rcParams['axes.prop_cycle'].by_key()['color'] # Grab the color pal\nimport os\nimport gc","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Create Path directory"},{"metadata":{"trusted":true},"cell_type":"code","source":"def mkdir(path):\n    import os\n    path = path.strip()\n    path = path.rstrip(\"\\\\\")\n    isExists = os.path.exists(path)\n    if not isExists:\n        os.makedirs(path)\n        print(path + 'Successufully established')\n        return True\n    else:\n        print(path + 'dir existed')\n        return False","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create Path"},{"metadata":{"trusted":true},"cell_type":"code","source":"DIRC_MENS_PATH = '../input/march-madness-analytics-2020/2020DataFiles/2020DataFiles/2020-Mens-Data/MDataFiles_Stage1'\nDIRC_WOMENS_PATH = '../input/march-madness-analytics-2020/2020DataFiles/2020DataFiles/2020-Womens-Data/WDataFiles_Stage1'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Load Data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"MensTeamsDT = pd.read_csv(f'{DIRC_MENS_PATH}/MTeams.csv')\nWoMensTeamsDT = pd.read_csv(f'{DIRC_WOMENS_PATH}/WTeams.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**View Data for male and female**"},{"metadata":{"trusted":true},"cell_type":"code","source":"MensTeamsDT","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"WoMensTeamsDT","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_drop_list = [\"FirstD1Season\",\"TeamName\",\"LastD1Season\"]\nW_drop_list = ['TeamName']\nMensTeamsDT.drop(M_drop_list,axis = 1,inplace = True)\nWoMensTeamsDT.drop(W_drop_list,axis = 1,inplace = True)\nMensTeamsDT.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(MensTeamsDT[\"TeamID\"].unique()))\nprint(len(WoMensTeamsDT[\"TeamID\"].unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Team_box\ntime_list = [2015,2016,2017,2018,2019]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"MensTeamsDT = pd.read_csv(f'{DIRC_MENS_PATH}/MTeams.csv')\nWoMensTeamsDT = pd.read_csv(f'{DIRC_WOMENS_PATH}/WTeams.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_Seed = pd.read_csv(f'{DIRC_MENS_PATH}/MNCAATourneySeeds.csv')\nW_Seed = pd.read_csv(f'{DIRC_WOMENS_PATH}/WNCAATourneySeeds.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_Team_seed = M_Seed[M_Seed['Season'].isin(time_list)]\nW_Team_seed = W_Seed[W_Seed['Season'].isin(time_list)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(W_Team_seed))\nprint(len(M_Team_seed))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_detailed =  pd.read_csv(f'{DIRC_MENS_PATH}/MRegularSeasonDetailedResults.csv')\nW_game_result_detailed =  pd.read_csv(f'{DIRC_WOMENS_PATH}/WRegularSeasonDetailedResults.csv')\n\nprint(M_game_result_detailed.head())\nprint(W_game_result_detailed.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_detailed = M_game_result_detailed[M_game_result_detailed['Season'].isin(time_list)]\nW_game_result_detailed = W_game_result_detailed[W_game_result_detailed['Season'].isin(time_list)]\nprint(M_game_result_detailed.head())\nprint(W_game_result_detailed.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(M_game_result_detailed))\nprint(len(W_game_result_detailed))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_detailed.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_game_result_detailed.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"win_list = ['Season','WTeamID','WScore','WFGM', 'WFGA', 'WFGM3', 'WFGA3', 'WFTM', 'WFTA', 'WOR', 'WDR',\n       'WAst', 'WTO', 'WStl', 'WBlk', 'WPF']\nM_game_result_win = M_game_result_detailed[win_list]\nW_game_result_win = W_game_result_detailed[win_list]\nW_game_result_win.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lose_list = ['Season','LTeamID','LScore','LFGM', 'LFGA', 'LFGM3', 'LFGA3',\n       'LFTM', 'LFTA', 'LOR', 'LDR', 'LAst', 'LTO', 'LStl', 'LBlk', 'LPF']\nM_game_result_lose = M_game_result_detailed[lose_list]\nW_game_result_lose = W_game_result_detailed[lose_list]\nW_game_result_lose.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_win_box = M_game_result_win.groupby(['WTeamID','Season']).count()\nW_team_win_box = W_game_result_win.groupby(['WTeamID','Season']).count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drop_list = ['WFGM', 'WFGA', 'WFGM3', 'WFGA3', 'WFTM', 'WFTA', 'WOR', 'WDR',\n       'WAst', 'WTO', 'WStl', 'WBlk', 'WPF']\nM_team_win_box = M_team_win_box.drop(drop_list,axis = 1)\nW_team_win_box = W_team_win_box.drop(drop_list,axis = 1)\nM_team_win_box = M_team_win_box.rename(columns = {'WScore':'count'})\nW_team_win_box = W_team_win_box.rename(columns = {'WScore':'count'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_count = M_team_win_box.reset_index()\nW_team_count = W_team_win_box.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_win_box = M_game_result_win.groupby(['WTeamID','Season']).sum()\nM_team_win_box = M_team_win_box.reset_index()\nM_team_win_box = pd.merge(M_team_win_box,M_team_count,on = ['WTeamID','Season'])\nwin_rename_columns = {'WTeamID':\"TeamID\",\"WScore\":\"Score\",'WFGM':'FGM', 'WFGA':'FGA', 'WFGM3':'FGM3', 'WFGA3':'FGA3',\n       'WFTM':'FTM', 'WFTA':'FTA', 'WOR':'OR', 'WDR':'DR', 'WAst':'Ast', 'WTO':'TO', 'WStl':'Stl', 'WBlk':'Blk', 'WPF':'PF'}\nM_team_win_box = M_team_win_box.rename(columns=win_rename_columns)\nM_team_win_box.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_team_win_box = W_game_result_win.groupby(['WTeamID','Season']).sum()\nW_team_win_box = W_team_win_box.reset_index()\nW_team_win_box = pd.merge(W_team_win_box,W_team_count,on = ['WTeamID','Season'])\nW_team_win_box = W_team_win_box.rename(columns=win_rename_columns)\nW_team_win_box.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(W_team_win_box)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_game_result_lose","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_lose_box = M_game_result_lose.groupby(['LTeamID','Season']).count()\ndrop_list = ['LFGM', 'LFGA', 'LFGM3', 'LFGA3', 'LFTM', 'LFTA', 'LOR', 'LDR',\n       'LAst', 'LTO', 'LStl', 'LBlk', 'LPF']\nM_team_lose_box = M_team_lose_box.drop(drop_list,axis = 1)\nM_team_lose_box = M_team_lose_box.rename(columns = {'LScore':'count'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_team_lose_box = W_game_result_lose.groupby(['LTeamID','Season']).count()\nW_team_lose_box = W_team_lose_box.drop(drop_list,axis = 1)\nW_team_lose_box = W_team_lose_box.rename(columns = {'LScore':'count'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_lose_count = M_team_lose_box.reset_index()\nW_team_lose_count = W_team_lose_box.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result_lose = M_game_result_lose.groupby(['LTeamID','Season']).sum()\nM_team_lose_box = M_result_lose.reset_index()\nM_team_lose_box = pd.merge(M_team_lose_box,M_team_lose_count,on = ['LTeamID','Season'])\nrename_columns = {'LTeamID':\"TeamID\",\"LScore\":\"Score\",'LFGM':'FGM', 'LFGA':'FGA', 'LFGM3':'FGM3', 'LFGA3':'FGA3',\n       'LFTM':'FTM', 'LFTA':'FTA', 'LOR':'OR', 'LDR':'DR', 'LAst':'Ast', 'LTO':'TO', 'LStl':'Stl', 'LBlk':'Blk', 'LPF':'PF'}\nM_team_lose_box = M_team_lose_box.rename(columns=rename_columns)\nM_team_lose_box.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_result_lose = W_game_result_lose.groupby(['LTeamID','Season']).sum()\nW_team_lose_box = W_result_lose.reset_index()\nW_team_lose_box = pd.merge(W_team_lose_box,W_team_lose_count,on = ['LTeamID','Season'])\nW_team_lose_box = W_team_lose_box.rename(columns=rename_columns)\nW_team_lose_box.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"assert len(W_team_win_box.columns) == len(W_team_lose_box.columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result = pd.merge(M_team_win_box,M_team_lose_box,on=['TeamID','Season'])\nW_result = pd.merge(W_team_win_box,W_team_lose_box,on=['TeamID','Season'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result = M_team_win_box.append(M_team_lose_box)\nW_result = W_team_win_box.append(W_team_lose_box)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result = M_result.groupby(['TeamID','Season']).sum()\nW_result = W_result.groupby(['TeamID','Season']).sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"element_list = ['Score','FGM','FGA','FGM3','FGA3','FTM','FTA','OR','DR','TO','Stl','Blk','PF','Ast']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result = M_result[element_list].apply(lambda x:x/M_result['count'])\nW_result = W_result[element_list].apply(lambda x:x/W_result['count'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result_withseed = pd.merge(M_result,M_Team_seed,on=['TeamID','Season'],how = 'outer')\nW_result_withseed = pd.merge(W_result,W_Team_seed,on=['TeamID','Season'],how = 'outer')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_result_withseed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_team_win_box = M_result_withseed.rename(columns = {'TeamID':'WTeamID'})\nM_team_lose_box = M_result_withseed.rename(columns = {'TeamID':'LTeamID'})\nW_team_win_box = W_result_withseed.rename(columns = {'TeamID':'WTeamID'})\nW_team_lose_box = W_result_withseed.rename(columns = {'TeamID':'LTeamID'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_result_withseed.to_csv('M_result.csv')\nW_result_withseed.to_csv('W_result.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To load the data of regular season, use 'MRegularSeasibCompactResults.csv' or 'WRegularSeasibCompactResults.csv'"},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result =  pd.read_csv(f'{DIRC_MENS_PATH}/MNCAATourneyCompactResults.csv')\nM_game_result = M_game_result[M_game_result['Season'].isin(time_list)]\nW_game_result =  pd.read_csv(f'{DIRC_WOMENS_PATH}/WNCAATourneyCompactResults.csv')\nW_game_result = W_game_result[W_game_result['Season'].isin(time_list)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_game_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_team_lose_box","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_1 = pd.merge(M_game_result,M_team_win_box,on=['WTeamID','Season'],how = 'left')\nW_game_result_1 = pd.merge(W_game_result,W_team_win_box,on=['WTeamID','Season'],how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_game_result_1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_final = pd.merge(M_game_result_1,M_team_lose_box,on=['LTeamID','Season'],how = 'left')\nW_game_result_final = pd.merge(W_game_result_1,W_team_lose_box,on=['LTeamID','Season'],how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"W_game_result_final[['Score_x','Score_y']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_game_result_final.to_csv('M_result_by_game_tourney.csv')\nW_game_result_final.to_csv('W_result_by_game_tourney.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Logistic Regression"},{"metadata":{"trusted":true},"cell_type":"code","source":"import statsmodels.api as sm\n\nfrom matplotlib import pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.datasets import load_diabetes\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import accuracy_score\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = M_game_result_final.copy()\ndf1=df.copy()\ndf_WTeamID=df['WTeamID']\ndf_LTeamID=df['LTeamID']\ndf_WScore=df['WScore']\ndf_LScore=df['LScore']\ndf1['WTeamID']=df_LTeamID\ndf1['LTeamID']=df_WTeamID\ndf1['WScore']=df_LScore\ndf1['LScore']=df_WScore\nlabel_x=df1.columns[8:23]\nlabel_y=df1.columns[23:38]\nlabel_x=list(label_x)\nlabel_y=list(label_y)\nfor i in range(len(label_x)):\n    df1[label_x[i]]=df[label_y[i]]\n    df1[label_y[i]]=df[label_x[i]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['target']=1\ndf1['target']=0\ndf_final=df.append(df1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W = W_game_result_final.copy()\ndf_W1=df_W.copy()\ndf_W_WTeamID=df_W['WTeamID']\ndf_W_LTeamID=df_W['LTeamID']\ndf_W_WScore=df_W['WScore']\ndf_W_LScore=df_W['LScore']\ndf_W1['WTeamID']=df_W_LTeamID\ndf_W1['LTeamID']=df_W_WTeamID\ndf_W1['WScore']=df_W_LScore\ndf_W1['LScore']=df_W_WScore\nlabel_x_W=df_W1.columns[8:23]\nlabel_y_W=df_W1.columns[23:38]\nlabel_x_W=list(label_x_W)\nlabel_y_W=list(label_y_W)\nfor i in range(len(label_x_W)):\n    df_W1[label_x_W[i]]=df_W[label_y_W[i]]\n    df_W1[label_y_W[i]]=df_W[label_x_W[i]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W['target']=1\ndf_W1['target']=0\ndf_W_final=df_W.append(df_W1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.to_csv('M_result_by_game_tourney_editored.csv')\ndf_W_final.to_csv('W_result_by_game_tourney_editored.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m,n=np.shape(df_final)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.reset_index(inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final = df_final.drop(columns = 'index')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=df_final.copy()\nwseed = data[\"Seed_x\"]\nlseed = data[\"Seed_y\"]\nWseed  = np.zeros([m])\nLseed = np.zeros([m])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(m):\n    Wseed[i] = wseed[i][1:3]\n    Lseed[i] = lseed[i][1:3]\n    \nseeddiff=Wseed-Lseed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final=df_final.drop(['WLoc','Seed_x','Seed_y','WTeamID','WScore','LTeamID','LScore'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m,n=np.shape(df_final)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.insert(n-1,'Seeddiff',seeddiff) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data \ndf_train=df_final[df_final['Season']<2019]\ndf_test=df_final[df_final['Season']>2018]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = df_train.iloc[:,0:n].values\ny_train = df_train.target.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test = df_test.iloc[:,0:n].values\ny_test = df_test.target.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegressionCV\n\nlogreg = LogisticRegressionCV(cv=5,random_state=0, solver='newton-cg')\nlogreg.fit(x_train, y_train)\n\ny_pred_train = logreg.predict(x_train)\ny_pred_test = logreg.predict(x_test)\n\nprint(\"Coefficients :\", np.round(logreg.intercept_,4), np.round(logreg.coef_,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = logreg.predict(x_train)\ny_pred_test = logreg.predict(x_test)\n\naccuracy_train = accuracy_score(y_train, y_pred_train)\naccuracy_test = accuracy_score(y_test, y_pred_test)\nprint('Accuracy on the training set =', np.round(accuracy_train,4))\nprint('Accuracy on the test set =', np.round(accuracy_test,4))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## data for women"},{"metadata":{"trusted":true},"cell_type":"code","source":"m,n=np.shape(df_W_final)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W_final.reset_index(inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W_final = df_W_final.drop(columns = 'index')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=df_W_final.copy()\nwseed = data[\"Seed_x\"]\nlseed = data[\"Seed_y\"]\nWseed  = np.zeros([m])\nLseed = np.zeros([m])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(m):\n    Wseed[i] = wseed[i][1:3]\n    Lseed[i] = lseed[i][1:3]\n    \nseeddiff=Wseed-Lseed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W_final=df_W_final.drop(['WLoc','Seed_x','Seed_y','WTeamID','WScore','LTeamID','LScore'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m,n=np.shape(df_W_final)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_W_final.insert(n-1,'Seeddiff',seeddiff) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data \ndf_train_W=df_W_final[df_W_final['Season']<2019]\ndf_test_W=df_W_final[df_W_final['Season']>2018]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train_W = df_train_W.iloc[:,0:n].values\ny_train_W = df_train_W.target.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test_W = df_test_W.iloc[:,0:n].values\ny_test_W = df_test_W.target.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegressionCV\n\nlogreg = LogisticRegressionCV(cv=5,random_state=0, solver='newton-cg')\nlogreg.fit(x_train, y_train)\n\ny_pred_train = logreg.predict(x_train_W)\ny_pred_test = logreg.predict(x_test_W)\n\nprint(\"Coefficients :\", np.round(logreg.intercept_,4), np.round(logreg.coef_,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = logreg.predict(x_train_W)\ny_pred_test = logreg.predict(x_test_W)\n\naccuracy_train = accuracy_score(y_train_W, y_pred_train)\naccuracy_test = accuracy_score(y_test_W, y_pred_test)\nprint('Accuracy on the training set =', np.round(accuracy_train,4))\nprint('Accuracy on the test set =', np.round(accuracy_test,4))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# XGboost"},{"metadata":{"trusted":true},"cell_type":"code","source":"import xgboost as xgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb = pd.read_csv(\"M_result_by_game_tourney_editored.csv\")\ndata_xgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wseed = data_xgb[\"Seed_x\"]\nlseed = data_xgb[\"Seed_y\"]\nWseed  = np.zeros([data_xgb.shape[0]])\nLseed = np.zeros([data_xgb.shape[0]])\nfor i in range(data_xgb.shape[0]):\n    Wseed[i] = wseed[i][1:3]\n    Lseed[i] = lseed[i][1:3]\ndata_xgb[\"Seed_x\"] = Wseed\ndata_xgb[\"Seed_y\"] = Lseed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb[\"Score_diff\"] = data_xgb[\"Score_x\"]-data_xgb[\"Score_y\"]\ndata_xgb[\"FGM_diff\"] = data_xgb[\"FGM_x\"]-data_xgb[\"FGM_y\"]\ndata_xgb[\"FGA_diff\"] = data_xgb[\"FGA_x\"]-data_xgb[\"FGA_y\"]\ndata_xgb[\"FGM3_diff\"] = data_xgb[\"FGM3_x\"]-data_xgb[\"FGM3_y\"]\ndata_xgb[\"FGA3_diff\"] = data_xgb[\"FGA3_x\"]-data_xgb[\"FGA3_y\"]\ndata_xgb[\"FTM_diff\"] = data_xgb[\"FTM_x\"]-data_xgb[\"FTM_y\"]\ndata_xgb[\"FTA_diff\"] = data_xgb[\"FTA_x\"]-data_xgb[\"FTA_y\"]\ndata_xgb[\"OR_diff\"] = data_xgb[\"OR_x\"]-data_xgb[\"OR_y\"]\ndata_xgb[\"DR_diff\"] = data_xgb[\"DR_x\"]-data_xgb[\"DR_y\"]\ndata_xgb[\"TO_diff\"] = data_xgb[\"TO_x\"]-data_xgb[\"TO_y\"]\ndata_xgb[\"Stl_diff\"] = data_xgb[\"Stl_x\"]-data_xgb[\"Stl_y\"]\ndata_xgb[\"Blk_diff\"] = data_xgb[\"Blk_x\"]-data_xgb[\"Blk_y\"]\ndata_xgb[\"PF_diff\"] = data_xgb[\"PF_x\"]-data_xgb[\"PF_y\"]\ndata_xgb[\"Seed_diff\"] = data_xgb[\"Seed_x\"]-data_xgb[\"Seed_y\"]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainlabel = data_xgb[data_xgb['Season']<2019]['target']\ntestlabel = data_xgb[data_xgb['Season']==2019]['target']\ntrainlabel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindata = data_xgb[data_xgb['Season']<2019]\ntestdata = data_xgb[data_xgb['Season']==2019]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"droplist = [\"Unnamed: 0\",\"LTeamID\",\"WTeamID\",\"WLoc\",\"target\",\"WScore\",\"Score_x\",\"FGM_x\",\"FGA_x\",\"FGM3_x\",\"FGA3_x\",\n            \"LScore\",\"FTM_x\",\"FTA_x\",\"OR_x\",\"DR_x\",\"TO_x\",\"Stl_x\",\"Blk_x\",\n           \"PF_x\",\"Score_y\",\"FGM_y\",\"FGA_y\",\"FGM3_y\",\"FGA3_y\",\"FTM_y\",\"FTA_y\",\n            \"OR_y\",\"DR_y\",\"TO_y\",\"Stl_y\",\"Blk_y\",\"PF_y\",\"Seed_x\",\"Seed_y\"]\ntraindata.drop(droplist,axis=1,inplace = True)\ntestdata.drop(droplist,axis=1,inplace = True)\ntraindata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xg_reg = xgb.XGBRegressor(objective ='binary:logistic', colsample_bytree = 0.8, learning_rate = 0.001,\n                max_depth = 10, alpha = 7, n_estimators = 50000)\nxg_reg.fit(traindata,trainlabel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds  = xg_reg.predict(testdata)\npreds = np.floor(preds+0.5)\nnp.sum(preds==testlabel)/testdata.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xgb.plot_tree(xg_reg,num_trees=0)\nplt.rcParams['figure.figsize'] = [500, 400]\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# xgb.plot_importance(xg_reg)\n#plt.rcParams['figure.figsize'] = [500,400]\n# plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## xgboost for women"},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb = pd.read_csv(\"W_result_by_game_tourney_editored.csv\")\n\nwseed = data_xgb[\"Seed_x\"]\nlseed = data_xgb[\"Seed_y\"]\nWseed  = np.zeros([data_xgb.shape[0]])\nLseed = np.zeros([data_xgb.shape[0]])\nfor i in range(data_xgb.shape[0]):\n    Wseed[i] = wseed[i][1:3]\n    Lseed[i] = lseed[i][1:3]\ndata_xgb[\"Seed_x\"] = Wseed\ndata_xgb[\"Seed_y\"] = Lseed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb[['Score_x','Score_y']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb[\"Score_diff\"] = data_xgb[\"Score_x\"]-data_xgb[\"Score_y\"]\ndata_xgb[\"FGM_diff\"] = data_xgb[\"FGM_x\"]-data_xgb[\"FGM_y\"]\ndata_xgb[\"FGA_diff\"] = data_xgb[\"FGA_x\"]-data_xgb[\"FGA_y\"]\ndata_xgb[\"FGM3_diff\"] = data_xgb[\"FGM3_x\"]-data_xgb[\"FGM3_y\"]\ndata_xgb[\"FGA3_diff\"] = data_xgb[\"FGA3_x\"]-data_xgb[\"FGA3_y\"]\ndata_xgb[\"FTM_diff\"] = data_xgb[\"FTM_x\"]-data_xgb[\"FTM_y\"]\ndata_xgb[\"FTA_diff\"] = data_xgb[\"FTA_x\"]-data_xgb[\"FTA_y\"]\ndata_xgb[\"OR_diff\"] = data_xgb[\"OR_x\"]-data_xgb[\"OR_y\"]\ndata_xgb[\"DR_diff\"] = data_xgb[\"DR_x\"]-data_xgb[\"DR_y\"]\ndata_xgb[\"TO_diff\"] = data_xgb[\"TO_x\"]-data_xgb[\"TO_y\"]\ndata_xgb[\"Stl_diff\"] = data_xgb[\"Stl_x\"]-data_xgb[\"Stl_y\"]\ndata_xgb[\"Blk_diff\"] = data_xgb[\"Blk_x\"]-data_xgb[\"Blk_y\"]\ndata_xgb[\"PF_diff\"] = data_xgb[\"PF_x\"]-data_xgb[\"PF_y\"]\ndata_xgb[\"Seed_diff\"] = data_xgb[\"Seed_x\"]-data_xgb[\"Seed_y\"]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_xgb['Score_y']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainlabel = data_xgb[data_xgb['Season']<2019]['target']\ntestlabel = data_xgb[data_xgb['Season']==2019]['target']\ntrainlabel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindata = data_xgb[data_xgb['Season']<2019]\ntestdata = data_xgb[data_xgb['Season']==2019]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"droplist = [\"Unnamed: 0\",\"LTeamID\",\"WTeamID\",\"WLoc\",\"target\",\"WScore\",\"Score_x\",\"FGM_x\",\"FGA_x\",\"FGM3_x\",\"FGA3_x\",\n            \"LScore\",\"FTM_x\",\"FTA_x\",\"OR_x\",\"DR_x\",\"TO_x\",\"Stl_x\",\"Blk_x\",\n           \"PF_x\",\"Score_y\",\"FGM_y\",\"FGA_y\",\"FGM3_y\",\"FGA3_y\",\"FTM_y\",\"FTA_y\",\n            \"OR_y\",\"DR_y\",\"TO_y\",\"Stl_y\",\"Blk_y\",\"PF_y\",\"Seed_x\",\"Seed_y\"]\ntraindata.drop(droplist,axis=1,inplace = True)\ntestdata.drop(droplist,axis=1,inplace = True)\ntraindata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xg_reg = xgb.XGBRegressor(objective ='binary:logistic', colsample_bytree = 0.8, learning_rate = 0.001,\n                max_depth = 10, alpha = 7, n_estimators = 50000)\nxg_reg.fit(traindata,trainlabel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds  = xg_reg.predict(testdata)\npreds = np.floor(preds+0.5)\nnp.sum(preds==testlabel)/testdata.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#xgb.plot_tree(xg_reg,num_trees=0)\n#plt.rcParams['figure.figsize'] = [500, 400]\n#plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# xgb.plot_importance(xg_reg)\n#plt.rcParams['figure.figsize'] = [500,400]\n# plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Using PyGam"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install pygam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pygam import LinearGAM,f,s,l\nimport eli5\nfrom eli5.sklearn import PermutationImportance","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## lasso selection"},{"metadata":{"trusted":true},"cell_type":"code","source":"X=x_train\ny=y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# perform LASSO CV\n# Note that the regularization strength is denoted by alpha in sklearn.\nfrom sklearn import linear_model\ncv = 10\nlassocv = linear_model.LassoCV(cv=cv)\nlassocv.fit(X, y)\nprint('alpha =',lassocv.alpha_.round(4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# draw solution path\nalphas = np.logspace(-8,1,21)\nalphas_lassocv, coefs_lassocv, _ = lassocv.path(X, y, alphas=alphas)\nlog_alphas_lassocv = np.log10(alphas_lassocv)\n\nplt.figure(figsize=(12,8)) \nplt.plot(log_alphas_lassocv,coefs_lassocv.T)\nplt.vlines(x=np.log10(lassocv.alpha_), ymin=np.min(coefs_lassocv), ymax=np.max(coefs_lassocv), \n           color='b',linestyle='-.',label = 'alpha chosen')\nplt.axhline(y=0, color='black',linestyle='--')\nplt.xlabel(r'$\\log_{10}(\\alpha)$', fontsize=12)\nplt.ylabel(r'$\\hat{\\beta}$', fontsize=12, rotation=0)\nplt.title('Solution Path',fontsize=12)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# fit a multiple layer percepton (neural network)\nfrom sklearn.neural_network import MLPClassifier\nnames=df_train.drop('target',axis=1).columns\nnames=list(names)\nclf = MLPClassifier(max_iter=1000, random_state=0)\nclf.fit(X, y)\n# define a permutation importance object\nperm = PermutationImportance(clf).fit(X, y)\n# show the importance\neli5.show_weights(perm, feature_names=names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train =df_train[['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']].values\nx_test =df_test[['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"names = ['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']\nnames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pygam import LogisticGAM,f,s,l\n\ngam = LogisticGAM().fit(x_train,y_train)\n# f: factor term\n# some parameters combinations in grid search meet the error exception.\ngam.gridsearch(x_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plotting\nfig, axs = plt.subplots(2,3,figsize=(20,8))\nfor i, ax in enumerate(axs.flatten()):\n    XX = gam.generate_X_grid(term=i)\n    plt.subplot(ax)\n    plt.plot(XX[:, i], gam.partial_dependence(term=i, X=XX))\n    plt.plot(XX[:, i], gam.partial_dependence(term=i, X=XX, width=.95)[1], c='grey', ls='--')\n    plt.title(names[i])\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = gam.predict(x_train)\ny_pred_test = gam.predict(x_test)\n\nprint('The Acc on training set:',accuracy_score(y_train,y_pred_train))\nprint('The Acc on testing set:',accuracy_score(y_test,y_pred_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X=x_train_W\ny=y_train_W","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# perform LASSO CV\n# Note that the regularization strength is denoted by alpha in sklearn.\nfrom sklearn import linear_model\ncv = 10\nlassocv = linear_model.LassoCV(cv=cv)\nlassocv.fit(X, y)\nprint('alpha =',lassocv.alpha_.round(4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# draw solution path\nalphas = np.logspace(-8,1,21)\nalphas_lassocv, coefs_lassocv, _ = lassocv.path(X, y, alphas=alphas)\nlog_alphas_lassocv = np.log10(alphas_lassocv)\n\nplt.figure(figsize=(12,8)) \nplt.plot(log_alphas_lassocv,coefs_lassocv.T)\nplt.vlines(x=np.log10(lassocv.alpha_), ymin=np.min(coefs_lassocv), ymax=np.max(coefs_lassocv), \n           color='b',linestyle='-.',label = 'alpha chosen')\nplt.axhline(y=0, color='black',linestyle='--')\nplt.xlabel(r'$\\log_{10}(\\alpha)$', fontsize=12)\nplt.ylabel(r'$\\hat{\\beta}$', fontsize=12, rotation=0)\nplt.title('Solution Path',fontsize=12)\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# fit a multiple layer percepton (neural network)\nfrom sklearn.neural_network import MLPClassifier\nnames=df_train.drop('target',axis=1).columns\nnames=list(names)\nclf = MLPClassifier(max_iter=1000, random_state=0)\nclf.fit(X, y)\n# define a permutation importance object\nperm = PermutationImportance(clf).fit(X, y)\n# show the importance\neli5.show_weights(perm, feature_names=names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train =df_train[['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']].values\nx_test =df_test[['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"names = ['Seeddiff','Score_y','FGM_y','FGA_y','FTM_x','FTA_x']\nnames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gam = LogisticGAM().fit(x_train,y_train)\n# f: factor term\n# some parameters combinations in grid search meet the error exception.\ngam.gridsearch(x_train_W,y_train_W)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plotting\nfig, axs = plt.subplots(2,3,figsize=(20,8))\nfor i, ax in enumerate(axs.flatten()):\n    XX = gam.generate_X_grid(term=i)\n    plt.subplot(ax)\n    plt.plot(XX[:, i], gam.partial_dependence(term=i, X=XX))\n    plt.plot(XX[:, i], gam.partial_dependence(term=i, X=XX, width=.95)[1], c='grey', ls='--')\n    plt.title(names[i])\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = gam.predict(x_train)\ny_pred_test = gam.predict(x_test)\n\nprint('The Acc on training set:',accuracy_score(y_train,y_pred_train))\nprint('The Acc on testing set:',accuracy_score(y_test,y_pred_test))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## variable importance"},{"metadata":{"trusted":true},"cell_type":"code","source":"names=df_train.drop('target',axis=1).columns\nnames=list(names)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## using advanced statistics"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# Begining of player stats mining"},{"metadata":{"trusted":true},"cell_type":"code","source":"DIRC_MENS_PATH_player = '../input/march-madness-analytics-2020/2020DataFiles/2020DataFiles/2020-Mens-Data'\nDIRC_WOMENS_PATH_player = '../input/march-madness-analytics-2020/2020DataFiles/2020DataFiles/2020-Womens-Data'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"M_players = pd.read_csv(f'{DIRC_MENS_PATH_player}/MPlayers.csv')\nW_players = pd.read_csv(f'{DIRC_WOMENS_PATH_player}/WPlayers.csv')\nW_players.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##  analysis by year"},{"metadata":{"trusted":true},"cell_type":"code","source":"def high_order_stats(event_by_player):\n    event_by_player['Points'] = event_by_player['made1']+2*event_by_player['made2']+3*event_by_player['made3']\n    element_list = ['assist','block','foul','made1','made2','made3','miss1','miss2','miss3',\n               'reb','steal','sub','timeout','turnover','Points']\n    for item in element_list:\n        event_by_player[item] = event_by_player[item]/event_by_player['count']\n    event_by_player['Field_goal'] = (event_by_player['made2']+\n                                 event_by_player['made3'])/(event_by_player['made2']+\n                                                            event_by_player['made3']+\n                                                           event_by_player['miss2']+\n                                                            event_by_player['miss3'])\n    event_by_player['FT_goal'] = event_by_player['made1']/(event_by_player['miss1']+event_by_player['made1'])\n    event_by_player['3PT'] = event_by_player['made3']/(event_by_player['miss3']+event_by_player['made3'])\n    # assist vs turnover\n    event_by_player['AT'] = event_by_player['assist']/event_by_player['turnover']\n    event_by_player['eFG'] = (event_by_player['made2']+\n                          0.5*event_by_player['made3'])/(event_by_player['made2']+\n                                                         event_by_player['made3']+\n                                                         event_by_player['miss2']+\n                                                         event_by_player['miss3'])\n    event_by_player['TS'] = event_by_player['Points']/(2*((event_by_player['made2']+\n                                                         event_by_player['made3']+\n                                                         event_by_player['miss2']+\n                                                         event_by_player['miss3'])\n                                                      +0.44*(event_by_player['made1']+\n                                                         event_by_player['made1'])))\n    event_by_player['PER'] = (event_by_player['Points']+event_by_player['assist']+event_by_player['reb']+event_by_player['steal']+\n                         event_by_player['block']-event_by_player['miss1']-event_by_player['turnover'])/event_by_player['count']\n    return event_by_player\n    \n\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mkdir('mens_stats')\nmkdir('womens_stats')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# year of stats\n\nyear = 2015\nfor year in range(2015,2020):\n    M_events = pd.read_csv(f'{DIRC_MENS_PATH_player}/MEvents{year}.csv')\n    W_events = pd.read_csv(f'{DIRC_WOMENS_PATH_player}/WEvents{year}.csv')\n    M_game_made = M_events[['DayNum','EventPlayerID']]\n    W_game_made = W_events[['DayNum','EventPlayerID']]\n    M_game_made['count'] = 0\n    W_game_made['count'] = 0\n    M_game_made = M_game_made.drop_duplicates()\n    W_game_made = W_game_made.drop_duplicates()\n    M_game_made = M_game_made.groupby(['EventPlayerID']).count()\n    M_game_made.reset_index(inplace = True)\n    W_game_made = W_game_made.groupby(['EventPlayerID']).count()\n    W_game_made.reset_index(inplace = True)\n    M_game_made.drop(index = 0,inplace = True)\n    W_game_made.drop(index = 0,inplace = True)\n    M_game_made = M_game_made[['EventPlayerID','count']]\n    W_game_made = W_game_made[['EventPlayerID','count']]\n    M_events_useful = M_events[['EventPlayerID','EventType']]\n    W_events_useful = W_events[['EventPlayerID','EventType']]\n    M_events_useful['count'] = 0\n    W_events_useful['count'] = 0\n    M_events_useful = M_events_useful.groupby(['EventPlayerID','EventType']).count()\n    W_events_useful = W_events_useful.groupby(['EventPlayerID','EventType']).count()\n    M_events_reindex = M_events_useful.reset_index()\n    W_events_reindex = W_events_useful.reset_index()\n    M_events_pivoted=M_events_reindex.pivot('EventPlayerID', 'EventType', 'count')\n    W_events_pivoted=W_events_reindex.pivot('EventPlayerID', 'EventType', 'count')\n    M_event_by_player = M_events_pivoted.fillna(0)\n    W_event_by_player = W_events_pivoted.fillna(0)\n    M_event_by_player = M_event_by_player.drop(index = 0)\n    W_event_by_player = W_event_by_player.drop(index = 0)\n    M_event_by_player = pd.merge(M_event_by_player, M_game_made,on = 'EventPlayerID')\n    W_event_by_player = pd.merge(W_event_by_player, W_game_made,on = 'EventPlayerID')\n    M_event_by_player = high_order_stats(M_event_by_player)\n    W_event_by_player = high_order_stats(W_event_by_player)\n    M_players.rename(columns = {'PlayerID':'EventPlayerID'},inplace = True)\n    W_players.rename(columns = {'PlayerID':'EventPlayerID'},inplace = True)\n\n    M_player_stats = pd.merge(M_players,M_event_by_player,on = 'EventPlayerID',how = 'left')\n    W_player_stats = pd.merge(W_players,W_event_by_player,on = 'EventPlayerID',how = 'left')\n    M_player_stats = M_player_stats.fillna(0)\n    W_player_stats = W_player_stats.fillna(0)\n    \n    \n    M_player_stats.to_csv(f'mens_stats/M_player_stats_{year}.csv')\n    W_player_stats.to_csv(f'womens_stats/W_player_stats_{year}.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pre_processer(df):\n    player_information=df.iloc[:,0:4]\n    EventPlayerID=player_information['EventPlayerID']\n    df=df.drop(['EventPlayerID','LastName','FirstName'],axis=1)\n    df[df==0]=np.nan\n    pd.isnull(df)\n    df=df.dropna(how='all')\n    df.tail()\n    df.insert(0,'EventPlayerID',EventPlayerID)\n    df=pd.merge(player_information, df, on='EventPlayerID')\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def Team_member(df):\n    number_of_teamplayer=Counter(df['TeamID'])\n    #number=list(number_of_tramplayer)\n    number=number_of_teamplayer.values()\n    number=list(number)\n    np.shape(number)\n    \n    return number","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def TeamID_made(df):\n    TeamID=list(df.drop_duplicates(['TeamID']).TeamID)\n    k=len(TeamID)\n    \n    return TeamID","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def calculator_advanced(i,df,TeamID): \n    df1=df[df['TeamID']==TeamID[i]][['AT','eFG','TS','PER']].sum()\n    #df_sum=df1.append([df2,df3,df4],ignore_index = False)\n    df_sum=pd.DataFrame(df1,columns=[TeamID[i]])\n    return df_sum","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sum_final(df,k,TeamID):\n    df[df['AT']>10000]=0\n    # k=len(TeamID)\n    df_sum_final=calculator_advanced(0,df,TeamID)\n    for i in range (1,k):\n        df_temp=calculator_advanced(i,df,TeamID)\n        df_sum_final=pd.concat([df_sum_final,df_temp],axis=1)\n        #df_sum_final=df_sum_final.append([df_temp],ignore_index = False)\n    return df_sum_final","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from collections import Counter\nimport warnings\nwarnings.filterwarnings('ignore')\n\ndf_2015=pd.read_csv(\"mens_stats/M_player_stats_2015.csv\")\ndf_2016=pd.read_csv(\"mens_stats/M_player_stats_2016.csv\")\ndf_2017=pd.read_csv(\"mens_stats/M_player_stats_2017.csv\")\ndf_2018=pd.read_csv(\"mens_stats/M_player_stats_2018.csv\")\ndf_2019=pd.read_csv(\"mens_stats/M_player_stats_2019.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pre_processer(df):\n    player_information=df.iloc[:,0:4]\n    EventPlayerID=player_information['EventPlayerID']\n    df=df.drop(['EventPlayerID','LastName','FirstName'],axis=1)\n    df[df==0]=np.nan\n    pd.isnull(df)\n    df=df.dropna(how='all')\n    df.tail()\n    df.insert(0,'EventPlayerID',EventPlayerID)\n    df=pd.merge(player_information, df, on='EventPlayerID')\n    return df\n\ndef Team_member(df):\n    number_of_teamplayer=Counter(df['TeamID'])\n    #number=list(number_of_tramplayer)\n    number=number_of_teamplayer.values()\n    number=list(number)\n    np.shape(number)\n    return number\n\n\ndef TeamID_made(df):\n    TeamID=list(df.drop_duplicates(['TeamID']).TeamID)\n    k=len(TeamID)\n    \n    return TeamID\n\ndef calculator_advanced(i,df,TeamID): \n    df1=df[df['TeamID']==TeamID[i]][['AT','eFG','TS','PER']].sum()\n    #df_sum=df1.append([df2,df3,df4],ignore_index = False)\n    df_sum=pd.DataFrame(df1,columns=[TeamID[i]])\n    return df_sum\n\n\ndef calculator_advanced(i,df,TeamID): \n    df1=df[df['TeamID']==TeamID[i]][['AT','eFG','TS','PER']].sum()\n    #df_sum=df1.append([df2,df3,df4],ignore_index = False)\n    df_sum=pd.DataFrame(df1,columns=[TeamID[i]])\n    return df_sum\n\ndef sum_final(df,k,TeamID):\n    df[df['AT']>10000]=0\n    # k=len(TeamID)\n    df_sum_final=calculator_advanced(0,df,TeamID)\n    for i in range (1,k):\n        df_temp=calculator_advanced(i,df,TeamID)\n        df_sum_final=pd.concat([df_sum_final,df_temp],axis=1)\n        #df_sum_final=df_sum_final.append([df_temp],ignore_index = False)\n    return df_sum_final","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015=pre_processer(df_2015)\nnumber_2015=Team_member(df_2015)\nTeamID_2015=TeamID_made(df_2015)\nk_2015=len(TeamID_2015)\ndf_sum_final_2015=sum_final(df_2015,k_2015,TeamID_2015)\nfor i in range(k_2015):\n    df_sum_final_2015.iloc[:,i]=df_sum_final_2015.iloc[:,i]/number_2015[i]\n\ndf_2016=pre_processer(df_2016)\nnumber_2016=Team_member(df_2016)\nTeamID_2016=TeamID_made(df_2016)\nk_2016=len(TeamID_2016)\ndf_sum_final_2016=sum_final(df_2016,k_2016,TeamID_2016)\nfor i in range(k_2016):\n    df_sum_final_2016.iloc[:,i]=df_sum_final_2016.iloc[:,i]/number_2016[i]\n\ndf_2017=pre_processer(df_2017)\nnumber_2017=Team_member(df_2017)\nTeamID_2017=TeamID_made(df_2017)\nk_2017=len(TeamID_2017)\ndf_sum_final_2017=sum_final(df_2017,k_2017,TeamID_2017)\nfor i in range(k_2017):\n    df_sum_final_2017.iloc[:,i]=df_sum_final_2017.iloc[:,i]/number_2017[i]\n    \ndf_2018=pre_processer(df_2018)\nnumber_2018=Team_member(df_2018)\nTeamID_2018=TeamID_made(df_2018)\nk_2018=len(TeamID_2018)\ndf_sum_final_2018=sum_final(df_2018,k_2018,TeamID_2018)\nfor i in range(k_2018):\n    df_sum_final_2018.iloc[:,i]=df_sum_final_2018.iloc[:,i]/number_2018[i]\n\ndf_2019=pre_processer(df_2019)\nnumber_2019=Team_member(df_2019)\nTeamID_2019=TeamID_made(df_2019)\nk_2019=len(TeamID_2019)\ndf_sum_final_2019=sum_final(df_2019,k_2019,TeamID_2019)\nfor i in range(k_2019):\n    df_sum_final_2019.iloc[:,i]=df_sum_final_2019.iloc[:,i]/number_2019[i]\ndf_sum_final_2019.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mkdir('player_as')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sum_final_2015.to_csv('player_as/df_sum_final_2015.csv')\ndf_sum_final_2016.to_csv('player_as/df_sum_final_2016.csv')\ndf_sum_final_2017.to_csv('player_as/df_sum_final_2017.csv')\ndf_sum_final_2018.to_csv('player_as/df_sum_final_2018.csv')\ndf_sum_final_2019.to_csv('player_as/df_sum_final_2019.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target=pd.read_csv(\"M_result_by_game_tourney_editored.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target=df_target[['Season','WTeamID','LTeamID','target']]\ndf_target.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_win(df,i,TeamID):\n    df_new=df.T\n    df_new.columns=[['AT_win','eFG_win','TS_win','PER_win']]\n    df_new['Season']=i\n    df_new['WTeamID']=TeamID\n    \n    return df_new","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_lose(df,i,TeamID):\n    df_new=df.T\n    df_new.columns=[['AT_lose','eFG_lose','TS_lose','PER_lose']]\n    df_new['Season']=i\n    df_new['LTeamID']=TeamID\n    \n    return df_new","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015_new_win=preprocess_win(df_sum_final_2015,2015,TeamID_2015)\ndf_2016_new_win=preprocess_win(df_sum_final_2016,2016,TeamID_2016)\ndf_2017_new_win=preprocess_win(df_sum_final_2017,2017,TeamID_2017)\ndf_2018_new_win=preprocess_win(df_sum_final_2018,2018,TeamID_2018)\ndf_2019_new_win=preprocess_win(df_sum_final_2019,2019,TeamID_2019)\ndf_2015_new_lose=preprocess_lose(df_sum_final_2015,2015,TeamID_2015)\ndf_2016_new_lose=preprocess_lose(df_sum_final_2016,2016,TeamID_2016)\ndf_2017_new_lose=preprocess_lose(df_sum_final_2017,2017,TeamID_2017)\ndf_2018_new_lose=preprocess_lose(df_sum_final_2018,2018,TeamID_2018)\ndf_2019_new_lose=preprocess_lose(df_sum_final_2019,2019,TeamID_2019)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=df_2015_new_win.append(df_2016_new_win)\ndf2=df1.append(df_2017_new_win)\ndf3=df2.append(df_2018_new_win)\ndf4=df3.append(df_2019_new_win)\ndf_final_win=df4\ndf_final_win","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=df_2015_new_lose.append(df_2016_new_lose)\ndf2=df1.append(df_2017_new_lose)\ndf3=df2.append(df_2018_new_lose)\ndf4=df3.append(df_2019_new_lose)\ndf_final_lose=df4\ndf_final_lose","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final_win.columns=df_final_win.columns.get_level_values(0)\ndf_final_win.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#第一句话不被运行是第一次的时候才用：\ndf_target_new=pd.merge(df_target,df_final_win,on = ['Season','WTeamID'],how='left')\ndf_target_new.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final_lose.columns=df_final_lose.columns.get_level_values(0)\ndf_final_lose.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target_new=pd.merge(df_target_new,df_final_lose,on = ['Season','LTeamID'],how='left')\ndf_target_new.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target_new.to_csv('with_advanced_stat.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015=pd.read_csv(\"womens_stats/W_player_stats_2015.csv\")\ndf_2016=pd.read_csv(\"womens_stats/W_player_stats_2016.csv\")\ndf_2017=pd.read_csv(\"womens_stats/W_player_stats_2017.csv\")\ndf_2018=pd.read_csv(\"womens_stats/W_player_stats_2018.csv\")\ndf_2019=pd.read_csv(\"womens_stats/W_player_stats_2019.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015=pre_processer(df_2015)\nnumber_2015=Team_member(df_2015)\nTeamID_2015=TeamID_made(df_2015)\nk_2015=len(TeamID_2015)\ndf_sum_final_2015=sum_final(df_2015,k_2015,TeamID_2015)\nfor i in range(k_2015):\n    df_sum_final_2015.iloc[:,i]=df_sum_final_2015.iloc[:,i]/number_2015[i]\n\ndf_2016=pre_processer(df_2016)\nnumber_2016=Team_member(df_2016)\nTeamID_2016=TeamID_made(df_2016)\nk_2016=len(TeamID_2016)\ndf_sum_final_2016=sum_final(df_2016,k_2016,TeamID_2016)\nfor i in range(k_2016):\n    df_sum_final_2016.iloc[:,i]=df_sum_final_2016.iloc[:,i]/number_2016[i]\n\ndf_2017=pre_processer(df_2017)\nnumber_2017=Team_member(df_2017)\nTeamID_2017=TeamID_made(df_2017)\nk_2017=len(TeamID_2017)\ndf_sum_final_2017=sum_final(df_2017,k_2017,TeamID_2017)\nfor i in range(k_2017):\n    df_sum_final_2017.iloc[:,i]=df_sum_final_2017.iloc[:,i]/number_2017[i]\n    \ndf_2018=pre_processer(df_2018)\nnumber_2018=Team_member(df_2018)\nTeamID_2018=TeamID_made(df_2018)\nk_2018=len(TeamID_2018)\ndf_sum_final_2018=sum_final(df_2018,k_2018,TeamID_2018)\nfor i in range(k_2018):\n    df_sum_final_2018.iloc[:,i]=df_sum_final_2018.iloc[:,i]/number_2018[i]\n\ndf_2019=pre_processer(df_2019)\nnumber_2019=Team_member(df_2019)\nTeamID_2019=TeamID_made(df_2019)\nk_2019=len(TeamID_2019)\ndf_sum_final_2019=sum_final(df_2019,k_2019,TeamID_2019)\nfor i in range(k_2019):\n    df_sum_final_2019.iloc[:,i]=df_sum_final_2019.iloc[:,i]/number_2019[i]\ndf_sum_final_2019.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sum_final_2015.to_csv('player_as/W_df_sum_final_2015.csv')\ndf_sum_final_2016.to_csv('player_as/W_df_sum_final_2016.csv')\ndf_sum_final_2017.to_csv('player_as/W_df_sum_final_2017.csv')\ndf_sum_final_2018.to_csv('player_as/W_df_sum_final_2018.csv')\ndf_sum_final_2019.to_csv('player_as/W_df_sum_final_2019.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target=pd.read_csv(\"W_result_by_game_tourney_editored.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target=df_target[['Season','WTeamID','LTeamID','target']]\ndf_target.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015_new_win=preprocess_win(df_sum_final_2015,2015,TeamID_2015)\ndf_2016_new_win=preprocess_win(df_sum_final_2016,2016,TeamID_2016)\ndf_2017_new_win=preprocess_win(df_sum_final_2017,2017,TeamID_2017)\ndf_2018_new_win=preprocess_win(df_sum_final_2018,2018,TeamID_2018)\ndf_2019_new_win=preprocess_win(df_sum_final_2019,2019,TeamID_2019)\ndf_2015_new_lose=preprocess_lose(df_sum_final_2015,2015,TeamID_2015)\ndf_2016_new_lose=preprocess_lose(df_sum_final_2016,2016,TeamID_2016)\ndf_2017_new_lose=preprocess_lose(df_sum_final_2017,2017,TeamID_2017)\ndf_2018_new_lose=preprocess_lose(df_sum_final_2018,2018,TeamID_2018)\ndf_2019_new_lose=preprocess_lose(df_sum_final_2019,2019,TeamID_2019)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=df_2015_new_win.append(df_2016_new_win)\ndf2=df1.append(df_2017_new_win)\ndf3=df2.append(df_2018_new_win)\ndf4=df3.append(df_2019_new_win)\ndf_final_win=df4\ndf_final_win","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=df_2015_new_lose.append(df_2016_new_lose)\ndf2=df1.append(df_2017_new_lose)\ndf3=df2.append(df_2018_new_lose)\ndf4=df3.append(df_2019_new_lose)\ndf_final_lose=df4\ndf_final_lose","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final_win.columns=df_final_win.columns.get_level_values(0)\ndf_final_win.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#第一句话不被运行是第一次的时候才用：\ndf_target_new=pd.merge(df_target,df_final_win,on = ['Season','WTeamID'],how='left')\ndf_target_new.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final_lose.columns=df_final_lose.columns.get_level_values(0)\ndf_final_lose.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target_new_W=pd.merge(df_target_new,df_final_lose,on = ['Season','LTeamID'],how='left')\ndf_target_new_W.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target_new_W.to_csv('W_with_advanced_stat.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_target_new_W","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Regression"},{"metadata":{"trusted":true},"cell_type":"code","source":"df=df_target_new\n#order=['Season','WTeamID','LTeamID','AT_win','eFG_win','TS_win','PER_win','AT_lose','eFG_lose','TS_lose','PER_lose','target']\ntarget=df['target']\ndf=df.drop('target',axis=1)\ndf['target']=target\ndf.tail()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### We use pairplot to show the bivariate data relation.\n#### Where we find \"target=1\" group shows a tighter convergence than \"target=0\" group. There is a different distribution of these four advanced statistics(AT,eFG,TS,PER) according to these two \"targets\", that is to say to some degree, \"target\" can be explained by these four stats. Hence, we then use Logistic Regression."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_plot=df[['target','AT_win','eFG_win','TS_win','PER_win']]\nsns.pairplot(df_plot, hue=\"target\", size=3, diag_kind=\"kde\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data \ndf_train=df[df['Season']<2019]\ndf_test=df[df['Season']>2018]\n\ndf_train=df_train.drop(['Season','WTeamID','LTeamID'],axis=1)\ndf_test=df_test.drop(['Season','WTeamID','LTeamID'],axis=1)\n\nm,n=np.shape(df_train)\n\nx_train = df_train.iloc[:,0:n-1].values\ny_train = df_train.target.values\n\nx_test = df_test.iloc[:,0:n-1].values\ny_test = df_test.target.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=df_target_new_W\n#order=['Season','WTeamID','LTeamID','AT_win','eFG_win','TS_win','PER_win','AT_lose','eFG_lose','TS_lose','PER_lose','target']\ntarget=df['target']\ndf=df.drop('target',axis=1)\ndf['target']=target\ndf.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_plot=df[['target','AT_win','eFG_win','TS_win','PER_win']]\nsns.pairplot(df_plot, hue=\"target\", size=3, diag_kind=\"kde\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data \ndf_train=df[df['Season']<2019]\ndf_test=df[df['Season']>2018]\n\ndf_train=df_train.drop(['Season','WTeamID','LTeamID'],axis=1)\ndf_test=df_test.drop(['Season','WTeamID','LTeamID'],axis=1)\n\nm,n=np.shape(df_train)\n\nx_train = df_train.iloc[:,0:n-1].values\ny_train = df_train.target.values\n\nx_test = df_test.iloc[:,0:n-1].values\ny_test = df_test.target.values","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## same for women"},{"metadata":{"trusted":true},"cell_type":"code","source":"df=df_target_new_W\n#order=['Season','WTeamID','LTeamID','AT_win','eFG_win','TS_win','PER_win','AT_lose','eFG_lose','TS_lose','PER_lose','target']\ntarget=df['target']\ndf=df.drop('target',axis=1)\ndf['target']=target","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_plot=df[['target','AT_win','eFG_win','TS_win','PER_win']]\nsns.pairplot(df_plot, hue=\"target\", size=3, diag_kind=\"kde\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data \ndf_train=df[df['Season']<2019]\ndf_test=df[df['Season']>2018]\n\ndf_train=df_train.drop(['Season','WTeamID','LTeamID'],axis=1)\ndf_test=df_test.drop(['Season','WTeamID','LTeamID'],axis=1)\n\nm,n=np.shape(df_train)\n\nx_train = df_train.iloc[:,0:n-1].values\ny_train = df_train.target.values\n\nx_test = df_test.iloc[:,0:n-1].values\ny_test = df_test.target.values","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## logistic regression with advanced stats"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LogisticRegressionCV\nfrom sklearn.metrics import accuracy_score\nlogreg = LogisticRegressionCV(cv=5,random_state=0, solver='newton-cg')\nlogreg.fit(x_train, y_train)\n\ny_pred_train = logreg.predict(x_train)\ny_pred_test = logreg.predict(x_test)\n\nprint(\"Coefficients :\", np.round(logreg.intercept_,4), np.round(logreg.coef_,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_train = logreg.predict(x_train)\ny_pred_test = logreg.predict(x_test)\n\naccuracy_train = accuracy_score(y_train, y_pred_train)\naccuracy_test = accuracy_score(y_test, y_pred_test)\nprint('Accuracy on the training set =', np.round(accuracy_train,4))\nprint('Accuracy on the test set =', np.round(accuracy_test,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015=df_2015[['AT','eFG','TS','PER','TeamID']]\ndf_2016=df_2016[['AT','eFG','TS','PER','TeamID']]\ndf_2017=df_2017[['AT','eFG','TS','PER','TeamID']]\ndf_2018=df_2018[['AT','eFG','TS','PER','TeamID']]\ndf_2019=df_2019[['AT','eFG','TS','PER','TeamID']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"coef=np.round(logreg.coef_,4)\ncoef[0][0:4]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def project(df):\n    df_player_influence=0\n    for i in range(4):  \n        df_player_influence=coef[0][i]*df.iloc[:,i]+df_player_influence\n    df['player_influence']=df_player_influence\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015_new=project(df_2015)\ndf_2016_new=project(df_2016)\ndf_2017_new=project(df_2017)\ndf_2018_new=project(df_2018)\ndf_2019_new=project(df_2019)\n\ndf_2015_new.dropna(axis=0,how='any',inplace=True)\ndf_2016_new.dropna(axis=0,how='any',inplace=True)\ndf_2017_new.dropna(axis=0,how='any',inplace=True)\ndf_2018_new.dropna(axis=0,how='any',inplace=True)\ndf_2019_new.dropna(axis=0,how='any',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sum_influence(df,k,TeamID,q,number):\n    # k=len(TeamID)\n    df_sum_final=[]\n    df_sum_final.append(df[df['TeamID']==TeamID[0]]['player_influence'].sum())\n    lists=['influence_2015','influence_2016','influence_2017','influence_2018','influence_2019']\n    for i in range (1,k):\n        df_temp=df[df['TeamID']==TeamID[i]]['player_influence'].sum()\n        df_sum_final.append(df_temp)\n        #df_sum_final=df_sum_final.append([df_temp],ignore_index = False)\n    df_sum_final=pd.DataFrame(df_sum_final,columns=[lists[q]])\n    for i in range(k):\n        df_sum_final.loc[i]=df_sum_final.loc[i]/number[i]\n    return df_sum_final","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"team_2015_influence=sum_influence(df_2015_new,k_2015,TeamID_2015,0,number_2015)\nteam_2015_influence['Season_2015']=2015\nteam_2015_influence['TeamID']=TeamID_2015\n\nteam_2016_influence=sum_influence(df_2016_new,k_2016,TeamID_2016,1,number_2016)\nteam_2016_influence['Season_2016']=2016\nteam_2016_influence['TeamID']=TeamID_2016\n\nteam_2017_influence=sum_influence(df_2017_new,k_2017,TeamID_2017,2,number_2017)\nteam_2017_influence['Season_2017']=2017\nteam_2017_influence['TeamID']=TeamID_2017\n\nteam_2018_influence=sum_influence(df_2018_new,k_2018,TeamID_2018,3,number_2018)\nteam_2018_influence['Season_2018']=2018\nteam_2018_influence['TeamID']=TeamID_2018\n\nteam_2019_influence=sum_influence(df_2019_new,k_2019,TeamID_2019,4,number_2019)\nteam_2019_influence['Season_2019']=2019\nteam_2019_influence['TeamID']=TeamID_2019","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_1=pd.merge(team_2015_influence,team_2016_influence,on=['TeamID'])\ndf_2=pd.merge(df_1,team_2017_influence,on=['TeamID'])\ndf_3=pd.merge(df_2,team_2018_influence,on=['TeamID'])\ndf_final_influence=pd.merge(df_3,team_2019_influence,on=['TeamID'])\ndf_final_influence=df_final_influence.drop(['Season_2015','Season_2016','Season_2017','Season_2018','Season_2019'],axis=1)\nTeamID=df_final_influence['TeamID']\ndf_final_influence.drop(['TeamID'],axis=1,inplace=True)\ndf_final_influence.insert(0,'TeamID',TeamID)\ndf_final_influence.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final_influence.to_csv('team_influence_time_series.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install pmdarima","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pmdarima as pm\nfrom statsmodels.tsa.arima_model import ARIMA\nfrom statsmodels.tsa.stattools import adfuller\n\ndf_input=df_final_influence.iloc[:,1:6]\ndf_input=df_input.T\ndf_input.tail()\n\n\ndef auto_arima(df,i):\n    model = pm.auto_arima(df.iloc[:,i], trace=False, error_action='ignore', suppress_warnings=True)\n    model.fit(df.iloc[:,i])\n    forecast = model.predict(n_periods=1)\n    \n    return forecast","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m,n=np.shape(df_input)\nresult=[]\nfor i in range(n):\n    temp=auto_arima(df_input,i)\n    result.append(temp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result=pd.DataFrame(result,columns=['prediction_2020'])\nresult=result.T\nresult","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_output=df_input.append(result)\ndf_output.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_output.to_csv('team_influence_prediction.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install chart_studio\n!pip install bubbly","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_output_new=df_output.T\ndf_1=df_output_new[['influence_2015']]\ndf_1.rename(columns={'influence_2015': 'influence'}, inplace=True)\ndf_1['teamid']=TeamID\ndf_1['season']='Real'\n\ndf_2=df_output_new[['influence_2016']]\ndf_2.rename(columns={'influence_2016': 'influence'}, inplace=True)\ndf_2['teamid']=TeamID\ndf_2['season']='Real'\n\ndf_3=df_output_new[['influence_2017']]\ndf_3.rename(columns={'influence_2017': 'influence'}, inplace=True)\ndf_3['teamid']=TeamID\ndf_3['season']='Real'\n\ndf_4=df_output_new[['influence_2018']]\ndf_4.rename(columns={'influence_2018': 'influence'}, inplace=True)\ndf_4['teamid']=TeamID\ndf_4['season']='Real'\n\ndf_5=df_output_new[['influence_2019']]\ndf_5.rename(columns={'influence_2019': 'influence'}, inplace=True)\ndf_5['teamid']=TeamID\ndf_5['season']='Real'\n\ndf_6=df_output_new[['prediction_2020']]\ndf_6.rename(columns={'prediction_2020': 'influence'}, inplace=True)\ndf_6['teamid']=TeamID\ndf_6['season']='Prediction'\n\ndf_bubble=pd.concat([df_1,df_2,df_3,df_4,df_5,df_6],axis=0)\ndf_bubble\n\n#df_bubble.iloc[:,0]=df_bubble.iloc[:,0]-min(df_bubble.iloc[:,0])\ndf_bubble.reset_index(drop=True, inplace=True)\ndf_bubble.sort_values(by='influence',ascending=False,inplace=True)\ndf_bubble_plot=df_bubble.head(100)\ndf_bubble_plot.tail()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from bubbly.bubbly import bubbleplot \nfrom plotly.offline import iplot\nimport chart_studio.plotly as py\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"figure = bubbleplot(dataset=df_bubble_plot, x_column='teamid', y_column='influence', \n                    bubble_column='season', size_column='influence', color_column='season', \n                    x_logscale=True, scale_bubble=2, height=350)\n\niplot(figure)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\ndf_influence = df_bubble['influence']\nplt.figure(figsize=(8,6))\nsns.set_style(\"darkgrid\")\nsns.kdeplot(data=df_influence,label=\"Team_Competitiveness\" ,shade=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"logreg = LogisticRegressionCV(cv=5,random_state=0, solver='newton-cg')\nlogreg.fit(x_train_W, y_train_W)\n\ny_pred_train = logreg.predict(x_train_W)\ny_pred_test = logreg.predict(x_test_W)\n\nprint(\"Coefficients :\", np.round(logreg.intercept_,4), np.round(logreg.coef_,4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_2015=df_2015[['AT','eFG','TS','PER','TeamID']]\ndf_2016=df_2016[['AT','eFG','TS','PER','TeamID']]\ndf_2017=df_2017[['AT','eFG','TS','PER','TeamID']]\ndf_2018=df_2018[['AT','eFG','TS','PER','TeamID']]\ndf_2019=df_2019[['AT','eFG','TS','PER','TeamID']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}