calibration - 多类分类的校准预测
问题描述
我们如何进行多类分类的校准预测?
我尝试遵循https://machinelearningmastery.com/calibrated-classification-model-in-scikit-learn/,但这不适用于多类问题,因为当我使用 sklearn.calibration.calibration_curve 时出现以下错误:
ValueError:仅支持二进制分类。提供标签 ['x' 'y' 'z' 'a' 'b']。
解决方案
一种方法是使用OneVsRestClassifier重新训练您的多类模型,然后将每个类视为一个单独的模型。我在下面粘贴了一个在 NLP 项目中使用的简单示例,希望对您有所帮助。
# Binarize the output
X_train = train_df['Text']
X_test = test_df['Text']
lb = LabelBinarizer()
y_train = lb.fit_transform(train_df['Target'])
y_test = lb.transform(test_df['Target'])
# Train a model with tfidf-vectorizer and LinearSVC
tfidf = TfidfVectorizer()
clf = LinearSVC()
clf = CalibratedClassifierCV(clf)
clf = OneVsRestClassifier(clf)
# Fit the model
pipe = Pipeline([('tfidf', tfidf), ('clf', clf)])
pipe.fit(X_train, y_train)
# Plot the Calibration Curve for every class
plt.figure(figsize=(20, 10))
ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
ax2 = plt.subplot2grid((3, 1), (2, 0))
ax1.plot([0, 1], [0, 1], "k:", label="Perfectly calibrated")
targets = range(len(lb.classes_))
for target in targets:
prob_pos = pipe.predict_proba(X_test)[:, target]
fraction_of_positives, mean_predicted_value = calibration_curve(y_test[:, target], prob_pos, n_bins=10)
name = lb.classes_[target]
ax1.plot(mean_predicted_value, fraction_of_positives, "s-", label="%s" % (name, ))
ax2.hist(prob_pos, range=(0, 1), bins=10, label=name, histtype="step", lw=2)
ax1.set_ylabel("The proportion of samples whose class is the positive class")
ax1.set_xlabel("The mean predicted probability in each bin")
ax1.set_ylim([-0.05, 1.05])
ax1.legend(loc="lower right")
ax1.set_title('Calibration plots (reliability curve)')
ax2.set_xlabel("Mean predicted value")
ax2.set_ylabel("Count")
ax2.legend(loc="upper center", ncol=2)
plt.tight_layout()
plt.show()
推荐阅读
- php - 生产中的 Docker + symfony 资产 404 + nginx
- jquery - 使用没有任何类的主体更改标签的颜色
- node.js - 测试完成而不运行任何断言(ava-nodejs)
- php - Laravel 删除只是不工作
- javascript - 加载资源失败:服务器响应状态为 405 ()
- ruby-on-rails - 撬显示模型命令不起作用
- javascript - 错误:需要数据和盐参数(节点未接收来自角度的用户输入对象?)
- python - ModuleNotFoundError:没有名为“cherrypy”的模块
- ember.js - 无法使用“get”Ember 3.2 加载关系
- vue.js - vuetify v1.1 我可以为 v-card 颜色设置一些透明度吗?