{"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":"2023-01-08T18:42:21.247645Z","iopub.execute_input":"2023-01-08T18:42:21.248132Z","iopub.status.idle":"2023-01-08T18:42:21.257134Z","shell.execute_reply.started":"2023-01-08T18:42:21.248098Z","shell.execute_reply":"2023-01-08T18:42:21.255918Z"},"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":"2023-01-08T18:42:21.259634Z","iopub.execute_input":"2023-01-08T18:42:21.259975Z","iopub.status.idle":"2023-01-08T18:42:29.311578Z","shell.execute_reply.started":"2023-01-08T18:42:21.259933Z","shell.execute_reply":"2023-01-08T18:42:29.310167Z"},"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":"2023-01-08T18:42:29.313266Z","iopub.execute_input":"2023-01-08T18:42:29.313851Z","iopub.status.idle":"2023-01-08T18:42:29.319852Z","shell.execute_reply.started":"2023-01-08T18:42:29.313818Z","shell.execute_reply":"2023-01-08T18:42:29.31799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:29.322005Z","iopub.execute_input":"2023-01-08T18:42:29.322948Z","iopub.status.idle":"2023-01-08T18:42:30.189079Z","shell.execute_reply.started":"2023-01-08T18:42:29.322914Z","shell.execute_reply":"2023-01-08T18:42:30.18794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:30.190274Z","iopub.execute_input":"2023-01-08T18:42:30.190589Z","iopub.status.idle":"2023-01-08T18:42:31.348778Z","shell.execute_reply.started":"2023-01-08T18:42:30.190562Z","shell.execute_reply":"2023-01-08T18:42:31.34767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:31.350601Z","iopub.execute_input":"2023-01-08T18:42:31.350939Z","iopub.status.idle":"2023-01-08T18:42:31.456303Z","shell.execute_reply.started":"2023-01-08T18:42:31.350909Z","shell.execute_reply":"2023-01-08T18:42:31.45494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:31.457845Z","iopub.execute_input":"2023-01-08T18:42:31.458223Z","iopub.status.idle":"2023-01-08T18:42:31.544725Z","shell.execute_reply.started":"2023-01-08T18:42:31.458191Z","shell.execute_reply":"2023-01-08T18:42:31.543209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:31.546529Z","iopub.execute_input":"2023-01-08T18:42:31.546945Z","iopub.status.idle":"2023-01-08T18:42:31.64377Z","shell.execute_reply.started":"2023-01-08T18:42:31.546893Z","shell.execute_reply":"2023-01-08T18:42:31.642519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:31.645335Z","iopub.execute_input":"2023-01-08T18:42:31.645793Z","iopub.status.idle":"2023-01-08T18:42:31.723938Z","shell.execute_reply.started":"2023-01-08T18:42:31.645753Z","shell.execute_reply":"2023-01-08T18:42:31.722944Z"},"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":"2023-01-08T18:42:31.727116Z","iopub.execute_input":"2023-01-08T18:42:31.727474Z","iopub.status.idle":"2023-01-08T18:42:31.827937Z","shell.execute_reply.started":"2023-01-08T18:42:31.727441Z","shell.execute_reply":"2023-01-08T18:42:31.826807Z"},"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":"2023-01-08T18:42:31.829153Z","iopub.execute_input":"2023-01-08T18:42:31.829514Z","iopub.status.idle":"2023-01-08T18:42:32.17231Z","shell.execute_reply.started":"2023-01-08T18:42:31.829482Z","shell.execute_reply":"2023-01-08T18:42:32.171114Z"},"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":"2023-01-08T18:42:32.173587Z","iopub.execute_input":"2023-01-08T18:42:32.173903Z","iopub.status.idle":"2023-01-08T18:42:50.011167Z","shell.execute_reply.started":"2023-01-08T18:42:32.173874Z","shell.execute_reply":"2023-01-08T18:42:50.010318Z"},"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.png')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:48:17.894667Z","iopub.execute_input":"2023-01-08T18:48:17.895083Z","iopub.status.idle":"2023-01-08T18:48:18.5722Z","shell.execute_reply.started":"2023-01-08T18:48:17.895051Z","shell.execute_reply":"2023-01-08T18:48:18.570925Z"},"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":"2023-01-08T18:42:50.760587Z","iopub.execute_input":"2023-01-08T18:42:50.761453Z","iopub.status.idle":"2023-01-08T18:42:50.776025Z","shell.execute_reply.started":"2023-01-08T18:42:50.761405Z","shell.execute_reply":"2023-01-08T18:42:50.775193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_features = feature_imp[:6]['Feature'].tolist()\nselected_features","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:50.777267Z","iopub.execute_input":"2023-01-08T18:42:50.778015Z","iopub.status.idle":"2023-01-08T18:42:50.790288Z","shell.execute_reply.started":"2023-01-08T18:42:50.77798Z","shell.execute_reply":"2023-01-08T18:42:50.7894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:42:50.791746Z","iopub.execute_input":"2023-01-08T18:42:50.7925Z","iopub.status.idle":"2023-01-08T18:42:50.80221Z","shell.execute_reply.started":"2023-01-08T18:42:50.792462Z","shell.execute_reply":"2023-01-08T18:42:50.801269Z"},"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":"2023-01-08T18:42:50.803552Z","iopub.execute_input":"2023-01-08T18:42:50.804088Z","iopub.status.idle":"2023-01-08T18:46:58.093475Z","shell.execute_reply.started":"2023-01-08T18:42:50.804058Z","shell.execute_reply":"2023-01-08T18:46:58.092328Z"},"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":"2023-01-08T18:46:58.095263Z","iopub.execute_input":"2023-01-08T18:46:58.095603Z","iopub.status.idle":"2023-01-08T18:47:12.874916Z","shell.execute_reply.started":"2023-01-08T18:46:58.095572Z","shell.execute_reply":"2023-01-08T18:47:12.873986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(y_predict).mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T18:47:12.876575Z","iopub.execute_input":"2023-01-08T18:47:12.877227Z","iopub.status.idle":"2023-01-08T18:47:12.924195Z","shell.execute_reply.started":"2023-01-08T18:47:12.877185Z","shell.execute_reply":"2023-01-08T18:47:12.923461Z"},"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":"2023-01-08T18:47:12.925322Z","iopub.execute_input":"2023-01-08T18:47:12.925774Z","iopub.status.idle":"2023-01-08T18:47:13.124839Z","shell.execute_reply.started":"2023-01-08T18:47:12.925745Z","shell.execute_reply":"2023-01-08T18:47:13.1238Z"},"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":"2023-01-08T18:47:13.126268Z","iopub.execute_input":"2023-01-08T18:47:13.12671Z","iopub.status.idle":"2023-01-08T18:47:14.738322Z","shell.execute_reply.started":"2023-01-08T18:47:13.12667Z","shell.execute_reply":"2023-01-08T18:47:14.737541Z"},"trusted":true},"execution_count":null,"outputs":[]}]}