{"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-05T11:45:44.142718Z","iopub.execute_input":"2022-09-05T11:45:44.143218Z","iopub.status.idle":"2022-09-05T11:45:44.165720Z","shell.execute_reply.started":"2022-09-05T11:45:44.143177Z","shell.execute_reply":"2022-09-05T11:45:44.164518Z"},"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-05T11:45:44.170983Z","iopub.execute_input":"2022-09-05T11:45:44.176725Z","iopub.status.idle":"2022-09-05T11:45:44.193847Z","shell.execute_reply.started":"2022-09-05T11:45:44.176662Z","shell.execute_reply":"2022-09-05T11:45:44.189409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RandomForest Regressor - 수익률 예측(분류 x, 회귀선을 통한 예측)","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\n\nfrom pathlib import Path\nimport os, sys\nimport numpy as np\n\nfrom numpy.random import choice\nimport pandas as pd\nfrom scipy.stats import spearmanr\n\nfrom sklearn.ensemble import RandomForestRegressor, RandomForestClassifier\nfrom sklearn.model_selection import GridSearchCV, cross_val_score\nfrom sklearn.metrics import make_scorer\nimport joblib\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:45:44.197092Z","iopub.execute_input":"2022-09-05T11:45:44.198616Z","iopub.status.idle":"2022-09-05T11:45:44.225187Z","shell.execute_reply.started":"2022-09-05T11:45:44.198554Z","shell.execute_reply":"2022-09-05T11:45:44.221522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rank_correl(y, y_pred):\n    return spearmanr(y, y_pred)[0]\n\nic = make_scorer(rank_correl)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:45:44.227207Z","iopub.execute_input":"2022-09-05T11:45:44.227795Z","iopub.status.idle":"2022-09-05T11:45:44.237193Z","shell.execute_reply.started":"2022-09-05T11:45:44.227744Z","shell.execute_reply":"2022-09-05T11:45:44.236094Z"},"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-05T11:45:44.240283Z","iopub.execute_input":"2022-09-05T11:45:44.241421Z","iopub.status.idle":"2022-09-05T11:46:10.792344Z","shell.execute_reply.started":"2022-09-05T11:45:44.241372Z","shell.execute_reply":"2022-09-05T11:46:10.791179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:46:10.794158Z","iopub.execute_input":"2022-09-05T11:46:10.794917Z","iopub.status.idle":"2022-09-05T11:46:11.500783Z","shell.execute_reply.started":"2022-09-05T11:46:10.794870Z","shell.execute_reply":"2022-09-05T11:46:11.499409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('r05_fwd', axis=1)\ny = train[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:46:11.502679Z","iopub.execute_input":"2022-09-05T11:46:11.503449Z","iopub.status.idle":"2022-09-05T11:46:11.716109Z","shell.execute_reply.started":"2022-09-05T11:46:11.503402Z","shell.execute_reply":"2022-09-05T11:46:11.714774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습과 검증 데이터 세트 8:2 분리\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:46:11.719510Z","iopub.execute_input":"2022-09-05T11:46:11.720004Z","iopub.status.idle":"2022-09-05T11:46:13.765653Z","shell.execute_reply.started":"2022-09-05T11:46:11.719958Z","shell.execute_reply":"2022-09-05T11:46:13.764369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"parameter setting","metadata":{}},{"cell_type":"code","source":"# model_rf_reg = RandomForestRegressor(n_estimators=100, \n#                                 max_depth=None, \n#                                 min_samples_split=2, \n#                                 min_samples_leaf=1, \n#                                 min_weight_fraction_leaf=0.0, \n#                                 max_features='auto', \n#                                 max_leaf_nodes=None, \n#                                 min_impurity_decrease=0.0, \n#                                 # min_impurity_split=None, \n#                                 bootstrap=True, \n#                                 oob_score=False, \n#                                 n_jobs=-1, \n#                                 random_state=None, \n#                                 verbose=0, \n#                                 warm_start=False)\nmodel_rf_reg = RandomForestRegressor(n_estimators=10, max_depth=5, random_state=2022)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:54:13.185767Z","iopub.execute_input":"2022-09-05T11:54:13.186210Z","iopub.status.idle":"2022-09-05T11:54:13.192797Z","shell.execute_reply.started":"2022-09-05T11:54:13.186177Z","shell.execute_reply":"2022-09-05T11:54:13.191644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_rf_reg.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:54:16.099437Z","iopub.execute_input":"2022-09-05T11:54:16.099868Z","iopub.status.idle":"2022-09-05T11:56:53.193330Z","shell.execute_reply.started":"2022-09-05T11:54:16.099836Z","shell.execute_reply":"2022-09-05T11:56:53.192060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/financemachinelearning/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:59:12.667468Z","iopub.execute_input":"2022-09-05T11:59:12.667920Z","iopub.status.idle":"2022-09-05T11:59:14.787903Z","shell.execute_reply.started":"2022-09-05T11:59:12.667885Z","shell.execute_reply":"2022-09-05T11:59:14.786697Z"},"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-05T11:59:38.376557Z","iopub.execute_input":"2022-09-05T11:59:38.376976Z","iopub.status.idle":"2022-09-05T11:59:38.403165Z","shell.execute_reply.started":"2022-09-05T11:59:38.376938Z","shell.execute_reply":"2022-09-05T11:59:38.401884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model_rf_reg.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:59:17.387869Z","iopub.execute_input":"2022-09-05T11:59:17.388322Z","iopub.status.idle":"2022-09-05T11:59:17.492377Z","shell.execute_reply.started":"2022-09-05T11:59:17.388271Z","shell.execute_reply":"2022-09-05T11:59:17.491071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(zip(id_, pred), columns=['id', 'r05_fwd'])","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:59:40.424130Z","iopub.execute_input":"2022-09-05T11:59:40.425047Z","iopub.status.idle":"2022-09-05T11:59:40.704620Z","shell.execute_reply.started":"2022-09-05T11:59:40.424988Z","shell.execute_reply":"2022-09-05T11:59:40.703530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:59:43.115658Z","iopub.execute_input":"2022-09-05T11:59:43.116060Z","iopub.status.idle":"2022-09-05T11:59:43.130109Z","shell.execute_reply.started":"2022-09-05T11:59:43.116023Z","shell.execute_reply":"2022-09-05T11:59:43.128834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T11:59:47.628877Z","iopub.execute_input":"2022-09-05T11:59:47.629724Z","iopub.status.idle":"2022-09-05T11:59:48.234970Z","shell.execute_reply.started":"2022-09-05T11:59:47.629675Z","shell.execute_reply":"2022-09-05T11:59:48.233289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}