{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":38680,"databundleVersionId":4211944,"sourceType":"competition"}],"dockerImageVersionId":30235,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-11-19T07:18:52.479666Z","iopub.execute_input":"2023-11-19T07:18:52.480218Z","iopub.status.idle":"2023-11-19T07:18:52.491376Z","shell.execute_reply.started":"2023-11-19T07:18:52.480176Z","shell.execute_reply":"2023-11-19T07:18:52.489684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n# 변환을 위한 라이브러리 불러오기\nfrom sklearn.decomposition import PCA\nfrom sklearn.impute import SimpleImputer\n# 통계 모델 라이브러리 불러오기\nfrom sklearn.linear_model import LogisticRegressionCV\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:18:52.494429Z","iopub.execute_input":"2023-11-19T07:18:52.495953Z","iopub.status.idle":"2023-11-19T07:18:52.509772Z","shell.execute_reply.started":"2023-11-19T07:18:52.495876Z","shell.execute_reply":"2023-11-19T07:18:52.508005Z"},"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-11-19T07:18:52.512431Z","iopub.execute_input":"2023-11-19T07:18:52.512926Z","iopub.status.idle":"2023-11-19T07:19:13.270691Z","shell.execute_reply.started":"2023-11-19T07:18:52.512854Z","shell.execute_reply":"2023-11-19T07:19:13.269371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('r05_fwd', axis=1)\ny = train[['r05_fwd']]\n\n# train data split\nX_train, X_val, y_train, y_val = train_test_split(X, y,test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:13.273898Z","iopub.execute_input":"2023-11-19T07:19:13.274478Z","iopub.status.idle":"2023-11-19T07:19:14.791641Z","shell.execute_reply.started":"2023-11-19T07:19:13.274420Z","shell.execute_reply":"2023-11-19T07:19:14.790210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape, X_val.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:14.793471Z","iopub.execute_input":"2023-11-19T07:19:14.794046Z","iopub.status.idle":"2023-11-19T07:19:14.807930Z","shell.execute_reply.started":"2023-11-19T07:19:14.793991Z","shell.execute_reply":"2023-11-19T07:19:14.805473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import linear_model","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:14.810195Z","iopub.execute_input":"2023-11-19T07:19:14.810687Z","iopub.status.idle":"2023-11-19T07:19:14.819635Z","shell.execute_reply.started":"2023-11-19T07:19:14.810645Z","shell.execute_reply":"2023-11-19T07:19:14.817955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMRegressor","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:14.822082Z","iopub.execute_input":"2023-11-19T07:19:14.822751Z","iopub.status.idle":"2023-11-19T07:19:15.892379Z","shell.execute_reply.started":"2023-11-19T07:19:14.822692Z","shell.execute_reply":"2023-11-19T07:19:15.890999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 기본 모델링\nlgbm = LGBMRegressor(random_state=0)\nrf = RandomForestRegressor(n_estimators=10, max_depth=5, random_state=0)\n#lr = LogisticRegression(C=1)\nreg = linear_model.LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:15.893817Z","iopub.execute_input":"2023-11-19T07:19:15.894394Z","iopub.status.idle":"2023-11-19T07:19:15.904835Z","shell.execute_reply.started":"2023-11-19T07:19:15.894333Z","shell.execute_reply":"2023-11-19T07:19:15.903208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 학습\nlgbm.fit(X_train, y_train)\nrf.fit(X_train, y_train)\nreg.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T07:19:15.907151Z","iopub.execute_input":"2023-11-19T07:19:15.907859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_pred = lgbm.predict(X_val)\nrf_pred = rf.predict(X_val)\n#reg_pred = reg.predict(X_val)\n#print(\"lgbm: {0:.4f}, rf:{1:.4f}, lr:{2:.4f}\".format(accuracy_score(y_val, lgbm_pred),\n#                                                    accuracy_score(y_val, rf_pred),\n#                                                    accuracy_score(y_val, reg_pred)))\n#print(\"lgbm: {0:.4f}, rf:{1:.4f}, lr:{2:.4f}\".format(accuracy_score(y_val, lgbm_pred), accuracy_score(y_val, rf_pred)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_pred.shape, rf_pred.shape, #reg_pred.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 예측한 데이터를 새로운 데이터로 활용\nnew_data = np.array([lgbm_pred, rf_pred])\nnew_data.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data = np.transpose(new_data)\nnew_data.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import Ridge","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = Ridge(alpha=1)\nclf.fit(new_data, y_val)\nclf_pred = clf.predict(new_data)\n#print(\"정확도:{0:.4f}\".format(accuracy_score(y_val, clf_pred)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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":"clf_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_pred = clf_pred.reshape(-1,)\nclf_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(zip(id_,clf_pred), columns=['id','r05_fwd'])\nsubmission","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":[]}]}