{"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":"markdown","source":"**import**","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport optuna\nimport gc\n\nfrom sklearn.metrics import roc_auc_score, accuracy_score\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:16.316760Z","iopub.execute_input":"2022-12-28T20:06:16.317227Z","iopub.status.idle":"2022-12-28T20:06:16.324624Z","shell.execute_reply.started":"2022-12-28T20:06:16.317189Z","shell.execute_reply":"2022-12-28T20:06:16.323009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**load**","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:16.327061Z","iopub.execute_input":"2022-12-28T20:06:16.327536Z","iopub.status.idle":"2022-12-28T20:06:16.346091Z","shell.execute_reply.started":"2022-12-28T20:06:16.327489Z","shell.execute_reply":"2022-12-28T20:06:16.344819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Read**","metadata":{}},{"cell_type":"markdown","source":"","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\")\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:16.491020Z","iopub.execute_input":"2022-12-28T20:06:16.491897Z","iopub.status.idle":"2022-12-28T20:06:24.467990Z","shell.execute_reply.started":"2022-12-28T20:06:16.491863Z","shell.execute_reply":"2022-12-28T20:06:24.466631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Understanding Data**","metadata":{}},{"cell_type":"markdown","source":"* describe","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:24.470988Z","iopub.execute_input":"2022-12-28T20:06:24.471479Z","iopub.status.idle":"2022-12-28T20:06:25.626798Z","shell.execute_reply.started":"2022-12-28T20:06:24.471411Z","shell.execute_reply":"2022-12-28T20:06:25.625628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:25.628329Z","iopub.execute_input":"2022-12-28T20:06:25.628780Z","iopub.status.idle":"2022-12-28T20:06:26.504198Z","shell.execute_reply.started":"2022-12-28T20:06:25.628746Z","shell.execute_reply":"2022-12-28T20:06:26.502947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Infos","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:26.505875Z","iopub.execute_input":"2022-12-28T20:06:26.506198Z","iopub.status.idle":"2022-12-28T20:06:26.610660Z","shell.execute_reply.started":"2022-12-28T20:06:26.506167Z","shell.execute_reply":"2022-12-28T20:06:26.609862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have :\n\n* 16 float64 columns\n* 16 int64 columns\n* 1 object columns","metadata":{}},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:26.612771Z","iopub.execute_input":"2022-12-28T20:06:26.613097Z","iopub.status.idle":"2022-12-28T20:06:26.737796Z","shell.execute_reply.started":"2022-12-28T20:06:26.613054Z","shell.execute_reply":"2022-12-28T20:06:26.736495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Check missing columns","metadata":{}},{"cell_type":"code","source":"train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:26.739503Z","iopub.execute_input":"2022-12-28T20:06:26.740147Z","iopub.status.idle":"2022-12-28T20:06:26.836181Z","shell.execute_reply.started":"2022-12-28T20:06:26.740100Z","shell.execute_reply":"2022-12-28T20:06:26.835016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:26.838488Z","iopub.execute_input":"2022-12-28T20:06:26.839262Z","iopub.status.idle":"2022-12-28T20:06:26.915063Z","shell.execute_reply.started":"2022-12-28T20:06:26.839214Z","shell.execute_reply":"2022-12-28T20:06:26.913954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Feature Engineering**","metadata":{}},{"cell_type":"markdown","source":"* \"We read the data and apply minimal feature engineering: We only split the f_27 string into ten separate features as described in the EDA, and we count the unique characters in the string.\"","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    # Next feature is from https://www.kaggle.com/code/cabaxiom/tps-may-22-eda-lgbm-model\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-28T20:06:26.916268Z","iopub.execute_input":"2022-12-28T20:06:26.916614Z","iopub.status.idle":"2022-12-28T20:06:40.852861Z","shell.execute_reply.started":"2022-12-28T20:06:26.916582Z","shell.execute_reply":"2022-12-28T20:06:40.851741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"markdown","source":"Installing EvalML","metadata":{}},{"cell_type":"code","source":"!python3 -m pip install -q evalml==0.28.0","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:40.854196Z","iopub.execute_input":"2022-12-28T20:06:40.854556Z","iopub.status.idle":"2022-12-28T20:06:53.046224Z","shell.execute_reply.started":"2022-12-28T20:06:40.854524Z","shell.execute_reply":"2022-12-28T20:06:53.044741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.automl import AutoMLSearch","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:53.047723Z","iopub.execute_input":"2022-12-28T20:06:53.048115Z","iopub.status.idle":"2022-12-28T20:06:53.055165Z","shell.execute_reply.started":"2022-12-28T20:06:53.048074Z","shell.execute_reply":"2022-12-28T20:06:53.053632Z"},"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-28T20:06:53.056822Z","iopub.execute_input":"2022-12-28T20:06:53.057245Z","iopub.status.idle":"2022-12-28T20:06:53.415655Z","shell.execute_reply.started":"2022-12-28T20:06:53.057209Z","shell.execute_reply":"2022-12-28T20:06:53.414746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n_, X_test, _, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:53.417032Z","iopub.execute_input":"2022-12-28T20:06:53.417375Z","iopub.status.idle":"2022-12-28T20:06:53.968911Z","shell.execute_reply.started":"2022-12-28T20:06:53.417343Z","shell.execute_reply":"2022-12-28T20:06:53.967891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training Model**","metadata":{}},{"cell_type":"code","source":"## run model\nmodel_evalml = AutoMLSearch(X_train=X, y_train=y, problem_type='binary', max_time=3600)\nmodel_evalml.search()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T20:06:53.970124Z","iopub.execute_input":"2022-12-28T20:06:53.970457Z","iopub.status.idle":"2022-12-28T21:58:38.124046Z","shell.execute_reply.started":"2022-12-28T20:06:53.970414Z","shell.execute_reply":"2022-12-28T21:58:38.122948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check leaderboard\nmodel_evalml.rankings","metadata":{"execution":{"iopub.status.busy":"2022-12-28T21:58:38.128076Z","iopub.execute_input":"2022-12-28T21:58:38.128469Z","iopub.status.idle":"2022-12-28T21:58:38.160611Z","shell.execute_reply.started":"2022-12-28T21:58:38.128403Z","shell.execute_reply":"2022-12-28T21:58:38.159485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model_evalml.best_pipeline.predict_proba(X_test)[True]\n\nmae = mean_absolute_error(y_test, pred)\nmse = mean_squared_error(y_test, pred)\n\nprint(\"MAE : %.7f\" % mae)\nprint(\"MSE : %.7f\" % mse)\nprint(\"Roc : %.7f\" % roc_auc_score(y_test, pred))","metadata":{"execution":{"iopub.status.busy":"2022-12-28T21:58:38.162086Z","iopub.execute_input":"2022-12-28T21:58:38.162418Z","iopub.status.idle":"2022-12-28T21:58:47.601955Z","shell.execute_reply.started":"2022-12-28T21:58:38.162388Z","shell.execute_reply":"2022-12-28T21:58:47.600956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:07:51.124712Z","iopub.execute_input":"2022-12-28T22:07:51.125196Z","iopub.status.idle":"2022-12-28T22:07:51.134221Z","shell.execute_reply.started":"2022-12-28T22:07:51.125155Z","shell.execute_reply":"2022-12-28T22:07:51.133394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Free Memory**","metadata":{}},{"cell_type":"code","source":"del train\ndel X\ndel y\ndel X_test\ndel y_test\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:09:12.491651Z","iopub.execute_input":"2022-12-28T22:09:12.492127Z","iopub.status.idle":"2022-12-28T22:09:12.821459Z","shell.execute_reply.started":"2022-12-28T22:09:12.492085Z","shell.execute_reply":"2022-12-28T22:09:12.820298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"## generate predictions\ny_predict = model_evalml.best_pipeline.predict_proba(test)[True]\n\n#pred = automl.predict(test)\nlen(y_predict)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:09:39.536814Z","iopub.execute_input":"2022-12-28T22:09:39.537227Z","iopub.status.idle":"2022-12-28T22:10:18.477023Z","shell.execute_reply.started":"2022-12-28T22:09:39.537193Z","shell.execute_reply":"2022-12-28T22:10:18.475769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:10:32.359956Z","iopub.execute_input":"2022-12-28T22:10:32.360360Z","iopub.status.idle":"2022-12-28T22:10:32.372618Z","shell.execute_reply.started":"2022-12-28T22:10:32.360328Z","shell.execute_reply":"2022-12-28T22:10:32.371350Z"},"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-28T22:11:34.778098Z","iopub.execute_input":"2022-12-28T22:11:34.778546Z","iopub.status.idle":"2022-12-28T22:11:35.036875Z","shell.execute_reply.started":"2022-12-28T22:11:34.778508Z","shell.execute_reply":"2022-12-28T22:11:35.035449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pandas -U\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:11:56.532527Z","iopub.execute_input":"2022-12-28T22:11:56.532963Z","iopub.status.idle":"2022-12-28T22:12:16.026416Z","shell.execute_reply.started":"2022-12-28T22:11:56.532906Z","shell.execute_reply":"2022-12-28T22:12:16.025012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target'] = y_predict # np.array(y_predict).mean(axis=0)\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-12-28T22:12:54.210037Z","iopub.execute_input":"2022-12-28T22:12:54.210558Z","iopub.status.idle":"2022-12-28T22:12:55.840050Z","shell.execute_reply.started":"2022-12-28T22:12:54.210506Z","shell.execute_reply":"2022-12-28T22:12:55.838739Z"},"trusted":true},"execution_count":null,"outputs":[]}]}