{"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-09-05T10:47:43.065586Z","iopub.execute_input":"2022-09-05T10:47:43.065959Z","iopub.status.idle":"2022-09-05T10:47:43.072978Z","shell.execute_reply.started":"2022-09-05T10:47:43.065929Z","shell.execute_reply":"2022-09-05T10:47:43.071922Z"},"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-09-05T10:49:18.027473Z","iopub.execute_input":"2022-09-05T10:49:18.027815Z","iopub.status.idle":"2022-09-05T10:49:19.305108Z","shell.execute_reply.started":"2022-09-05T10:49:18.027786Z","shell.execute_reply":"2022-09-05T10:49:19.304192Z"},"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-09-05T10:49:00.237604Z","iopub.execute_input":"2022-09-05T10:49:00.238658Z","iopub.status.idle":"2022-09-05T10:49:11.170648Z","shell.execute_reply.started":"2022-09-05T10:49:00.238600Z","shell.execute_reply":"2022-09-05T10:49:11.169770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:49:21.036916Z","iopub.execute_input":"2022-09-05T10:49:21.038944Z","iopub.status.idle":"2022-09-05T10:49:21.213891Z","shell.execute_reply.started":"2022-09-05T10:49:21.038734Z","shell.execute_reply":"2022-09-05T10:49:21.212843Z"},"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-09-05T10:50:06.949551Z","iopub.execute_input":"2022-09-05T10:50:06.950389Z","iopub.status.idle":"2022-09-05T10:50:07.025074Z","shell.execute_reply.started":"2022-09-05T10:50:06.950355Z","shell.execute_reply":"2022-09-05T10:50:07.023969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:50:37.482894Z","iopub.execute_input":"2022-09-05T10:50:37.483332Z","iopub.status.idle":"2022-09-05T10:50:37.490637Z","shell.execute_reply.started":"2022-09-05T10:50:37.483296Z","shell.execute_reply":"2022-09-05T10:50:37.489851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:50:42.259783Z","iopub.execute_input":"2022-09-05T10:50:42.260187Z","iopub.status.idle":"2022-09-05T10:50:42.350983Z","shell.execute_reply.started":"2022-09-05T10:50:42.260155Z","shell.execute_reply":"2022-09-05T10:50:42.349522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split Train, Valid Data","metadata":{}},{"cell_type":"code","source":"# 학습과 검증 데이터 세트 7:3 분리\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:52:17.553955Z","iopub.execute_input":"2022-09-05T10:52:17.555344Z","iopub.status.idle":"2022-09-05T10:52:18.227811Z","shell.execute_reply.started":"2022-09-05T10:52:17.555300Z","shell.execute_reply":"2022-09-05T10:52:18.226719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Parameter Setting","metadata":{}},{"cell_type":"code","source":"params = {'learning_rate': 0.1, \n          'max_depth': 16, \n          'boosting': 'gbdt', \n          'objective': 'regression', \n          'metric': 'mse', \n          'is_training_metric': True, \n          'num_leaves': 144, \n          'feature_fraction': 0.9, \n          'bagging_fraction': 0.7, \n          'bagging_freq': 5, \n          'seed':2018}","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:52:55.452826Z","iopub.execute_input":"2022-09-05T10:52:55.453237Z","iopub.status.idle":"2022-09-05T10:52:55.459912Z","shell.execute_reply.started":"2022-09-05T10:52:55.453205Z","shell.execute_reply":"2022-09-05T10:52:55.458018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09-05T10:53:48.480263Z","iopub.execute_input":"2022-09-05T10:53:48.480630Z","iopub.status.idle":"2022-09-05T10:53:48.486446Z","shell.execute_reply.started":"2022-09-05T10:53:48.480602Z","shell.execute_reply":"2022-09-05T10:53:48.485079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train data","metadata":{}},{"cell_type":"code","source":"model = lgbm.train(params, train_dataset, 100, valid_sets=valid_dataset,\n                  verbose_eval=10, early_stopping_rounds=10)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:54:53.427357Z","iopub.execute_input":"2022-09-05T10:54:53.427745Z","iopub.status.idle":"2022-09-05T10:55:12.971555Z","shell.execute_reply.started":"2022-09-05T10:54:53.427715Z","shell.execute_reply":"2022-09-05T10:55:12.970861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Feature Importance","metadata":{}},{"cell_type":"code","source":"importance_ = model.feature_importance()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:55:49.178018Z","iopub.execute_input":"2022-09-05T10:55:49.178411Z","iopub.status.idle":"2022-09-05T10:55:49.183416Z","shell.execute_reply.started":"2022-09-05T10:55:49.178381Z","shell.execute_reply":"2022-09-05T10:55:49.182346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance = pd.DataFrame([importance_], columns=X.columns,\n                         index=['values']).T.sort_values('values')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:57:00.787722Z","iopub.execute_input":"2022-09-05T10:57:00.788076Z","iopub.status.idle":"2022-09-05T10:57:00.798158Z","shell.execute_reply.started":"2022-09-05T10:57:00.788021Z","shell.execute_reply":"2022-09-05T10:57:00.797115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16,9))","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:57:26.450174Z","iopub.execute_input":"2022-09-05T10:57:26.450546Z","iopub.status.idle":"2022-09-05T10:57:26.833920Z","shell.execute_reply.started":"2022-09-05T10:57:26.450513Z","shell.execute_reply":"2022-09-05T10:57:26.833077Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:58:18.156491Z","iopub.execute_input":"2022-09-05T10:58:18.156911Z","iopub.status.idle":"2022-09-05T10:58:19.536869Z","shell.execute_reply.started":"2022-09-05T10:58:18.156874Z","shell.execute_reply":"2022-09-05T10:58:19.535944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:58:22.549350Z","iopub.execute_input":"2022-09-05T10:58:22.550899Z","iopub.status.idle":"2022-09-05T10:58:22.579972Z","shell.execute_reply.started":"2022-09-05T10:58:22.550849Z","shell.execute_reply":"2022-09-05T10:58:22.579016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\nX_test = test[X.columns]","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:59:31.960389Z","iopub.execute_input":"2022-09-05T10:59:31.960774Z","iopub.status.idle":"2022-09-05T10:59:31.979460Z","shell.execute_reply.started":"2022-09-05T10:59:31.960741Z","shell.execute_reply":"2022-09-05T10:59:31.978700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:59:42.939765Z","iopub.execute_input":"2022-09-05T10:59:42.940168Z","iopub.status.idle":"2022-09-05T10:59:43.418694Z","shell.execute_reply.started":"2022-09-05T10:59:42.940134Z","shell.execute_reply":"2022-09-05T10:59:43.417901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:59:47.356968Z","iopub.execute_input":"2022-09-05T10:59:47.357779Z","iopub.status.idle":"2022-09-05T10:59:47.364592Z","shell.execute_reply.started":"2022-09-05T10:59:47.357746Z","shell.execute_reply":"2022-09-05T10:59:47.363735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(zip(id_, pred), columns=['id', 'r05_fwd'])","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:00:20.311975Z","iopub.execute_input":"2022-09-05T11:00:20.312395Z","iopub.status.idle":"2022-09-05T11:00:20.467864Z","shell.execute_reply.started":"2022-09-05T11:00:20.312363Z","shell.execute_reply":"2022-09-05T11:00:20.466609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:00:34.175541Z","iopub.execute_input":"2022-09-05T11:00:34.175909Z","iopub.status.idle":"2022-09-05T11:00:34.190801Z","shell.execute_reply.started":"2022-09-05T11:00:34.175880Z","shell.execute_reply":"2022-09-05T11:00:34.189693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:00:56.380821Z","iopub.execute_input":"2022-09-05T11:00:56.381259Z","iopub.status.idle":"2022-09-05T11:00:56.793244Z","shell.execute_reply.started":"2022-09-05T11:00:56.381211Z","shell.execute_reply":"2022-09-05T11:00:56.791727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}