本文簡要介紹python語言中 sklearn.model_selection.LeavePGroupsOut
的用法。
用法:
class sklearn.model_selection.LeavePGroupsOut(n_groups)
將 P 組排除在外cross-validator
提供訓練/測試索引以根據第三方提供的組拆分數據。該組信息可用於將樣本的任意域特定分層編碼為整數。
例如,這些組可以是樣本收集的年份,因此允許針對基於時間的拆分進行交叉驗證。
LeavePGroupsOut 和 LeaveOneGroupOut 之間的區別在於,前者使用分配給
p
不同組值的所有樣本來構建測試集,而後者使用所有分配給相同組的樣本。在用戶指南中閱讀更多信息。
- n_groups:int
要在測試拆分中遺漏的組數 (
p
)。
參數:
例子:
>>> import numpy as np >>> from sklearn.model_selection import LeavePGroupsOut >>> X = np.array([[1, 2], [3, 4], [5, 6]]) >>> y = np.array([1, 2, 1]) >>> groups = np.array([1, 2, 3]) >>> lpgo = LeavePGroupsOut(n_groups=2) >>> lpgo.get_n_splits(X, y, groups) 3 >>> lpgo.get_n_splits(groups=groups) # 'groups' is always required 3 >>> print(lpgo) LeavePGroupsOut(n_groups=2) >>> for train_index, test_index in lpgo.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] TEST: [0 1] [[5 6]] [[1 2] [3 4]] [1] [1 2] TRAIN: [1] TEST: [0 2] [[3 4]] [[1 2] [5 6]] [2] [1 1] TRAIN: [0] TEST: [1 2] [[1 2]] [[3 4] [5 6]] [1] [2 1]
相關用法
- Python sklearn LeavePOut用法及代碼示例
- Python sklearn LeaveOneOut用法及代碼示例
- Python sklearn LeaveOneGroupOut用法及代碼示例
- Python sklearn LedoitWolf用法及代碼示例
- Python sklearn LarsCV用法及代碼示例
- Python sklearn Lars用法及代碼示例
- Python sklearn LocalOutlierFactor.kneighbors_graph用法及代碼示例
- Python sklearn Lasso用法及代碼示例
- Python sklearn LabelPropagation用法及代碼示例
- Python sklearn LassoLars用法及代碼示例
- Python sklearn LogisticRegression用法及代碼示例
- Python sklearn LassoLarsIC用法及代碼示例
- Python sklearn LocallyLinearEmbedding用法及代碼示例
- Python sklearn LassoCV.path用法及代碼示例
- Python sklearn LogisticRegressionCV用法及代碼示例
- Python sklearn LinearDiscriminantAnalysis用法及代碼示例
- Python sklearn LassoCV用法及代碼示例
- Python sklearn LabelSpreading用法及代碼示例
- Python sklearn LabelEncoder用法及代碼示例
- Python sklearn LinearRegression用法及代碼示例
- Python sklearn LocalOutlierFactor.kneighbors用法及代碼示例
- Python sklearn LabelBinarizer用法及代碼示例
- Python sklearn LinearSVR用法及代碼示例
- Python sklearn LinearSVC用法及代碼示例
- Python sklearn LassoLarsCV用法及代碼示例
注:本文由純淨天空篩選整理自scikit-learn.org大神的英文原創作品 sklearn.model_selection.LeavePGroupsOut。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。