{"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":"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-02-29T16:37:08.434542Z","iopub.execute_input":"2024-02-29T16:37:08.435126Z","iopub.status.idle":"2024-02-29T16:37:09.786144Z","shell.execute_reply.started":"2024-02-29T16:37:08.435079Z","shell.execute_reply":"2024-02-29T16:37:09.784575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom collections import Counter\nfrom imblearn.under_sampling import RandomUnderSampler\n\nimport numpy as np\nimport pandas as pd\nimport polars as pl\nimport pyarrow.parquet as pq\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:39:45.419670Z","iopub.execute_input":"2024-02-29T16:39:45.420085Z","iopub.status.idle":"2024-02-29T16:39:45.428088Z","shell.execute_reply.started":"2024-02-29T16:39:45.420046Z","shell.execute_reply":"2024-02-29T16:39:45.425640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base_path = '/kaggle/input/home-credit-credit-risk-model-stability/parquet_files/train/train_base.parquet'","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:39:56.487798Z","iopub.execute_input":"2024-02-29T16:39:56.488242Z","iopub.status.idle":"2024-02-29T16:39:56.493733Z","shell.execute_reply.started":"2024-02-29T16:39:56.488210Z","shell.execute_reply":"2024-02-29T16:39:56.492364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the Parquet file into a Pandas DataFrame\ntrain_base_df = pd.read_parquet(train_base_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:40:03.369118Z","iopub.execute_input":"2024-02-29T16:40:03.369522Z","iopub.status.idle":"2024-02-29T16:40:03.533470Z","shell.execute_reply.started":"2024-02-29T16:40:03.369484Z","shell.execute_reply":"2024-02-29T16:40:03.532028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define X and y\nX = train_base_df.drop(columns=['target'])  # Extract features\ny = train_base_df['target']  # Extract target\n\n# Apply random under-sampling with specified sampling strategy\nrus = RandomUnderSampler(random_state=42)\nX_res, y_res = rus.fit_resample(X, y)\n\n# Print resampled class distribution\nprint('Resampled dataset shape %s' % Counter(y_res))","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:40:04.304376Z","iopub.execute_input":"2024-02-29T16:40:04.305225Z","iopub.status.idle":"2024-02-29T16:40:04.860557Z","shell.execute_reply.started":"2024-02-29T16:40:04.305178Z","shell.execute_reply":"2024-02-29T16:40:04.859052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new DataFrame for the resampled dataset\nresampled_df = pd.DataFrame(X_res, columns=X.columns)\nresampled_df['target'] = y_res\n","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:40:18.245322Z","iopub.execute_input":"2024-02-29T16:40:18.245765Z","iopub.status.idle":"2024-02-29T16:40:18.252894Z","shell.execute_reply.started":"2024-02-29T16:40:18.245728Z","shell.execute_reply":"2024-02-29T16:40:18.251494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Calculate the counts of each target category\ntarget_counts = resampled_df['target'].value_counts()\n\n# Plot a pie chart\nplt.figure(figsize=(8, 8))\nplt.pie(target_counts, labels=target_counts.index, autopct='%1.1f%%', startangle=140)\nplt.title('Target Distribution')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:40:34.012195Z","iopub.execute_input":"2024-02-29T16:40:34.012612Z","iopub.status.idle":"2024-02-29T16:40:34.263639Z","shell.execute_reply.started":"2024-02-29T16:40:34.012580Z","shell.execute_reply":"2024-02-29T16:40:34.261920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Thank you ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}