{"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-03T10:07:38.242897Z","iopub.execute_input":"2022-09-03T10:07:38.243352Z","iopub.status.idle":"2022-09-03T10:07:38.274395Z","shell.execute_reply.started":"2022-09-03T10:07:38.243263Z","shell.execute_reply":"2022-09-03T10:07:38.273106Z"},"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-03T10:07:38.276244Z","iopub.execute_input":"2022-09-03T10:07:38.276578Z","iopub.status.idle":"2022-09-03T10:07:40.374239Z","shell.execute_reply.started":"2022-09-03T10:07:38.276547Z","shell.execute_reply":"2022-09-03T10:07:40.372828Z"},"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-03T10:09:41.992940Z","iopub.execute_input":"2022-09-03T10:09:41.993816Z","iopub.status.idle":"2022-09-03T10:09:56.660619Z","shell.execute_reply.started":"2022-09-03T10:09:41.993768Z","shell.execute_reply":"2022-09-03T10:09:56.659564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:09:56.662332Z","iopub.execute_input":"2022-09-03T10:09:56.662738Z","iopub.status.idle":"2022-09-03T10:09:57.084267Z","shell.execute_reply.started":"2022-09-03T10:09:56.662707Z","shell.execute_reply":"2022-09-03T10:09:57.083167Z"},"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-03T10:10:21.968926Z","iopub.execute_input":"2022-09-03T10:10:21.969450Z","iopub.status.idle":"2022-09-03T10:10:22.182181Z","shell.execute_reply.started":"2022-09-03T10:10:21.969402Z","shell.execute_reply":"2022-09-03T10:10:22.180585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:10:28.983848Z","iopub.execute_input":"2022-09-03T10:10:28.985135Z","iopub.status.idle":"2022-09-03T10:10:28.996217Z","shell.execute_reply.started":"2022-09-03T10:10:28.985089Z","shell.execute_reply":"2022-09-03T10:10:28.995156Z"},"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-09-03T10:11:33.683970Z","iopub.execute_input":"2022-09-03T10:11:33.685155Z","iopub.status.idle":"2022-09-03T10:11:34.591267Z","shell.execute_reply.started":"2022-09-03T10:11:33.685108Z","shell.execute_reply":"2022-09-03T10:11:34.590069Z"},"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': 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-03T10:12:35.345008Z","iopub.execute_input":"2022-09-03T10:12:35.345879Z","iopub.status.idle":"2022-09-03T10:12:35.351858Z","shell.execute_reply.started":"2022-09-03T10:12:35.345835Z","shell.execute_reply":"2022-09-03T10:12:35.350660Z"},"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-09-03T10:13:52.782278Z","iopub.execute_input":"2022-09-03T10:13:52.782667Z","iopub.status.idle":"2022-09-03T10:13:52.788627Z","shell.execute_reply.started":"2022-09-03T10:13:52.782635Z","shell.execute_reply":"2022-09-03T10:13:52.787359Z"},"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-09-03T10:14:48.988157Z","iopub.execute_input":"2022-09-03T10:14:48.989042Z","iopub.status.idle":"2022-09-03T10:18:06.070673Z","shell.execute_reply.started":"2022-09-03T10:14:48.989000Z","shell.execute_reply":"2022-09-03T10:18:06.069264Z"},"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-03T10:22:53.555934Z","iopub.execute_input":"2022-09-03T10:22:53.556354Z","iopub.status.idle":"2022-09-03T10:22:53.562018Z","shell.execute_reply.started":"2022-09-03T10:22:53.556317Z","shell.execute_reply":"2022-09-03T10:22:53.560926Z"},"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-09-03T10:23:16.511867Z","iopub.execute_input":"2022-09-03T10:23:16.512289Z","iopub.status.idle":"2022-09-03T10:23:16.526031Z","shell.execute_reply.started":"2022-09-03T10:23:16.512255Z","shell.execute_reply":"2022-09-03T10:23:16.524697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:23:50.172155Z","iopub.execute_input":"2022-09-03T10:23:50.172543Z","iopub.status.idle":"2022-09-03T10:23:50.694637Z","shell.execute_reply.started":"2022-09-03T10:23:50.172511Z","shell.execute_reply":"2022-09-03T10:23:50.693288Z"},"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-03T10:25:20.277341Z","iopub.execute_input":"2022-09-03T10:25:20.277773Z","iopub.status.idle":"2022-09-03T10:25:22.131318Z","shell.execute_reply.started":"2022-09-03T10:25:20.277721Z","shell.execute_reply":"2022-09-03T10:25:22.130220Z"},"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-03T10:25:56.837156Z","iopub.execute_input":"2022-09-03T10:25:56.837575Z","iopub.status.idle":"2022-09-03T10:25:56.867348Z","shell.execute_reply.started":"2022-09-03T10:25:56.837540Z","shell.execute_reply":"2022-09-03T10:25:56.865959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:26:07.433981Z","iopub.execute_input":"2022-09-03T10:26:07.434381Z","iopub.status.idle":"2022-09-03T10:26:18.917992Z","shell.execute_reply.started":"2022-09-03T10:26:07.434349Z","shell.execute_reply":"2022-09-03T10:26:18.916640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:26:18.919668Z","iopub.execute_input":"2022-09-03T10:26:18.920065Z","iopub.status.idle":"2022-09-03T10:26:18.928390Z","shell.execute_reply.started":"2022-09-03T10:26:18.920031Z","shell.execute_reply":"2022-09-03T10:26:18.926988Z"},"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-09-03T10:28:42.111214Z","iopub.execute_input":"2022-09-03T10:28:42.111617Z","iopub.status.idle":"2022-09-03T10:28:42.302593Z","shell.execute_reply.started":"2022-09-03T10:28:42.111584Z","shell.execute_reply":"2022-09-03T10:28:42.301508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:28:49.434534Z","iopub.execute_input":"2022-09-03T10:28:49.435151Z","iopub.status.idle":"2022-09-03T10:28:49.448818Z","shell.execute_reply.started":"2022-09-03T10:28:49.435101Z","shell.execute_reply":"2022-09-03T10:28:49.447731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T10:33:51.844067Z","iopub.execute_input":"2022-09-03T10:33:51.845241Z","iopub.status.idle":"2022-09-03T10:33:52.396405Z","shell.execute_reply.started":"2022-09-03T10:33:51.845185Z","shell.execute_reply":"2022-09-03T10:33:52.395402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}