{"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)\nimport os\nimport optuna\nimport gc\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-01T15:27:34.920948Z","iopub.execute_input":"2023-01-01T15:27:34.921469Z","iopub.status.idle":"2023-01-01T15:27:35.375205Z","shell.execute_reply.started":"2023-01-01T15:27:34.921429Z","shell.execute_reply":"2023-01-01T15:27:35.373819Z"},"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-01T15:27:35.376994Z","iopub.execute_input":"2023-01-01T15:27:35.377366Z","iopub.status.idle":"2023-01-01T15:27:44.705565Z","shell.execute_reply.started":"2023-01-01T15:27:35.377334Z","shell.execute_reply":"2023-01-01T15:27:44.704068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:44.707015Z","iopub.execute_input":"2023-01-01T15:27:44.707448Z","iopub.status.idle":"2023-01-01T15:27:44.716544Z","shell.execute_reply.started":"2023-01-01T15:27:44.707412Z","shell.execute_reply":"2023-01-01T15:27:44.714680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:44.720418Z","iopub.execute_input":"2023-01-01T15:27:44.721071Z","iopub.status.idle":"2023-01-01T15:27:45.982519Z","shell.execute_reply.started":"2023-01-01T15:27:44.721032Z","shell.execute_reply":"2023-01-01T15:27:45.981230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:45.984476Z","iopub.execute_input":"2023-01-01T15:27:45.985284Z","iopub.status.idle":"2023-01-01T15:27:46.935453Z","shell.execute_reply.started":"2023-01-01T15:27:45.985245Z","shell.execute_reply":"2023-01-01T15:27:46.934269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:46.936996Z","iopub.execute_input":"2023-01-01T15:27:46.937941Z","iopub.status.idle":"2023-01-01T15:27:47.043998Z","shell.execute_reply.started":"2023-01-01T15:27:46.937903Z","shell.execute_reply":"2023-01-01T15:27:47.042802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:47.045638Z","iopub.execute_input":"2023-01-01T15:27:47.046720Z","iopub.status.idle":"2023-01-01T15:27:47.134909Z","shell.execute_reply.started":"2023-01-01T15:27:47.046683Z","shell.execute_reply":"2023-01-01T15:27:47.133726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:27:47.136281Z","iopub.execute_input":"2023-01-01T15:27:47.137189Z","iopub.status.idle":"2023-01-01T15:27:47.233074Z","shell.execute_reply.started":"2023-01-01T15:27:47.137150Z","shell.execute_reply":"2023-01-01T15:27:47.231886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-01-01T15:27:47.235178Z","iopub.execute_input":"2023-01-01T15:27:47.235688Z","iopub.status.idle":"2023-01-01T15:28:01.651781Z","shell.execute_reply.started":"2023-01-01T15:27:47.235635Z","shell.execute_reply":"2023-01-01T15:28:01.650702Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:28:01.655449Z","iopub.execute_input":"2023-01-01T15:28:01.657040Z","iopub.status.idle":"2023-01-01T15:28:01.664095Z","shell.execute_reply.started":"2023-01-01T15:28:01.656971Z","shell.execute_reply":"2023-01-01T15:28:01.662375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-01-01T15:28:01.666218Z","iopub.execute_input":"2023-01-01T15:28:01.667039Z","iopub.status.idle":"2023-01-01T15:28:02.042702Z","shell.execute_reply.started":"2023-01-01T15:28:01.666988Z","shell.execute_reply":"2023-01-01T15:28:02.040842Z"},"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-01T15:28:02.044213Z","iopub.execute_input":"2023-01-01T15:28:02.044738Z","iopub.status.idle":"2023-01-01T15:28:02.619340Z","shell.execute_reply.started":"2023-01-01T15:28:02.044686Z","shell.execute_reply":"2023-01-01T15:28:02.618012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\n\nimport time","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:28:02.621866Z","iopub.execute_input":"2023-01-01T15:28:02.622774Z","iopub.status.idle":"2023-01-01T15:28:02.628861Z","shell.execute_reply.started":"2023-01-01T15:28:02.622720Z","shell.execute_reply":"2023-01-01T15:28:02.627467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-01-01T15:28:02.630413Z","iopub.execute_input":"2023-01-01T15:28:02.631024Z","iopub.status.idle":"2023-01-01T15:28:24.354958Z","shell.execute_reply.started":"2023-01-01T15:28:02.630987Z","shell.execute_reply":"2023-01-01T15:28:24.353582Z"},"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":"2023-01-01T15:28:24.356417Z","iopub.execute_input":"2023-01-01T15:28:24.356787Z","iopub.status.idle":"2023-01-01T15:28:24.945184Z","shell.execute_reply.started":"2023-01-01T15:28:24.356755Z","shell.execute_reply":"2023-01-01T15:28:24.943928Z"},"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-01T15:28:24.946569Z","iopub.execute_input":"2023-01-01T15:28:24.946935Z","iopub.status.idle":"2023-01-01T15:28:24.962852Z","shell.execute_reply.started":"2023-01-01T15:28:24.946902Z","shell.execute_reply":"2023-01-01T15:28:24.961393Z"},"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-01T15:28:24.964675Z","iopub.execute_input":"2023-01-01T15:28:24.965098Z","iopub.status.idle":"2023-01-01T15:28:24.975893Z","shell.execute_reply.started":"2023-01-01T15:28:24.965059Z","shell.execute_reply":"2023-01-01T15:28:24.974042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:28:24.978127Z","iopub.execute_input":"2023-01-01T15:28:24.978827Z","iopub.status.idle":"2023-01-01T15:28:24.988746Z","shell.execute_reply.started":"2023-01-01T15:28:24.978774Z","shell.execute_reply":"2023-01-01T15:28:24.987072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-01-01T15:28:24.990443Z","iopub.execute_input":"2023-01-01T15:28:24.990874Z","iopub.status.idle":"2023-01-01T15:28:24.998257Z","shell.execute_reply.started":"2023-01-01T15:28:24.990839Z","shell.execute_reply":"2023-01-01T15:28:24.996962Z"},"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":"2023-01-01T15:28:24.999564Z","iopub.execute_input":"2023-01-01T15:28:25.000026Z","iopub.status.idle":"2023-01-01T15:28:25.253995Z","shell.execute_reply.started":"2023-01-01T15:28:24.999980Z","shell.execute_reply":"2023-01-01T15:28:25.252590Z"},"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        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":"2023-01-01T15:28:25.256657Z","iopub.execute_input":"2023-01-01T15:28:25.257185Z","iopub.status.idle":"2023-01-01T15:29:18.464149Z","shell.execute_reply.started":"2023-01-01T15:28:25.257138Z","shell.execute_reply":"2023-01-01T15:29:18.462545Z"},"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-01T15:30:20.100446Z","iopub.execute_input":"2023-01-01T15:30:20.100947Z","iopub.status.idle":"2023-01-01T15:30:41.465773Z","shell.execute_reply.started":"2023-01-01T15:30:20.100913Z","shell.execute_reply":"2023-01-01T15:30:41.464671Z"},"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":"2023-01-01T15:31:03.421986Z","iopub.execute_input":"2023-01-01T15:31:03.422374Z","iopub.status.idle":"2023-01-01T15:31:03.605645Z","shell.execute_reply.started":"2023-01-01T15:31:03.422343Z","shell.execute_reply":"2023-01-01T15:31:03.604253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(y_predict).mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-01T15:31:17.939888Z","iopub.execute_input":"2023-01-01T15:31:17.940322Z","iopub.status.idle":"2023-01-01T15:31:17.956138Z","shell.execute_reply.started":"2023-01-01T15:31:17.940281Z","shell.execute_reply":"2023-01-01T15:31:17.955145Z"},"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-01T15:31:23.903835Z","iopub.execute_input":"2023-01-01T15:31:23.904253Z","iopub.status.idle":"2023-01-01T15:31:24.171847Z","shell.execute_reply.started":"2023-01-01T15:31:23.904222Z","shell.execute_reply":"2023-01-01T15:31:24.170562Z"},"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-01T15:31:28.659847Z","iopub.execute_input":"2023-01-01T15:31:28.661118Z","iopub.status.idle":"2023-01-01T15:31:30.334163Z","shell.execute_reply.started":"2023-01-01T15:31:28.661056Z","shell.execute_reply":"2023-01-01T15:31:30.332830Z"},"trusted":true},"execution_count":null,"outputs":[]}]}