{"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-08T14:46:30.574236Z","iopub.execute_input":"2023-01-08T14:46:30.574684Z","iopub.status.idle":"2023-01-08T14:46:30.583968Z","shell.execute_reply.started":"2023-01-08T14:46:30.574648Z","shell.execute_reply":"2023-01-08T14:46:30.582762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"import pandas as pd\npd.reset_option('display.max_colwidth', None)\n\ntrain = 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-08T14:46:30.586042Z","iopub.execute_input":"2023-01-08T14:46:30.586772Z","iopub.status.idle":"2023-01-08T14:46:38.486822Z","shell.execute_reply.started":"2023-01-08T14:46:30.586728Z","shell.execute_reply":"2023-01-08T14:46:38.485594Z"},"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":"2023-01-08T14:46:38.488951Z","iopub.execute_input":"2023-01-08T14:46:38.489302Z","iopub.status.idle":"2023-01-08T14:46:38.497552Z","shell.execute_reply.started":"2023-01-08T14:46:38.489270Z","shell.execute_reply":"2023-01-08T14:46:38.496302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Data","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:38.498829Z","iopub.execute_input":"2023-01-08T14:46:38.499147Z","iopub.status.idle":"2023-01-08T14:46:39.659160Z","shell.execute_reply.started":"2023-01-08T14:46:38.499119Z","shell.execute_reply":"2023-01-08T14:46:39.657996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:39.661650Z","iopub.execute_input":"2023-01-08T14:46:39.661972Z","iopub.status.idle":"2023-01-08T14:46:40.550799Z","shell.execute_reply.started":"2023-01-08T14:46:39.661943Z","shell.execute_reply":"2023-01-08T14:46:40.549631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:40.552103Z","iopub.execute_input":"2023-01-08T14:46:40.552441Z","iopub.status.idle":"2023-01-08T14:46:40.657981Z","shell.execute_reply.started":"2023-01-08T14:46:40.552410Z","shell.execute_reply":"2023-01-08T14:46:40.656810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:40.659332Z","iopub.execute_input":"2023-01-08T14:46:40.659682Z","iopub.status.idle":"2023-01-08T14:46:40.743261Z","shell.execute_reply.started":"2023-01-08T14:46:40.659651Z","shell.execute_reply":"2023-01-08T14:46:40.742197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# checking for missing values","metadata":{}},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:40.744825Z","iopub.execute_input":"2023-01-08T14:46:40.745318Z","iopub.status.idle":"2023-01-08T14:46:40.839558Z","shell.execute_reply.started":"2023-01-08T14:46:40.745275Z","shell.execute_reply":"2023-01-08T14:46:40.838639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:40.840829Z","iopub.execute_input":"2023-01-08T14:46:40.841240Z","iopub.status.idle":"2023-01-08T14:46:40.916635Z","shell.execute_reply.started":"2023-01-08T14:46:40.841209Z","shell.execute_reply":"2023-01-08T14:46:40.915742Z"},"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":"2023-01-08T14:46:40.918581Z","iopub.execute_input":"2023-01-08T14:46:40.919438Z","iopub.status.idle":"2023-01-08T14:46:55.623298Z","shell.execute_reply.started":"2023-01-08T14:46:40.919392Z","shell.execute_reply":"2023-01-08T14:46:55.622113Z"},"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":"2023-01-08T14:46:55.626735Z","iopub.execute_input":"2023-01-08T14:46:55.627097Z","iopub.status.idle":"2023-01-08T14:46:55.631868Z","shell.execute_reply.started":"2023-01-08T14:46:55.627066Z","shell.execute_reply":"2023-01-08T14:46:55.630849Z"},"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":"2023-01-08T14:46:55.633359Z","iopub.execute_input":"2023-01-08T14:46:55.633754Z","iopub.status.idle":"2023-01-08T14:46:55.956203Z","shell.execute_reply.started":"2023-01-08T14:46:55.633720Z","shell.execute_reply":"2023-01-08T14:46:55.954984Z"},"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-08T14:46:55.957462Z","iopub.execute_input":"2023-01-08T14:46:55.958029Z","iopub.status.idle":"2023-01-08T14:46:56.473393Z","shell.execute_reply.started":"2023-01-08T14:46:55.957996Z","shell.execute_reply":"2023-01-08T14:46:56.472219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Search best params ","metadata":{}},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\n\nimport time","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:56.474914Z","iopub.execute_input":"2023-01-08T14:46:56.476680Z","iopub.status.idle":"2023-01-08T14:46:56.481666Z","shell.execute_reply.started":"2023-01-08T14:46:56.476632Z","shell.execute_reply":"2023-01-08T14:46:56.480589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef objectivesLGBM(trial):\n    model = LGBMClassifier()\n    model.fit(X,y)\n\n    return model.score(X,y)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:46:56.482880Z","iopub.execute_input":"2023-01-08T14:46:56.483323Z","iopub.status.idle":"2023-01-08T14:46:56.491966Z","shell.execute_reply.started":"2023-01-08T14:46:56.483276Z","shell.execute_reply":"2023-01-08T14:46:56.490812Z"},"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":"2023-01-08T14:46:56.493795Z","iopub.execute_input":"2023-01-08T14:46:56.494467Z","iopub.status.idle":"2023-01-08T14:47:18.345541Z","shell.execute_reply.started":"2023-01-08T14:46:56.494431Z","shell.execute_reply":"2023-01-08T14:47:18.344139Z"},"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-08T14:47:18.347228Z","iopub.execute_input":"2023-01-08T14:47:18.347954Z","iopub.status.idle":"2023-01-08T14:47:18.919584Z","shell.execute_reply.started":"2023-01-08T14:47:18.347906Z","shell.execute_reply":"2023-01-08T14:47:18.918499Z"},"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-08T14:47:18.921396Z","iopub.execute_input":"2023-01-08T14:47:18.921861Z","iopub.status.idle":"2023-01-08T14:47:18.936599Z","shell.execute_reply.started":"2023-01-08T14:47:18.921818Z","shell.execute_reply":"2023-01-08T14:47:18.935265Z"},"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-08T14:47:18.938219Z","iopub.execute_input":"2023-01-08T14:47:18.938687Z","iopub.status.idle":"2023-01-08T14:47:18.948439Z","shell.execute_reply.started":"2023-01-08T14:47:18.938643Z","shell.execute_reply":"2023-01-08T14:47:18.947183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:47:18.949979Z","iopub.execute_input":"2023-01-08T14:47:18.950304Z","iopub.status.idle":"2023-01-08T14:47:18.960143Z","shell.execute_reply.started":"2023-01-08T14:47:18.950276Z","shell.execute_reply":"2023-01-08T14:47:18.959133Z"},"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":"2023-01-08T14:47:18.961785Z","iopub.execute_input":"2023-01-08T14:47:18.962278Z","iopub.status.idle":"2023-01-08T14:47:18.971041Z","shell.execute_reply.started":"2023-01-08T14:47:18.962231Z","shell.execute_reply":"2023-01-08T14:47:18.970002Z"},"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-08T14:47:18.972649Z","iopub.execute_input":"2023-01-08T14:47:18.973309Z","iopub.status.idle":"2023-01-08T14:47:19.211799Z","shell.execute_reply.started":"2023-01-08T14:47:18.973274Z","shell.execute_reply":"2023-01-08T14:47:19.210455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cross Validation\n","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           # 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","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:47:19.213236Z","iopub.execute_input":"2023-01-08T14:47:19.213651Z","iopub.status.idle":"2023-01-08T14:52:40.305043Z","shell.execute_reply.started":"2023-01-08T14:47:19.213616Z","shell.execute_reply":"2023-01-08T14:52:40.303907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, y_train, model, X_val, y_val, y_pred","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:52:40.306173Z","iopub.execute_input":"2023-01-08T14:52:40.306470Z","iopub.status.idle":"2023-01-08T14:52:40.313072Z","shell.execute_reply.started":"2023-01-08T14:52:40.306442Z","shell.execute_reply":"2023-01-08T14:52:40.312215Z"},"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":"2023-01-08T14:53:44.700595Z","iopub.execute_input":"2023-01-08T14:53:44.701041Z","iopub.status.idle":"2023-01-08T14:54:05.302880Z","shell.execute_reply.started":"2023-01-08T14:53:44.701004Z","shell.execute_reply":"2023-01-08T14:54:05.301797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(y_predict).mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T14:54:53.018879Z","iopub.execute_input":"2023-01-08T14:54:53.019425Z","iopub.status.idle":"2023-01-08T14:54:53.071653Z","shell.execute_reply.started":"2023-01-08T14:54:53.019377Z","shell.execute_reply":"2023-01-08T14:54:53.070404Z"},"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":"2023-01-08T14:56:47.557577Z","iopub.execute_input":"2023-01-08T14:56:47.558064Z","iopub.status.idle":"2023-01-08T14:56:47.744555Z","shell.execute_reply.started":"2023-01-08T14:56:47.558026Z","shell.execute_reply":"2023-01-08T14:56:47.743606Z"},"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-08T14:57:10.366829Z","iopub.execute_input":"2023-01-08T14:57:10.367225Z","iopub.status.idle":"2023-01-08T14:57:12.011867Z","shell.execute_reply.started":"2023-01-08T14:57:10.367195Z","shell.execute_reply":"2023-01-08T14:57:12.011086Z"},"trusted":true},"execution_count":null,"outputs":[]}]}