{"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-10-29T05:54:56.327529Z","iopub.execute_input":"2023-10-29T05:54:56.328160Z","iopub.status.idle":"2023-10-29T05:54:56.340303Z","shell.execute_reply.started":"2023-10-29T05:54:56.328110Z","shell.execute_reply":"2023-10-29T05:54:56.338477Z"},"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-10-29T05:54:56.344005Z","iopub.execute_input":"2023-10-29T05:54:56.344763Z","iopub.status.idle":"2023-10-29T05:54:56.356247Z","shell.execute_reply.started":"2023-10-29T05:54:56.344694Z","shell.execute_reply":"2023-10-29T05:54:56.354415Z"},"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-10-29T05:54:56.358635Z","iopub.execute_input":"2023-10-29T05:54:56.359741Z","iopub.status.idle":"2023-10-29T05:55:07.614731Z","shell.execute_reply.started":"2023-10-29T05:54:56.359681Z","shell.execute_reply":"2023-10-29T05:55:07.613396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:55:07.616320Z","iopub.execute_input":"2023-10-29T05:55:07.616740Z","iopub.status.idle":"2023-10-29T05:55:07.648391Z","shell.execute_reply.started":"2023-10-29T05:55:07.616702Z","shell.execute_reply":"2023-10-29T05:55:07.647219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:55:07.651365Z","iopub.execute_input":"2023-10-29T05:55:07.651763Z","iopub.status.idle":"2023-10-29T05:55:07.661883Z","shell.execute_reply.started":"2023-10-29T05:55:07.651729Z","shell.execute_reply":"2023-10-29T05:55:07.660630Z"},"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-10-29T05:55:07.663717Z","iopub.execute_input":"2023-10-29T05:55:07.664945Z","iopub.status.idle":"2023-10-29T05:55:07.802271Z","shell.execute_reply.started":"2023-10-29T05:55:07.664891Z","shell.execute_reply":"2023-10-29T05:55:07.800981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:55:07.803875Z","iopub.execute_input":"2023-10-29T05:55:07.804544Z","iopub.status.idle":"2023-10-29T05:55:07.819671Z","shell.execute_reply.started":"2023-10-29T05:55:07.804503Z","shell.execute_reply":"2023-10-29T05:55:07.818266Z"},"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-10-29T05:55:07.821250Z","iopub.execute_input":"2023-10-29T05:55:07.821664Z","iopub.status.idle":"2023-10-29T05:55:08.556384Z","shell.execute_reply.started":"2023-10-29T05:55:07.821628Z","shell.execute_reply":"2023-10-29T05:55:08.554781Z"},"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-10-29T05:55:08.558403Z","iopub.execute_input":"2023-10-29T05:55:08.558819Z","iopub.status.idle":"2023-10-29T05:55:08.565134Z","shell.execute_reply.started":"2023-10-29T05:55:08.558776Z","shell.execute_reply":"2023-10-29T05:55:08.563922Z"},"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-10-29T05:55:08.566850Z","iopub.execute_input":"2023-10-29T05:55:08.567317Z","iopub.status.idle":"2023-10-29T05:55:08.575677Z","shell.execute_reply.started":"2023-10-29T05:55:08.567282Z","shell.execute_reply":"2023-10-29T05:55:08.574490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.data.tail(2)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:55:08.577290Z","iopub.execute_input":"2023-10-29T05:55:08.577776Z","iopub.status.idle":"2023-10-29T05:55:08.610073Z","shell.execute_reply.started":"2023-10-29T05:55:08.577718Z","shell.execute_reply":"2023-10-29T05:55:08.608765Z"},"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-10-29T05:55:08.611586Z","iopub.execute_input":"2023-10-29T05:55:08.612348Z","iopub.status.idle":"2023-10-29T05:58:58.553532Z","shell.execute_reply.started":"2023-10-29T05:55:08.612295Z","shell.execute_reply":"2023-10-29T05:58:58.552230Z"},"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-10-29T05:58:58.555704Z","iopub.execute_input":"2023-10-29T05:58:58.556142Z","iopub.status.idle":"2023-10-29T05:58:58.566793Z","shell.execute_reply.started":"2023-10-29T05:58:58.556096Z","shell.execute_reply":"2023-10-29T05:58:58.565497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:58:58.574023Z","iopub.execute_input":"2023-10-29T05:58:58.574356Z","iopub.status.idle":"2023-10-29T05:58:58.581891Z","shell.execute_reply.started":"2023-10-29T05:58:58.574326Z","shell.execute_reply":"2023-10-29T05:58:58.580724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame([importance_], columns=X.columns, index=['values'])","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:58:58.583723Z","iopub.execute_input":"2023-10-29T05:58:58.584467Z","iopub.status.idle":"2023-10-29T05:58:58.611704Z","shell.execute_reply.started":"2023-10-29T05:58:58.584393Z","shell.execute_reply":"2023-10-29T05:58:58.610485Z"},"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-10-29T05:58:58.613797Z","iopub.execute_input":"2023-10-29T05:58:58.614264Z","iopub.status.idle":"2023-10-29T05:58:58.637769Z","shell.execute_reply.started":"2023-10-29T05:58:58.614218Z","shell.execute_reply":"2023-10-29T05:58:58.636493Z"},"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-10-29T05:58:58.639901Z","iopub.execute_input":"2023-10-29T05:58:58.640647Z","iopub.status.idle":"2023-10-29T05:58:58.651894Z","shell.execute_reply.started":"2023-10-29T05:58:58.640602Z","shell.execute_reply":"2023-10-29T05:58:58.650513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:58:58.653505Z","iopub.execute_input":"2023-10-29T05:58:58.653917Z","iopub.status.idle":"2023-10-29T05:58:59.164574Z","shell.execute_reply.started":"2023-10-29T05:58:58.653881Z","shell.execute_reply":"2023-10-29T05:58:59.163491Z"},"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-10-29T05:58:59.165908Z","iopub.execute_input":"2023-10-29T05:58:59.166242Z","iopub.status.idle":"2023-10-29T05:59:00.865116Z","shell.execute_reply.started":"2023-10-29T05:58:59.166212Z","shell.execute_reply":"2023-10-29T05:59:00.863453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\nX_test = test[X.columns]","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:59:00.866805Z","iopub.execute_input":"2023-10-29T05:59:00.867324Z","iopub.status.idle":"2023-10-29T05:59:00.896361Z","shell.execute_reply.started":"2023-10-29T05:59:00.867280Z","shell.execute_reply":"2023-10-29T05:59:00.893990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:59:00.899361Z","iopub.execute_input":"2023-10-29T05:59:00.900462Z","iopub.status.idle":"2023-10-29T05:59:12.183776Z","shell.execute_reply.started":"2023-10-29T05:59:00.900379Z","shell.execute_reply":"2023-10-29T05:59:12.182603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:59:12.189942Z","iopub.execute_input":"2023-10-29T05:59:12.193363Z","iopub.status.idle":"2023-10-29T05:59:12.202233Z","shell.execute_reply.started":"2023-10-29T05:59:12.193314Z","shell.execute_reply":"2023-10-29T05:59:12.201210Z"},"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-10-29T05:59:12.204019Z","iopub.execute_input":"2023-10-29T05:59:12.204405Z","iopub.status.idle":"2023-10-29T05:59:12.534423Z","shell.execute_reply.started":"2023-10-29T05:59:12.204369Z","shell.execute_reply":"2023-10-29T05:59:12.533327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:59:12.536017Z","iopub.execute_input":"2023-10-29T05:59:12.536500Z","iopub.status.idle":"2023-10-29T05:59:12.554524Z","shell.execute_reply.started":"2023-10-29T05:59:12.536450Z","shell.execute_reply":"2023-10-29T05:59:12.553029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission4.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T05:59:12.556516Z","iopub.execute_input":"2023-10-29T05:59:12.557028Z","iopub.status.idle":"2023-10-29T05:59:13.408868Z","shell.execute_reply.started":"2023-10-29T05:59:12.556979Z","shell.execute_reply":"2023-10-29T05:59:13.407862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}