{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Classification on Imbalanced Data\n\nThis notebook demonstrates how to perform classification on data that is imbalanced.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns \n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.svm import SVC\nfrom xgboost import XGBClassifier\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.utils import resample\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-28T05:36:30.303061Z","iopub.execute_input":"2023-01-28T05:36:30.303383Z","iopub.status.idle":"2023-01-28T05:36:31.746647Z","shell.execute_reply.started":"2023-01-28T05:36:30.303310Z","shell.execute_reply":"2023-01-28T05:36:31.744896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_players = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/players.csv')\ndf_games = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/games.csv')\ndf_plays = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/plays.csv')\ndf_scouting = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:31.748616Z","iopub.execute_input":"2023-01-28T05:36:31.749297Z","iopub.status.idle":"2023-01-28T05:36:32.044940Z","shell.execute_reply.started":"2023-01-28T05:36:31.749256Z","shell.execute_reply":"2023-01-28T05:36:32.044057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_field_goal = df_plays[df_plays['specialTeamsPlayType'] == 'Field Goal']\ndf_field_goal.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.046825Z","iopub.execute_input":"2023-01-28T05:36:32.047195Z","iopub.status.idle":"2023-01-28T05:36:32.084313Z","shell.execute_reply.started":"2023-01-28T05:36:32.047162Z","shell.execute_reply":"2023-01-28T05:36:32.082699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['yardsToGo', 'yardlineNumber', 'kickLength', 'playResult', 'specialTeamsResult']\ndf = df_field_goal[cols]\ndf","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.086327Z","iopub.execute_input":"2023-01-28T05:36:32.086630Z","iopub.status.idle":"2023-01-28T05:36:32.107890Z","shell.execute_reply.started":"2023-01-28T05:36:32.086602Z","shell.execute_reply":"2023-01-28T05:36:32.107242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.108900Z","iopub.execute_input":"2023-01-28T05:36:32.109194Z","iopub.status.idle":"2023-01-28T05:36:32.117377Z","shell.execute_reply.started":"2023-01-28T05:36:32.109170Z","shell.execute_reply":"2023-01-28T05:36:32.116758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df.dropna()\nx['specialTeamsResult'] = x['specialTeamsResult'].apply(lambda x: 1 if str(x) in 'Kick Attempt Good' else 0)\nx","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.118320Z","iopub.execute_input":"2023-01-28T05:36:32.118786Z","iopub.status.idle":"2023-01-28T05:36:32.139641Z","shell.execute_reply.started":"2023-01-28T05:36:32.118761Z","shell.execute_reply":"2023-01-28T05:36:32.138726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=x, x=\"specialTeamsResult\", hue='specialTeamsResult')","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.140922Z","iopub.execute_input":"2023-01-28T05:36:32.142187Z","iopub.status.idle":"2023-01-28T05:36:32.360104Z","shell.execute_reply.started":"2023-01-28T05:36:32.142151Z","shell.execute_reply":"2023-01-28T05:36:32.358721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = x['specialTeamsResult']\nX_train, X_test, y_train, y_test = train_test_split(x.drop(columns=['specialTeamsResult'], axis=1), y, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.361334Z","iopub.execute_input":"2023-01-28T05:36:32.362191Z","iopub.status.idle":"2023-01-28T05:36:32.370628Z","shell.execute_reply.started":"2023-01-28T05:36:32.362159Z","shell.execute_reply":"2023-01-28T05:36:32.369448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ada_clf = AdaBoostClassifier()\nknn_clf = KNeighborsClassifier()\nsvc_clf = SVC(kernel=\"linear\", class_weight='balanced')\nxgbc_clf = XGBClassifier(scale_pos_weight=5)\nrnd_frst = RandomForestClassifier(n_estimators=100, class_weight=\"balanced\")","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.371851Z","iopub.execute_input":"2023-01-28T05:36:32.372736Z","iopub.status.idle":"2023-01-28T05:36:32.378734Z","shell.execute_reply.started":"2023-01-28T05:36:32.372703Z","shell.execute_reply":"2023-01-28T05:36:32.378038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifiers = [ada_clf, knn_clf, svc_clf, rnd_frst, xgbc_clf]\n\ndef run_classifier_models(cls_models, X_train, y_train, X_test, y_test):\n    for clsfr in cls_models:\n        clf = make_pipeline(StandardScaler(), clsfr)\n        clf.fit(X_train, y_train)\n        score = clf.score(X_test, y_test)\n        print('score for ', clsfr.__class__.__name__, score)\n        if \"SVC\" not in clsfr.__class__.__name__:\n            print('roc auc score', roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1]))\n        print(\n            f\"Classification report for classifier {clf}:\\n\"\n            f\"{classification_report(y_test, clf.predict(X_test))}\\n\"\n        )\n        \nrun_classifier_models(classifiers, X_train, y_train, X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:32.381401Z","iopub.execute_input":"2023-01-28T05:36:32.382328Z","iopub.status.idle":"2023-01-28T05:36:33.580295Z","shell.execute_reply.started":"2023-01-28T05:36:32.382300Z","shell.execute_reply":"2023-01-28T05:36:33.579366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_majority = x[x.specialTeamsResult==1]\ndf_minority = x[x.specialTeamsResult==0]\n\nnum_samples = 2218\n\ndf_minority_upsampled = resample(df_minority, replace=True, n_samples=num_samples)\n\ndf_upsampled = pd.concat([df_majority, df_minority_upsampled])\n \ndf_upsampled.specialTeamsResult.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:33.581449Z","iopub.execute_input":"2023-01-28T05:36:33.582001Z","iopub.status.idle":"2023-01-28T05:36:33.597776Z","shell.execute_reply.started":"2023-01-28T05:36:33.581946Z","shell.execute_reply":"2023-01-28T05:36:33.595556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df_upsampled, x=\"specialTeamsResult\", hue='specialTeamsResult')","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:33.600936Z","iopub.execute_input":"2023-01-28T05:36:33.601237Z","iopub.status.idle":"2023-01-28T05:36:33.749837Z","shell.execute_reply.started":"2023-01-28T05:36:33.601213Z","shell.execute_reply":"2023-01-28T05:36:33.748369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_scaled_train, X_scaled_test, y_scaled_train, y_scaled_test = train_test_split(df_upsampled.drop(columns=['specialTeamsResult'], axis=1), df_upsampled['specialTeamsResult'], test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:33.753483Z","iopub.execute_input":"2023-01-28T05:36:33.756772Z","iopub.status.idle":"2023-01-28T05:36:33.766418Z","shell.execute_reply.started":"2023-01-28T05:36:33.756711Z","shell.execute_reply":"2023-01-28T05:36:33.765627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_classifier_models(classifiers, X_scaled_train, y_scaled_train, X_scaled_test, y_scaled_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-28T05:36:33.768891Z","iopub.execute_input":"2023-01-28T05:36:33.769708Z","iopub.status.idle":"2023-01-28T05:36:34.654351Z","shell.execute_reply.started":"2023-01-28T05:36:33.769679Z","shell.execute_reply":"2023-01-28T05:36:34.653665Z"},"trusted":true},"execution_count":null,"outputs":[]}]}