python-3.x - 使用 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
以计算recall
和precision
指标而不是默认精度?
谢谢,
解决方案
你需要:
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)
推荐阅读
- php - PHP:如何获取请求页面并获取正文和 http 错误代码
- xamarin.forms - 如何在 xamarin 表单的工具栏项中添加字体图标
- php - 为什么 JSON 值不显示在 PHP 中?
- python - 成功运行任务但不跳过
- python - 我收到“RequestsDependencyWarning:urllib3 (1.25.3) 或 chardet (3.0.4) 与支持的版本不匹配!”
- asp.net-core - Blazor:如何从子组件中的事件中获取发件人
- flutter - 如何从类字段设置提供者值?
- parallel-processing - 如何在 Vulkan 中适当地设置工作组计数
- ansible - 无法使用循环复制文件
- regex - 修剪标签之间的空间