{"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":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":"2024-05-28T13:57:42.975425Z","iopub.execute_input":"2024-05-28T13:57:42.975819Z","iopub.status.idle":"2024-05-28T13:57:42.984787Z","shell.execute_reply.started":"2024-05-28T13:57:42.975789Z","shell.execute_reply":"2024-05-28T13:57:42.983148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\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')\ntrain.head()\ntest.head()\ntrain.info()\ntest.info()\n\n\nX_train = train.drop(['id', 'Class'], axis = 1)\ny_train = train['Class']\n# y_train = train.pop('Class')   # 원본 변형 발생\nX_test = test.drop('id', axis = 1)\ntrain.shape, X_train.shape, y_train.shape, test.shape, X_test.shape\n\nfrom sklearn.model_selection import train_test_split\nX_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state = 2024)\nX_tr.shape, X_val.shape, y_tr.shape, y_val.shape\n\nfrom sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier(random_state = 2024)\nclf.fit(X_tr, y_tr)\n\nfrom sklearn.metrics import roc_auc_score\nras = roc_auc_score(y_val, clf.predict_proba(X_val)[:, 1])\nras\n\n# print(clf.predict(X_test))\n# y_pred = clf.predict(X_test)\n# submit1 = pd.DataFrame({'id': test['id'], 'Class': y_pred})\n# submit1.to_csv('submission.csv', index=False)\n\nprint(clf.predict_proba(X_test))\ny_pred_proba = clf.predict_proba(X_test)\nsubmit2 = pd.DataFrame({'id': test['id'], 'Class': y_pred_proba[:,1]})\nsubmit2.to_csv('submission.csv', index=False)\n\n\n# import sklearn.ensemble\n# sklearn.ensemble.__all__\n# help(sklearn.ensemble.RandomForestClassifier)\n\n# from sklearn.model_selection import GridSearchCV\n# params ={\n#     'n_estimators':[100],\n#     'max_depth':[6,8,10,12],\n#     'min_samples_leaf':[8,12,18],\n#     'min_samples_split':[8,16,20]\n# }\n# rf = RandomForestClassifier(random_state=0, n_jobs=-1)\n# grid_cv = GridSearchCV(rf, param_grid=params, cv=2, n_jobs=-1)\n# grid_cv.fit(X_tr,y_tr)\n\n# estimator =grid_cv.best_estimator_ \n# pred = estimator.predict(X_val) # estimator.predict(test)\n\n# print('최적 하이퍼 파라미터:\\n', grid_cv.best_params_)\n# print('최고 예측 정확도: {0:.4f}'.format(grid_cv.best_score_))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\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":{"execution":{"iopub.status.busy":"2024-05-28T16:45:07.266146Z","iopub.execute_input":"2024-05-28T16:45:07.266571Z","iopub.status.idle":"2024-05-28T16:45:10.122635Z","shell.execute_reply.started":"2024-05-28T16:45:07.266539Z","shell.execute_reply":"2024-05-28T16:45:10.121390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:12.380406Z","iopub.execute_input":"2024-05-28T16:45:12.380842Z","iopub.status.idle":"2024-05-28T16:45:12.413033Z","shell.execute_reply.started":"2024-05-28T16:45:12.380808Z","shell.execute_reply":"2024-05-28T16:45:12.411670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:15.153119Z","iopub.execute_input":"2024-05-28T16:45:15.153545Z","iopub.status.idle":"2024-05-28T16:45:15.184961Z","shell.execute_reply.started":"2024-05-28T16:45:15.153512Z","shell.execute_reply":"2024-05-28T16:45:15.183748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:17.688435Z","iopub.execute_input":"2024-05-28T16:45:17.688931Z","iopub.status.idle":"2024-05-28T16:45:17.713948Z","shell.execute_reply.started":"2024-05-28T16:45:17.688894Z","shell.execute_reply":"2024-05-28T16:45:17.712582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:20.948819Z","iopub.execute_input":"2024-05-28T16:45:20.949267Z","iopub.status.idle":"2024-05-28T16:45:20.970914Z","shell.execute_reply.started":"2024-05-28T16:45:20.949230Z","shell.execute_reply":"2024-05-28T16:45:20.969548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_train = train.pop('Class')   # 원본 변형 발생\n# y_train.head()\n\nX_train = train.drop(['id', 'Class'], axis = 1)\ny_train = train['Class']\nX_test = test.drop('id', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:24.489980Z","iopub.execute_input":"2024-05-28T16:45:24.490481Z","iopub.status.idle":"2024-05-28T16:45:24.625741Z","shell.execute_reply.started":"2024-05-28T16:45:24.490442Z","shell.execute_reply":"2024-05-28T16:45:24.624435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, X_train.shape, y_train.shape, test.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:27.565958Z","iopub.execute_input":"2024-05-28T16:45:27.566409Z","iopub.status.idle":"2024-05-28T16:45:27.577828Z","shell.execute_reply.started":"2024-05-28T16:45:27.566375Z","shell.execute_reply":"2024-05-28T16:45:27.573962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T15:00:24.774229Z","iopub.execute_input":"2024-05-28T15:00:24.774650Z","iopub.status.idle":"2024-05-28T15:00:24.780107Z","shell.execute_reply.started":"2024-05-28T15:00:24.774618Z","shell.execute_reply":"2024-05-28T15:00:24.778870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T15:01:54.652252Z","iopub.execute_input":"2024-05-28T15:01:54.652649Z","iopub.status.idle":"2024-05-28T15:01:54.657644Z","shell.execute_reply.started":"2024-05-28T15:01:54.652600Z","shell.execute_reply":"2024-05-28T15:01:54.656416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-28T15:02:01.043415Z","iopub.execute_input":"2024-05-28T15:02:01.043794Z","iopub.status.idle":"2024-05-28T15:02:01.048785Z","shell.execute_reply.started":"2024-05-28T15:02:01.043765Z","shell.execute_reply":"2024-05-28T15:02:01.047637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size = 0.3, random_state = 2024)\nX_tr.shape, X_val.shape, y_tr.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:31.713668Z","iopub.execute_input":"2024-05-28T16:45:31.714216Z","iopub.status.idle":"2024-05-28T16:45:31.793151Z","shell.execute_reply.started":"2024-05-28T16:45:31.714176Z","shell.execute_reply":"2024-05-28T16:45:31.791868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier(random_state = 2025)\nclf.fit(X_tr, y_tr)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:45:34.944971Z","iopub.execute_input":"2024-05-28T16:45:34.945384Z","iopub.status.idle":"2024-05-28T16:47:23.508185Z","shell.execute_reply.started":"2024-05-28T16:45:34.945349Z","shell.execute_reply":"2024-05-28T16:47:23.506945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier(max_depth = 10, min_samples_leaf = 8, min_samples_split = 8, n_estimators = 100, random_state = 202401)\nclf.fit(X_tr, y_tr)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T15:53:54.800393Z","iopub.execute_input":"2024-05-28T15:53:54.800826Z","iopub.status.idle":"2024-05-28T15:55:28.283847Z","shell.execute_reply.started":"2024-05-28T15:53:54.800790Z","shell.execute_reply":"2024-05-28T15:55:28.282635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nparams ={\n    'n_estimators':[100],\n    'max_depth':[6,8,10,12],\n    'min_samples_leaf':[8,12,18],\n    'min_samples_split':[8,16,20]\n}\nrf = RandomForestClassifier(random_state=0, n_jobs=-1)\ngrid_cv = GridSearchCV(rf, param_grid=params, cv=2, n_jobs=-1)\ngrid_cv.fit(X_tr,y_tr)\n\nestimator =grid_cv.best_estimator_ \npred = estimator.predict(X_val) # estimator.predict(test)\n\nprint('최적 하이퍼 파라미터:\\n', grid_cv.best_params_)\nprint('최고 예측 정확도: {0:.4f}'.format(grid_cv.best_score_))","metadata":{"execution":{"iopub.status.busy":"2024-05-28T15:27:23.537834Z","iopub.execute_input":"2024-05-28T15:27:23.538266Z","iopub.status.idle":"2024-05-28T15:44:40.767996Z","shell.execute_reply.started":"2024-05-28T15:27:23.538231Z","shell.execute_reply":"2024-05-28T15:44:40.766166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nras = roc_auc_score(y_val, clf.predict_proba(X_val)[:, 1])\nras","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:53:27.563305Z","iopub.execute_input":"2024-05-28T16:53:27.563709Z","iopub.status.idle":"2024-05-28T16:53:28.078954Z","shell.execute_reply.started":"2024-05-28T16:53:27.563677Z","shell.execute_reply":"2024-05-28T16:53:28.077791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(clf.predict(X_test))\n# y_pred = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T14:11:13.421490Z","iopub.execute_input":"2024-05-28T14:11:13.421898Z","iopub.status.idle":"2024-05-28T14:11:15.730178Z","shell.execute_reply.started":"2024-05-28T14:11:13.421865Z","shell.execute_reply":"2024-05-28T14:11:15.729248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submit1 = pd.DataFrame({'id': test['id'], 'Class': y_pred})\n# submit1.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clf.predict_proba(X_test))\ny_pred_proba = clf.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:53:33.142547Z","iopub.execute_input":"2024-05-28T16:53:33.142942Z","iopub.status.idle":"2024-05-28T16:53:35.293204Z","shell.execute_reply.started":"2024-05-28T16:53:33.142912Z","shell.execute_reply":"2024-05-28T16:53:35.291751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit2 = pd.DataFrame({'id': test['id'], 'Class': y_pred_proba[:,1]})\nsubmit2.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:53:53.616211Z","iopub.execute_input":"2024-05-28T16:53:53.616621Z","iopub.status.idle":"2024-05-28T16:53:53.837192Z","shell.execute_reply.started":"2024-05-28T16:53:53.616589Z","shell.execute_reply":"2024-05-28T16:53:53.835886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.ensemble\nsklearn.ensemble.__all__","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:27:36.754695Z","iopub.execute_input":"2024-05-28T16:27:36.755134Z","iopub.status.idle":"2024-05-28T16:27:36.763377Z","shell.execute_reply.started":"2024-05-28T16:27:36.755097Z","shell.execute_reply":"2024-05-28T16:27:36.762081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"help(sklearn.ensemble.RandomForestClassifier)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T16:27:41.238801Z","iopub.execute_input":"2024-05-28T16:27:41.239258Z","iopub.status.idle":"2024-05-28T16:27:41.254733Z","shell.execute_reply.started":"2024-05-28T16:27:41.239220Z","shell.execute_reply":"2024-05-28T16:27:41.253295Z"},"trusted":true},"execution_count":null,"outputs":[]}]}