{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nimport os\nimport optuna\nimport gc\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-27T14:50:53.451986Z","iopub.execute_input":"2022-12-27T14:50:53.452484Z","iopub.status.idle":"2022-12-27T14:50:53.754027Z","shell.execute_reply.started":"2022-12-27T14:50:53.452446Z","shell.execute_reply":"2022-12-27T14:50:53.752962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load Data**","metadata":{}},{"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-27T14:50:53.755821Z","iopub.execute_input":"2022-12-27T14:50:53.756248Z","iopub.status.idle":"2022-12-27T14:51:12.691972Z","shell.execute_reply.started":"2022-12-27T14:50:53.756199Z","shell.execute_reply":"2022-12-27T14:51:12.690733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**data shape**","metadata":{}},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:12.693340Z","iopub.execute_input":"2022-12-27T14:51:12.693719Z","iopub.status.idle":"2022-12-27T14:51:12.703830Z","shell.execute_reply.started":"2022-12-27T14:51:12.693687Z","shell.execute_reply":"2022-12-27T14:51:12.702539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualizing Data**","metadata":{}},{"cell_type":"markdown","source":"describe","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:12.707803Z","iopub.execute_input":"2022-12-27T14:51:12.708334Z","iopub.status.idle":"2022-12-27T14:51:13.986768Z","shell.execute_reply.started":"2022-12-27T14:51:12.708284Z","shell.execute_reply":"2022-12-27T14:51:13.985576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:13.988104Z","iopub.execute_input":"2022-12-27T14:51:13.988491Z","iopub.status.idle":"2022-12-27T14:51:14.960764Z","shell.execute_reply.started":"2022-12-27T14:51:13.988455Z","shell.execute_reply":"2022-12-27T14:51:14.959738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**info**","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:14.962219Z","iopub.execute_input":"2022-12-27T14:51:14.963397Z","iopub.status.idle":"2022-12-27T14:51:15.093312Z","shell.execute_reply.started":"2022-12-27T14:51:14.963352Z","shell.execute_reply":"2022-12-27T14:51:15.092096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train data have : \n*     16 x float64\n*     16 x int64\n*     1 object","metadata":{}},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:15.094889Z","iopub.execute_input":"2022-12-27T14:51:15.095382Z","iopub.status.idle":"2022-12-27T14:51:15.181822Z","shell.execute_reply.started":"2022-12-27T14:51:15.095336Z","shell.execute_reply":"2022-12-27T14:51:15.180497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train data have : \n*     16 x float64\n*     15 x int64\n*     1 object","metadata":{}},{"cell_type":"markdown","source":"**checking for missing values**","metadata":{}},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:15.183141Z","iopub.execute_input":"2022-12-27T14:51:15.183489Z","iopub.status.idle":"2022-12-27T14:51:15.278748Z","shell.execute_reply.started":"2022-12-27T14:51:15.183458Z","shell.execute_reply":"2022-12-27T14:51:15.277431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:15.281812Z","iopub.execute_input":"2022-12-27T14:51:15.282322Z","iopub.status.idle":"2022-12-27T14:51:15.357930Z","shell.execute_reply.started":"2022-12-27T14:51:15.282273Z","shell.execute_reply":"2022-12-27T14:51:15.356752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Feature Engineering**","metadata":{}},{"cell_type":"code","source":"for df in [train, test]:\n    for i in range(10):\n        df[f'ch{i}'] = df.f_27.str.get(i).apply(ord) - ord('A')\n    df[\"unique_characters\"] = df.f_27.apply(lambda s: len(set(s)))\nfeatures = [f for f in test.columns if f != 'id' and f != 'f_27']\ntest[features].head(2)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:15.361099Z","iopub.execute_input":"2022-12-27T14:51:15.361476Z","iopub.status.idle":"2022-12-27T14:51:30.520767Z","shell.execute_reply.started":"2022-12-27T14:51:15.361442Z","shell.execute_reply":"2022-12-27T14:51:30.519134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Modeling**","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:30.522443Z","iopub.execute_input":"2022-12-27T14:51:30.522877Z","iopub.status.idle":"2022-12-27T14:51:30.658258Z","shell.execute_reply.started":"2022-12-27T14:51:30.522839Z","shell.execute_reply":"2022-12-27T14:51:30.656882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Split Data**","metadata":{}},{"cell_type":"code","source":"y = 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-27T14:51:30.660041Z","iopub.execute_input":"2022-12-27T14:51:30.660415Z","iopub.status.idle":"2022-12-27T14:51:31.058005Z","shell.execute_reply.started":"2022-12-27T14:51:30.660382Z","shell.execute_reply":"2022-12-27T14:51:31.056695Z"},"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-27T14:51:31.059689Z","iopub.execute_input":"2022-12-27T14:51:31.060038Z","iopub.status.idle":"2022-12-27T14:51:31.711997Z","shell.execute_reply.started":"2022-12-27T14:51:31.060008Z","shell.execute_reply":"2022-12-27T14:51:31.710648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Search best params with optuna**","metadata":{}},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\n\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:31.713615Z","iopub.execute_input":"2022-12-27T14:51:31.714055Z","iopub.status.idle":"2022-12-27T14:51:32.763209Z","shell.execute_reply.started":"2022-12-27T14:51:31.714018Z","shell.execute_reply":"2022-12-27T14:51:32.762005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optuna.logging.set_verbosity(optuna.logging.WARNING)\n\ndef objectivesLGBM(trial):\n    model = LGBMClassifier()\n    model.fit(X,y)\n\n    return model.score(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:51:32.764305Z","iopub.execute_input":"2022-12-27T14:51:32.764723Z","iopub.status.idle":"2022-12-27T14:51:32.773097Z","shell.execute_reply.started":"2022-12-27T14:51:32.764680Z","shell.execute_reply":"2022-12-27T14:51:32.771799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check features important**","metadata":{}},{"cell_type":"code","source":"model = 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-27T14:57:11.280784Z","iopub.execute_input":"2022-12-27T14:57:11.281272Z","iopub.status.idle":"2022-12-27T14:57:35.941012Z","shell.execute_reply.started":"2022-12-27T14:57:11.281226Z","shell.execute_reply":"2022-12-27T14:57:35.939640Z"},"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-27T14:57:58.197607Z","iopub.execute_input":"2022-12-27T14:57:58.198179Z","iopub.status.idle":"2022-12-27T14:57:58.941382Z","shell.execute_reply.started":"2022-12-27T14:57:58.198140Z","shell.execute_reply":"2022-12-27T14:57:58.940124Z"},"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-27T14:58:17.779169Z","iopub.execute_input":"2022-12-27T14:58:17.779670Z","iopub.status.idle":"2022-12-27T14:58:17.795546Z","shell.execute_reply.started":"2022-12-27T14:58:17.779611Z","shell.execute_reply":"2022-12-27T14:58:17.794162Z"},"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-27T14:58:48.585399Z","iopub.execute_input":"2022-12-27T14:58:48.585842Z","iopub.status.idle":"2022-12-27T14:58:48.594533Z","shell.execute_reply.started":"2022-12-27T14:58:48.585809Z","shell.execute_reply":"2022-12-27T14:58:48.593352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:58:51.769592Z","iopub.execute_input":"2022-12-27T14:58:51.770193Z","iopub.status.idle":"2022-12-27T14:58:51.780873Z","shell.execute_reply.started":"2022-12-27T14:58:51.770141Z","shell.execute_reply":"2022-12-27T14:58:51.779395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Add Basics Features**","metadata":{}},{"cell_type":"code","source":"def add_basics_features(data, features):\n    \n    for feature in features:\n        \"\"\"\n        new_feature_name = str(feature) + '_min'\n        data[new_feature_name] = data[feature].min()\n        \n        new_feature_name = str(feature) + '_max'\n        data[new_feature_name] = data[feature].max()\n        \"\"\"\n        \n        new_feature_name = str(feature) + '_mean'\n        data[new_feature_name] = data[feature].mean()\n        \n        new_feature_name = str(feature) + '_std'\n        data[new_feature_name] = data[feature].std()\n        \n        new_feature_name = str(feature) + '_max_min'\n        data[new_feature_name] = data[feature].max() - data[feature].min()\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:59:13.872731Z","iopub.execute_input":"2022-12-27T14:59:13.873143Z","iopub.status.idle":"2022-12-27T14:59:13.883221Z","shell.execute_reply.started":"2022-12-27T14:59:13.873112Z","shell.execute_reply":"2022-12-27T14:59:13.881690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = add_basics_features(X, selected_features)\ntest = add_basics_features(test, selected_features)\n\nX.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T14:59:23.601545Z","iopub.execute_input":"2022-12-27T14:59:23.601992Z","iopub.status.idle":"2022-12-27T14:59:23.802744Z","shell.execute_reply.started":"2022-12-27T14:59:23.601957Z","shell.execute_reply":"2022-12-27T14:59:23.801504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cross Validation**","metadata":{}},{"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        print('*'*14, f\" Fold {fold} \", '*'*14, '\\n')\n        \n        # Split Data\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        # Create Model here\n        model = LGBMClassifier()\n        \n        # Fit Model\n        model.fit(X_train,y_train)\n\n        # Make X_val prediction\n        y_pred = model.predict_proba(X_val)[:,1]\n        \n        # Make Test prediction\n        y_predict.append(model.predict_proba(test)[:,1])\n\n        # Evaluate Model\n        print(\"Training score :\", model.score(X_train, y_train))\n        print(\"Roc auc score  :\", roc_auc_score(y_val, y_pred), '\\n')\n        \n        # Free the memory\n        del X_train, y_train, model, X_val, y_val, y_pred\n        gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T15:02:37.385445Z","iopub.execute_input":"2022-12-27T15:02:37.386468Z","iopub.status.idle":"2022-12-27T15:08:22.589499Z","shell.execute_reply.started":"2022-12-27T15:02:37.386420Z","shell.execute_reply":"2022-12-27T15:08:22.587968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training with complet data**","metadata":{}},{"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-27T15:08:29.780671Z","iopub.execute_input":"2022-12-27T15:08:29.781076Z","iopub.status.idle":"2022-12-27T15:08:55.249066Z","shell.execute_reply.started":"2022-12-27T15:08:29.781042Z","shell.execute_reply":"2022-12-27T15:08:55.247914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model, X, y, test, X_test, y_test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T15:08:58.248707Z","iopub.execute_input":"2022-12-27T15:08:58.249579Z","iopub.status.idle":"2022-12-27T15:08:58.391926Z","shell.execute_reply.started":"2022-12-27T15:08:58.249528Z","shell.execute_reply":"2022-12-27T15:08:58.390498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(y_predict).mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T15:09:00.796689Z","iopub.execute_input":"2022-12-27T15:09:00.797108Z","iopub.status.idle":"2022-12-27T15:09:00.846098Z","shell.execute_reply.started":"2022-12-27T15:09:00.797073Z","shell.execute_reply":"2022-12-27T15:09:00.844922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"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-27T15:10:00.661105Z","iopub.execute_input":"2022-12-27T15:10:00.661537Z","iopub.status.idle":"2022-12-27T15:10:00.866866Z","shell.execute_reply.started":"2022-12-27T15:10:00.661501Z","shell.execute_reply":"2022-12-27T15:10:00.865693Z"},"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-27T15:10:12.654310Z","iopub.execute_input":"2022-12-27T15:10:12.654789Z","iopub.status.idle":"2022-12-27T15:10:14.460953Z","shell.execute_reply.started":"2022-12-27T15:10:12.654750Z","shell.execute_reply":"2022-12-27T15:10:14.459720Z"},"trusted":true},"execution_count":null,"outputs":[]}]}