{"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-06T11:54:31.019130Z","iopub.execute_input":"2022-09-06T11:54:31.019589Z","iopub.status.idle":"2022-09-06T11:54:31.051761Z","shell.execute_reply.started":"2022-09-06T11:54:31.019491Z","shell.execute_reply":"2022-09-06T11:54:31.050630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(action='ignore')\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, r2_score","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:58:04.491894Z","iopub.execute_input":"2022-09-06T11:58:04.492348Z","iopub.status.idle":"2022-09-06T11:58:04.603920Z","shell.execute_reply.started":"2022-09-06T11:58:04.492308Z","shell.execute_reply":"2022-09-06T11:58:04.602590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터 세트 불러오기\ntrain = pd.read_csv('/kaggle/input/financemachinelearning/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:58:26.305103Z","iopub.execute_input":"2022-09-06T11:58:26.305509Z","iopub.status.idle":"2022-09-06T11:58:44.975868Z","shell.execute_reply.started":"2022-09-06T11:58:26.305474Z","shell.execute_reply":"2022-09-06T11:58:44.974566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-09-06T11:58:59.847995Z","iopub.execute_input":"2022-09-06T11:58:59.849491Z","iopub.status.idle":"2022-09-06T11:59:00.751690Z","shell.execute_reply.started":"2022-09-06T11:58:59.849428Z","shell.execute_reply":"2022-09-06T11:59:00.750472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split X,y","metadata":{}},{"cell_type":"code","source":"# 수익률 값을 결과값으로 하는 데이터 셋 분류\nX = train.drop('r05_fwd', axis=1)\ny = train[['r05_fwd']]\n\n# 특성 이름 리스트화\nfeature_names = X.columns.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:01:25.225728Z","iopub.execute_input":"2022-09-06T12:01:25.226909Z","iopub.status.idle":"2022-09-06T12:01:25.634733Z","shell.execute_reply.started":"2022-09-06T12:01:25.226845Z","shell.execute_reply":"2022-09-06T12:01:25.633543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(feature_names)) # 특성치(독립변수) 30개","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:01:56.396969Z","iopub.execute_input":"2022-09-06T12:01:56.397446Z","iopub.status.idle":"2022-09-06T12:01:56.403511Z","shell.execute_reply.started":"2022-09-06T12:01:56.397398Z","shell.execute_reply":"2022-09-06T12:01:56.402582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 특성치 30개 학습이 너무 오래 걸리므로 특성치 5개로 줄여서 모델링\nfrom sklearn.feature_selection import SelectKBest, f_regression\n\nX = SelectKBest(f_regression, k=5).fit_transform(X,y)\nX","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:27:51.339286Z","iopub.execute_input":"2022-09-06T12:27:51.339728Z","iopub.status.idle":"2022-09-06T12:27:52.093687Z","shell.execute_reply.started":"2022-09-06T12:27:51.339680Z","shell.execute_reply":"2022-09-06T12:27:52.092186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 특징을 전부 적용했을 때의 modeling(가우시안) 후 정확도 계산 - 7:3 분리\nX_train, X_valid, y_train, y_valid = train_test_split(X,y, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:28:23.332783Z","iopub.execute_input":"2022-09-06T12:28:23.333466Z","iopub.status.idle":"2022-09-06T12:28:23.683127Z","shell.execute_reply.started":"2022-09-06T12:28:23.333410Z","shell.execute_reply":"2022-09-06T12:28:23.681798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:28:25.478295Z","iopub.execute_input":"2022-09-06T12:28:25.478787Z","iopub.status.idle":"2022-09-06T12:28:25.486756Z","shell.execute_reply.started":"2022-09-06T12:28:25.478745Z","shell.execute_reply":"2022-09-06T12:28:25.485336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 import\n#from sklearn.naive_bayes import *\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.metrics import *\n\n# test 데이터 세트 대신 valid 셋 이용\nmodel = MLPRegressor(hidden_layer_sizes=(50,), solver='adam', random_state=0, max_iter=4000, verbose=False)\nmodel.fit(X_train, y_train)\npred = model.predict(X_valid)\n\n# accuracy = accuracy_score(y_vaild, pred)\n# print(accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:28:38.498656Z","iopub.execute_input":"2022-09-06T12:28:38.499975Z","iopub.status.idle":"2022-09-06T12:30:04.525078Z","shell.execute_reply.started":"2022-09-06T12:28:38.499913Z","shell.execute_reply":"2022-09-06T12:30:04.523611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습, 검증 데이터 정확도\nprint(model.score(X_train, y_train))\nprint(model.score(X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:30:15.792778Z","iopub.execute_input":"2022-09-06T12:30:15.793221Z","iopub.status.idle":"2022-09-06T12:30:17.376673Z","shell.execute_reply.started":"2022-09-06T12:30:15.793184Z","shell.execute_reply":"2022-09-06T12:30:17.374833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nrmse = mean_squared_error(y_valid, pred, squared=True)\nrmse","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:33:12.467135Z","iopub.execute_input":"2022-09-06T12:33:12.467624Z","iopub.status.idle":"2022-09-06T12:33:12.480543Z","shell.execute_reply.started":"2022-09-06T12:33:12.467582Z","shell.execute_reply":"2022-09-06T12:33:12.479556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/financemachinelearning/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:36:15.065589Z","iopub.execute_input":"2022-09-06T12:36:15.066050Z","iopub.status.idle":"2022-09-06T12:36:16.540240Z","shell.execute_reply.started":"2022-09-06T12:36:15.066011Z","shell.execute_reply":"2022-09-06T12:36:16.539037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = test['id']\npred","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:37:12.779550Z","iopub.execute_input":"2022-09-06T12:37:12.780077Z","iopub.status.idle":"2022-09-06T12:37:12.788899Z","shell.execute_reply.started":"2022-09-06T12:37:12.780036Z","shell.execute_reply":"2022-09-06T12:37:12.787623Z"},"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-06T12:37:38.894340Z","iopub.execute_input":"2022-09-06T12:37:38.894764Z","iopub.status.idle":"2022-09-06T12:37:39.096456Z","shell.execute_reply.started":"2022-09-06T12:37:38.894726Z","shell.execute_reply":"2022-09-06T12:37:39.095213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:37:41.869945Z","iopub.execute_input":"2022-09-06T12:37:41.870334Z","iopub.status.idle":"2022-09-06T12:37:41.886636Z","shell.execute_reply.started":"2022-09-06T12:37:41.870303Z","shell.execute_reply":"2022-09-06T12:37:41.885465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-06T12:38:10.337737Z","iopub.execute_input":"2022-09-06T12:38:10.338166Z","iopub.status.idle":"2022-09-06T12:38:10.893303Z","shell.execute_reply.started":"2022-09-06T12:38:10.338132Z","shell.execute_reply":"2022-09-06T12:38:10.892059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}