【问题标题】:How to calculate sensitivity score of Leave One Out Cross Validation?如何计算 Leave One Out Cross Validation 的敏感性分数?
【发布时间】:2019-11-21 21:29:12
【问题描述】:

我尝试对分类模型实施保留一组交叉验证。到目前为止,我用这段代码来做简历。

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 的准确度分数。我的问题:如何获得每个折叠的敏感度分数?

【问题讨论】:

    标签: python machine-learning scikit-learn cross-validation


    【解决方案1】:

    你只需要从sklearn.metrics导入recall_score并使用它,如下所示:

    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))
    

    希望这会有所帮助!

    【讨论】:

    • 一些错误:ValueError:发现输入变量的样本数不一致:[20, 158]
    • 我不认为我可以在这个有限的评论部分发帖。
    • 好的,你在哪一行得到这个错误,还有X_trainy_trainX_testy_test的尺寸是多少?我想这与所涉及实体的维度有关。
    猜你喜欢
    • 2016-01-19
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2021-04-03
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多