{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9878620,"sourceType":"datasetVersion","datasetId":6065052}],"dockerImageVersionId":30786,"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","trusted":true},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:27:41.970636Z","iopub.execute_input":"2024-11-12T02:27:41.971229Z","iopub.status.idle":"2024-11-12T02:27:43.466048Z","shell.execute_reply.started":"2024-11-12T02:27:41.971139Z","shell.execute_reply":"2024-11-12T02:27:43.464258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/creditcardfraud/train.csv\") \ntest = pd.read_csv(\"/kaggle/input/creditcardfraud/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/creditcardfraud/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:32:40.103463Z","iopub.execute_input":"2024-11-12T02:32:40.103988Z","iopub.status.idle":"2024-11-12T02:32:44.708331Z","shell.execute_reply.started":"2024-11-12T02:32:40.103943Z","shell.execute_reply":"2024-11-12T02:32:44.707039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:09:57.457576Z","iopub.execute_input":"2024-11-12T03:09:57.458091Z","iopub.status.idle":"2024-11-12T03:09:57.481832Z","shell.execute_reply.started":"2024-11-12T03:09:57.458043Z","shell.execute_reply":"2024-11-12T03:09:57.480367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:36:45.609485Z","iopub.execute_input":"2024-11-12T02:36:45.610035Z","iopub.status.idle":"2024-11-12T02:36:45.634126Z","shell.execute_reply.started":"2024-11-12T02:36:45.609989Z","shell.execute_reply":"2024-11-12T02:36:45.632779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:45:42.647203Z","iopub.execute_input":"2024-11-12T02:45:42.647659Z","iopub.status.idle":"2024-11-12T02:45:42.659975Z","shell.execute_reply.started":"2024-11-12T02:45:42.647613Z","shell.execute_reply":"2024-11-12T02:45:42.658437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" # 전체 결측치 개수 확인\ntotal_missing_train = train.isnull().sum().sum()\ntotal_missing_test = test.isnull().sum().sum()\n\nprint(\"\\nTotal missing in train:\", total_missing_train)\nprint(\"\\nTotal missing in test:\", total_missing_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:51:46.085480Z","iopub.execute_input":"2024-11-12T03:51:46.086031Z","iopub.status.idle":"2024-11-12T03:51:46.109051Z","shell.execute_reply.started":"2024-11-12T03:51:46.085983Z","shell.execute_reply":"2024-11-12T03:51:46.107775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\n\n# 피처 분포 시각화 - Amount\nplt.figure(figsize=(10, 4))\nplt.hist(train['Amount'], bins=50)\nplt.title(\"Distribution of 'Amount'\")\nplt.xlabel(\"Amount\")\nplt.ylabel(\"Frequency\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:56:20.370451Z","iopub.execute_input":"2024-11-12T03:56:20.370953Z","iopub.status.idle":"2024-11-12T03:56:20.834393Z","shell.execute_reply.started":"2024-11-12T03:56:20.370907Z","shell.execute_reply":"2024-11-12T03:56:20.832950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 피처 분포 시각화 - Time\nplt.figure(figsize=(10, 4))\nplt.hist(train['Time'], bins=50)\nplt.title(\"Distribution of 'Time'\")\nplt.xlabel(\"Time\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:56:50.397199Z","iopub.execute_input":"2024-11-12T03:56:50.397650Z","iopub.status.idle":"2024-11-12T03:56:50.924978Z","shell.execute_reply.started":"2024-11-12T03:56:50.397602Z","shell.execute_reply":"2024-11-12T03:56:50.923687Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 목표 변수 분포 시각화 - Class\nplt.figure(figsize=(6, 4))\nplt.bar(train['Class'].value_counts().index, train['Class'].value_counts().values)\nplt.title(\"Distribution of 'Class'\")\nplt.xlabel(\"Class\")\nplt.ylabel(\"Frequency\")\nplt.xticks([0, 1], ['Non-Fraud (0)', 'Fraud (1)'])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:57:13.681685Z","iopub.execute_input":"2024-11-12T03:57:13.682137Z","iopub.status.idle":"2024-11-12T03:57:13.955009Z","shell.execute_reply.started":"2024-11-12T03:57:13.682086Z","shell.execute_reply":"2024-11-12T03:57:13.953532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 익명화된 피처(V1~V5)의 분포 시각화\nplt.figure(figsize=(10, 4))\nfor col in ['V1', 'V2', 'V3', 'V4', 'V5']:\n    plt.hist(train[col], bins=50, alpha=0.5, label=col)\nplt.title(\"Distribution of selected anonymized features (V1 to V5)\")\nplt.xlabel(\"Value\")\nplt.ylabel(\"Frequency\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T03:57:32.708356Z","iopub.execute_input":"2024-11-12T03:57:32.708845Z","iopub.status.idle":"2024-11-12T03:57:33.619908Z","shell.execute_reply.started":"2024-11-12T03:57:32.708780Z","shell.execute_reply":"2024-11-12T03:57:33.618647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\n\n# 입력 특성과 타겟 변수 분리\nX_train = train.drop(['id', 'Class'], axis=1)\ny_train = train['Class']\n\nX_test = test.drop(['id'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:45:49.151763Z","iopub.execute_input":"2024-11-12T02:45:49.152244Z","iopub.status.idle":"2024-11-12T02:45:50.290973Z","shell.execute_reply.started":"2024-11-12T02:45:49.152198Z","shell.execute_reply":"2024-11-12T02:45:50.289569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 로지스틱 회귀 모델 학습\nmodel = RandomForestClassifier()\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:45:59.722442Z","iopub.execute_input":"2024-11-12T02:45:59.723055Z","iopub.status.idle":"2024-11-12T02:48:58.211378Z","shell.execute_reply.started":"2024-11-12T02:45:59.723012Z","shell.execute_reply":"2024-11-12T02:48:58.209926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 테스트 데이터에 대한 예측\ny_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T02:55:20.171324Z","iopub.execute_input":"2024-11-12T02:55:20.171870Z","iopub.status.idle":"2024-11-12T02:55:21.379024Z","shell.execute_reply.started":"2024-11-12T02:55:20.171821Z","shell.execute_reply":"2024-11-12T02:55:21.377640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}