<jupyter_start><jupyter_text>檢視資料中各個欄位類型的數量<jupyter_code>app_train.dtypes.value_counts()<jupyter_output><empty_output><jupyter_text>檢視資料中類別型欄位各自類別的數量<jupyter_code>app_train.select_dtypes(include=["object"]).apply(pd.Series.nunique, axis = 0)<jupyter_output><empty_output><jupyter_text>Label encoding有仔細閱讀[參考資料](https://medium.com/@contactsunny/label-encoder-vs-one-hot-encoder-in-machine-learning-3fc273365621)的人可以發現，Label encoding 的表示方式會讓同一個欄位底下的類別之間有大小關係 (0<1<2<...)，所以在這裡我們只對有類別數量小於等於 2 的類別型欄位示範使用 Label encoding，但不表示這樣處理是最好的，一切取決於欄位本身的意義適合哪一種表示方法<jupyter_code>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
            
print('%d columns were label encoded.' % le_count)<jupyter_output>3 columns were label encoded.<jupyter_text>One Hot encodingpandas 中的 one hot encoding 非常方便，一行程式碼就搞定<jupyter_code>app_train = pd.get_dummies(app_train)
app_test = pd.get_dummies(app_test)

print(app_train['CODE_GENDER_F'].head())
print(app_train['CODE_GENDER_M'].head())
print(app_train['NAME_EDUCATION_TYPE_Academic degree'].head())<jupyter_output>0    0
1    1
2    0
3    1
4    0
Name: CODE_GENDER_F, dtype: uint8
0    1
1    0
2    1
3    0
4    1
Name: CODE_GENDER_M, dtype: uint8
0    0
1    0
2    0
3    0
4    0
Name: NAME_EDUCATION_TYPE_Academic degree, dtype: uint8<jupyter_text>可以觀察到原來的類別型欄位都轉為 0/1 了  作業將下列部分資料片段 sub_train 使用 One Hot encoding, 並觀察轉換前後的欄位數量 (使用 shape) 與欄位名稱 (使用 head) 變化<jupyter_code>app_train = pd.read_csv(f_app_train)
sub_train = pd.DataFrame(app_train['WEEKDAY_APPR_PROCESS_START'])
print(sub_train.shape)
sub_train.head()
"""
Your Code Here
"""<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10322874033651659, 'fraction_numerical': 0.02501136880400182, 'mean_word_length': 4.513784461152882, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '28074', 'license': 'MIT', 'n_tokens_mistral': 990, 'n_tokens_neox': 1007, 'n_words': 160, 'path': 'homeworks/D004/Day_004_column_data_type.ipynb', 'repo_name': 'peteryuX/100Day-ML-Marathon', 'size': '2199'}