{"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":"2022-11-08T12:03:30.086167Z","iopub.execute_input":"2022-11-08T12:03:30.087223Z","iopub.status.idle":"2022-11-08T12:03:30.095546Z","shell.execute_reply.started":"2022-11-08T12:03:30.087170Z","shell.execute_reply":"2022-11-08T12:03:30.094176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport lightgbm as lgbm","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:03:13.776054Z","iopub.execute_input":"2022-11-08T12:03:13.776632Z","iopub.status.idle":"2022-11-08T12:03:16.102626Z","shell.execute_reply.started":"2022-11-08T12:03:13.776525Z","shell.execute_reply":"2022-11-08T12:03:16.101363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/financemachinelearning/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:03:49.411003Z","iopub.execute_input":"2022-11-08T12:03:49.411371Z","iopub.status.idle":"2022-11-08T12:04:07.166557Z","shell.execute_reply.started":"2022-11-08T12:03:49.411342Z","shell.execute_reply":"2022-11-08T12:04:07.164903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:04:07.168378Z","iopub.execute_input":"2022-11-08T12:04:07.168759Z","iopub.status.idle":"2022-11-08T12:04:07.203827Z","shell.execute_reply.started":"2022-11-08T12:04:07.168727Z","shell.execute_reply":"2022-11-08T12:04:07.202668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split X, y","metadata":{}},{"cell_type":"code","source":"X = train.drop('r05_fwd', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:04:47.649125Z","iopub.execute_input":"2022-11-08T12:04:47.650481Z","iopub.status.idle":"2022-11-08T12:04:47.715213Z","shell.execute_reply.started":"2022-11-08T12:04:47.650419Z","shell.execute_reply":"2022-11-08T12:04:47.713678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:04:49.188001Z","iopub.execute_input":"2022-11-08T12:04:49.188643Z","iopub.status.idle":"2022-11-08T12:04:49.199163Z","shell.execute_reply.started":"2022-11-08T12:04:49.188586Z","shell.execute_reply":"2022-11-08T12:04:49.198273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split Train, Valid Data","metadata":{}},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=.3)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:05:10.589530Z","iopub.execute_input":"2022-11-08T12:05:10.589916Z","iopub.status.idle":"2022-11-08T12:05:11.459417Z","shell.execute_reply.started":"2022-11-08T12:05:10.589884Z","shell.execute_reply":"2022-11-08T12:05:11.458169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Parameter Setting","metadata":{}},{"cell_type":"code","source":"params = {'learning_rate': 0.01, \n          'max_depth': 16, \n          'boosting': 'gbdt', \n          'objective': 'regression', \n          'metric': 'mse', \n          'is_training_metric': True, \n          'num_leaves': 164, \n          'feature_fraction': 0.9, \n          'bagging_fraction': 0.7, \n          'bagging_freq': 4, \n          'seed':2018}","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:06:44.222712Z","iopub.execute_input":"2022-11-08T12:06:44.223115Z","iopub.status.idle":"2022-11-08T12:06:44.230224Z","shell.execute_reply.started":"2022-11-08T12:06:44.223084Z","shell.execute_reply":"2022-11-08T12:06:44.228621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert to LGBM data format","metadata":{}},{"cell_type":"code","source":"train_dataset = lgbm.Dataset(X_train, label=y_train)\nvalid_dataset = lgbm.Dataset(X_valid, label=y_valid)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:06:50.742484Z","iopub.execute_input":"2022-11-08T12:06:50.742908Z","iopub.status.idle":"2022-11-08T12:06:50.748781Z","shell.execute_reply.started":"2022-11-08T12:06:50.742874Z","shell.execute_reply":"2022-11-08T12:06:50.747839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.data.tail(2)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:09:27.331787Z","iopub.execute_input":"2022-11-08T12:09:27.332223Z","iopub.status.idle":"2022-11-08T12:09:27.359432Z","shell.execute_reply.started":"2022-11-08T12:09:27.332188Z","shell.execute_reply":"2022-11-08T12:09:27.358451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"model = lgbm.train(params, train_dataset, 1000, valid_sets=valid_dataset, verbose_eval=100, early_stopping_rounds=100)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:11:02.864884Z","iopub.execute_input":"2022-11-08T12:11:02.866129Z","iopub.status.idle":"2022-11-08T12:13:57.577832Z","shell.execute_reply.started":"2022-11-08T12:11:02.866080Z","shell.execute_reply":"2022-11-08T12:13:57.576183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Importance","metadata":{}},{"cell_type":"code","source":"importance_ = model.feature_importance()\nimportance_","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:14:33.326223Z","iopub.execute_input":"2022-11-08T12:14:33.326703Z","iopub.status.idle":"2022-11-08T12:14:33.337155Z","shell.execute_reply.started":"2022-11-08T12:14:33.326665Z","shell.execute_reply":"2022-11-08T12:14:33.335252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:14:50.305864Z","iopub.execute_input":"2022-11-08T12:14:50.306281Z","iopub.status.idle":"2022-11-08T12:14:50.315387Z","shell.execute_reply.started":"2022-11-08T12:14:50.306250Z","shell.execute_reply":"2022-11-08T12:14:50.313521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame([importance_], columns=X.columns, index=['values'])","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:15:35.369934Z","iopub.execute_input":"2022-11-08T12:15:35.370408Z","iopub.status.idle":"2022-11-08T12:15:35.391358Z","shell.execute_reply.started":"2022-11-08T12:15:35.370371Z","shell.execute_reply":"2022-11-08T12:15:35.390290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame([importance_], columns=X.columns, index=['values']).T.sort_values('values')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:16:05.753512Z","iopub.execute_input":"2022-11-08T12:16:05.753965Z","iopub.status.idle":"2022-11-08T12:16:05.769234Z","shell.execute_reply.started":"2022-11-08T12:16:05.753929Z","shell.execute_reply":"2022-11-08T12:16:05.767744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance = pd.DataFrame([importance_], columns=X.columns, index=['values']).T.sort_values('values')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:14:25.915024Z","iopub.execute_input":"2022-11-08T12:14:25.915461Z","iopub.status.idle":"2022-11-08T12:14:25.932265Z","shell.execute_reply.started":"2022-11-08T12:14:25.915423Z","shell.execute_reply":"2022-11-08T12:14:25.931014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:16:30.469193Z","iopub.execute_input":"2022-11-08T12:16:30.469713Z","iopub.status.idle":"2022-11-08T12:16:30.868141Z","shell.execute_reply.started":"2022-11-08T12:16:30.469667Z","shell.execute_reply":"2022-11-08T12:16:30.867311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/financemachinelearning/test.csv')\ntest.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:17:14.518533Z","iopub.execute_input":"2022-11-08T12:17:14.519007Z","iopub.status.idle":"2022-11-08T12:17:15.526402Z","shell.execute_reply.started":"2022-11-08T12:17:14.518973Z","shell.execute_reply":"2022-11-08T12:17:15.524769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\nX_test = test[X.columns]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:18:48.492387Z","iopub.execute_input":"2022-11-08T12:18:48.492794Z","iopub.status.idle":"2022-11-08T12:18:48.505171Z","shell.execute_reply.started":"2022-11-08T12:18:48.492763Z","shell.execute_reply":"2022-11-08T12:18:48.503739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:18:49.368319Z","iopub.execute_input":"2022-11-08T12:18:49.368911Z","iopub.status.idle":"2022-11-08T12:18:57.329498Z","shell.execute_reply.started":"2022-11-08T12:18:49.368877Z","shell.execute_reply":"2022-11-08T12:18:57.328721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:18:57.333791Z","iopub.execute_input":"2022-11-08T12:18:57.335033Z","iopub.status.idle":"2022-11-08T12:18:57.342676Z","shell.execute_reply.started":"2022-11-08T12:18:57.335000Z","shell.execute_reply":"2022-11-08T12:18:57.341078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(zip(id_, predict), columns=['id', 'r05_fwd'])","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:19:17.948105Z","iopub.execute_input":"2022-11-08T12:19:17.948505Z","iopub.status.idle":"2022-11-08T12:19:18.096067Z","shell.execute_reply.started":"2022-11-08T12:19:17.948470Z","shell.execute_reply":"2022-11-08T12:19:18.094555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:19:21.307692Z","iopub.execute_input":"2022-11-08T12:19:21.308208Z","iopub.status.idle":"2022-11-08T12:19:21.324261Z","shell.execute_reply.started":"2022-11-08T12:19:21.308170Z","shell.execute_reply":"2022-11-08T12:19:21.322680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission4.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:19:33.722328Z","iopub.execute_input":"2022-11-08T12:19:33.722765Z","iopub.status.idle":"2022-11-08T12:19:34.129490Z","shell.execute_reply.started":"2022-11-08T12:19:33.722726Z","shell.execute_reply":"2022-11-08T12:19:34.127927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}