เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การเข้ารหัสข้อมูลประเภท Categorical

เพื่อนร่วมงานของคุณได้แปลงคอลัมน์ในชุดข้อมูลสินเชื่อให้เป็นค่าตัวเลขโดยใช้ LabelEncoder() แต่ยังเหลือคอลัมน์ credit_history ซึ่งบันทึกประวัติสินเชื่อของผู้สมัคร คุณต้องการสร้างชุดข้อมูล 2 เวอร์ชันเพื่อเปรียบเทียบกัน โดยเวอร์ชันแรกใช้ LabelEncoder() และเวอร์ชันที่สองใช้ one-hot encoding เมทริกซ์ฟีเจอร์มีอยู่แล้วในตัวแปร credit และมี LabelEncoder() กับ pandas เป็น pd โหลดไว้ให้เรียบร้อยแล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การออกแบบ Machine Learning Workflows ด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • เข้ารหัส credit_history โดยใช้ LabelEncoder()
  • เชื่อมต่อผลลัพธ์ที่ได้เข้ากับ DataFrame เดิม
  • สร้าง DataFrame ใหม่โดยเชื่อมต่อ dummies จาก 1-hot encoding เข้ากับ DataFrame เดิม
  • ยืนยันว่า 1-hot encoding สร้างคอลัมน์ได้มากกว่า label encoding

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create numeric encoding for credit_history
credit_history_num = ____.____(
  credit[____])

# Create a new feature matrix including the numeric encoding
X_num = pd.concat([X, pd.Series(____)], ____)

# Create new feature matrix with dummies for credit_history
X_hot = pd.concat(
  [X, ____.____(credit[____])], ____)

# Compare the number of features of the resulting DataFrames
print(X_hot.shape[____] > X_num.shape[____])
แก้ไขและรันโค้ด