{"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:26:06.431693Z","iopub.execute_input":"2022-09-05T10:26:06.432110Z","iopub.status.idle":"2022-09-05T10:26:06.460769Z","shell.execute_reply.started":"2022-09-05T10:26:06.432026Z","shell.execute_reply":"2022-09-05T10:26:06.459743Z"},"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:26:06.463311Z","iopub.execute_input":"2022-09-05T10:26:06.464203Z","iopub.status.idle":"2022-09-05T10:26:08.669868Z","shell.execute_reply.started":"2022-09-05T10:26:06.464157Z","shell.execute_reply":"2022-09-05T10:26:08.668059Z"},"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:26:08.672072Z","iopub.execute_input":"2022-09-05T10:26:08.672460Z","iopub.status.idle":"2022-09-05T10:26:23.079762Z","shell.execute_reply.started":"2022-09-05T10:26:08.672420Z","shell.execute_reply":"2022-09-05T10:26:23.078873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:26:23.082116Z","iopub.execute_input":"2022-09-05T10:26:23.082536Z","iopub.status.idle":"2022-09-05T10:26:23.519930Z","shell.execute_reply.started":"2022-09-05T10:26:23.082494Z","shell.execute_reply":"2022-09-05T10:26:23.518898Z"},"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:26:52.696274Z","iopub.execute_input":"2022-09-05T10:26:52.696754Z","iopub.status.idle":"2022-09-05T10:26:52.877814Z","shell.execute_reply.started":"2022-09-05T10:26:52.696714Z","shell.execute_reply":"2022-09-05T10:26:52.876311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:27:00.395762Z","iopub.execute_input":"2022-09-05T10:27:00.396574Z","iopub.status.idle":"2022-09-05T10:27:00.404574Z","shell.execute_reply.started":"2022-09-05T10:27:00.396539Z","shell.execute_reply":"2022-09-05T10:27:00.403376Z"},"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:27:02.165661Z","iopub.execute_input":"2022-09-05T10:27:02.166456Z","iopub.status.idle":"2022-09-05T10:27:02.918431Z","shell.execute_reply.started":"2022-09-05T10:27:02.166405Z","shell.execute_reply":"2022-09-05T10:27:02.917233Z"},"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-05T10:27:03.644366Z","iopub.execute_input":"2022-09-05T10:27:03.644766Z","iopub.status.idle":"2022-09-05T10:27:03.650449Z","shell.execute_reply.started":"2022-09-05T10:27:03.644733Z","shell.execute_reply":"2022-09-05T10:27:03.649389Z"},"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:27:05.441412Z","iopub.execute_input":"2022-09-05T10:27:05.441762Z","iopub.status.idle":"2022-09-05T10:27:05.447023Z","shell.execute_reply.started":"2022-09-05T10:27:05.441735Z","shell.execute_reply":"2022-09-05T10:27:05.445907Z"},"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-05T10:27:06.691362Z","iopub.execute_input":"2022-09-05T10:27:06.691718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Importance","metadata":{}},{"cell_type":"code","source":"importance_ = model.feature_importance()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance = pd.DataFrame([importance_], columns=X.columns, index=['values']).T.sort_values('values')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\nX_test = test[X.columns]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(zip(id_, predict), columns=['id', 'r05_fwd'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}