{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":80687,"databundleVersionId":8672016,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#1. 데이터 불러오기  \n\nimport pandas as pd\n\nTrain = pd.read_csv('/kaggle/input/big-data-analytics-certification-kr-2024-2/train.csv')\nTest = pd.read_csv('/kaggle/input/big-data-analytics-certification-kr-2024-2/test.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-28T13:14:46.556095Z","iopub.execute_input":"2024-05-28T13:14:46.556631Z","iopub.status.idle":"2024-05-28T13:14:49.308926Z","shell.execute_reply.started":"2024-05-28T13:14:46.556584Z","shell.execute_reply":"2024-05-28T13:14:49.307645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2. 데이터 개요 확인 \nprint(Train.head())\nprint(Test.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:14:50.39314Z","iopub.execute_input":"2024-05-28T13:14:50.393546Z","iopub.status.idle":"2024-05-28T13:14:50.417739Z","shell.execute_reply.started":"2024-05-28T13:14:50.393496Z","shell.execute_reply":"2024-05-28T13:14:50.416614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 3. 모델링 위한 데이터 정제 \n\n# Train 데이터에서 예측하고자 하는 column인 Class 데이터를 떼서 저장하기\nTarget = Train.pop('Class')\nprint(Target.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:14:52.526743Z","iopub.execute_input":"2024-05-28T13:14:52.527242Z","iopub.status.idle":"2024-05-28T13:14:52.53634Z","shell.execute_reply.started":"2024-05-28T13:14:52.527203Z","shell.execute_reply":"2024-05-28T13:14:52.534788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습 데이터에서 예측에 쓸모없는 ID column 삭제 \nTrain = Train.drop('id', axis= 1)\nprint(Train.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:14:54.425391Z","iopub.execute_input":"2024-05-28T13:14:54.42674Z","iopub.status.idle":"2024-05-28T13:14:54.475435Z","shell.execute_reply.started":"2024-05-28T13:14:54.426683Z","shell.execute_reply":"2024-05-28T13:14:54.474217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test 데이터에서 알고자 하는 주체인 column인 ID를 떼서 저장하기 \nTest_id = Test.pop('id')\nprint(Test_id.head())","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:14:56.314416Z","iopub.execute_input":"2024-05-28T13:14:56.316049Z","iopub.status.idle":"2024-05-28T13:14:56.323813Z","shell.execute_reply.started":"2024-05-28T13:14:56.315986Z","shell.execute_reply":"2024-05-28T13:14:56.322392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터 학습 \nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:26:22.631132Z","iopub.execute_input":"2024-05-28T13:26:22.631646Z","iopub.status.idle":"2024-05-28T13:26:22.804078Z","shell.execute_reply.started":"2024-05-28T13:26:22.631591Z","shell.execute_reply":"2024-05-28T13:26:22.80248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습데이터, 테스트 데이터 분리 \n# 학습데이터의 독립변수, 테스트 데이터의 독립변수, 학습데이터의 종속변수, 테스트데이터의 종속변수 \nX_train, X_test, Y_train, Y_test = train_test_split(Train, Target, test_size = 0.2, random_state = 123)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:29:48.182425Z","iopub.execute_input":"2024-05-28T13:29:48.182907Z","iopub.status.idle":"2024-05-28T13:29:48.254778Z","shell.execute_reply.started":"2024-05-28T13:29:48.182866Z","shell.execute_reply":"2024-05-28T13:29:48.253642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 랜덤포레스트 모델 지정 \nfrom sklearn.ensemble import RandomForestClassifier\nmodel = RandomForestClassifier(random_state = 123)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:33:03.685658Z","iopub.execute_input":"2024-05-28T13:33:03.686105Z","iopub.status.idle":"2024-05-28T13:33:04.54243Z","shell.execute_reply.started":"2024-05-28T13:33:03.686075Z","shell.execute_reply":"2024-05-28T13:33:04.54098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 학습 \n\n# 학습 데이터의 독립변수, 학습 데이터의 종속변수 \nmodel.fit(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:34:33.099744Z","iopub.execute_input":"2024-05-28T13:34:33.100254Z","iopub.status.idle":"2024-05-28T13:36:46.714681Z","shell.execute_reply.started":"2024-05-28T13:34:33.10021Z","shell.execute_reply":"2024-05-28T13:36:46.713352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 예측 \n# 테스트 데이터의 독립변수를 가지고 예측해봐 \npred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:44:44.507915Z","iopub.execute_input":"2024-05-28T13:44:44.508415Z","iopub.status.idle":"2024-05-28T13:44:44.874784Z","shell.execute_reply.started":"2024-05-28T13:44:44.508378Z","shell.execute_reply":"2024-05-28T13:44:44.873183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 예측 평가를 위한 모델 불러오기 \nfrom sklearn.metrics import roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:45:36.733124Z","iopub.execute_input":"2024-05-28T13:45:36.733628Z","iopub.status.idle":"2024-05-28T13:45:36.738835Z","shell.execute_reply.started":"2024-05-28T13:45:36.733585Z","shell.execute_reply":"2024-05-28T13:45:36.737628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(roc_auc_score(Y_test, pred))","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:50:29.543923Z","iopub.execute_input":"2024-05-28T13:50:29.544429Z","iopub.status.idle":"2024-05-28T13:50:29.5683Z","shell.execute_reply.started":"2024-05-28T13:50:29.544388Z","shell.execute_reply":"2024-05-28T13:50:29.566857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(Test)\nsubmit = pd.DataFrame({\"id\" :Test_id, \"Class\" : pred})","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:50:58.311889Z","iopub.execute_input":"2024-05-28T13:50:58.312733Z","iopub.status.idle":"2024-05-28T13:50:59.408078Z","shell.execute_reply.started":"2024-05-28T13:50:58.312521Z","shell.execute_reply":"2024-05-28T13:50:59.407006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame({\"id\" : Test_id, \"Class\" : pred})\n\n# print(submit.head())\n\n# submit.to_csv(\"수험번호.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T13:51:09.951229Z","iopub.execute_input":"2024-05-28T13:51:09.951705Z","iopub.status.idle":"2024-05-28T13:51:09.96764Z","shell.execute_reply.started":"2024-05-28T13:51:09.951669Z","shell.execute_reply":"2024-05-28T13:51:09.966265Z"},"trusted":true},"execution_count":null,"outputs":[]}]}