【发布时间】:2017-10-12 15:06:21
【问题描述】:
我正在尝试使用我提供的拆分在sklearn 中运行cross_val_score。 sklearn 文档为 here 提供了以下示例:
>>> from sklearn.model_selection import PredefinedSplit
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> test_fold = [0, 1, -1, 1]
>>> ps = PredefinedSplit(test_fold)
>>> ps.get_n_splits()
2
>>> print(ps)
PredefinedSplit(test_fold=array([ 0, 1, -1, 1]))
>>> for train_index, test_index in ps.split():
... 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]
TRAIN: [1 2 3] TEST: [0]
TRAIN: [0 2] TEST: [1 3]
我无法理解这个例子。特别是,
- 为什么
ps.get_n_splits()在这个例子中返回2;和 - 为什么
test_fold数组会导致代码 sn-p 底部显示的拆分?
另外,我想问一下,在这种情况下,如果我将 ps 对象传递给sklearn 中的cross_val_score 函数,它会不会对这两个拆分进行交叉验证?
【问题讨论】:
-
你可以看看my answer here
-
您好,非常感谢。那么我的问题是重复的。
标签: python scikit-learn