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


Python sklearn LeaveOneGroupOut用法及代碼示例


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

用法:

class sklearn.model_selection.LeaveOneGroupOut

留下一組cross-validator

提供訓練/測試索引以根據第三方提供的組拆分數據。該組信息可用於將樣本的任意域特定分層編碼為整數。

例如,這些組可以是樣本收集的年份,因此允許針對基於時間的拆分進行交叉驗證。

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

例子

>>> import numpy as np
>>> from sklearn.model_selection import LeaveOneGroupOut
>>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> y = np.array([1, 2, 1, 2])
>>> groups = np.array([1, 1, 2, 2])
>>> logo = LeaveOneGroupOut()
>>> logo.get_n_splits(X, y, groups)
2
>>> logo.get_n_splits(groups=groups)  # 'groups' is always required
2
>>> print(logo)
LeaveOneGroupOut()
>>> for train_index, test_index in logo.split(X, y, groups):
...     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: [2 3] TEST: [0 1]
[[5 6]
 [7 8]] [[1 2]
 [3 4]] [1 2] [1 2]
TRAIN: [0 1] TEST: [2 3]
[[1 2]
 [3 4]] [[5 6]
 [7 8]] [1 2] [1 2]

相關用法


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