简体   繁体   中英

Machine Learning - Stratified K-Fold CV

I've unbalanced binary classifier data and want to Stratified K-Fold CV. I'm getting the below error:

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)

Here, pipeline is:

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)

You are not passing valid labels , in fact in your code labels and data is the same thing:

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

while probably you wanted something among the lines of

train_y = y[train_indices]

and the same for test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM