ผลลัพธ์จากโมเดล Lasso
หลังจาก train โมเดล Lasso แล้ว ขั้นตอนต่อไปคือการประเมินความสามารถในการทำนาย (\(R^2\)) บน test set และนับจำนวน feature ที่ถูกละเลย เนื่องจากค่าสัมประสิทธิ์ถูกลดลงเหลือศูนย์
ชุดข้อมูล X_test และ y_test ถูกโหลดไว้ให้แล้ว
โมเดล Lasso() และ StandardScaler() ถูก instantiate เป็น la และ scaler ตามลำดับ และทั้งคู่ถูก fit กับข้อมูล training แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การลดมิติข้อมูลใน Python
คำแนะนำการฝึกหัด
- Transform test set ด้วย scaler ที่ fit ไว้แล้ว
- คำนวณค่า \(R^2\) บนข้อมูล test ที่ผ่านการ scale แล้ว
- สร้างรายการที่มีค่า True เมื่อค่าสัมประสิทธิ์เท่ากับ 0
- คำนวณจำนวน feature ทั้งหมดที่มีค่าสัมประสิทธิ์เป็น 0
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Transform the test set with the pre-fitted scaler
X_test_std = scaler.____
# Calculate the coefficient of determination (R squared) on X_test_std
r_squared = la.____(____, ____)
print(f"The model can predict {r_squared:.1%} of the variance in the test set.")
# Create a list that has True values when coefficients equal 0
zero_coef = la.____ == ____
# Calculate how many features have a zero coefficient
n_ignored = sum(____)
print(f"The model has ignored {n_ignored} out of {len(la.coef_)} features.")