机器学习 - 分层K-fold CV

时间:2016-08-05 22:11:24

标签: python machine-learning

我有不平衡的二元分类器数据,想要分层K-Fold CV。我收到以下错误:

data = DataFrame(df,columns=names)
train,test = cross_validation.train_test_split(df,test_size=0.20)
train_data,test_data = pd.DataFrame(train,columns=names),pd.DataFrame(test,columns=names)
y = test_data['Classifier'].values
k_fold = StratifiedKFold(y, n_folds=3, shuffle=False, random_state=None)
scores = []

for train_indices, test_indices in k_fold:
    print(train_indices)
    print(test_indices)
    train_text = train.iloc[train_indices]
    train_y = train.iloc[train_indices]
    print(train_y)
    test_text  = test.iloc[test_indices]
    test_y = test.iloc[test_indices]
    pipeline.fit(train_text, train_y)

这里,管道是:

pipeline = Pipeline([
  ('count_vectorizer',   CountVectorizer(ngram_range=(1, 2))),
  ('tfidf_transformer',  TfidfTransformer()),
  ('classifier',         MultinomialNB()) ]) . The error is occurring in pipeline.Below is the error.
C:\SMS\Anaconda32bit\lib\site-packages\sklearn\utils\validation.pyc in column_or_1d(y, warn)
    549         return np.ravel(y)
   --> 551     raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (54, 3)

1 个答案:

答案 0 :(得分:0)

您没有传递有效的标签,事实上在您的代码标签中,数据是一回事:

train_text = train.iloc[train_indices]
train_y = train.iloc[train_indices]

虽然你可能想要

中的某些内容
train_y = y[train_indices]

和测试相同。