我正在尝试对一些模型进行二元分类。我希望根据分数和f2分数对这些模型进行分类。
对于“分数”,我使用了以下代码
for name, clf in zip(models, classifiers): clf.fit(X_train, y_train) score = clf.score(X_test, y_test) scores.append(score)
这给出了所有模型的分数,但我无法找到所有模型的f2分数。谁能建议我应该使用什么代码?
回答:
你可以使用fbeta_score来实现这一点,只需将β设置为2即可。
from sklearn.metrics import fbeta_scorescores = []f2_score = []for name, clf in zip(models, classifiers): clf.fit(X_train, y_train) y_pred = clf.predict(X_test) f2 = fbeta_score(y_test, y_pred, beta=2, average='binary') score = clf.score(X_test, y_test) scores.append(score) f2_score.append(f2)