我正在尝试对我的模型使用Kfold
交叉价值,但这样做时会出现此错误。我知道KFold
只接受1D数组,但即使将长度输入转换为数组,它也会给我这个问题。
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import KFold
if __name__ == "__main__":
np.random.seed(1335)
verbose = True
shuffle = False
n_folds = 5
y = np.array(y)
if shuffle:
idx = np.random.permutation(y.size)
X_train = X_train[idx]
y = y[idx]
skf = KFold(y, n_folds)
models = [RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='gini'),ExtraTreesClassifier(n_estimators=100, n_jobs=-1, criterion='entropy')]
print("Stacking in progress")
A = []
for j, clf in enumerate(models):
print(j, clf)
for i, (itrain, itest) in enumerate(skf):
print("Fold :", i)
x_train = X_train[itrain]
x_test = X_train[itest]
y_train = y[itrain]
y_test = y[itest]
print(x_train.shape, x_test.shape)
print(len(x_train), len(x_test))
clf.fit(x_train, y_train)
pred = clf.predict_proba(x_test)
A.append(pred)
我收到“skf = KFold(y, n_folds)
”行的错误。任何有关这方面的帮助将不胜感激。
答案 0 :(得分:1)
从its doc开始,KFold()
不会指望y
作为输入,而只会指望拆分数(n_folds)。
获得KFold
的实例后,执行myKfold.split(x)
(x
作为所有输入数据)以获取迭代器,产生列车和测试索引。从sklearn doc粘贴的示例副本:
>>> from sklearn.model_selection import KFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4])
>>> kf = KFold(n_splits=2)
>>> kf.get_n_splits(X)
2
>>> print(kf)
KFold(n_splits=2, random_state=None, shuffle=False)
>>> for train_index, test_index in kf.split(X):
... 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: [2 3] TEST: [0 1]
TRAIN: [0 1] TEST: [2 3]