计算使用分层随机拆分时的召回指标



以下方法使用带有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指标,而不是默认精度?

谢谢

你需要:

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)

最新更新