首页 > 解决方案 > 使用 StratifiedShuffleSplit 时计算召回指标

问题描述

以下方法使用带有 StratifiedShuffleSplit 的 KNN 分类器,因为我有一个不平衡的数据集:

def KNN(train_x, train_y):
    skf = StratifiedShuffleSplit()
    scores = []
    for train, test in skf.split(train_x, train_y):
        clf = KNeighborsClassifier(n_neighbors=2, n_jobs=-1)
        clf.fit(train_x.loc[train], train_y.loc[train])
        score = clf.score(train_x.loc[test], train_y.loc[test])
        scores.append(score)

    res = np.asarray(scores).mean()
    print(res)

如何修改scores以计算recallprecision指标而不是默认精度?

谢谢,

标签: python-3.xmachine-learningscikit-learnknn

解决方案


你需要:

sklearn.metrics.recall_score(y_true, y_pred)
sklearn.metrics.precision_score(y_true, y_pred)
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score

def KNN(train_x, train_y):
    skf = StratifiedShuffleSplit()
    scores = []
    scores2 = []
    for train, test in skf.split(train_x, train_y):
        clf = KNeighborsClassifier(n_neighbors=2, n_jobs=-1)
        clf.fit(train_x.loc[train], train_y.loc[train])
        y_pred = clf.predict(train_x.loc[test]) # predict the labels of the test set
        y_true = train_y.loc[test] # get the true labels of the test test
        score = recall_score(y_true, y_pred) # recall estimation
        score2 = precision_score(y_true, y_pred) # precision estimation
        scores.append(score)
        scores2.append(score2)



推荐阅读