{"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":"import numpy as np\nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-08T11:31:28.935238Z","iopub.execute_input":"2023-01-08T11:31:28.936094Z","iopub.status.idle":"2023-01-08T11:31:28.943796Z","shell.execute_reply.started":"2023-01-08T11:31:28.936035Z","shell.execute_reply":"2023-01-08T11:31:28.942582Z"},"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-08T11:31:28.945319Z","iopub.execute_input":"2023-01-08T11:31:28.946304Z","iopub.status.idle":"2023-01-08T11:31:34.288326Z","shell.execute_reply.started":"2023-01-08T11:31:28.946238Z","shell.execute_reply":"2023-01-08T11:31:34.286792Z"},"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-08T11:31:34.289690Z","iopub.execute_input":"2023-01-08T11:31:34.289977Z","iopub.status.idle":"2023-01-08T11:31:34.296599Z","shell.execute_reply.started":"2023-01-08T11:31:34.289954Z","shell.execute_reply":"2023-01-08T11:31:34.295228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:34.298830Z","iopub.execute_input":"2023-01-08T11:31:34.299184Z","iopub.status.idle":"2023-01-08T11:31:35.072074Z","shell.execute_reply.started":"2023-01-08T11:31:34.299124Z","shell.execute_reply":"2023-01-08T11:31:35.070572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:35.073651Z","iopub.execute_input":"2023-01-08T11:31:35.074024Z","iopub.status.idle":"2023-01-08T11:31:35.659525Z","shell.execute_reply.started":"2023-01-08T11:31:35.073991Z","shell.execute_reply":"2023-01-08T11:31:35.658344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:35.660815Z","iopub.execute_input":"2023-01-08T11:31:35.661124Z","iopub.status.idle":"2023-01-08T11:31:35.731699Z","shell.execute_reply.started":"2023-01-08T11:31:35.661100Z","shell.execute_reply":"2023-01-08T11:31:35.730365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:35.732953Z","iopub.execute_input":"2023-01-08T11:31:35.733613Z","iopub.status.idle":"2023-01-08T11:31:35.790746Z","shell.execute_reply.started":"2023-01-08T11:31:35.733587Z","shell.execute_reply":"2023-01-08T11:31:35.789386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:35.791970Z","iopub.execute_input":"2023-01-08T11:31:35.792280Z","iopub.status.idle":"2023-01-08T11:31:35.857299Z","shell.execute_reply.started":"2023-01-08T11:31:35.792256Z","shell.execute_reply":"2023-01-08T11:31:35.855822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:35.860376Z","iopub.execute_input":"2023-01-08T11:31:35.860661Z","iopub.status.idle":"2023-01-08T11:31:35.910424Z","shell.execute_reply.started":"2023-01-08T11:31:35.860633Z","shell.execute_reply":"2023-01-08T11:31:35.909422Z"},"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-08T11:31:35.911645Z","iopub.execute_input":"2023-01-08T11:31:35.911916Z","iopub.status.idle":"2023-01-08T11:31:48.084847Z","shell.execute_reply.started":"2023-01-08T11:31:35.911893Z","shell.execute_reply":"2023-01-08T11:31:48.083661Z"},"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-08T11:31:48.086194Z","iopub.execute_input":"2023-01-08T11:31:48.086478Z","iopub.status.idle":"2023-01-08T11:31:48.092183Z","shell.execute_reply.started":"2023-01-08T11:31:48.086455Z","shell.execute_reply":"2023-01-08T11:31:48.090666Z"},"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-08T11:31:48.093632Z","iopub.execute_input":"2023-01-08T11:31:48.094299Z","iopub.status.idle":"2023-01-08T11:31:48.241325Z","shell.execute_reply.started":"2023-01-08T11:31:48.094273Z","shell.execute_reply":"2023-01-08T11:31:48.240018Z"},"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-08T11:31:48.242950Z","iopub.execute_input":"2023-01-08T11:31:48.243266Z","iopub.status.idle":"2023-01-08T11:31:48.662116Z","shell.execute_reply.started":"2023-01-08T11:31:48.243243Z","shell.execute_reply":"2023-01-08T11:31:48.661004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\n\nimport time","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:31:48.663641Z","iopub.execute_input":"2023-01-08T11:31:48.663939Z","iopub.status.idle":"2023-01-08T11:31:48.669289Z","shell.execute_reply.started":"2023-01-08T11:31:48.663915Z","shell.execute_reply":"2023-01-08T11:31:48.667922Z"},"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-08T11:31:48.671337Z","iopub.execute_input":"2023-01-08T11:31:48.672030Z","iopub.status.idle":"2023-01-08T11:32:11.094113Z","shell.execute_reply.started":"2023-01-08T11:31:48.671997Z","shell.execute_reply":"2023-01-08T11:32:11.093206Z"},"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-08T11:32:11.095434Z","iopub.execute_input":"2023-01-08T11:32:11.095789Z","iopub.status.idle":"2023-01-08T11:32:11.751631Z","shell.execute_reply.started":"2023-01-08T11:32:11.095756Z","shell.execute_reply":"2023-01-08T11:32:11.750649Z"},"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-08T11:32:11.752770Z","iopub.execute_input":"2023-01-08T11:32:11.753032Z","iopub.status.idle":"2023-01-08T11:32:11.764742Z","shell.execute_reply.started":"2023-01-08T11:32:11.753009Z","shell.execute_reply":"2023-01-08T11:32:11.763737Z"},"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-08T11:32:11.766347Z","iopub.execute_input":"2023-01-08T11:32:11.766732Z","iopub.status.idle":"2023-01-08T11:32:11.780174Z","shell.execute_reply.started":"2023-01-08T11:32:11.766699Z","shell.execute_reply":"2023-01-08T11:32:11.779034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:32:11.781720Z","iopub.execute_input":"2023-01-08T11:32:11.782074Z","iopub.status.idle":"2023-01-08T11:32:11.791501Z","shell.execute_reply.started":"2023-01-08T11:32:11.782042Z","shell.execute_reply":"2023-01-08T11:32:11.790208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_basics_features(data, features):\n    \n    for feature in features:\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-08T11:32:11.794332Z","iopub.execute_input":"2023-01-08T11:32:11.795427Z","iopub.status.idle":"2023-01-08T11:32:11.802947Z","shell.execute_reply.started":"2023-01-08T11:32:11.795385Z","shell.execute_reply":"2023-01-08T11:32:11.801670Z"},"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-08T11:32:11.804344Z","iopub.execute_input":"2023-01-08T11:32:11.804803Z","iopub.status.idle":"2023-01-08T11:32:11.924824Z","shell.execute_reply.started":"2023-01-08T11:32:11.804767Z","shell.execute_reply":"2023-01-08T11:32:11.923946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = StratifiedKFold(n_splits=14, shuffle=True, random_state=0)\ny_predict = []\n\n# Iterate over the folds\nfor i, (train_index, val_index) in enumerate(z.split(X, y)):\n   \n    # Split the data into training and validation sets\n\n    X_train, X_val = X.iloc[train_index], X.iloc[val_index]\n    y_train, y_val = y.iloc[train_index], y.iloc[val_index]\n\n    # Create the model\n    model = LGBMClassifier()\n\n    # Fit the model\n    model.fit(X_train, y_train)\n\n    # Make predictions on the validation set\n    y_pred = model.predict_proba(X_val)[:, 1]\n\n    # Make predictions on the test set\n    y_predict.append(model.predict_proba(test)[:, 1])\n\n    # Evaluate the model\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-08T11:32:11.926180Z","iopub.execute_input":"2023-01-08T11:32:11.927398Z","iopub.status.idle":"2023-01-08T11:37:34.899320Z","shell.execute_reply.started":"2023-01-08T11:32:11.927366Z","shell.execute_reply":"2023-01-08T11:37:34.898307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = LGBMClassifier()\n\nmodel.fit(X, y)\n\ny_predict.append(model.predict_proba(test)[:, 1])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:37:34.900628Z","iopub.execute_input":"2023-01-08T11:37:34.900937Z","iopub.status.idle":"2023-01-08T11:37:57.011419Z","shell.execute_reply.started":"2023-01-08T11:37:34.900909Z","shell.execute_reply":"2023-01-08T11:37:57.010669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_predictions = np.mean(y_predict, axis=0)\n\nprint(mean_predictions)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:37:57.016512Z","iopub.execute_input":"2023-01-08T11:37:57.016859Z","iopub.status.idle":"2023-01-08T11:37:57.041292Z","shell.execute_reply.started":"2023-01-08T11:37:57.016832Z","shell.execute_reply":"2023-01-08T11:37:57.039947Z"},"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-08T11:37:57.042484Z","iopub.execute_input":"2023-01-08T11:37:57.042787Z","iopub.status.idle":"2023-01-08T11:37:57.229989Z","shell.execute_reply.started":"2023-01-08T11:37:57.042763Z","shell.execute_reply":"2023-01-08T11:37:57.228667Z"},"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-08T11:37:57.231649Z","iopub.execute_input":"2023-01-08T11:37:57.232268Z","iopub.status.idle":"2023-01-08T11:37:58.416132Z","shell.execute_reply.started":"2023-01-08T11:37:57.232136Z","shell.execute_reply":"2023-01-08T11:37:58.415523Z"},"trusted":true},"execution_count":null,"outputs":[]}]}