{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-31T11:30:54.857770Z","iopub.execute_input":"2022-12-31T11:30:54.858775Z","iopub.status.idle":"2022-12-31T11:30:54.889094Z","shell.execute_reply.started":"2022-12-31T11:30:54.858672Z","shell.execute_reply":"2022-12-31T11:30:54.887769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:31:38.920413Z","iopub.execute_input":"2022-12-31T11:31:38.920857Z","iopub.status.idle":"2022-12-31T11:31:58.535650Z","shell.execute_reply.started":"2022-12-31T11:31:38.920823Z","shell.execute_reply":"2022-12-31T11:31:58.534628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train shape: \", train.shape, \", and Test shape:\", test.shape, \".\")","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:19.040744Z","iopub.execute_input":"2022-12-31T11:33:19.041172Z","iopub.status.idle":"2022-12-31T11:33:19.048779Z","shell.execute_reply.started":"2022-12-31T11:33:19.041130Z","shell.execute_reply":"2022-12-31T11:33:19.047256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:21.215164Z","iopub.execute_input":"2022-12-31T11:33:21.216031Z","iopub.status.idle":"2022-12-31T11:33:22.630751Z","shell.execute_reply.started":"2022-12-31T11:33:21.215973Z","shell.execute_reply":"2022-12-31T11:33:22.629548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:26.122887Z","iopub.execute_input":"2022-12-31T11:33:26.123315Z","iopub.status.idle":"2022-12-31T11:33:27.191309Z","shell.execute_reply.started":"2022-12-31T11:33:26.123277Z","shell.execute_reply":"2022-12-31T11:33:27.190182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:30.798105Z","iopub.execute_input":"2022-12-31T11:33:30.798524Z","iopub.status.idle":"2022-12-31T11:33:30.918103Z","shell.execute_reply.started":"2022-12-31T11:33:30.798474Z","shell.execute_reply":"2022-12-31T11:33:30.916813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:34.843185Z","iopub.execute_input":"2022-12-31T11:33:34.843606Z","iopub.status.idle":"2022-12-31T11:33:34.946780Z","shell.execute_reply.started":"2022-12-31T11:33:34.843570Z","shell.execute_reply":"2022-12-31T11:33:34.945530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:38.447451Z","iopub.execute_input":"2022-12-31T11:33:38.448299Z","iopub.status.idle":"2022-12-31T11:33:38.549879Z","shell.execute_reply.started":"2022-12-31T11:33:38.448252Z","shell.execute_reply":"2022-12-31T11:33:38.548782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:41.640931Z","iopub.execute_input":"2022-12-31T11:33:41.641322Z","iopub.status.idle":"2022-12-31T11:33:41.721829Z","shell.execute_reply.started":"2022-12-31T11:33:41.641290Z","shell.execute_reply":"2022-12-31T11:33:41.720814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, accuracy_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import train_test_split\n\ny = train[\"target\"]\nX = train.drop(columns=[\"id\",\"f_27\", \"target\"])\n\ntest = test.drop(columns=[\"id\",\"f_27\"])\n\nX.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:44.359255Z","iopub.execute_input":"2022-12-31T11:33:44.359681Z","iopub.status.idle":"2022-12-31T11:33:45.105234Z","shell.execute_reply.started":"2022-12-31T11:33:44.359645Z","shell.execute_reply":"2022-12-31T11:33:45.103984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:48.671321Z","iopub.execute_input":"2022-12-31T11:33:48.671722Z","iopub.status.idle":"2022-12-31T11:33:49.294678Z","shell.execute_reply.started":"2022-12-31T11:33:48.671691Z","shell.execute_reply":"2022-12-31T11:33:49.293797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\n\nimport time\n\nmodel = LGBMClassifier()\nmodel.fit(X, y)\n\nprint(\"Training score :\", model.score(X, y))\n\npred_y_test = model.predict(X_test)\nprint(\"Roc auc score  :\", roc_auc_score(y_test, pred_y_test))","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:33:54.576673Z","iopub.execute_input":"2022-12-31T11:33:54.577059Z","iopub.status.idle":"2022-12-31T11:34:16.216394Z","shell.execute_reply.started":"2022-12-31T11:33:54.577029Z","shell.execute_reply":"2022-12-31T11:34:16.213251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\n\n# sorted(zip(clf.feature_importances_, X.columns), reverse=True)\nfeature_imp = pd.DataFrame(sorted(zip(model.feature_importances_,X.columns)), columns=['Value','Feature'])\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=feature_imp.sort_values(by=\"Value\", ascending=False))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()\nplt.show()\nplt.savefig('lgbm_importances-01.png')","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:34:25.598467Z","iopub.execute_input":"2022-12-31T11:34:25.598920Z","iopub.status.idle":"2022-12-31T11:34:26.380267Z","shell.execute_reply.started":"2022-12-31T11:34:25.598883Z","shell.execute_reply":"2022-12-31T11:34:26.379039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_imp = feature_imp.sort_values(by = \"Value\", ascending=False)\nfeature_imp","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:35:03.036989Z","iopub.execute_input":"2022-12-31T11:35:03.037458Z","iopub.status.idle":"2022-12-31T11:35:03.051319Z","shell.execute_reply.started":"2022-12-31T11:35:03.037421Z","shell.execute_reply":"2022-12-31T11:35:03.050103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_features = feature_imp[:6]['Feature'].tolist()\nselected_features","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:35:06.575970Z","iopub.execute_input":"2022-12-31T11:35:06.576411Z","iopub.status.idle":"2022-12-31T11:35:06.585368Z","shell.execute_reply.started":"2022-12-31T11:35:06.576374Z","shell.execute_reply":"2022-12-31T11:35:06.584107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:35:08.904261Z","iopub.execute_input":"2022-12-31T11:35:08.904683Z","iopub.status.idle":"2022-12-31T11:35:08.911792Z","shell.execute_reply.started":"2022-12-31T11:35:08.904637Z","shell.execute_reply":"2022-12-31T11:35:08.910676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kf = StratifiedKFold(n_splits=14, shuffle=True, random_state = 0)\ny_predict = []\n\nfor fold, (train_index, val_index) in enumerate(kf.split(X, y)):\n        X_train = X.loc[train_index]\n        X_val = X.loc[val_index]\n\n        y_train = y.loc[train_index]\n        y_val = y.loc[val_index]\n        \n        model = LGBMClassifier()\n        \n        model.fit(X_train,y_train)\n\n        y_pred = model.predict_proba(X_val)[:,1]\n        \n        y_predict.append(model.predict_proba(test)[:,1])\n\n        print(\"Training score :\", model.score(X_train, y_train))\n        print(\"Roc auc score  :\", roc_auc_score(y_val, y_pred), '\\n')","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:38:01.043157Z","iopub.execute_input":"2022-12-31T11:38:01.043707Z","iopub.status.idle":"2022-12-31T11:42:33.569117Z","shell.execute_reply.started":"2022-12-31T11:38:01.043667Z","shell.execute_reply":"2022-12-31T11:42:33.567809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LGBMClassifier()\n\nmodel.fit(X, y)\n\ny_predict.append(model.predict_proba(test)[:,1])","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:45:03.108345Z","iopub.execute_input":"2022-12-31T11:45:03.108784Z","iopub.status.idle":"2022-12-31T11:45:19.137937Z","shell.execute_reply.started":"2022-12-31T11:45:03.108751Z","shell.execute_reply":"2022-12-31T11:45:19.136649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(y_predict).mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:46:37.076357Z","iopub.execute_input":"2022-12-31T11:46:37.077425Z","iopub.status.idle":"2022-12-31T11:46:37.132509Z","shell.execute_reply.started":"2022-12-31T11:46:37.077380Z","shell.execute_reply":"2022-12-31T11:46:37.131075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:48:47.072191Z","iopub.execute_input":"2022-12-31T11:48:47.072609Z","iopub.status.idle":"2022-12-31T11:48:47.293011Z","shell.execute_reply.started":"2022-12-31T11:48:47.072576Z","shell.execute_reply":"2022-12-31T11:48:47.292000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target'] = np.array(y_predict).mean(axis=0)\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-12-31T11:49:02.233693Z","iopub.execute_input":"2022-12-31T11:49:02.234132Z","iopub.status.idle":"2022-12-31T11:49:04.009079Z","shell.execute_reply.started":"2022-12-31T11:49:02.234098Z","shell.execute_reply":"2022-12-31T11:49:04.007719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}