{"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":"**Getting rid of warnings**","metadata":{"id":"fPU631nOAlm8"}},{"cell_type":"code","source":"!pip install shutup","metadata":{"id":"K8wkEEoYAXY5","outputId":"64d3715c-8fe3-439a-e6dc-6721f14d3420","execution":{"iopub.status.busy":"2022-09-20T07:09:21.640283Z","iopub.execute_input":"2022-09-20T07:09:21.640666Z","iopub.status.idle":"2022-09-20T07:09:34.260224Z","shell.execute_reply.started":"2022-09-20T07:09:21.640586Z","shell.execute_reply":"2022-09-20T07:09:34.258995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutup; shutup.please()","metadata":{"id":"je2vctd9AqOx","execution":{"iopub.status.busy":"2022-09-20T07:09:34.263199Z","iopub.execute_input":"2022-09-20T07:09:34.264814Z","iopub.status.idle":"2022-09-20T07:09:34.274361Z","shell.execute_reply.started":"2022-09-20T07:09:34.264771Z","shell.execute_reply":"2022-09-20T07:09:34.273477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Kaggle API token for Google Colab**\n\n**The following steps are only for running notebook in Google Colab Environment**","metadata":{"id":"Sk5d8SfnAs34"}},{"cell_type":"code","source":"#!pip install kaggle","metadata":{"id":"fs9cxFsEAwmG","outputId":"753732ab-ca7c-4fba-d08b-d341d1ebeb0e","execution":{"iopub.status.busy":"2022-09-20T07:09:34.276123Z","iopub.execute_input":"2022-09-20T07:09:34.276501Z","iopub.status.idle":"2022-09-20T07:09:34.285482Z","shell.execute_reply.started":"2022-09-20T07:09:34.276467Z","shell.execute_reply":"2022-09-20T07:09:34.284563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!mkdir ~/.kaggle\n#!cp /content/kaggle.json ~/.kaggle/kaggle.json\n#!chmod 600 /root/.kaggle/kaggle.json","metadata":{"id":"3dHbe7JxAzG4","outputId":"f91c324f-f590-43e4-bb65-48fe3a561da3","execution":{"iopub.status.busy":"2022-09-20T07:09:34.290019Z","iopub.execute_input":"2022-09-20T07:09:34.290310Z","iopub.status.idle":"2022-09-20T07:09:34.295999Z","shell.execute_reply.started":"2022-09-20T07:09:34.290287Z","shell.execute_reply":"2022-09-20T07:09:34.295129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!kaggle datasets download raddar/amex-data-integer-dtypes-parquet-format","metadata":{"id":"xO-BQaUeA15x","outputId":"73de90a8-acbb-4540-f238-ab6c8adc17f8","execution":{"iopub.status.busy":"2022-09-20T07:09:34.297747Z","iopub.execute_input":"2022-09-20T07:09:34.298162Z","iopub.status.idle":"2022-09-20T07:09:34.306032Z","shell.execute_reply.started":"2022-09-20T07:09:34.298126Z","shell.execute_reply":"2022-09-20T07:09:34.304983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#! kaggle competitions download amex-default-prediction -f train_labels.csv","metadata":{"id":"P1gH98zcNER_","outputId":"37a374b2-1aff-4475-ccc5-dfa1424e8f41","execution":{"iopub.status.busy":"2022-09-20T07:09:34.307456Z","iopub.execute_input":"2022-09-20T07:09:34.308599Z","iopub.status.idle":"2022-09-20T07:09:34.314748Z","shell.execute_reply.started":"2022-09-20T07:09:34.308563Z","shell.execute_reply":"2022-09-20T07:09:34.313846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!unzip amex-data-integer-dtypes-parquet-format.zip","metadata":{"id":"bU0vd9-pA4WT","outputId":"98e662ce-f439-4730-c82d-a676f1b55883","execution":{"iopub.status.busy":"2022-09-20T07:09:34.316438Z","iopub.execute_input":"2022-09-20T07:09:34.317233Z","iopub.status.idle":"2022-09-20T07:09:34.324169Z","shell.execute_reply.started":"2022-09-20T07:09:34.317197Z","shell.execute_reply":"2022-09-20T07:09:34.323130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!unzip train_labels.csv.zip","metadata":{"id":"UH2VzYEbOVyM","outputId":"91293f38-2fa2-4172-fb1e-8d0684000b25","execution":{"iopub.status.busy":"2022-09-20T07:09:34.325561Z","iopub.execute_input":"2022-09-20T07:09:34.326537Z","iopub.status.idle":"2022-09-20T07:09:34.332502Z","shell.execute_reply.started":"2022-09-20T07:09:34.326446Z","shell.execute_reply":"2022-09-20T07:09:34.331675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing all Required Libraries**","metadata":{"id":"M-nqY3ErA6FX"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport gc\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style(\"darkgrid\", {\"grid.color\": \".6\", \"grid.linestyle\": \":\"})\nimport os\n\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom keras.callbacks import EarlyStopping","metadata":{"id":"Uo9iOpnGA-v4","execution":{"iopub.status.busy":"2022-09-20T07:09:34.333833Z","iopub.execute_input":"2022-09-20T07:09:34.334698Z","iopub.status.idle":"2022-09-20T07:09:40.537301Z","shell.execute_reply.started":"2022-09-20T07:09:34.334664Z","shell.execute_reply":"2022-09-20T07:09:40.536310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the Dataset","metadata":{"id":"E34naMc4BPAx"}},{"cell_type":"markdown","source":"Loading the 2 most recent transactions of the customers.","metadata":{"id":"A8HkOx3uBgbK"}},{"cell_type":"code","source":"number_of_transactions = 2","metadata":{"id":"2_pmeqX_BULG","execution":{"iopub.status.busy":"2022-09-20T07:09:40.542238Z","iopub.execute_input":"2022-09-20T07:09:40.543466Z","iopub.status.idle":"2022-09-20T07:09:40.549496Z","shell.execute_reply.started":"2022-09-20T07:09:40.543425Z","shell.execute_reply":"2022-09-20T07:09:40.548527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_parquet('../input/amex-data-integer-dtypes-parquet-format/train.parquet').groupby('customer_ID').tail(number_of_transactions).set_index('customer_ID', drop=True).sort_index()\nlabels = pd.read_csv('../input/amex-default-prediction/train_labels.csv').set_index('customer_ID', drop=True).sort_index()","metadata":{"id":"odp_TJGaBisV","execution":{"iopub.status.busy":"2022-09-20T07:09:40.550777Z","iopub.execute_input":"2022-09-20T07:09:40.552151Z","iopub.status.idle":"2022-09-20T07:10:13.018897Z","shell.execute_reply.started":"2022-09-20T07:09:40.552076Z","shell.execute_reply":"2022-09-20T07:10:13.017850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Merging the Training data with Labels**","metadata":{"id":"t5hg1e_vBnn4"}},{"cell_type":"code","source":"train_df = pd.merge(data, labels, left_index=True, right_index=True)  ","metadata":{"id":"QYEMvCUsBn1b","execution":{"iopub.status.busy":"2022-09-20T07:10:13.020365Z","iopub.execute_input":"2022-09-20T07:10:13.021030Z","iopub.status.idle":"2022-09-20T07:10:33.829151Z","shell.execute_reply.started":"2022-09-20T07:10:13.020990Z","shell.execute_reply":"2022-09-20T07:10:33.828086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T07:10:33.830938Z","iopub.execute_input":"2022-09-20T07:10:33.831340Z","iopub.status.idle":"2022-09-20T07:10:33.875193Z","shell.execute_reply.started":"2022-09-20T07:10:33.831298Z","shell.execute_reply":"2022-09-20T07:10:33.874205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The number of observations in Training Dataset is :\",len(train_df))","metadata":{"id":"w5DTZ9UGEVhR","outputId":"72e5b210-631e-4e36-b849-065fc74db396","execution":{"iopub.status.busy":"2022-09-20T07:10:33.876928Z","iopub.execute_input":"2022-09-20T07:10:33.877364Z","iopub.status.idle":"2022-09-20T07:10:33.883500Z","shell.execute_reply.started":"2022-09-20T07:10:33.877326Z","shell.execute_reply":"2022-09-20T07:10:33.881911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"id":"pBGzATy0EZFC","outputId":"1ab18502-14cf-439a-e8cc-95302f8d7dd6","execution":{"iopub.status.busy":"2022-09-20T07:10:33.885410Z","iopub.execute_input":"2022-09-20T07:10:33.885859Z","iopub.status.idle":"2022-09-20T07:10:33.915036Z","shell.execute_reply.started":"2022-09-20T07:10:33.885825Z","shell.execute_reply":"2022-09-20T07:10:33.913880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Descriptive Statistics**","metadata":{"id":"nRGCF2bEE3JY"}},{"cell_type":"code","source":"train_df.describe().T","metadata":{"id":"2QWKLIPoE2bG","outputId":"12d71c26-78bb-49f1-c54b-270b0a376d87","execution":{"iopub.status.busy":"2022-09-20T07:10:33.916737Z","iopub.execute_input":"2022-09-20T07:10:33.917118Z","iopub.status.idle":"2022-09-20T07:10:38.319523Z","shell.execute_reply.started":"2022-09-20T07:10:33.917081Z","shell.execute_reply":"2022-09-20T07:10:38.318459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualisation**\n\nCount Plot of the Target Variable","metadata":{"id":"hxwLA4XdBu13"}},{"cell_type":"code","source":"sns.countplot(x = 'target',data = train_df)","metadata":{"id":"0rL_r7pUEhwB","outputId":"b921b346-7d0c-4a00-9a54-d250c00c029b","execution":{"iopub.status.busy":"2022-09-20T07:10:38.321159Z","iopub.execute_input":"2022-09-20T07:10:38.321838Z","iopub.status.idle":"2022-09-20T07:10:38.794482Z","shell.execute_reply.started":"2022-09-20T07:10:38.321780Z","shell.execute_reply":"2022-09-20T07:10:38.793482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-Processing","metadata":{"id":"JUU83Qu4wE12"}},{"cell_type":"markdown","source":"**Dropping the Transaction Dates**","metadata":{"id":"SRyHBP2RBwez"}},{"cell_type":"code","source":"drop_cols = ['S_2'] \ntrain_df.drop(drop_cols, inplace=True, axis=1)","metadata":{"id":"klVfB1i1BxCg","execution":{"iopub.status.busy":"2022-09-20T07:10:38.796123Z","iopub.execute_input":"2022-09-20T07:10:38.796547Z","iopub.status.idle":"2022-09-20T07:10:38.977981Z","shell.execute_reply.started":"2022-09-20T07:10:38.796491Z","shell.execute_reply":"2022-09-20T07:10:38.976990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating Training Labels and Data**","metadata":{"id":"3OsTsjEjBy9X"}},{"cell_type":"code","source":"y = train_df['target']\nX = train_df.drop('target', axis=1)","metadata":{"id":"Zf1DTdlGB1ax","execution":{"iopub.status.busy":"2022-09-20T07:10:38.980343Z","iopub.execute_input":"2022-09-20T07:10:38.981396Z","iopub.status.idle":"2022-09-20T07:10:39.154181Z","shell.execute_reply.started":"2022-09-20T07:10:38.981357Z","shell.execute_reply":"2022-09-20T07:10:39.153081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Handling missing values**\n\nMissing values are imputed with the respective column mean.","metadata":{"id":"LHrq7Zb9B23r"}},{"cell_type":"code","source":"col_names = X.columns\nimputer = SimpleImputer()\nX = pd.DataFrame(imputer.fit_transform(X))  \nX.columns = col_names","metadata":{"id":"8daiUaCVB60d","execution":{"iopub.status.busy":"2022-09-20T07:10:39.158182Z","iopub.execute_input":"2022-09-20T07:10:39.158496Z","iopub.status.idle":"2022-09-20T07:10:42.164966Z","shell.execute_reply.started":"2022-09-20T07:10:39.158457Z","shell.execute_reply":"2022-09-20T07:10:42.163914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Standardization**\n\nTo ascertain features are standardized by subtracting mean and scaling to unit variance.","metadata":{"id":"qh6JrJl_B8yx"}},{"cell_type":"code","source":"scaler = StandardScaler()\nX = pd.DataFrame(scaler.fit_transform(X), index=X.index, columns=X.columns) ","metadata":{"id":"-3PRnyWtCAn0","execution":{"iopub.status.busy":"2022-09-20T07:10:42.166565Z","iopub.execute_input":"2022-09-20T07:10:42.167230Z","iopub.status.idle":"2022-09-20T07:10:44.549247Z","shell.execute_reply.started":"2022-09-20T07:10:42.167188Z","shell.execute_reply":"2022-09-20T07:10:44.548029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Splitting into training and validation sets**\n\nThe stratify method ensures that target variable does not get improperly distrubted leading to class imbalance.","metadata":{"id":"mZtA4IiPCBxH"}},{"cell_type":"code","source":"train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 1, shuffle=True, stratify=y, test_size=0.2)","metadata":{"id":"7u5aT94eCA9m","execution":{"iopub.status.busy":"2022-09-20T07:10:44.550767Z","iopub.execute_input":"2022-09-20T07:10:44.551267Z","iopub.status.idle":"2022-09-20T07:10:45.852508Z","shell.execute_reply.started":"2022-09-20T07:10:44.551216Z","shell.execute_reply":"2022-09-20T07:10:45.851563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Memory cleanup to prevent out of memory error.","metadata":{"id":"yoq_QnFpCH15"}},{"cell_type":"code","source":"del data, labels, X, y\ngc.collect()","metadata":{"id":"k4nD19XqCJIq","outputId":"0c1ef5bf-ddf3-43c9-de2c-a5748ae92d2d","execution":{"iopub.status.busy":"2022-09-20T07:10:45.854055Z","iopub.execute_input":"2022-09-20T07:10:45.854699Z","iopub.status.idle":"2022-09-20T07:10:46.048617Z","shell.execute_reply.started":"2022-09-20T07:10:45.854661Z","shell.execute_reply":"2022-09-20T07:10:46.047545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{"id":"U6Nv22blCKx7"}},{"cell_type":"markdown","source":"Models have been made in order of increasing complexity to check if a simpler model gives a better accuracy than a complex one then we select that model.","metadata":{"id":"iK2bxDp-vnK2"}},{"cell_type":"code","source":"from sklearn.metrics import classification_report,accuracy_score\nfrom sklearn import metrics","metadata":{"id":"ELQKKq3uFbPT","execution":{"iopub.status.busy":"2022-09-20T07:10:46.050207Z","iopub.execute_input":"2022-09-20T07:10:46.050632Z","iopub.status.idle":"2022-09-20T07:10:46.055613Z","shell.execute_reply.started":"2022-09-20T07:10:46.050594Z","shell.execute_reply":"2022-09-20T07:10:46.054451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Logistic Regression**","metadata":{"id":"JZjVnacECOCR"}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"id":"Byu6NMmRCKXR","execution":{"iopub.status.busy":"2022-09-20T07:10:46.056963Z","iopub.execute_input":"2022-09-20T07:10:46.057662Z","iopub.status.idle":"2022-09-20T07:10:46.066314Z","shell.execute_reply.started":"2022-09-20T07:10:46.057628Z","shell.execute_reply":"2022-09-20T07:10:46.065315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_lr = LogisticRegression(n_jobs=1, C=1e5)\nclf_lr.fit(train_X, train_y)","metadata":{"id":"B_9n2xQ3FmpM","outputId":"b0aaa516-66ef-4018-e745-31ee7972dbe9","execution":{"iopub.status.busy":"2022-09-20T07:10:46.067793Z","iopub.execute_input":"2022-09-20T07:10:46.068129Z","iopub.status.idle":"2022-09-20T07:11:16.495281Z","shell.execute_reply.started":"2022-09-20T07:10:46.068096Z","shell.execute_reply":"2022-09-20T07:11:16.493648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred_val_lr = clf_lr.predict(val_X)\nprint('Accuracy on Validation set :',accuracy_score(val_y, y_pred_val_lr))\nprint(\"\\n\")\nprint(classification_report(val_y, y_pred_val_lr))","metadata":{"id":"4Xmda0i5FvLf","outputId":"00ed1922-0b76-4a4c-d1d7-cbcc90d0cd1e","execution":{"iopub.status.busy":"2022-09-20T07:11:16.497125Z","iopub.execute_input":"2022-09-20T07:11:16.497491Z","iopub.status.idle":"2022-09-20T07:11:17.163835Z","shell.execute_reply.started":"2022-09-20T07:11:16.497456Z","shell.execute_reply":"2022-09-20T07:11:17.162594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Confusion Matrix : \\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_lr))","metadata":{"id":"lEb0wN6BFxfH","outputId":"0fa40ce3-af5b-49f9-bb80-a6aeaca1444d","execution":{"iopub.status.busy":"2022-09-20T07:11:17.170874Z","iopub.execute_input":"2022-09-20T07:11:17.171171Z","iopub.status.idle":"2022-09-20T07:11:17.473772Z","shell.execute_reply.started":"2022-09-20T07:11:17.171142Z","shell.execute_reply":"2022-09-20T07:11:17.472688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalized Confusion Matrix :\\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_lr,normalize='all'))","metadata":{"id":"gxkQ45mdFy4i","outputId":"6c1da0e6-961d-4117-b97b-9ac139e8750a","execution":{"iopub.status.busy":"2022-09-20T07:11:17.475488Z","iopub.execute_input":"2022-09-20T07:11:17.476083Z","iopub.status.idle":"2022-09-20T07:11:17.770607Z","shell.execute_reply.started":"2022-09-20T07:11:17.476035Z","shell.execute_reply":"2022-09-20T07:11:17.769103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Support Vector Classifier**","metadata":{"id":"H-phn6QDC-e0"}},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier","metadata":{"id":"A8TcKulKC-vj","execution":{"iopub.status.busy":"2022-09-20T07:11:17.772264Z","iopub.execute_input":"2022-09-20T07:11:17.772713Z","iopub.status.idle":"2022-09-20T07:11:17.777693Z","shell.execute_reply.started":"2022-09-20T07:11:17.772675Z","shell.execute_reply":"2022-09-20T07:11:17.776551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, random_state=42, max_iter=5, tol=None)\nsgd.fit(train_X, train_y)","metadata":{"id":"GaeEa0IOF1CF","outputId":"f03048f1-1b25-48c6-f4a1-a7980c0f3957","execution":{"iopub.status.busy":"2022-09-20T07:11:17.779487Z","iopub.execute_input":"2022-09-20T07:11:17.779898Z","iopub.status.idle":"2022-09-20T07:11:21.197146Z","shell.execute_reply.started":"2022-09-20T07:11:17.779863Z","shell.execute_reply":"2022-09-20T07:11:21.196058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred_val_sgd = sgd.predict(val_X)\nprint('Accuracy on Validation set :',accuracy_score(val_y, y_pred_val_sgd))\nprint(\"\\n\")\nprint(classification_report(val_y, y_pred_val_sgd))","metadata":{"id":"Ni1xIl5DF7iu","outputId":"e23a9d4a-fb3d-4499-b4ae-0dddf6887ba7","execution":{"iopub.status.busy":"2022-09-20T07:11:21.198694Z","iopub.execute_input":"2022-09-20T07:11:21.199301Z","iopub.status.idle":"2022-09-20T07:11:21.718508Z","shell.execute_reply.started":"2022-09-20T07:11:21.199262Z","shell.execute_reply":"2022-09-20T07:11:21.717345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Confusion Matrix : \\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_sgd))","metadata":{"id":"7leH0GlIF87y","outputId":"9e45335c-5927-40eb-ebb0-dff7948d2e37","execution":{"iopub.status.busy":"2022-09-20T07:11:21.720233Z","iopub.execute_input":"2022-09-20T07:11:21.720949Z","iopub.status.idle":"2022-09-20T07:11:22.002334Z","shell.execute_reply.started":"2022-09-20T07:11:21.720906Z","shell.execute_reply":"2022-09-20T07:11:22.001450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalized Confusion Matrix :\\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_sgd,normalize='all'))","metadata":{"id":"3ZlyZUSVF93r","outputId":"163fdb38-7407-49a9-f47d-8f33d8af064a","execution":{"iopub.status.busy":"2022-09-20T07:11:22.003831Z","iopub.execute_input":"2022-09-20T07:11:22.004421Z","iopub.status.idle":"2022-09-20T07:11:22.288041Z","shell.execute_reply.started":"2022-09-20T07:11:22.004381Z","shell.execute_reply":"2022-09-20T07:11:22.287118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Decision Tree Classifier**","metadata":{"id":"qk1H7JDKDDeO"}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier","metadata":{"id":"BH_8TJw5DHLi","execution":{"iopub.status.busy":"2022-09-20T07:11:22.289311Z","iopub.execute_input":"2022-09-20T07:11:22.289768Z","iopub.status.idle":"2022-09-20T07:11:22.318610Z","shell.execute_reply.started":"2022-09-20T07:11:22.289730Z","shell.execute_reply":"2022-09-20T07:11:22.317765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dc = DecisionTreeClassifier(random_state=0)\ndc.fit(train_X, train_y)","metadata":{"id":"Zt6oA1xFF_oA","outputId":"50a46811-d876-4b2a-8db6-2ac716d188b7","execution":{"iopub.status.busy":"2022-09-20T07:11:22.320137Z","iopub.execute_input":"2022-09-20T07:11:22.320514Z","iopub.status.idle":"2022-09-20T07:17:01.052009Z","shell.execute_reply.started":"2022-09-20T07:11:22.320478Z","shell.execute_reply":"2022-09-20T07:17:01.050900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred_val_dc = dc.predict(val_X)\nprint('Accuracy on Validation set :',accuracy_score(val_y, y_pred_val_dc))\nprint(\"\\n\")\nprint(classification_report(val_y, y_pred_val_dc))","metadata":{"id":"Xuq8ysSrGEJu","outputId":"7f5423ab-8f45-4798-e3de-ece1c07a2468","execution":{"iopub.status.busy":"2022-09-20T07:17:01.053401Z","iopub.execute_input":"2022-09-20T07:17:01.053872Z","iopub.status.idle":"2022-09-20T07:17:01.433625Z","shell.execute_reply.started":"2022-09-20T07:17:01.053828Z","shell.execute_reply":"2022-09-20T07:17:01.432530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Confusion Matrix : \\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_dc))","metadata":{"id":"7EQRL22nGF-r","outputId":"515c737d-92c1-47e0-b936-ad60c41f621c","execution":{"iopub.status.busy":"2022-09-20T07:17:01.435245Z","iopub.execute_input":"2022-09-20T07:17:01.435661Z","iopub.status.idle":"2022-09-20T07:17:01.719342Z","shell.execute_reply.started":"2022-09-20T07:17:01.435622Z","shell.execute_reply":"2022-09-20T07:17:01.718445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalized Confusion Matrix :\\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_dc,normalize='all'))","metadata":{"id":"EJKzRsJWGGeQ","outputId":"13f3aced-7466-4797-c265-9bf81f19ad1f","execution":{"iopub.status.busy":"2022-09-20T07:17:01.720806Z","iopub.execute_input":"2022-09-20T07:17:01.721163Z","iopub.status.idle":"2022-09-20T07:17:01.999298Z","shell.execute_reply.started":"2022-09-20T07:17:01.721129Z","shell.execute_reply":"2022-09-20T07:17:01.998396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Light GBM Classifier**","metadata":{"id":"yuevDbYDDKL2"}},{"cell_type":"code","source":"from lightgbm import LGBMClassifier","metadata":{"id":"pNm7Khj8DNlu","execution":{"iopub.status.busy":"2022-09-20T07:17:02.000611Z","iopub.execute_input":"2022-09-20T07:17:02.001223Z","iopub.status.idle":"2022-09-20T07:17:03.566912Z","shell.execute_reply.started":"2022-09-20T07:17:02.001185Z","shell.execute_reply":"2022-09-20T07:17:03.565949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm = LGBMClassifier()\nlgbm.fit(train_X, train_y)","metadata":{"id":"UkmMNOetGvjo","outputId":"573453cc-9f95-4f7f-ac87-045ac50dec0a","execution":{"iopub.status.busy":"2022-09-20T07:17:03.568481Z","iopub.execute_input":"2022-09-20T07:17:03.568895Z","iopub.status.idle":"2022-09-20T07:18:04.544921Z","shell.execute_reply.started":"2022-09-20T07:17:03.568848Z","shell.execute_reply":"2022-09-20T07:18:04.544192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred_val_lgbm = lgbm.predict(val_X)\nprint('Accuracy on Validation set :',accuracy_score(val_y, y_pred_val_lgbm))\nprint(\"\\n\")\nprint(classification_report(val_y, y_pred_val_lgbm))","metadata":{"id":"ur2JUHdHGzGy","outputId":"89758e96-dfaf-4ab4-933b-c8587c664018","execution":{"iopub.status.busy":"2022-09-20T07:18:04.548231Z","iopub.execute_input":"2022-09-20T07:18:04.550245Z","iopub.status.idle":"2022-09-20T07:18:05.823474Z","shell.execute_reply.started":"2022-09-20T07:18:04.550214Z","shell.execute_reply":"2022-09-20T07:18:05.822197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Confusion Matrix : \\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_lgbm))","metadata":{"id":"7NjRqDq-G1YX","outputId":"177601a7-872e-45b9-fb17-21e5ef055378","execution":{"iopub.status.busy":"2022-09-20T07:18:05.825063Z","iopub.execute_input":"2022-09-20T07:18:05.825821Z","iopub.status.idle":"2022-09-20T07:18:06.110318Z","shell.execute_reply.started":"2022-09-20T07:18:05.825776Z","shell.execute_reply":"2022-09-20T07:18:06.109400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalized Confusion Matrix :\\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_lgbm,normalize='all'))","metadata":{"id":"BrCuydEKG2nR","outputId":"a90bde9e-cb4b-4ca0-9a3b-a0611ebbbacd","execution":{"iopub.status.busy":"2022-09-20T07:18:06.111652Z","iopub.execute_input":"2022-09-20T07:18:06.112253Z","iopub.status.idle":"2022-09-20T07:18:06.388965Z","shell.execute_reply.started":"2022-09-20T07:18:06.112215Z","shell.execute_reply":"2022-09-20T07:18:06.388024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MLP Classifier**","metadata":{"id":"lE7xQwBZDchN"}},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier","metadata":{"id":"-gNnPw3gDdCW","execution":{"iopub.status.busy":"2022-09-20T07:18:06.390497Z","iopub.execute_input":"2022-09-20T07:18:06.391172Z","iopub.status.idle":"2022-09-20T07:18:06.401970Z","shell.execute_reply.started":"2022-09-20T07:18:06.391132Z","shell.execute_reply":"2022-09-20T07:18:06.400967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mlp = MLPClassifier(random_state=1, max_iter=300)\nmlp.fit(train_X, train_y)","metadata":{"id":"eTtqeCcTGcUT","outputId":"e0ce14b7-f1c5-425b-932e-1b307d57ade9","execution":{"iopub.status.busy":"2022-09-20T07:18:06.403563Z","iopub.execute_input":"2022-09-20T07:18:06.403999Z","iopub.status.idle":"2022-09-20T07:33:09.599135Z","shell.execute_reply.started":"2022-09-20T07:18:06.403964Z","shell.execute_reply":"2022-09-20T07:33:09.598161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred_val_mlp = mlp.predict(val_X)\nprint('Accuracy on Validation set :',accuracy_score(val_y, y_pred_val_mlp))\nprint(\"\\n\")\nprint(classification_report(val_y, y_pred_val_mlp))","metadata":{"id":"QBFNk4JSGcIo","outputId":"03066b5a-ea09-4e9f-ac7e-af5375fcc5cc","execution":{"iopub.status.busy":"2022-09-20T07:33:09.600671Z","iopub.execute_input":"2022-09-20T07:33:09.601703Z","iopub.status.idle":"2022-09-20T07:33:10.218538Z","shell.execute_reply.started":"2022-09-20T07:33:09.601667Z","shell.execute_reply":"2022-09-20T07:33:10.217310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Confusion Matrix : \\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_mlp))","metadata":{"id":"GDmtloBgGgY4","outputId":"0fc8bfd0-e8b4-4dc2-8cbf-3d4bcd763311","execution":{"iopub.status.busy":"2022-09-20T07:33:10.220052Z","iopub.execute_input":"2022-09-20T07:33:10.221025Z","iopub.status.idle":"2022-09-20T07:33:10.604677Z","shell.execute_reply.started":"2022-09-20T07:33:10.220985Z","shell.execute_reply":"2022-09-20T07:33:10.603744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Normalized Confusion Matrix :\\n',metrics.ConfusionMatrixDisplay.from_predictions(val_y, y_pred_val_mlp,normalize='all'))","metadata":{"id":"ua9jxlyLGidI","outputId":"6e945bd2-8e57-4046-8310-a84efaf834b8","execution":{"iopub.status.busy":"2022-09-20T07:33:10.606007Z","iopub.execute_input":"2022-09-20T07:33:10.608405Z","iopub.status.idle":"2022-09-20T07:33:10.979806Z","shell.execute_reply.started":"2022-09-20T07:33:10.608369Z","shell.execute_reply":"2022-09-20T07:33:10.978884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Listing Accuracies on Validation Data by Models so far**","metadata":{"id":"sIyn5JyGDWRY"}},{"cell_type":"code","source":"print('\\nAccuracy of Logistic Regression :',accuracy_score(val_y, y_pred_val_lr))\nprint('\\nAccuracy of Support Vector :',accuracy_score(val_y, y_pred_val_sgd))\nprint('\\nAccuracy of Decision Tree :',accuracy_score(val_y, y_pred_val_dc))\nprint('\\nAccuracy of MLP Classifier :',accuracy_score(val_y, y_pred_val_mlp))\nprint('\\nAccuracy Light GBM Classifier :',accuracy_score(val_y, y_pred_val_lgbm))","metadata":{"id":"FKta4yAFDYKV","outputId":"769e151f-da91-47fd-99cc-61755a101555","execution":{"iopub.status.busy":"2022-09-20T07:33:10.983774Z","iopub.execute_input":"2022-09-20T07:33:10.986231Z","iopub.status.idle":"2022-09-20T07:33:11.094429Z","shell.execute_reply.started":"2022-09-20T07:33:10.986193Z","shell.execute_reply":"2022-09-20T07:33:11.093569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inferences\n\nThe best accuracy is obtained using Light GBM Classifier.\n\n# Points for Future Scope\n\n\n*   Using PCA during Pre-processing to select the important contributing factors\n*   Using a Custom Neural Network\n","metadata":{"id":"DnCmxTH1v1fY"}}]}