Bù giá trị phân loại

Xử lý Dữ liệu Khuyết trong Python

Suraj Donthi

Deep Learning & Computer Vision Consultant

Độ phức tạp với giá trị phân loại

  • Phần lớn giá trị phân loại là chuỗi
  • Không thể tính toán trực tiếp trên chuỗi
  • Cần chuyển/mã hóa chuỗi thành số rồi bù thiếu
Xử lý Dữ liệu Khuyết trong Python

Kỹ thuật chuyển đổi

One-hot encoder
Màu Color_Red Color_Green Color_Blue
Red 1 0 0
Green 0 1 0
Blue 0 0 1
Red 1 0 0
Blue 0 0 1
Blue 0 0 1
Ordinal encoder
Màu Giá trị
Red 0
Green 1
Blue 2
Red 0
Blue 2
Blue 2
Xử lý Dữ liệu Khuyết trong Python

Kỹ thuật bù thiếu

  • Điền bằng hạng mục xuất hiện nhiều nhất
  • Bù thiếu bằng mô hình thống kê như KNN
Xử lý Dữ liệu Khuyết trong Python

Dữ liệu hồ sơ người dùng

users = pd.read_csv('userprofile.csv')
users.head()
    smoker    drink_level    dress_preference ambience    hijos        activity     budget
0    False    abstemious        informal      family    independent    student      medium
1    False    abstemious        informal      family    independent    student      low
2    False    social drinker    formal        family    independent    student      low
3    False    abstemious        informal      family    independent    professional medium
4    False    abstemious     no preference    family    independent    student      medium
Xử lý Dữ liệu Khuyết trong Python

Mã hóa thứ bậc (Ordinal)

from sklearn.preprocessing import OrdinalEncoder

# Tạo Ordinal Encoder ambience_ord_enc = OrdinalEncoder() # Chọn giá trị khác thiếu trong ambience ambience = users['ambience'] ambience_not_null = ambience[ambience.notnull()] reshaped_vals = ambience_not_null.values.reshape(-1, 1)
# Mã hóa các giá trị khác thiếu của ambience encoded_vals = ambience_ord_enc.fit_transform(reshaped_vals)
# Thay cột ambience bằng giá trị thứ bậc users.loc[ambience.notnull(), 'ambience'] = np.squeeze(encoded_vals)
Xử lý Dữ liệu Khuyết trong Python

Mã hóa thứ bậc (Ordinal)

# Tạo từ điển cho các Ordinal Encoder
ordinal_enc_dict = {}

# Lặp qua các cột cần mã hóa
for col_name in users:
    # Tạo encoder cho cột
    ordinal_enc_dict[col_name] = OrdinalEncoder()
    col = users[col_name]

    # Chọn giá trị khác thiếu trong cột
    col_not_null = col[col.notnull()]
    reshaped_vals = col_not_null.values.reshape(-1, 1)

    # Mã hóa các giá trị khác thiếu của cột
    encoded_vals = ordinal_enc_dict[col_name].fit_transform(reshaped_vals)

    # Thay các giá trị trong cột bằng giá trị thứ bậc
    users.loc[col.notnull(), col_name] = np.squeeze(encoded_vals)
Xử lý Dữ liệu Khuyết trong Python

Bù thiếu bằng KNN

users_KNN_imputed = users.copy(deep=True)

# Tạo KNN imputer KNN_imputer = KNN()
users_KNN_imputed.iloc[:, :] = np.round(KNN_imputer.fit_transform(users))
for col_name in users_KNN_imputed: # Định dạng về 2 chiều để tránh lỗi khi lưu vào DataFrame reshaped = users_KNN_imputed[col_name].values.reshape(-1, 1) users_KNN_imputed[col_name] = \ ordinal_enc_dict[col_name].inverse_transform(reshaped)
Xử lý Dữ liệu Khuyết trong Python

Tóm tắt

Các bước bù giá trị phân loại

  • Chuyển các cột phân loại không thiếu sang giá trị thứ bậc
  • Bù giá trị thiếu trên DataFrame thứ bậc
  • Chuyển lại từ giá trị thứ bậc về phân loại
Xử lý Dữ liệu Khuyết trong Python

Ayo berlatih!

Xử lý Dữ liệu Khuyết trong Python

Preparing Video For Download...