{"cells":[{"metadata":{},"cell_type":"markdown","source":"[](https://www.google.com/url?sa=i&url=http%3A%2F%2Fqbardarien.com%2Fcalendar%2Fq-event-list%2F293-march-madness-2020-at-q-bar-darien%3Fdate%3D2020-03-26-17-00&psig=AOvVaw28oFASgDMYBa6bE2-zqmKC&ust=1584120730706000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCLDAn_-LlugCFQAAAAAdAAAAABAD)"},{"metadata":{},"cell_type":"markdown","source":"## Overview"},{"metadata":{},"cell_type":"markdown","source":"This Notebook inspired by the Starter kernel [Basic Starter Kernel - NCAA Women's Dataset](https://www.kaggle.com/juliaelliott/basic-starter-kernel-ncaa-women-s-dataset) by [Jullia Eulliot](https://www.kaggle.com/juliaelliott)\n\nThe idea is to create a basic Logistic Regression Model based on the seed differences between teams !\nNote : The Stage's 1 Submissions File is based on expected outcomes \nFor Stage 2, you will be predicting future outcomes based on the teams selected for the tournament.\nif you need to know more about the competition you can check this [comment](https://www.kaggle.com/c/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/discussion/135057#769427)"},{"metadata":{},"cell_type":"markdown","source":"## Acknowledgements\n\n#### This Notebook uses such great kernels : \n\n- [Logistic Regression on Tournament seeds](https://www.kaggle.com/kplauritzen/notebookde27b18258?scriptVersionId=804590)\n- [2020 March Madness Data - First Look EDA](https://www.kaggle.com/robikscube/2020-march-madness-data-first-look-eda)\n- [NCAAM20no-leak starter](https://www.kaggle.com/code1110/ncaam20-finally-no-leak-starter)\n- [Basic Starter Kernel - NCAA Women's Dataset](https://www.kaggle.com/juliaelliott/basic-starter-kernel-ncaa-women-s-dataset)\n\nBefore we get started I want to thank Kaggle and each member here for sharing such huge concepts , without their efforts i can't write this notebook \nwith such ease !\n\nSo, without wasting any time, let's start with importing some important python modules that I'll be using.\n\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom sklearn.linear_model import LogisticRegression\nimport matplotlib.pyplot as plt\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import GridSearchCV\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 the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Loading Data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data_dir = '../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/'\ndf_seeds = pd.read_csv(data_dir + 'MNCAATourneySeeds.csv')\ndf_tour = pd.read_csv(data_dir + 'MNCAATourneyCompactResults.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_seeds.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_tour.head()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"then , Let's drop the columns we are not planning on using\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def seed_to_int(seed):\n    \n    \"\"\"Get just the digits from the seeding. Return as int\n    \"\"\"\n    s_int = int(seed[1:3])\n    return(s_int)\n\ndf_seeds['seed_int'] = df_seeds.Seed.apply(seed_to_int)\ndf_seeds.drop(labels=['Seed'], inplace=True, axis=1) # here the string label\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's Check the new dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_seeds.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Merge seed for each team\nWe want the seeds in the same DataFrame as the game results\nthat's why we gonna Merge the Seeds with their corresponding TeamIDs in the compact results dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_winseeds = df_seeds.rename(columns={'TeamID':'WTeamID', 'seed_int':'WSeed'})\ndf_lossseeds = df_seeds.rename(columns={'TeamID':'LTeamID', 'seed_int':'LSeed'})\n\ndf_dummy = pd.merge(left=df_tour, right=df_winseeds, how='left', on=['Season', 'WTeamID'])\ndf_concat = pd.merge(left=df_dummy, right=df_lossseeds, on=['Season', 'LTeamID'])\n\ndf_concat['SeedDiff'] = df_concat.WSeed - df_concat.LSeed # difference seed\n\n# Let's concat dataframes and show it !\n\ndf_concat.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Next  we'll create a new  dataframe that summarizes wins & losses along with their corresponding seed differences.\n\nThis is the meat of what we'll be creating our model on."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Winners \n\ndf_wins = pd.DataFrame()\ndf_wins['SeedDiff'] = df_concat['SeedDiff']\ndf_wins['result'] = 1\n\n# Losses\ndf_losses = pd.DataFrame()\ndf_losses['SeedDiff'] = -df_concat['SeedDiff']\ndf_losses['result'] = 0\n\n# Concat them together\n\ndf_for_predictions = pd.concat((df_wins, df_losses))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_for_predictions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = df_for_predictions.SeedDiff.values.reshape(-1,1)\n\ny_train = df_for_predictions.result.values\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's Shuffle Our DataFrame"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, y_train = shuffle(X_train, y_train)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Train Our Linear Regression Model\nWe'll Use a basic logistic regression to train Our model. \nYou can set different C values to see how performance changes.\n\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"logregModel = LogisticRegression()\n\nparams = {'C': np.logspace(start=-5, stop=3, num=9)}\nclf = GridSearchCV(logregModel, params, scoring='neg_log_loss', refit=True)\nclf.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Evaluate the Model and show the best score"},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Best log_loss: {:.4}, with best C: {}'.format(clf.best_score_, clf.best_params_['C']))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Examine the classifier predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"X = np.arange(-15, 15).reshape(-1, 1)\npreds = clf.predict_proba(X)[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(X, preds)\nplt.xlabel('Team1 seed - Team2 seed')\nplt.ylabel('P(Team1 will win)')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Note :  as Shown below  the probability a team will win decreases as the seed differential to its opponent decreases as well ."},{"metadata":{},"cell_type":"markdown","source":"## Make predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sample_sub = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')\n\nn_test = len(df_sample_sub)\n\n# get the year\n\ndef get_year_t1_t2(ID):\n    \"\"\"Return a tuple with ints `year`, `team1` and `team2`.\"\"\"\n    return (int(x) for x in ID.split('_'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = np.zeros(shape=(n_test, 1))\n\nfor i, row in df_sample_sub.iterrows():\n    \n    year, t1, t2 = get_year_t1_t2(row.ID)\n    \n    t1_seed = df_seeds[(df_seeds.TeamID == t1) & (df_seeds.Season == year)].seed_int.values[0]\n    t2_seed = df_seeds[(df_seeds.TeamID == t2) & (df_seeds.Season == year)].seed_int.values[0]\n    diff_seed = t1_seed - t2_seed\n    X_test[i, 0] = diff_seed","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Next , we will make prediction with our LR Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = clf.predict_proba(X_test)[:,1] # predictor\n\n\nclipped_preds = np.clip(preds, 0.05, 0.95) # clipped predictions\n\ndf_sample_sub.Pred = clipped_preds\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Show up the result"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sample_sub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Finally, create your submission file!\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sample_sub.to_csv('Submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## I hope you find this kernel useful and enjoyable.\n## Your comments and feedback are most welcome.\n\n## Happy Kaggling"},{"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}