ปรับมาตรฐานตัวแปร
ถึงขั้นตอนสุดท้ายของการเตรียมข้อมูลแล้ว คราวนี้จะแปลงชุดข้อมูล wholesale_boxcox ที่ปรับความเบ้แล้ว ให้อยู่ในสเกลเดียวกัน นั่นคือทุกคอลัมน์มีค่าเฉลี่ยเท่ากับศูนย์และส่วนเบี่ยงเบนมาตรฐานเท่ากับ 1 โดยใช้ฟังก์ชัน StandardScaler จากโมดูล sklearn.preprocessing
ชุดข้อมูล wholesale_coxbox ที่ปรับความเบ้แล้วจากแบบฝึกหัดก่อนหน้าถูกนำเข้ามาในรูปแบบ pandas DataFrame และได้สร้าง instance ของ StandardScaler() ไว้แล้วในชื่อ scaler
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับการตลาดด้วย Python
คำแนะนำการฝึกหัด
- Fit instance ของ
scalerที่เตรียมไว้บนชุดข้อมูลที่ผ่านการแปลง Box-Cox - แปลงชุดข้อมูลและเก็บผลลัพธ์ที่ปรับสเกลแล้วไว้ในชื่อ
wholesale_scaled - สร้าง
pandasDataFrame จากชุดข้อมูลที่ปรับสเกลแล้ว - แสดงค่าเฉลี่ยและส่วนเบี่ยงเบนมาตรฐานของทุกคอลัมน์
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Fit the initialized `scaler` instance on the Box-Cox transformed dataset
scaler.___(wholesale_boxcox)
# Transform and store the scaled dataset as `wholesale_scaled`
wholesale_scaled = scaler.___(wholesale_boxcox)
# Create a `pandas` DataFrame from the scaled dataset
wholesale_scaled_df = pd.DataFrame(data=___,
index=wholesale_boxcox.___,
columns=wholesale_boxcox.columns)
# Print the mean and standard deviation for all columns
print(wholesale_scaled_df.agg(['___','std']).round())