默认阈值
您想确认 DecisionTreeClassifier() 是否使用与上一课提到的一样的默认分类阈值 0.5。所有分类器都用同一个阈值听起来有点奇怪。我们来验证一下!已为您预加载了一个已拟合的决策树分类器 clf,以及训练集和测试集(常用变量名):X_train、X_test、y_train 和 y_test。您需要使用 .predict_proba() 方法从分类器中提取概率分数。
本练习是课程的一部分
用 Python 设计机器学习工作流
练习说明
- 使用预加载的分类器
clf为测试样本生成分数。 - 现在从分数中提取标签。请记住,每个样本对应一对分数,而不是单个分数,其中第二个元素是正类的概率。
- 现在使用标准的
.predict()方法为测试数据生成标签。 - 最后,将其与前面得到的预测进行比较。它们是否完全一致?
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Score the test data using the given classifier
scores = clf.____(____)
# Get labels from the scores using the default threshold
preds = [s[____] > ____ for s in scores]
# Use the predict method to label the test data again
preds_default = clf.____(____)
# Compare the two sets of predictions
____(preds == preds_default)