我正在尝试使用 scikit Learn 包中的 train_test_split
,但我在使用参数 stratify
时遇到问题。以下是代码:
from sklearn import cross_validation, datasets
X = iris.data[:,:2]
y = iris.target
cross_validation.train_test_split(X,y,stratify=y)
但是,我不断遇到以下问题:
raise TypeError("Invalid parameters passed: %s" % str(options))
TypeError: Invalid parameters passed: {'stratify': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])}
有人知道发生了什么事吗?下面是函数文档。
[...]
stratify : array-like or None (default is None)
If not None, data is split in a stratified fashion, using this as the labels array.
New in version 0.17: stratify splitting
[...]
最佳答案
此stratify
参数进行分割,以便生成的样本中的值的比例与提供给参数stratify
的值的比例相同。
例如,如果变量 y
是值为 0
和 1
的二元分类变量,并且有 25% 的零和 75%其中,stratify=y
将确保您的随机分割包含 25% 的 0
和 75% 的 1
。
关于split - 方法 "stratify"中的参数 "train_test_split"(scikit Learn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842405/