python-3.x - 使用 StratifiedShuffleSplit 时计算召回率指标

标签 python-3.x machine-learning scikit-learn knn

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

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)

如何修改分数来计算召回率精度指标而不是默认准确率?

谢谢,

最佳答案

您需要:

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)


关于python-3.x - 使用 StratifiedShuffleSplit 时计算召回率指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59007270/

相关文章:

python-3.x - 使用 pandas 在两个条件下创建列

python - 如何始终忽略字符串中的一个字节

python - QProgressBar不显示复制进度

machine-learning - weka中成本/ yield 分析中随机和增益的含义

python - 为 scikit-learn CountVectorizer 提供带有空格的词汇表

python - 将 sklearn 从 0.14.1 升级到 0.16.1 不起作用

python - 比较字符串 Python 的最快方法

numpy - 数据集中模式的随机化

matlab - 图像大小调整和裁剪的顺序重要吗?

python - 如何使用 MLPClassifier Sklearn 在神经网络中使用自定义损失函数?