Logistic regression แบบ Regularized
ในบทที่ 1 คุณได้ใช้ logistic regression กับชุดข้อมูลตัวเลขลายมือ ในแบบฝึกหัดนี้ เราจะมาสำรวจผลกระทบของ L2 regularization กัน
ชุดข้อมูลตัวเลขลายมือถูกโหลด แบ่ง และจัดเก็บไว้ในตัวแปร X_train, y_train, X_valid และ y_valid เรียบร้อยแล้ว ส่วนตัวแปร train_errs และ valid_errs ได้รับการกำหนดค่าเริ่มต้นเป็นลิสต์ว่างแล้วเช่นกัน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Linear Classifiers ใน Python
คำแนะนำการฝึกหัด
- วนลูปผ่านค่าต่าง ๆ ของ
C_valueโดยสร้างและ fit โมเดลLogisticRegressionในแต่ละรอบ - บันทึก error บนชุดข้อมูล training และ validation สำหรับแต่ละโมเดล
- สร้างกราฟแสดง training error และ testing error เทียบกับค่าพารามิเตอร์ regularization
C - ดูจากกราฟ ค่าที่ดีที่สุดของ
Cคือค่าใด?
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Train and validaton errors initialized as empty list
train_errs = list()
valid_errs = list()
# Loop over values of C_value
for C_value in [0.001, 0.01, 0.1, 1, 10, 100, 1000]:
# Create LogisticRegression object and fit
lr = ____
lr.fit(____)
# Evaluate error rates and append to lists
train_errs.append( 1.0 - lr.score(____) )
valid_errs.append( 1.0 - lr.score(____) )
# Plot results
plt.semilogx(C_values, train_errs, C_values, valid_errs)
plt.legend(("train", "validation"))
plt.show()