{"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":50160,"databundleVersionId":7602123,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install & Import Package & Read","metadata":{}},{"cell_type":"code","source":"!pip install googletrans==3.1.0a0 --upgrade --quiet","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:21:16.577224Z","iopub.execute_input":"2024-03-16T10:21:16.57764Z","iopub.status.idle":"2024-03-16T10:21:41.467619Z","shell.execute_reply.started":"2024-03-16T10:21:16.577607Z","shell.execute_reply":"2024-03-16T10:21:41.465842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from googletrans import Translator\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport polars as pl\nimport gc\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:11:55.361019Z","iopub.execute_input":"2024-03-16T12:11:55.361529Z","iopub.status.idle":"2024-03-16T12:11:55.370408Z","shell.execute_reply.started":"2024-03-16T12:11:55.361493Z","shell.execute_reply":"2024-03-16T12:11:55.368549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.float_format', lambda x: '%.2f' % x)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:11:56.426122Z","iopub.execute_input":"2024-03-16T12:11:56.426567Z","iopub.status.idle":"2024-03-16T12:11:56.43239Z","shell.execute_reply.started":"2024-03-16T12:11:56.426536Z","shell.execute_reply":"2024-03-16T12:11:56.43127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base = pd.read_csv(\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_base.csv\")\ndf = pd.read_csv(\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_debitcard_1.csv\")\ndf2 = pd.read_csv(\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_deposit_1.csv\")\nfd = pd.read_csv(\"/kaggle/input/home-credit-credit-risk-model-stability/feature_definitions.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:11:57.285632Z","iopub.execute_input":"2024-03-16T12:11:57.286381Z","iopub.status.idle":"2024-03-16T12:11:58.625958Z","shell.execute_reply.started":"2024-03-16T12:11:57.286341Z","shell.execute_reply":"2024-03-16T12:11:58.624284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = fd.loc[fd['Variable'].isin(list(df.columns))]\ntranslator = Translator()\ntmp[\"Description_ko\"] = tmp[\"Description\"].map(lambda x: translator.translate(x, src=\"en\", dest=\"ko\").text)\ntmp['fea_type'] = tmp['Variable'].apply(lambda x: x[-1])","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:21:58.730268Z","iopub.execute_input":"2024-03-16T10:21:58.730734Z","iopub.status.idle":"2024-03-16T10:21:59.229916Z","shell.execute_reply.started":"2024-03-16T10:21:58.730685Z","shell.execute_reply":"2024-03-16T10:21:59.228311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:22:03.164614Z","iopub.execute_input":"2024-03-16T10:22:03.166059Z","iopub.status.idle":"2024-03-16T10:22:03.187958Z","shell.execute_reply.started":"2024-03-16T10:22:03.165988Z","shell.execute_reply":"2024-03-16T10:22:03.18655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"직불카드 매출액 = 직불카드 거래액","metadata":{}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:22:28.791305Z","iopub.execute_input":"2024-03-16T10:22:28.791785Z","iopub.status.idle":"2024-03-16T10:22:28.813586Z","shell.execute_reply.started":"2024-03-16T10:22:28.791752Z","shell.execute_reply":"2024-03-16T10:22:28.812177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['case_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:38:10.604283Z","iopub.execute_input":"2024-03-16T12:38:10.604781Z","iopub.status.idle":"2024-03-16T12:38:10.645854Z","shell.execute_reply.started":"2024-03-16T12:38:10.604751Z","shell.execute_reply":"2024-03-16T12:38:10.644815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# null","metadata":{}},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:22:36.115747Z","iopub.execute_input":"2024-03-16T10:22:36.116191Z","iopub.status.idle":"2024-03-16T10:22:36.150292Z","shell.execute_reply.started":"2024-03-16T10:22:36.116157Z","shell.execute_reply":"2024-03-16T10:22:36.148753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(df.isnull().sum() / df.shape[0])*100","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:22:36.752314Z","iopub.execute_input":"2024-03-16T10:22:36.753505Z","iopub.status.idle":"2024-03-16T10:22:36.786962Z","shell.execute_reply.started":"2024-03-16T10:22:36.753452Z","shell.execute_reply":"2024-03-16T10:22:36.785651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# openingdate_857D가 null이 아닌 행 선택\nopeningdate_not_null = df[df['openingdate_857D'].notnull()]\n\n# case_id 별 openingdate_857D의 개수 계산\ncount_openingdate_by_case_id = openingdate_not_null.groupby('case_id')['openingdate_857D'].count()\n\n# 결과 출력\nprint(count_openingdate_by_case_id.sort_values())","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:22:50.333268Z","iopub.execute_input":"2024-03-16T10:22:50.333773Z","iopub.status.idle":"2024-03-16T10:22:50.411148Z","shell.execute_reply.started":"2024-03-16T10:22:50.333732Z","shell.execute_reply":"2024-03-16T10:22:50.409528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# last30dayturnover_651A => not null","metadata":{}},{"cell_type":"code","source":"l30dt_nn=df[df['last30dayturnover_651A'].notnull()]\nl30dt_nn","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:21:02.666634Z","iopub.execute_input":"2024-03-16T12:21:02.667129Z","iopub.status.idle":"2024-03-16T12:21:02.69201Z","shell.execute_reply.started":"2024-03-16T12:21:02.667096Z","shell.execute_reply":"2024-03-16T12:21:02.690277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# case_id 별 last30dayturnover_651A 합계 \n- case_id 개수 : 10272","metadata":{}},{"cell_type":"code","source":"l30dt_nn.groupby('case_id')['last30dayturnover_651A'].sum()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:21:22.280588Z","iopub.execute_input":"2024-03-16T12:21:22.281334Z","iopub.status.idle":"2024-03-16T12:21:22.29757Z","shell.execute_reply.started":"2024-03-16T12:21:22.281278Z","shell.execute_reply":"2024-03-16T12:21:22.295526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l30dt_nn.groupby('case_id')['last30dayturnover_651A'].sum().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:21:25.636686Z","iopub.execute_input":"2024-03-16T12:21:25.637182Z","iopub.status.idle":"2024-03-16T12:21:25.653873Z","shell.execute_reply.started":"2024-03-16T12:21:25.637147Z","shell.execute_reply":"2024-03-16T12:21:25.651807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l30dt_nn['case_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:15:16.976745Z","iopub.execute_input":"2024-03-16T12:15:16.977221Z","iopub.status.idle":"2024-03-16T12:15:16.99381Z","shell.execute_reply.started":"2024-03-16T12:15:16.977185Z","shell.execute_reply":"2024-03-16T12:15:16.992047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# last180dayturnover_1134A => not null","metadata":{}},{"cell_type":"code","source":"l180dt_nn=df[df['last180dayturnover_1134A'].notnull()]\nl180dt_nn","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:20:34.232128Z","iopub.execute_input":"2024-03-16T12:20:34.232612Z","iopub.status.idle":"2024-03-16T12:20:34.257073Z","shell.execute_reply.started":"2024-03-16T12:20:34.232581Z","shell.execute_reply":"2024-03-16T12:20:34.255964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# case_id 별 last180dayturnover_1134A 합계 \n- case_id 개수 : 10272","metadata":{}},{"cell_type":"code","source":"l180dt_nn.groupby('case_id')['last180dayturnover_1134A'].sum()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:20:08.379649Z","iopub.execute_input":"2024-03-16T12:20:08.380606Z","iopub.status.idle":"2024-03-16T12:20:08.394631Z","shell.execute_reply.started":"2024-03-16T12:20:08.380551Z","shell.execute_reply":"2024-03-16T12:20:08.393012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l180dt_nn['case_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:15:41.639876Z","iopub.execute_input":"2024-03-16T12:15:41.640457Z","iopub.status.idle":"2024-03-16T12:15:41.654822Z","shell.execute_reply.started":"2024-03-16T12:15:41.640412Z","shell.execute_reply":"2024-03-16T12:15:41.653132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# last180dayaveragebalance_704A => not null","metadata":{}},{"cell_type":"code","source":"l180ab_nn=df[df['last180dayaveragebalance_704A'].notnull()]\nl180ab_nn","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:14:09.403521Z","iopub.execute_input":"2024-03-16T12:14:09.404043Z","iopub.status.idle":"2024-03-16T12:14:09.427495Z","shell.execute_reply.started":"2024-03-16T12:14:09.404003Z","shell.execute_reply":"2024-03-16T12:14:09.426225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l180ab_nn['case_id']","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:05:38.879785Z","iopub.execute_input":"2024-03-16T12:05:38.880249Z","iopub.status.idle":"2024-03-16T12:05:38.891653Z","shell.execute_reply.started":"2024-03-16T12:05:38.880219Z","shell.execute_reply":"2024-03-16T12:05:38.890166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l180ab_nn['case_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:06:02.465919Z","iopub.execute_input":"2024-03-16T12:06:02.466389Z","iopub.status.idle":"2024-03-16T12:06:02.47881Z","shell.execute_reply.started":"2024-03-16T12:06:02.466358Z","shell.execute_reply":"2024-03-16T12:06:02.477727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l180ab_nn.groupby('case_id')['last180dayaveragebalance_704A'].sum()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:28:51.228961Z","iopub.execute_input":"2024-03-16T12:28:51.229456Z","iopub.status.idle":"2024-03-16T12:28:51.247468Z","shell.execute_reply.started":"2024-03-16T12:28:51.229423Z","shell.execute_reply":"2024-03-16T12:28:51.245926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l180ab_nn.groupby('case_id')['last180dayaveragebalance_704A'].sum().value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T12:08:36.980802Z","iopub.execute_input":"2024-03-16T12:08:36.9813Z","iopub.status.idle":"2024-03-16T12:08:36.99797Z","shell.execute_reply.started":"2024-03-16T12:08:36.981265Z","shell.execute_reply":"2024-03-16T12:08:36.99656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# 데이터 시리즈를 히스토그램으로 시각화\nplt.figure(figsize=(10, 6))\nplt.hist(l180ab_nn.groupby('case_id')['last180dayaveragebalance_704A'].sum(), bins=30, edgecolor='black')\n\n# 그래프 제목 설정\nplt.title('Distribution of last180dayaveragebalance_704A by case_id')","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:00.635006Z","iopub.execute_input":"2024-03-16T10:24:00.635863Z","iopub.status.idle":"2024-03-16T10:24:01.075202Z","shell.execute_reply.started":"2024-03-16T10:24:00.635823Z","shell.execute_reply":"2024-03-16T10:24:01.073797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2[df2['case_id'] == 1494474].sort_values(by = \"num_group1\")#.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:01.893249Z","iopub.execute_input":"2024-03-16T10:24:01.893725Z","iopub.status.idle":"2024-03-16T10:24:01.916115Z","shell.execute_reply.started":"2024-03-16T10:24:01.893659Z","shell.execute_reply":"2024-03-16T10:24:01.914781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['case_id'] == 1494474].sort_values(by = \"num_group1\")#","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:02.166097Z","iopub.execute_input":"2024-03-16T10:24:02.16701Z","iopub.status.idle":"2024-03-16T10:24:02.187552Z","shell.execute_reply.started":"2024-03-16T10:24:02.166973Z","shell.execute_reply":"2024-03-16T10:24:02.186347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# case_id가 121887인 데이터프레임에서 openingdate_857D와 num_group1 열 선택\ndata_case_121887 = df[df['case_id'] == 142868][['openingdate_857D', 'num_group1']].sort_values(by = \"openingdate_857D\")\nprint(data_case_121887)\n# openingdate_857D 열을 날짜 형식으로 변환\ndata_case_121887['openingdate_857D'] = pd.to_datetime(data_case_121887['openingdate_857D'])\n\n# openingdate_857D의 시계열 분포 시각화\nplt.figure(figsize=(10, 6))\nplt.plot(data_case_121887['openingdate_857D'], data_case_121887['num_group1'], marker='o', linestyle='-')\n\n# 그래프 제목 설정\nplt.title('Time Series Distribution of openingdate_857D for case_id 121887')\n\n# x축 라벨 설정\nplt.xlabel('Date')\n\n# y축 라벨 설정\nplt.ylabel('num_group1')\n\n# 그래프 출력\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:02.671Z","iopub.execute_input":"2024-03-16T10:24:02.67196Z","iopub.status.idle":"2024-03-16T10:24:03.110713Z","shell.execute_reply.started":"2024-03-16T10:24:02.671923Z","shell.execute_reply":"2024-03-16T10:24:03.108911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"case_id_counts = df['case_id'].value_counts()\ncase_id_counts","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:03.586531Z","iopub.execute_input":"2024-03-16T10:24:03.586974Z","iopub.status.idle":"2024-03-16T10:24:03.616046Z","shell.execute_reply.started":"2024-03-16T10:24:03.586942Z","shell.execute_reply":"2024-03-16T10:24:03.614763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 카드 계좌 개설일 매칭해서 봐보기","metadata":{}},{"cell_type":"markdown","source":"df[df['case_id']==1377353] 에서 openingdate_857D 중복날짜 개수, 유니크 개수,\n각 case_id 에서 openingdate_857D의 각 날짜의 중복일 개수\n각 case_id 에서 num_group1의 개수\n\n각 case_id 의 전체 데이터가 적혀진 모양을 보기 위해서 임의의 case_id 2개를 비교해서 보기\ndf['cae_id'].value_counts()\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# case_id 2\ndf[df['case_id']==2608173].sort_values(by='num_group1').head(100)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:53:15.603752Z","iopub.execute_input":"2024-03-16T10:53:15.604169Z","iopub.status.idle":"2024-03-16T10:53:15.62101Z","shell.execute_reply.started":"2024-03-16T10:53:15.604138Z","shell.execute_reply":"2024-03-16T10:53:15.61977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# case_id 3\ndf[df['case_id']==143867].sort_values(by='num_group1').head(100)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:53:16.379215Z","iopub.execute_input":"2024-03-16T10:53:16.380479Z","iopub.status.idle":"2024-03-16T10:53:16.397275Z","shell.execute_reply.started":"2024-03-16T10:53:16.380433Z","shell.execute_reply":"2024-03-16T10:53:16.395904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# case_id 4\ndf[df['case_id']==2534267].sort_values(by='num_group1').head(100)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:53:16.690987Z","iopub.execute_input":"2024-03-16T10:53:16.691415Z","iopub.status.idle":"2024-03-16T10:53:16.711506Z","shell.execute_reply.started":"2024-03-16T10:53:16.691382Z","shell.execute_reply":"2024-03-16T10:53:16.709288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# case_id 5\ndf[df['case_id']==random_value].sort_values(by='num_group1').head(100)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:03:31.057155Z","iopub.execute_input":"2024-03-16T11:03:31.057854Z","iopub.status.idle":"2024-03-16T11:03:31.072009Z","shell.execute_reply.started":"2024-03-16T11:03:31.057821Z","shell.execute_reply":"2024-03-16T11:03:31.071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"num_group1 의 값 1부터~66까지 openingdate_857D 값을 관찰 해본 결과, openingdate_857D 는 제각각으로 일정한 패턴은 보이지 않는다.  \n어떤 사람은 한 날짜에 10개를 만든 사람도 있고, 넘그룹 0부터 9까지 순서대로 날짜가 올라가는 경우도 있고, 그렇지 않는 경우도 있다.  \n또한 openingdate_857D 값이 널인 경우도 있다.  ","metadata":{}},{"cell_type":"code","source":"case_ids = case_id_counts[case_id_counts == 10].index\nrandom_value = np.random.choice(case_ids)\ndf[df['case_id']==random_value].sort_values(by='num_group1').head(100)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:08:42.877918Z","iopub.execute_input":"2024-03-16T11:08:42.878335Z","iopub.status.idle":"2024-03-16T11:08:42.89879Z","shell.execute_reply.started":"2024-03-16T11:08:42.878305Z","shell.execute_reply":"2024-03-16T11:08:42.897282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 직불카드 개수별 빈도(직불카드 0개인사람 82463명)\nfor index, i in enumerate(range(1, 67)):\n    frequency = len(case_id_counts[case_id_counts == i])\n    print(f'{index+1}: {frequency}')","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:05:00.192243Z","iopub.execute_input":"2024-03-16T11:05:00.192732Z","iopub.status.idle":"2024-03-16T11:05:00.226458Z","shell.execute_reply.started":"2024-03-16T11:05:00.192674Z","shell.execute_reply":"2024-03-16T11:05:00.224878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nrandom_number = random.randint(1, 66)\nprint(random_number)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:07.444636Z","iopub.execute_input":"2024-03-16T10:24:07.446129Z","iopub.status.idle":"2024-03-16T10:24:07.452344Z","shell.execute_reply.started":"2024-03-16T10:24:07.446074Z","shell.execute_reply":"2024-03-16T10:24:07.450863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = case_id_counts[case_id_counts == random_number].index","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:07.834511Z","iopub.execute_input":"2024-03-16T10:24:07.835144Z","iopub.status.idle":"2024-03-16T10:24:07.841829Z","shell.execute_reply.started":"2024-03-16T10:24:07.835108Z","shell.execute_reply":"2024-03-16T10:24:07.840481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nrandom_number = random.randint(1, 66)\nprint(random_number)\ntry:\n    a = case_id_counts[case_id_counts == random_number].index\n\n    random_index = random.choice(a)\n    print(random_index)\n\n    import matplotlib.pyplot as plt\n\n    # case_id가 121887인 데이터프레임에서 openingdate_857D와 num_group1 열 선택\n    data_case_121887 = df[df['case_id'] == random_index][['openingdate_857D', 'num_group1']].sort_values(by = \"openingdate_857D\")\n#     print(data_case_121887)\n    # openingdate_857D 열을 날짜 형식으로 변환\n    data_case_121887['openingdate_857D'] = pd.to_datetime(data_case_121887['openingdate_857D'])\n\n    # openingdate_857D의 시계열 분포 시각화\n    plt.figure(figsize=(10, 6))\n    plt.plot(data_case_121887['openingdate_857D'], data_case_121887['num_group1'], marker='o', linestyle='-')\n\n    # 그래프 제목 설정\n    plt.title('Time Series Distribution of openingdate_857D for case_id 121887')\n\n    # x축 라벨 설정\n    plt.xlabel('Date')\n\n    # y축 라벨 설정\n    plt.ylabel('num_group1')\n\n    # 그래프 출력\n    plt.show()\nexcept:\n    print(f\"{random_number} is empty\")","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:09.526436Z","iopub.status.idle":"2024-03-16T10:24:09.52753Z","shell.execute_reply.started":"2024-03-16T10:24:09.527269Z","shell.execute_reply":"2024-03-16T10:24:09.527292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# case_id_counts의 값으로부터 각 값의 빈도 계산\ncount_frequency = case_id_counts.value_counts().sort_index()\n\n# 개수의 길이를 시각화\nplt.figure(figsize=(10, 6))\nplt.bar(count_frequency.index, count_frequency.values)\n\n# 그래프 제목 설정\nplt.title('Frequency of case_id Counts')\n\n# x축 라벨 설정\nplt.xlabel('Number of case_id')\n\n# y축 라벨 설정\nplt.ylabel('Frequency')\n\n# 그래프 출력\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:09.529444Z","iopub.status.idle":"2024-03-16T10:24:09.530451Z","shell.execute_reply.started":"2024-03-16T10:24:09.530155Z","shell.execute_reply":"2024-03-16T10:24:09.530182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('case_id')['num_group1'].value_counts().head(50)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:09.76757Z","iopub.execute_input":"2024-03-16T10:24:09.768021Z","iopub.status.idle":"2024-03-16T10:24:09.84113Z","shell.execute_reply.started":"2024-03-16T10:24:09.767989Z","shell.execute_reply":"2024-03-16T10:24:09.839895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# 상위 10개 case_id의 값 분포를 시각화\ntop_case_ids = df['case_id'].value_counts().head(100)\ntop_case_ids.plot(kind='bar')\n\n# 그래프 제목 설정\nplt.title('Top 10 case_id Distribution')\n\n# x축 라벨 설정\nplt.xlabel('case_id')\n\n# y축 라벨 설정\nplt.ylabel('Frequency')\n\n# 그래프 출력\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:10.052548Z","iopub.execute_input":"2024-03-16T10:24:10.053093Z","iopub.status.idle":"2024-03-16T10:24:11.084205Z","shell.execute_reply.started":"2024-03-16T10:24:10.053053Z","shell.execute_reply":"2024-03-16T10:24:11.083081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['case_id']==1377353].sort_values(by='num_group1').head(30)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:11.086002Z","iopub.execute_input":"2024-03-16T10:24:11.08717Z","iopub.status.idle":"2024-03-16T10:24:11.108601Z","shell.execute_reply.started":"2024-03-16T10:24:11.087134Z","shell.execute_reply":"2024-03-16T10:24:11.106751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['num_group1'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:11.11015Z","iopub.execute_input":"2024-03-16T10:24:11.110959Z","iopub.status.idle":"2024-03-16T10:24:11.12315Z","shell.execute_reply.started":"2024-03-16T10:24:11.110916Z","shell.execute_reply":"2024-03-16T10:24:11.121643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['last180dayturnover_1134A'].notnull()]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:15.481453Z","iopub.execute_input":"2024-03-16T10:24:15.483003Z","iopub.status.idle":"2024-03-16T10:24:15.501484Z","shell.execute_reply.started":"2024-03-16T10:24:15.482961Z","shell.execute_reply":"2024-03-16T10:24:15.500152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:15.686945Z","iopub.execute_input":"2024-03-16T10:24:15.68816Z","iopub.status.idle":"2024-03-16T10:24:15.69611Z","shell.execute_reply.started":"2024-03-16T10:24:15.688115Z","shell.execute_reply":"2024-03-16T10:24:15.695153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(df.case_id))","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:16.072902Z","iopub.execute_input":"2024-03-16T10:24:16.073562Z","iopub.status.idle":"2024-03-16T10:24:16.120417Z","shell.execute_reply.started":"2024-03-16T10:24:16.073529Z","shell.execute_reply":"2024-03-16T10:24:16.118891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_base.case_id)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:16.274461Z","iopub.execute_input":"2024-03-16T10:24:16.27496Z","iopub.status.idle":"2024-03-16T10:24:16.283457Z","shell.execute_reply.started":"2024-03-16T10:24:16.274926Z","shell.execute_reply":"2024-03-16T10:24:16.281795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"100*round(len(set(df.case_id))/len(train_base.case_id),3)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T10:24:16.674455Z","iopub.execute_input":"2024-03-16T10:24:16.675077Z","iopub.status.idle":"2024-03-16T10:24:16.723244Z","shell.execute_reply.started":"2024-03-16T10:24:16.675036Z","shell.execute_reply":"2024-03-16T10:24:16.721716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# case_id의 개수를 base에 붙여서 1,0과의 관계를 보자","metadata":{}},{"cell_type":"code","source":"card_counts = df['case_id'].value_counts().reset_index()\ncard_counts.columns = ['case_id', 'card_counts']\ncard_counts","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:18:58.165054Z","iopub.execute_input":"2024-03-16T11:18:58.166382Z","iopub.status.idle":"2024-03-16T11:18:58.207028Z","shell.execute_reply.started":"2024-03-16T11:18:58.166329Z","shell.execute_reply":"2024-03-16T11:18:58.205786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:19:05.117923Z","iopub.execute_input":"2024-03-16T11:19:05.118424Z","iopub.status.idle":"2024-03-16T11:19:05.13761Z","shell.execute_reply.started":"2024-03-16T11:19:05.118386Z","shell.execute_reply":"2024-03-16T11:19:05.135801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# merge","metadata":{}},{"cell_type":"code","source":"train_base_debitcard_1 = pd.merge(train_base, card_counts, how='inner', on='case_id')\ntrain_base_debitcard_1","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:19:08.305666Z","iopub.execute_input":"2024-03-16T11:19:08.306104Z","iopub.status.idle":"2024-03-16T11:19:08.573332Z","shell.execute_reply.started":"2024-03-16T11:19:08.306075Z","shell.execute_reply":"2024-03-16T11:19:08.572271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_base_debitcard_1","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:20:09.570118Z","iopub.execute_input":"2024-03-16T11:20:09.570569Z","iopub.status.idle":"2024-03-16T11:20:09.576611Z","shell.execute_reply.started":"2024-03-16T11:20:09.570537Z","shell.execute_reply":"2024-03-16T11:20:09.575194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:22:57.527659Z","iopub.execute_input":"2024-03-16T11:22:57.528156Z","iopub.status.idle":"2024-03-16T11:22:57.561498Z","shell.execute_reply.started":"2024-03-16T11:22:57.528123Z","shell.execute_reply":"2024-03-16T11:22:57.560116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['target']==0]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:20:24.847087Z","iopub.execute_input":"2024-03-16T11:20:24.847527Z","iopub.status.idle":"2024-03-16T11:20:24.872172Z","shell.execute_reply.started":"2024-03-16T11:20:24.847495Z","shell.execute_reply":"2024-03-16T11:20:24.870582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['target']==1]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:20:36.348817Z","iopub.execute_input":"2024-03-16T11:20:36.349293Z","iopub.status.idle":"2024-03-16T11:20:36.368836Z","shell.execute_reply.started":"2024-03-16T11:20:36.349256Z","shell.execute_reply":"2024-03-16T11:20:36.367229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:48:21.860141Z","iopub.execute_input":"2024-03-16T11:48:21.860661Z","iopub.status.idle":"2024-03-16T11:48:21.87244Z","shell.execute_reply.started":"2024-03-16T11:48:21.860628Z","shell.execute_reply":"2024-03-16T11:48:21.871285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 히스토그램을 그리기 위한 데이터 준비\nrepaid = df[df['target'] == 0]['card_counts']\nnot_repaid = df[df['target'] == 1]['card_counts']","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:30:16.529322Z","iopub.execute_input":"2024-03-16T11:30:16.529827Z","iopub.status.idle":"2024-03-16T11:30:16.547377Z","shell.execute_reply.started":"2024-03-16T11:30:16.529792Z","shell.execute_reply":"2024-03-16T11:30:16.545881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 히스토그램 시각화\nplt.hist(repaid, bins=30, alpha=0.5, label='Repaid')\nplt.hist(not_repaid, bins=30, alpha=0.5, label='Not Repaid')\nplt.title('Histogram of Card Counts by Repayment Status')\nplt.xlabel('Card Counts')\nplt.ylabel('Frequency')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:30:21.574236Z","iopub.execute_input":"2024-03-16T11:30:21.574714Z","iopub.status.idle":"2024-03-16T11:30:22.033349Z","shell.execute_reply.started":"2024-03-16T11:30:21.574663Z","shell.execute_reply":"2024-03-16T11:30:22.031741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# 대출을 갚지 않는 고객('target' == 1)의 'card_counts' 데이터만 선택\nnot_repaid = df[df['target'] == 1]['card_counts']\n\nplt.figure(figsize=(10, 6))\nplt.hist(not_repaid, bins=30, alpha=0.5, color='red', label='Not Repaid')\nplt.title('Histogram of Card Counts for Non-Repayment')\nplt.xlabel('Card Counts')\nplt.ylabel('Frequency')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:32:48.909988Z","iopub.execute_input":"2024-03-16T11:32:48.910407Z","iopub.status.idle":"2024-03-16T11:32:49.271258Z","shell.execute_reply.started":"2024-03-16T11:32:48.910377Z","shell.execute_reply":"2024-03-16T11:32:49.270292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.hist(repaid, bins=30, alpha=0.5, density=True, label='Repaid')\nplt.hist(not_repaid, bins=30, alpha=0.5, density=True, label='Not Repaid')\nplt.title('Normalized Histogram of Card Counts by Repayment Status')\nplt.xlabel('Card Counts')\nplt.ylabel('Density')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:33:16.742135Z","iopub.execute_input":"2024-03-16T11:33:16.742613Z","iopub.status.idle":"2024-03-16T11:33:17.16625Z","shell.execute_reply.started":"2024-03-16T11:33:16.742576Z","shell.execute_reply":"2024-03-16T11:33:17.164867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.hist(repaid, bins=30, alpha=0.5, label='Repaid')\nplt.hist(not_repaid, bins=30, alpha=0.5, label='Not Repaid')\nplt.yscale('log')\nplt.title('Histogram of Card Counts by Repayment Status with Log Scale')\nplt.xlabel('Card Counts')\nplt.ylabel('Log Frequency')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:33:45.860229Z","iopub.execute_input":"2024-03-16T11:33:45.860657Z","iopub.status.idle":"2024-03-16T11:33:46.974253Z","shell.execute_reply.started":"2024-03-16T11:33:45.860626Z","shell.execute_reply":"2024-03-16T11:33:46.972523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nplt.figure(figsize=(10, 6))\nsns.kdeplot(df[df['target'] == 0]['card_counts'], label='Repaid (Target 0)', shade=True)\nsns.kdeplot(df[df['target'] == 1]['card_counts'], label='Not Repaid (Target 1)', shade=True)\nplt.title('Density Plot of Card Counts by Repayment Status')\nplt.xlabel('Card Counts')\nplt.ylabel('Density')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:48:25.403148Z","iopub.execute_input":"2024-03-16T11:48:25.403868Z","iopub.status.idle":"2024-03-16T11:48:26.43031Z","shell.execute_reply.started":"2024-03-16T11:48:25.403833Z","shell.execute_reply":"2024-03-16T11:48:26.428989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 대출을 갚지 않는 고객('target' == 1)의 'card_counts' 데이터만 선택\nnot_repaid = df[df['target'] == 1]['card_counts']\n\nplt.figure(figsize=(10, 6))\nplt.hist(not_repaid, bins=30, alpha=0.5, color='red', log=True, label='Not Repaid (Target 1)')\nplt.title('Log-Scale Histogram of Card Counts for Non-Repayment')\nplt.xlabel('Card Counts')\nplt.ylabel('Log Frequency')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:48:37.965815Z","iopub.execute_input":"2024-03-16T11:48:37.966279Z","iopub.status.idle":"2024-03-16T11:48:38.667371Z","shell.execute_reply.started":"2024-03-16T11:48:37.966243Z","shell.execute_reply":"2024-03-16T11:48:38.665986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_target1 = df[df['target']==1]\ndf_target1['card_counts'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:58:00.561936Z","iopub.execute_input":"2024-03-16T11:58:00.562384Z","iopub.status.idle":"2024-03-16T11:58:00.578917Z","shell.execute_reply.started":"2024-03-16T11:58:00.562353Z","shell.execute_reply":"2024-03-16T11:58:00.577426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_target1 = df[df['target']==0]\ndf_target1['card_counts'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:58:54.446532Z","iopub.execute_input":"2024-03-16T11:58:54.447026Z","iopub.status.idle":"2024-03-16T11:58:54.468389Z","shell.execute_reply.started":"2024-03-16T11:58:54.446993Z","shell.execute_reply":"2024-03-16T11:58:54.466863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 카드 개수가 10개 이상인 case_id는 제거한다. 총 제거한 row 개수 : 140개","metadata":{}},{"cell_type":"code","source":"df[df['card_counts']>=10]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:59:46.086314Z","iopub.execute_input":"2024-03-16T11:59:46.086831Z","iopub.status.idle":"2024-03-16T11:59:46.10558Z","shell.execute_reply.started":"2024-03-16T11:59:46.086793Z","shell.execute_reply":"2024-03-16T11:59:46.104256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df['card_counts']<=10]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T11:59:54.89522Z","iopub.execute_input":"2024-03-16T11:59:54.895669Z","iopub.status.idle":"2024-03-16T11:59:54.931457Z","shell.execute_reply.started":"2024-03-16T11:59:54.895637Z","shell.execute_reply":"2024-03-16T11:59:54.930174Z"},"trusted":true},"execution_count":null,"outputs":[]}]}