{"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":"2023-08-11T06:09:34.443807Z","iopub.execute_input":"2023-08-11T06:09:34.444770Z","iopub.status.idle":"2023-08-11T06:09:34.480741Z","shell.execute_reply.started":"2023-08-11T06:09:34.444643Z","shell.execute_reply":"2023-08-11T06:09:34.479550Z"},"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":"2023-08-11T06:09:47.061212Z","iopub.execute_input":"2023-08-11T06:09:47.061668Z","iopub.status.idle":"2023-08-11T06:09:48.739435Z","shell.execute_reply.started":"2023-08-11T06:09:47.061630Z","shell.execute_reply":"2023-08-11T06:09:48.738291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/financemachinelearning/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:10:02.585560Z","iopub.execute_input":"2023-08-11T06:10:02.586128Z","iopub.status.idle":"2023-08-11T06:10:21.067113Z","shell.execute_reply.started":"2023-08-11T06:10:02.586077Z","shell.execute_reply":"2023-08-11T06:10:21.066041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:10:24.953484Z","iopub.execute_input":"2023-08-11T06:10:24.953913Z","iopub.status.idle":"2023-08-11T06:10:24.994289Z","shell.execute_reply.started":"2023-08-11T06:10:24.953877Z","shell.execute_reply":"2023-08-11T06:10:24.993039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:12:23.664459Z","iopub.execute_input":"2023-08-11T06:12:23.666189Z","iopub.status.idle":"2023-08-11T06:12:23.674450Z","shell.execute_reply.started":"2023-08-11T06:12:23.666134Z","shell.execute_reply":"2023-08-11T06:12:23.673243Z"},"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":"2023-08-11T06:13:14.217600Z","iopub.execute_input":"2023-08-11T06:13:14.218681Z","iopub.status.idle":"2023-08-11T06:13:14.346951Z","shell.execute_reply.started":"2023-08-11T06:13:14.218615Z","shell.execute_reply":"2023-08-11T06:13:14.345190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:13:22.246547Z","iopub.execute_input":"2023-08-11T06:13:22.247053Z","iopub.status.idle":"2023-08-11T06:13:22.258135Z","shell.execute_reply.started":"2023-08-11T06:13:22.247015Z","shell.execute_reply":"2023-08-11T06:13:22.256438Z"},"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":"2023-08-11T06:13:34.366529Z","iopub.execute_input":"2023-08-11T06:13:34.367024Z","iopub.status.idle":"2023-08-11T06:13:35.284952Z","shell.execute_reply.started":"2023-08-11T06:13:34.366985Z","shell.execute_reply":"2023-08-11T06:13:35.283754Z"},"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":"2023-08-11T06:14:14.184233Z","iopub.execute_input":"2023-08-11T06:14:14.184710Z","iopub.status.idle":"2023-08-11T06:14:14.192619Z","shell.execute_reply.started":"2023-08-11T06:14:14.184673Z","shell.execute_reply":"2023-08-11T06:14:14.191080Z"},"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":"2023-08-11T06:14:23.038124Z","iopub.execute_input":"2023-08-11T06:14:23.038677Z","iopub.status.idle":"2023-08-11T06:14:23.045899Z","shell.execute_reply.started":"2023-08-11T06:14:23.038631Z","shell.execute_reply":"2023-08-11T06:14:23.044470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.data.tail(2)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:14:29.904109Z","iopub.execute_input":"2023-08-11T06:14:29.904793Z","iopub.status.idle":"2023-08-11T06:14:29.934398Z","shell.execute_reply.started":"2023-08-11T06:14:29.904754Z","shell.execute_reply":"2023-08-11T06:14:29.932996Z"},"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":"2023-08-11T06:15:10.040789Z","iopub.execute_input":"2023-08-11T06:15:10.041331Z","iopub.status.idle":"2023-08-11T06:19:06.233620Z","shell.execute_reply.started":"2023-08-11T06:15:10.041293Z","shell.execute_reply":"2023-08-11T06:19:06.232212Z"},"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":"2023-08-11T06:19:06.236210Z","iopub.execute_input":"2023-08-11T06:19:06.236589Z","iopub.status.idle":"2023-08-11T06:19:06.258443Z","shell.execute_reply.started":"2023-08-11T06:19:06.236553Z","shell.execute_reply":"2023-08-11T06:19:06.257102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:06.259818Z","iopub.execute_input":"2023-08-11T06:19:06.260169Z","iopub.status.idle":"2023-08-11T06:19:06.267268Z","shell.execute_reply.started":"2023-08-11T06:19:06.260138Z","shell.execute_reply":"2023-08-11T06:19:06.265904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame([importance_], columns=X.columns, index=['values'])","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:06.270449Z","iopub.execute_input":"2023-08-11T06:19:06.270820Z","iopub.status.idle":"2023-08-11T06:19:06.300385Z","shell.execute_reply.started":"2023-08-11T06:19:06.270768Z","shell.execute_reply":"2023-08-11T06:19:06.299358Z"},"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":"2023-08-11T06:19:06.302030Z","iopub.execute_input":"2023-08-11T06:19:06.303120Z","iopub.status.idle":"2023-08-11T06:19:06.326204Z","shell.execute_reply.started":"2023-08-11T06:19:06.303084Z","shell.execute_reply":"2023-08-11T06:19:06.324790Z"},"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":"2023-08-11T06:19:06.328234Z","iopub.execute_input":"2023-08-11T06:19:06.329022Z","iopub.status.idle":"2023-08-11T06:19:06.341598Z","shell.execute_reply.started":"2023-08-11T06:19:06.328983Z","shell.execute_reply":"2023-08-11T06:19:06.340354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:06.343078Z","iopub.execute_input":"2023-08-11T06:19:06.343912Z","iopub.status.idle":"2023-08-11T06:19:06.903375Z","shell.execute_reply.started":"2023-08-11T06:19:06.343865Z","shell.execute_reply":"2023-08-11T06:19:06.902176Z"},"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":"2023-08-11T06:19:06.905072Z","iopub.execute_input":"2023-08-11T06:19:06.909186Z","iopub.status.idle":"2023-08-11T06:19:09.139973Z","shell.execute_reply.started":"2023-08-11T06:19:06.909148Z","shell.execute_reply":"2023-08-11T06:19:09.138738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\nX_test = test[X.columns]","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:09.141244Z","iopub.execute_input":"2023-08-11T06:19:09.141559Z","iopub.status.idle":"2023-08-11T06:19:09.165268Z","shell.execute_reply.started":"2023-08-11T06:19:09.141529Z","shell.execute_reply":"2023-08-11T06:19:09.164145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:09.168670Z","iopub.execute_input":"2023-08-11T06:19:09.169036Z","iopub.status.idle":"2023-08-11T06:19:21.872761Z","shell.execute_reply.started":"2023-08-11T06:19:09.169002Z","shell.execute_reply":"2023-08-11T06:19:21.871712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2023-08-11T06:19:21.874091Z","iopub.execute_input":"2023-08-11T06:19:21.874680Z","iopub.status.idle":"2023-08-11T06:19:21.882522Z","shell.execute_reply.started":"2023-08-11T06:19:21.874628Z","shell.execute_reply":"2023-08-11T06:19:21.881291Z"},"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":"2023-08-11T06:19:21.886045Z","iopub.execute_input":"2023-08-11T06:19:21.886973Z","iopub.status.idle":"2023-08-11T06:19:22.203670Z","shell.execute_reply.started":"2023-08-11T06:19:21.886926Z","shell.execute_reply":"2023-08-11T06:19:22.202246Z"},"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.30817Z","shell.execute_reply":"2022-11-08T12:19:21.32268Z"},"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.12949Z","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":[]}]}