Logistic regression และการคัดเลือกฟีเจอร์
ในแบบฝึกหัดนี้ เราจะทำการคัดเลือกฟีเจอร์บนชุดข้อมูลการวิเคราะห์ความรู้สึกจากรีวิวภาพยนตร์โดยใช้ L1 regularization โดยฟีเจอร์และ target ถูกโหลดไว้ให้แล้วในตัวแปร X_train และ y_train
เราจะค้นหาค่าที่ดีที่สุดของ C โดยใช้ GridSearchCV() ของ scikit-learn ซึ่งได้เรียนไปแล้วในคอร์สก่อนหน้า
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Linear Classifiers ใน Python
คำแนะนำการฝึกหัด
- สร้างออบเจ็กต์ logistic regression ที่ใช้ L1 regularization
- หาค่า
Cที่ทำให้ cross-validation error ต่ำที่สุด - แสดงจำนวนฟีเจอร์ที่ถูกคัดเลือกสำหรับค่า
Cนี้
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Specify L1 regularization
lr = LogisticRegression(solver='liblinear', ____)
# Instantiate the GridSearchCV object and run the search
searcher = GridSearchCV(lr, {'C':[0.001, 0.01, 0.1, 1, 10]})
searcher.fit(X_train, y_train)
# Report the best parameters
print("Best CV params", searcher.best_params_)
# Find the number of nonzero coefficients (selected features)
best_lr = searcher.best_estimator_
coefs = best_lr.____
print("Total number of features:", coefs.size)
print("Number of selected features:", np.count_nonzero(coefs))