เข้ารหัสตัวแปรประเภทหมวดหมู่และปรับสเกลตัวแปรตัวเลข
ในขั้นตอนสุดท้ายนี้ จะทำการ one-hot encoding กับตัวแปรประเภทหมวดหมู่ แล้วจึงปรับสเกลคอลัมน์ตัวเลข ไลบรารี pandas ถูกโหลดให้แล้วในชื่อ pd พร้อมกับโมดูล StandardScaler จาก sklearn.preprocessing
ชุดข้อมูล telecom churn ดิบ telco_raw ถูกโหลดให้แล้วในรูปแบบ pandas DataFrame รวมถึงลิสต์ custid, target, categorical และ numerical ที่มีชื่อคอลัมน์ซึ่งสร้างไว้ในแบบฝึกหัดก่อนหน้า สามารถสำรวจชุดข้อมูลเพิ่มเติมได้ผ่าน console
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับการตลาดด้วย Python
คำแนะนำการฝึกหัด
- ทำ one-hot encoding กับตัวแปรประเภทหมวดหมู่
- สร้าง instance ของ
StandardScaler - Fit และแปลงข้อมูลด้วย
scalerบนคอลัมน์ตัวเลข - สร้าง DataFrame จาก
scaled_numerical
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Perform one-hot encoding to categorical variables
telco_raw = pd.get_dummies(data = ___, columns = categorical, drop_first=True)
# Initialize StandardScaler instance
scaler = ___()
# Fit and transform the scaler on numerical columns
scaled_numerical = ___.fit_transform(telco_raw[___])
# Build a DataFrame from scaled_numerical
scaled_numerical = pd.DataFrame(___, columns=numerical)