我在sklearn cross_validation train_testrongplit模块中使用pandas数据框。
d=pandas.DataFrame({'a':np.random.randn(300),
'c':np.array([el for el in np.ones(100)]+
[el for el in np.zeros(200)])})
from sklearn import cross_validation
(X,y)=(d['a'],d['c'])
这是X_train_and_cv, X_test,y_train_and_cv,y_test = sklearn.cross_validation.train_test_split(X,y,test_size=0.2,random_state=0)
X_train, X_cv,y_train,y_cv = sklearn.cross_validation.train_test_split(X_train_and_cv,y_train_and_cv,test_size=0.2,random_state=0)
为什么这个不行?
X_train_and_cv, X_test,y_train_and_cv,y_test = sklearn.cross_validation.train_test_split(X,y,test_size=0.2,random_state=0,stratify=y)
X_train, X_cv,y_train,y_cv = sklearn.cross_validation.train_test_split(X_train_and_cv,y_train_and_cv,test_size=0.2,random_state=0,stratify=y)
in _is_valid_list_like(self, key, axis)
1536 l = len(ax)
1537 if len(arr) and (arr.max() >= l or arr.min() < -l):
-> 1538 raise IndexError("positional indexers are out-of-bounds")
1539
1540 return True
IndexError: positional indexers are out-of-bounds
TL;DR:您对train_test_split
的第二次调用使用了与您使用的y
不同的stratify
数组长度。使用stratify=y_train_and_cv
首先,一个小边注:cross_validation
(这里是0.17.1文档)将很快被弃用,您应该使用model_selection.train_test_split (0.18.1)
代替。我将导入train_test_split itself
以缩短以下内容的长度:
# Same as this in older versions:
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
X_train_and_cv, X_test,y_train_and_cv,y_test = train_test_split(X,y,
test_size=0.2,
random_state=0,
stratify=y)
y=y_train_and_cv
(len=240) stratify=y
(len=300)
X_train, X_cv,y_train,y_cv = train_test_split(X_train_and_cv,
y_train_and_cv,
test_size=0.2,
random_state=0,
stratify=y)
替换为:
X_train, X_cv,y_train,y_cv = train_test_split(X_train_and_cv,
y_train_and_cv,
test_size=0.2,
random_state=0,
stratify=y_train_and_cv)