การแปลงข้อมูล Train และ Test (I)
จนถึงตอนนี้ได้สร้าง scaler จากคอลัมน์หนึ่ง แล้วนำ scaler นั้นไปใช้กับข้อมูลชุดเดียวกับที่ใช้ฝึก เมื่อสร้างโมเดล machine learning โดยทั่วไปจะสร้างโมเดลจากข้อมูลในอดีต (ชุด train) และนำโมเดลไปใช้กับข้อมูลใหม่ที่ยังไม่เคยเห็น (ชุด test) ในกรณีเหล่านี้ต้องแน่ใจว่าการ scaling เดิมถูกนำไปใช้กับทั้งข้อมูล train และ test
ในทางปฏิบัติ ให้ fit scaler บนชุด train และเก็บ scaler ที่ฝึกแล้วไว้เพื่อนำไปใช้กับชุด test โดยไม่ควร retrain scaler บนชุด test เด็ดขาด
สำหรับแบบฝึกหัดนี้และแบบถัดไป เราได้แบ่ง DataFrame so_numeric_df ออกเป็นชุด train (so_train_numeric) และชุด test (so_test_numeric)
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Feature Engineering สำหรับ Machine Learning ใน Python
คำแนะนำการฝึกหัด
- สร้าง instance ของ
StandardScaler()และกำหนดให้กับตัวแปรSS_scaler - Fit
StandardScalerบนคอลัมน์Age - Transform คอลัมน์
Ageในชุด test (so_test_numeric)
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import StandardScaler
from sklearn.preprocessing import StandardScaler
# Apply a standard scaler to the data
SS_scaler = ____
# Fit the standard scaler to the data
____
# Transform the test data using the fitted scaler
so_test_numeric['Age_ss'] = ____
print(so_test_numeric[['Age', 'Age_ss']].head())