我正在尝试仅输出KNeighborsClassifier中测试数据的加权f1分数。
我可以这样做:
neigh = KNeighborsClassifier(n_neighbors=10)
neigh.fit(X_train, y_train)
result = neigh.predict(X_test)
print(classification_report(test_tags, result))
返回:
precision recall f1-score support
0 1.00 0.40 0.57 5
2 0.00 0.00 0.00 1
3 0.20 1.00 0.33 1
avg / total 0.74 0.43 0.46 7
我也知道:
sklearn.metrics.f1_score
。
了解http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html上的示例
from sklearn.metrics import f1_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]
f1_score(y_true, y_pred, average='weighted')
但是如何将其应用于上面的KNeighborsClassifier代码?
答案 0 :(得分:0)
用以下解决。
from sklearn.metrics import precision_recall_fscore_support
neigh = KNeighborsClassifier(n_neighbors=10)
neigh.fit(X_train, y_train)
result = neigh.predict(X_test)
precision_recall_fscore_support(test_tags, result, average='weighted')[2]
其中test_tags
为真值,result
为预测值。