更复杂的 Bagging 模型
在探索过半导体数据后,现在请构建一个袋装法分类器,根据输入特征预测 'Pass/Fail' 标签。
预处理后的数据集已作为 uci_secom 提供在您的工作区中,训练集和测试集也已为您准备好。
由于目标变量存在严重类别不平衡,请在此使用 "balanced" 的逻辑回归作为基学习器。
我们还将通过设置 solver='liblinear' 来减少 LogisticRegression 的计算时间,它比默认求解器更快。
本练习是课程的一部分
Python 中的集成方法
练习说明
- 实例化一个逻辑回归作为基础分类器,参数为:
class_weight='balanced'、solver='liblinear'、random_state=42。 - 使用该逻辑回归作为基学习器构建一个袋装法分类器,指定最大特征数为
10,并启用袋外评分。 - 打印袋外评分,以便与准确率进行比较。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Build a balanced logistic regression
clf_lr = ____
# Build and fit a bagging classifier
clf_bag = ____(____, ____, ____, random_state=500)
clf_bag.fit(X_train, y_train)
# Evaluate the accuracy on the test set and show the out-of-bag score
pred = clf_bag.predict(X_test)
print('Accuracy: {:.2f}'.format(accuracy_score(y_test, pred)))
print('OOB-Score: {:.2f}'.format(____))
# Print the confusion matrix
print(confusion_matrix(y_test, pred))