{"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:12:36.844340Z","iopub.execute_input":"2022-09-05T10:12:36.846898Z","iopub.status.idle":"2022-09-05T10:12:36.880058Z","shell.execute_reply.started":"2022-09-05T10:12:36.846774Z","shell.execute_reply":"2022-09-05T10:12:36.878980Z"},"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:13:27.002259Z","iopub.execute_input":"2022-09-05T10:13:27.002666Z","iopub.status.idle":"2022-09-05T10:13:28.501889Z","shell.execute_reply.started":"2022-09-05T10:13:27.002634Z","shell.execute_reply":"2022-09-05T10:13:28.500651Z"},"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:13:44.130204Z","iopub.execute_input":"2022-09-05T10:13:44.130638Z","iopub.status.idle":"2022-09-05T10:14:00.675527Z","shell.execute_reply.started":"2022-09-05T10:13:44.130600Z","shell.execute_reply":"2022-09-05T10:14:00.674106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:14:10.965443Z","iopub.execute_input":"2022-09-05T10:14:10.966037Z","iopub.status.idle":"2022-09-05T10:14:11.623631Z","shell.execute_reply.started":"2022-09-05T10:14:10.965987Z","shell.execute_reply":"2022-09-05T10:14:11.622357Z"},"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:15:18.821730Z","iopub.execute_input":"2022-09-05T10:15:18.824226Z","iopub.status.idle":"2022-09-05T10:15:19.058766Z","shell.execute_reply.started":"2022-09-05T10:15:18.824153Z","shell.execute_reply":"2022-09-05T10:15:19.057539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:15:30.159254Z","iopub.execute_input":"2022-09-05T10:15:30.159669Z","iopub.status.idle":"2022-09-05T10:15:30.169497Z","shell.execute_reply.started":"2022-09-05T10:15:30.159635Z","shell.execute_reply":"2022-09-05T10:15:30.167984Z"},"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-05T10:15:38.975851Z","iopub.execute_input":"2022-09-05T10:15:38.976327Z","iopub.status.idle":"2022-09-05T10:15:39.972178Z","shell.execute_reply.started":"2022-09-05T10:15:38.976290Z","shell.execute_reply":"2022-09-05T10:15:39.970695Z"},"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:17:31.847225Z","iopub.execute_input":"2022-09-05T10:17:31.847770Z","iopub.status.idle":"2022-09-05T10:17:31.855548Z","shell.execute_reply.started":"2022-09-05T10:17:31.847730Z","shell.execute_reply":"2022-09-05T10:17:31.853709Z"},"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-05T10:17:42.948637Z","iopub.execute_input":"2022-09-05T10:17:42.949107Z","iopub.status.idle":"2022-09-05T10:17:42.955597Z","shell.execute_reply.started":"2022-09-05T10:17:42.949067Z","shell.execute_reply":"2022-09-05T10:17:42.954250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"model = lgbm.train(params, train_dataset, 100, valid_sets=valid_dataset, verbose_eval=10, early_stopping_rounds=10)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:18:10.601878Z","iopub.execute_input":"2022-09-05T10:18:10.602318Z","iopub.status.idle":"2022-09-05T10:18:33.771081Z","shell.execute_reply.started":"2022-09-05T10:18:10.602285Z","shell.execute_reply":"2022-09-05T10:18:33.770001Z"},"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:18:42.788863Z","iopub.execute_input":"2022-09-05T10:18:42.789296Z","iopub.status.idle":"2022-09-05T10:18:42.794817Z","shell.execute_reply.started":"2022-09-05T10:18:42.789262Z","shell.execute_reply":"2022-09-05T10:18:42.793877Z"},"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-05T10:18:54.670721Z","iopub.execute_input":"2022-09-05T10:18:54.671950Z","iopub.status.idle":"2022-09-05T10:18:54.689992Z","shell.execute_reply.started":"2022-09-05T10:18:54.671888Z","shell.execute_reply":"2022-09-05T10:18:54.688663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:19:00.710084Z","iopub.execute_input":"2022-09-05T10:19:00.710887Z","iopub.status.idle":"2022-09-05T10:19:01.293343Z","shell.execute_reply.started":"2022-09-05T10:19:00.710845Z","shell.execute_reply":"2022-09-05T10:19:01.291760Z"},"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:19:17.750421Z","iopub.execute_input":"2022-09-05T10:19:17.750840Z","iopub.status.idle":"2022-09-05T10:19:20.214235Z","shell.execute_reply.started":"2022-09-05T10:19:17.750806Z","shell.execute_reply":"2022-09-05T10:19:20.212989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:19:38.841068Z","iopub.execute_input":"2022-09-05T10:19:38.844087Z","iopub.status.idle":"2022-09-05T10:19:38.894478Z","shell.execute_reply.started":"2022-09-05T10:19:38.844031Z","shell.execute_reply":"2022-09-05T10:19:38.893267Z"},"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:20:40.967506Z","iopub.execute_input":"2022-09-05T10:20:40.967963Z","iopub.status.idle":"2022-09-05T10:20:40.996363Z","shell.execute_reply.started":"2022-09-05T10:20:40.967910Z","shell.execute_reply":"2022-09-05T10:20:40.995137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:20:46.777027Z","iopub.execute_input":"2022-09-05T10:20:46.777689Z","iopub.status.idle":"2022-09-05T10:20:47.526389Z","shell.execute_reply.started":"2022-09-05T10:20:46.777640Z","shell.execute_reply":"2022-09-05T10:20:47.525342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:20:53.527741Z","iopub.execute_input":"2022-09-05T10:20:53.528171Z","iopub.status.idle":"2022-09-05T10:20:53.537261Z","shell.execute_reply.started":"2022-09-05T10:20:53.528137Z","shell.execute_reply":"2022-09-05T10:20:53.535359Z"},"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-05T10:21:10.154336Z","iopub.execute_input":"2022-09-05T10:21:10.154817Z","iopub.status.idle":"2022-09-05T10:21:10.361618Z","shell.execute_reply.started":"2022-09-05T10:21:10.154775Z","shell.execute_reply":"2022-09-05T10:21:10.360480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:21:13.865535Z","iopub.execute_input":"2022-09-05T10:21:13.866047Z","iopub.status.idle":"2022-09-05T10:21:13.883725Z","shell.execute_reply.started":"2022-09-05T10:21:13.866004Z","shell.execute_reply":"2022-09-05T10:21:13.882391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:21:26.463938Z","iopub.execute_input":"2022-09-05T10:21:26.464361Z","iopub.status.idle":"2022-09-05T10:21:27.020976Z","shell.execute_reply.started":"2022-09-05T10:21:26.464326Z","shell.execute_reply":"2022-09-05T10:21:27.019614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}