{"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":7921029,"sourceType":"competition"}],"dockerImageVersionId":30664,"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)\nimport polars as pl\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-04-02T07:26:20.117026Z","iopub.execute_input":"2024-04-02T07:26:20.117462Z","iopub.status.idle":"2024-04-02T07:26:20.489241Z","shell.execute_reply.started":"2024-04-02T07:26:20.117427Z","shell.execute_reply":"2024-04-02T07:26:20.4882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_csv_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/\"\nbase_train_csv_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/\"\nbase_test_csv_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/test/\"\nfeature_def_path = \"/kaggle/input/home-credit-credit-risk-model-stability/feature_definitions.csv\"\\\n\n\nbase_parquet_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/parquet_files/\"\nbase_train_parquet_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/parquet_files/train/\"\nbase_test_parquet_dir = \"/kaggle/input/home-credit-credit-risk-model-stability/parquet_files/test/\"\nfeature_def_path = \"/kaggle/input/home-credit-credit-risk-model-stability/feature_definitions.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:24:30.461917Z","iopub.execute_input":"2024-04-02T07:24:30.462301Z","iopub.status.idle":"2024-04-02T07:24:30.468785Z","shell.execute_reply.started":"2024-04-02T07:24:30.462274Z","shell.execute_reply":"2024-04-02T07:24:30.467458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk(base_train_parquet_dir):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:25:01.102144Z","iopub.execute_input":"2024-04-02T07:25:01.102525Z","iopub.status.idle":"2024-04-02T07:25:01.109459Z","shell.execute_reply.started":"2024-04-02T07:25:01.102496Z","shell.execute_reply":"2024-04-02T07:25:01.108494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base_df = pl.read_parquet(base_train_parquet_dir + \"train_base.parquet\")\ntrain_base_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:29:26.992221Z","iopub.execute_input":"2024-04-02T07:29:26.99301Z","iopub.status.idle":"2024-04-02T07:29:27.123722Z","shell.execute_reply.started":"2024-04-02T07:29:26.992972Z","shell.execute_reply":"2024-04-02T07:29:27.122508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base_df['case_id'].count()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:29:53.842355Z","iopub.execute_input":"2024-04-02T07:29:53.843689Z","iopub.status.idle":"2024-04-02T07:29:53.852004Z","shell.execute_reply.started":"2024-04-02T07:29:53.843609Z","shell.execute_reply":"2024-04-02T07:29:53.850794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_def_df = pd.read_csv(feature_def_path)\nall_columns = feature_def_df['Variable'].to_list()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:01:17.995622Z","iopub.execute_input":"2024-03-16T14:01:17.995983Z","iopub.status.idle":"2024-03-16T14:01:18.016853Z","shell.execute_reply.started":"2024-03-16T14:01:17.995926Z","shell.execute_reply":"2024-03-16T14:01:18.016003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_credit_bureau_a_1_3.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:01:18.018336Z","iopub.execute_input":"2024-03-16T14:01:18.018631Z","iopub.status.idle":"2024-03-16T14:01:45.492281Z","shell.execute_reply.started":"2024-03-16T14:01:18.018605Z","shell.execute_reply":"2024-03-16T14:01:45.49125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = ['train_credit_bureau_a_2_8.csv',\n 'train_credit_bureau_a_2_9.csv',\n 'train_credit_bureau_a_2_10.csv',\n 'train_credit_bureau_b_1.csv',\n 'train_credit_bureau_b_2.csv',\n 'train_debitcard_1.csv',\n 'train_deposit_1.csv',\n 'train_other_1.csv']\n\n\nfor file in files:\n    file_name = file.split(\".\")[0] + \"_desc\" + \".csv\"\n    ens = []\n    chs = []\n    for column in train_credit_bureau_a_2_8_df.columns:\n        piece = []\n        if column in all_columns_dict:\n            en = all_columns_dict[column]\n            ch = trans(en)\n            piece = [column, en, ch]\n        else:\n            en = f\"{column} not in all_columns\"\n            ch = trans(en)\n        ens.append(en)\n        chs.append(ch)\n    trans_df = pd.DataFrame([ens, chs], columns=train_credit_bureau_a_2_8_df.columns)\n    head_df = train_credit_bureau_a_2_8_df.head(5)\n\n    df = pd.concat([trans_df, head_df])\n    df = df.reset_index().drop('index', axis = 1)\n    \n    df.to_csv(\"/kaggle/working/desc/\" + file_name)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:38:44.019212Z","iopub.execute_input":"2024-03-16T15:38:44.019624Z","iopub.status.idle":"2024-03-16T15:38:50.069665Z","shell.execute_reply.started":"2024-03-16T15:38:44.019595Z","shell.execute_reply":"2024-03-16T15:38:50.068827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_credit_bureau_a_2_8_path = base_train_csv_dir + files[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:01:45.503707Z","iopub.execute_input":"2024-03-16T14:01:45.504628Z","iopub.status.idle":"2024-03-16T14:01:45.524741Z","shell.execute_reply.started":"2024-03-16T14:01:45.504593Z","shell.execute_reply":"2024-03-16T14:01:45.523359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_credit_bureau_a_2_8_df = pd.read_csv(train_credit_bureau_a_2_8_path)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:01:45.526981Z","iopub.execute_input":"2024-03-16T14:01:45.52773Z","iopub.status.idle":"2024-03-16T14:02:37.514297Z","shell.execute_reply.started":"2024-03-16T14:01:45.527693Z","shell.execute_reply":"2024-03-16T14:02:37.513075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/desc","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:38:15.023591Z","iopub.execute_input":"2024-03-16T15:38:15.024018Z","iopub.status.idle":"2024-03-16T15:38:16.160013Z","shell.execute_reply.started":"2024-03-16T15:38:15.023976Z","shell.execute_reply":"2024-03-16T15:38:16.158366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pygtrans import Translate\n\n\ndef trans(source_text):\n    translator = Translate()\n    translated_text = translator.translate(source_text)\n    return translated_text.translatedText\n","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:24:01.137504Z","iopub.execute_input":"2024-03-16T15:24:01.137913Z","iopub.status.idle":"2024-03-16T15:24:01.142581Z","shell.execute_reply.started":"2024-03-16T15:24:01.137882Z","shell.execute_reply":"2024-03-16T15:24:01.141793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\ndef remove_nested_parentheses(text):\n    # 定义匹配括号内容的正则表达式\n    pattern = r'\\([^()]*\\)'\n    \n    # 使用递归匹配删除括号内的内容\n    while re.search(pattern, text):\n        text = re.sub(pattern, '', text)\n    \n    return text\ns = \"Days past due of the payment for the active contract (num_group1 - existing contract, num_group2 - payment)\"\n\nremove_nested_parentheses(s)","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:26:51.73753Z","iopub.execute_input":"2024-03-16T15:26:51.737927Z","iopub.status.idle":"2024-03-16T15:26:51.746916Z","shell.execute_reply.started":"2024-03-16T15:26:51.737897Z","shell.execute_reply":"2024-03-16T15:26:51.745704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_columns_dict = {}\n\nfor index, row in feature_def_df.iterrows():\n    all_columns_dict[row[0]] = row[1]","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:02:37.540617Z","iopub.execute_input":"2024-03-16T14:02:37.541063Z","iopub.status.idle":"2024-03-16T14:02:37.600753Z","shell.execute_reply.started":"2024-03-16T14:02:37.541026Z","shell.execute_reply":"2024-03-16T14:02:37.59953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train_credit_bureau_a_2_8_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-16T14:02:37.602286Z","iopub.execute_input":"2024-03-16T14:02:37.603027Z","iopub.status.idle":"2024-03-16T14:02:37.634001Z","shell.execute_reply.started":"2024-03-16T14:02:37.602996Z","shell.execute_reply":"2024-03-16T14:02:37.632725Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:30:33.806326Z","iopub.execute_input":"2024-03-16T15:30:33.806725Z","iopub.status.idle":"2024-03-16T15:30:34.587168Z","shell.execute_reply.started":"2024-03-16T15:30:33.806694Z","shell.execute_reply":"2024-03-16T15:30:34.586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:25:56.480259Z","iopub.execute_input":"2024-03-16T15:25:56.481053Z","iopub.status.idle":"2024-03-16T15:25:56.504399Z","shell.execute_reply.started":"2024-03-16T15:25:56.481018Z","shell.execute_reply":"2024-03-16T15:25:56.503152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:26:37.22715Z","iopub.execute_input":"2024-03-16T15:26:37.229333Z","iopub.status.idle":"2024-03-16T15:26:37.258084Z","shell.execute_reply.started":"2024-03-16T15:26:37.229278Z","shell.execute_reply":"2024-03-16T15:26:37.257134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:30:02.692672Z","iopub.execute_input":"2024-03-16T15:30:02.69309Z","iopub.status.idle":"2024-03-16T15:30:02.725621Z","shell.execute_reply.started":"2024-03-16T15:30:02.693059Z","shell.execute_reply":"2024-03-16T15:30:02.724347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pygtrans","metadata":{"execution":{"iopub.status.busy":"2024-03-16T15:06:21.392518Z","iopub.execute_input":"2024-03-16T15:06:21.39432Z","iopub.status.idle":"2024-03-16T15:06:36.800062Z","shell.execute_reply.started":"2024-03-16T15:06:21.394271Z","shell.execute_reply":"2024-03-16T15:06:36.798761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}