[英]sklearn stratified k-fold CV with linear model like ElasticNetCV
[英]Group/Cluster K-Fold CV with Sklearn
我需要在某些模型上进行K-fold CV,但我需要确保验证(测试)数据集按群组和t
年聚集在一起。 GroupKFold
很接近,但仍然会将验证集拆分(见第二次折叠)。
例如,如果我有一组2000年至2008年的数据,我想将K-fold分成3组。 适当的集合将是:验证:2000-2002,培训:2003-2008; V:2003-2005,T:2000-2002和2006-2008; 和V:2006-2008,T:2000-2005)。
有没有办法使用K-Fold CV对数据进行分组和聚类,其中验证集聚集了t
年?
from sklearn.model_selection import GroupKFold
X = [0.1, 0.2, 2.2, 2.4, 2.3, 4.55, 5.8, 8.8, 9, 10, 0.1, 0.2, 2.2]
y = ["a", "b", "b", "b", "c", "c", "c", "d", "d", "d", "a", "b", "b"]
groups = [1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4]
gkf = GroupKFold(n_splits=2)
for train_index, test_index in gkf.split(X, y, groups=groups):
print("Train:", train_index, "Validation:",test_index)
输出:
Train: [ 0 1 2 3 4 5 10 11 12] Validation: [6 7 8 9]
Train: [3 4 5 6 7 8 9] Validation: [ 0 1 2 10 11 12]
Train: [ 0 1 2 6 7 8 9 10 11 12] Validation: [3 4 5]
期望输出(假设每组2年):
Train: [ 7 8 9 10 11 12 ] Validation: [0 1 2 3 4 5 6]
Train: [0 1 2 10 11 12 ] Validation: [ 3 4 5 6 7 8 9 ]
Train: [ 0 1 2 3 4 5 ] Validation: [6 7 8 9 10 11 12]
虽然,测试和训练子集不是连续的,可以选择更多年份进行分组。
我希望我能正确理解你。
来自scikits model_selection
的LeaveOneGroupOut方法可能会有所帮助:
假设您将组标签0分配给2000-2002中的所有数据点,将标签1分配给2003年和2005年之间的所有数据点,将标签2分配给2006-2008中的数据。 然后,您可以使用以下方法创建训练和测试拆分,其中三个测试拆分是从三个组中的一个创建的:
from sklearn.model_selection import LeaveOneGroupOut
import numpy as np
groups=[1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,3]
X=np.random.random(len(groups))
y=np.random.randint(0,4,len(groups))
logo = LeaveOneGroupOut()
print("n_splits=", logo.get_n_splits(X,y,groups))
for train_index, test_index in logo.split(X, y, groups):
print("train_idx:", train_index, "test_idx:", test_index)
输出:
n_splits= 3
train_idx: [ 4 5 6 7 8 9 10 11 12 13 14 15 16 17] test_idx: [0 1 2 3]
train_idx: [ 0 1 2 3 10 11 12 13 14 15 16 17] test_idx: [4 5 6 7 8 9]
train_idx: [0 1 2 3 4 5 6 7 8 9] test_idx: [10 11 12 13 14 15 16 17]
我想我现在终于理解了你想要的东西。 对不起,我花了这么长时间。
我不认为你想要的分割方法已经在sklearn中实现了。 但我们可以轻松扩展BaseCrossValidator方法。
import numpy as np
from sklearn.model_selection import BaseCrossValidator
from sklearn.utils.validation import check_array
class GroupOfGroups(BaseCrossValidator):
def __init__(self, group_of_groups):
"""
:param group_of_groups: list with length n_splits. Each entry in the list is a list with group ids from
set(groups). In each of the n_splits splits, the groups given in the current group_of_groups sublist are used
for validation.
"""
self.group_of_groups = group_of_groups
def get_n_splits(self, X=None, y=None, groups=None):
return len(self.group_of_groups)
def _iter_test_masks(self, X=None, y=None, groups=None):
if groups is None:
raise ValueError("The 'groups' parameter should not be None.")
groups=check_array(groups, copy=True, ensure_2d=False, dtype=None)
for g in self.group_of_groups:
test_index = np.zeros(len(groups), dtype=np.bool)
for g_id in g:
test_index[groups == g_id] = True
yield test_index
用法很简单。 和以前一样,我们定义X,y
和groups
。 另外,我们定义了一个列表(组的组),它们定义了哪些组应该在哪个测试折叠中一起使用。 因此g_of_g=[[1,2],[2,3],[3,4]]
意味着第1组和第2组在第一次折叠中用作测试集,而其余组3和4用于训练。 在折叠2中,来自组2和3的数据用作测试集等。
我对命名为“GroupOfGroups”感到不满意,所以也许你会找到更好的东西。
现在我们可以测试这个交叉验证器:
X = [0.1, 0.2, 2.2, 2.4, 2.3, 4.55, 5.8, 8.8, 9, 10, 0.1, 0.2, 2.2]
y = ["a", "b", "b", "b", "c", "c", "c", "d", "d", "d", "a", "b", "b"]
groups = [1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4]
g_of_g = [[1,2],[2,3],[3,4]]
gg = GroupOfGroups(g_of_g)
print("n_splits=", gg.get_n_splits(X,y,groups))
for train_index, test_index in gg.split(X, y, groups):
print("train_idx:", train_index, "test_idx:", test_index)
输出:
n_splits= 3
train_idx: [ 6 7 8 9 10 11 12] test_idx: [0 1 2 3 4 5]
train_idx: [ 0 1 2 10 11 12] test_idx: [3 4 5 6 7 8 9]
train_idx: [0 1 2 3 4 5] test_idx: [ 6 7 8 9 10 11 12]
请记住,我没有包含大量支票,也没有进行彻底的测试。 因此请仔细核实这对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.