{"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 optuna\nimport gc\n\nfrom sklearn.metrics import roc_auc_score, accuracy_score\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error\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":"2022-12-26T21:18:25.160278Z","iopub.execute_input":"2022-12-26T21:18:25.160939Z","iopub.status.idle":"2022-12-26T21:18:25.182068Z","shell.execute_reply.started":"2022-12-26T21:18:25.160898Z","shell.execute_reply":"2022-12-26T21:18:25.180758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/emsi-dataset/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/emsi-dataset/test.csv\")\n\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:25.184236Z","iopub.execute_input":"2022-12-26T21:18:25.18498Z","iopub.status.idle":"2022-12-26T21:18:34.158009Z","shell.execute_reply.started":"2022-12-26T21:18:25.184922Z","shell.execute_reply":"2022-12-26T21:18:34.156681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:34.159416Z","iopub.execute_input":"2022-12-26T21:18:34.159753Z","iopub.status.idle":"2022-12-26T21:18:35.389838Z","shell.execute_reply.started":"2022-12-26T21:18:34.159722Z","shell.execute_reply":"2022-12-26T21:18:35.388669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:35.393027Z","iopub.execute_input":"2022-12-26T21:18:35.393556Z","iopub.status.idle":"2022-12-26T21:18:36.33192Z","shell.execute_reply.started":"2022-12-26T21:18:35.393497Z","shell.execute_reply":"2022-12-26T21:18:36.330333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:36.333458Z","iopub.execute_input":"2022-12-26T21:18:36.333837Z","iopub.status.idle":"2022-12-26T21:18:36.441709Z","shell.execute_reply.started":"2022-12-26T21:18:36.333804Z","shell.execute_reply":"2022-12-26T21:18:36.440469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:36.443327Z","iopub.execute_input":"2022-12-26T21:18:36.443702Z","iopub.status.idle":"2022-12-26T21:18:36.531422Z","shell.execute_reply.started":"2022-12-26T21:18:36.44367Z","shell.execute_reply":"2022-12-26T21:18:36.530141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum().any()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:36.533695Z","iopub.execute_input":"2022-12-26T21:18:36.534436Z","iopub.status.idle":"2022-12-26T21:18:36.635191Z","shell.execute_reply.started":"2022-12-26T21:18:36.534398Z","shell.execute_reply":"2022-12-26T21:18:36.633807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum().any()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:36.636677Z","iopub.execute_input":"2022-12-26T21:18:36.637017Z","iopub.status.idle":"2022-12-26T21:18:36.715631Z","shell.execute_reply.started":"2022-12-26T21:18:36.636988Z","shell.execute_reply":"2022-12-26T21:18:36.714275Z"},"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    # 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-26T21:18:36.716932Z","iopub.execute_input":"2022-12-26T21:18:36.717281Z","iopub.status.idle":"2022-12-26T21:18:50.834498Z","shell.execute_reply.started":"2022-12-26T21:18:36.717238Z","shell.execute_reply":"2022-12-26T21:18:50.833259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 -m pip install -q evalml==0.28.0\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:18:50.839033Z","iopub.execute_input":"2022-12-26T21:18:50.839406Z","iopub.status.idle":"2022-12-26T21:19:03.382677Z","shell.execute_reply.started":"2022-12-26T21:18:50.839372Z","shell.execute_reply":"2022-12-26T21:19:03.380108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.automl import AutoMLSearch\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T21:19:03.385227Z","iopub.execute_input":"2022-12-26T21:19:03.385738Z","iopub.status.idle":"2022-12-26T21:19:03.393574Z","shell.execute_reply.started":"2022-12-26T21:19:03.385695Z","shell.execute_reply":"2022-12-26T21:19:03.391944Z"},"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":"2022-12-26T21:19:03.396225Z","iopub.execute_input":"2022-12-26T21:19:03.396656Z","iopub.status.idle":"2022-12-26T21:19:03.775806Z","shell.execute_reply.started":"2022-12-26T21:19:03.396613Z","shell.execute_reply":"2022-12-26T21:19:03.774301Z"},"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-26T21:19:03.777672Z","iopub.execute_input":"2022-12-26T21:19:03.778086Z","iopub.status.idle":"2022-12-26T21:19:04.325581Z","shell.execute_reply.started":"2022-12-26T21:19:03.778053Z","shell.execute_reply":"2022-12-26T21:19:04.324139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T21:19:04.327443Z","iopub.execute_input":"2022-12-26T21:19:04.327815Z","iopub.status.idle":"2022-12-26T23:16:38.043553Z","shell.execute_reply.started":"2022-12-26T21:19:04.327785Z","shell.execute_reply":"2022-12-26T23:16:38.042306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## check leaderboard\nmodel_evalml.rankings","metadata":{"execution":{"iopub.status.busy":"2022-12-26T23:16:38.045471Z","iopub.execute_input":"2022-12-26T23:16:38.046406Z","iopub.status.idle":"2022-12-26T23:16:38.08316Z","shell.execute_reply.started":"2022-12-26T23:16:38.046357Z","shell.execute_reply":"2022-12-26T23:16:38.081781Z"},"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-26T23:16:38.084689Z","iopub.execute_input":"2022-12-26T23:16:38.085059Z","iopub.status.idle":"2022-12-26T23:16:47.950036Z","shell.execute_reply.started":"2022-12-26T23:16:38.085025Z","shell.execute_reply":"2022-12-26T23:16:47.948674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T23:16:47.951673Z","iopub.execute_input":"2022-12-26T23:16:47.952696Z","iopub.status.idle":"2022-12-26T23:16:47.96357Z","shell.execute_reply.started":"2022-12-26T23:16:47.952647Z","shell.execute_reply":"2022-12-26T23:16:47.962255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T23:16:47.965097Z","iopub.execute_input":"2022-12-26T23:16:47.966245Z","iopub.status.idle":"2022-12-26T23:16:48.270089Z","shell.execute_reply.started":"2022-12-26T23:16:47.966166Z","shell.execute_reply":"2022-12-26T23:16:48.268618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T23:16:48.272574Z","iopub.execute_input":"2022-12-26T23:16:48.272928Z","iopub.status.idle":"2022-12-26T23:17:27.771573Z","shell.execute_reply.started":"2022-12-26T23:16:48.272896Z","shell.execute_reply":"2022-12-26T23:17:27.770169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict\n","metadata":{"execution":{"iopub.status.busy":"2022-12-26T23:17:27.773684Z","iopub.execute_input":"2022-12-26T23:17:27.774543Z","iopub.status.idle":"2022-12-26T23:17:27.784656Z","shell.execute_reply.started":"2022-12-26T23:17:27.774493Z","shell.execute_reply":"2022-12-26T23:17:27.783681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/sample/sample_submission.csv\")\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-26T23:20:51.594926Z","iopub.execute_input":"2022-12-26T23:20:51.595497Z","iopub.status.idle":"2022-12-26T23:20:51.828611Z","shell.execute_reply.started":"2022-12-26T23:20:51.595454Z","shell.execute_reply":"2022-12-26T23:20:51.827297Z"},"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-26T23:20:55.248824Z","iopub.execute_input":"2022-12-26T23:20:55.249512Z","iopub.status.idle":"2022-12-26T23:21:14.994849Z","shell.execute_reply.started":"2022-12-26T23:20:55.249474Z","shell.execute_reply":"2022-12-26T23:21:14.993482Z"},"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-26T23:21:21.468181Z","iopub.execute_input":"2022-12-26T23:21:21.468801Z","iopub.status.idle":"2022-12-26T23:21:23.167001Z","shell.execute_reply.started":"2022-12-26T23:21:21.468749Z","shell.execute_reply":"2022-12-26T23:21:23.165654Z"},"trusted":true},"execution_count":null,"outputs":[]}]}