當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sklearn LeaveOneOut用法及代碼示例

本文簡要介紹python語言中 sklearn.model_selection.LeaveOneOut 的用法。

用法:

class sklearn.model_selection.LeaveOneOut

留一cross-validator

提供訓練/測試索引以拆分訓練/測試集中的數據。每個樣本被用作測試集(單例),而其餘樣本形成訓練集。

注意:LeaveOneOut() 等價於KFold(n_splits=n)LeavePOut(p=1),其中n 是樣本數。

由於測試集的數量很大(與樣本數量相同),這種交叉驗證方法可能非常昂貴。對於大型數據集,應該支持 KFold ShuffleSplit StratifiedKFold

在用戶指南中閱讀更多信息。

例子

>>> import numpy as np
>>> from sklearn.model_selection import LeaveOneOut
>>> X = np.array([[1, 2], [3, 4]])
>>> y = np.array([1, 2])
>>> loo = LeaveOneOut()
>>> loo.get_n_splits(X)
2
>>> print(loo)
LeaveOneOut()
>>> for train_index, test_index in loo.split(X):
...     print("TRAIN:", train_index, "TEST:", test_index)
...     X_train, X_test = X[train_index], X[test_index]
...     y_train, y_test = y[train_index], y[test_index]
...     print(X_train, X_test, y_train, y_test)
TRAIN: [1] TEST: [0]
[[3 4]] [[1 2]] [2] [1]
TRAIN: [0] TEST: [1]
[[1 2]] [[3 4]] [1] [2]

相關用法


注:本文由純淨天空篩選整理自scikit-learn.org大神的英文原創作品 sklearn.model_selection.LeaveOneOut。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。