我试图对分类模型实施一组交叉验证。到目前为止,我已经使用此代码进行了简历。
from sklearn.model_selection import LeaveOneGroupOut
X = X
y = np.array(df.loc[:, df.columns == 'label'])
scores=[]
groups = df["cow_id"].values
logo = LeaveOneGroupOut()
logo.get_n_splits(X, y, groups)
cv=logo.split(X, y, groups)
for train_index, test_index in cv:
print("Train Index: ", train_index, "\n")
print("Test Index: ", test_index)
X_train, X_test, y_train, y_test = X[train_index], X[test_index], y[train_index], y[test_index]
model.fit(X_train, y_train.ravel())
scores.append(model.score(X_test, y_test.ravel()))
从此代码中,我获得每一折的准确性得分。例如,如果我有35个小组,我将获得35个准确性得分。我的问题:如何获得每折的灵敏度得分?
答案 0 :(得分:0)
您只需要从recall_score
导入sklearn.metrics
即可使用它,如下所示:
from sklearn.model_selection import LeaveOneGroupOut
from sklearn.metrics import recall_score
X = X
y = np.array(df.loc[:, df.columns == 'label'])
scores=[]
senstivities = []
groups = df["cow_id"].values
logo = LeaveOneGroupOut()
logo.get_n_splits(X, y, groups)
cv=logo.split(X, y, groups)
for train_index, test_index in cv:
print("Train Index: ", train_index, "\n")
print("Test Index: ", test_index)
X_train, X_test, y_train, y_test = X[train_index], X[test_index], y[train_index], y[test_index]
model.fit(X_train, y_train.ravel())
y_pred = model.predict(X_test)
scores.append(model.score(X_test, y_test.ravel()))
senstivities.append(recall_score(y_test.ravel(), y_pred))
希望这会有所帮助!