<jupyter_start><jupyter_text>練習時間參考 Day 12 範例程式，離散化你覺得有興趣的欄位，並嘗試找出有趣的訊息<jupyter_code># Import 需要的套件
import os
import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>之前做過的處理<jupyter_code># 設定 data_path
dir_data = './data/'
f_app_train = os.path.join(dir_data, 'application_train.csv')
f_app_test = os.path.join(dir_data, 'application_test.csv')

app_train = pd.read_csv(f_app_train)
app_test = pd.read_csv(f_app_test)

from sklearn.preprocessing import LabelEncoder

# Create a label encoder object
le = LabelEncoder()
le_count = 0

# Iterate through the columns
for col in app_train:
    if app_train[col].dtype == 'object':
        # If 2 or fewer unique categories
        if len(list(app_train[col].unique())) <= 2:
            # Train on the training data
            le.fit(app_train[col])
            # Transform both training and testing data
            app_train[col] = le.transform(app_train[col])
            app_test[col] = le.transform(app_test[col])
            
            # Keep track of how many columns were label encoded
            le_count += 1
            
app_train = pd.get_dummies(app_train)
app_test = pd.get_dummies(app_test)

# Create an anomalous flag column
app_train['DAYS_EMPLOYED_ANOM'] = app_train["DAYS_EMPLOYED"] == 365243
app_train['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
# also apply to testing dataset
app_test['DAYS_EMPLOYED_ANOM'] = app_test["DAYS_EMPLOYED"] == 365243
app_test["DAYS_EMPLOYED"].replace({365243: np.nan}, inplace = True)

# absolute the value of DAYS_BIRTH
app_train['DAYS_BIRTH'] = abs(app_train['DAYS_BIRTH'])
app_test['DAYS_BIRTH'] = abs(app_test['DAYS_BIRTH'])
app_train['YEAR_BIRTH'] = app_train['DAYS_BIRTH']/365

lower_bound = np.floor(app_train['YEAR_BIRTH'].min()).astype(np.int)
higher_bound = np.ceil(app_train['YEAR_BIRTH'].max()).astype(np.int)
step_list = [5, 10]
for step in step_list:
    app_train['equal_width_age'] = pd.cut(x=app_train['YEAR_BIRTH'], bins=range(lower_bound, higher_bound + step, step))
    age_group = app_train.groupby(by=['equal_width_age']).mean()

    temp_x = list(range(1, len(age_group.index) + 1))
    x = temp_x
    y = age_group['AMT_CREDIT']
    plt.bar(x, y)
    plt.xticks(temp_x, age_group.index, rotation=50, fontsize=8)
    plt.title('AMT_CREDIT by AGE Group')
    plt.show()
x = age_group.index
y = age_group['AMT_CREDIT']
sns.barplot(x ,y)
plt.xticks(rotation=50, fontsize=8)
plt.title('AMT_CREDIT by AGE Group')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12804642166344293, 'fraction_numerical': 0.01702127659574468, 'mean_word_length': 4.671052631578948, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '155221', 'license': 'MIT', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1022, 'n_words': 190, 'path': 'Day_013_HW.ipynb', 'repo_name': 'hengbinxu/ML100-Days', 'size': '2585'}