ValueError: cross_val_predict only works for partitions

Viewed 607

I have a multi-class classification problem with feature array (X) and the dependent variable array (y). y has four classes: 1, 2, 3 and 4. There is a severe imbalance in data, there is only a handful of observations for class 4. I have chosen the classifier model as:

model = RandomForestClassifier(n_estimators=10, class_weight='balanced', random_state=0)

I have defined a custom scorer as:

custom_scorer = {'accuracy': make_scorer(accuracy_score),
                 'balanced_accuracy': make_scorer(balanced_accuracy_score),
                 'precision': make_scorer(precision_score, average='weighted'),
                 'recall': make_scorer(recall_score, average='weighted'),
                 'roc auc': make_scorer(roc_auc_score, multi_class='ovo', needs_proba=True),
                 'f1': make_scorer(f1_score, average='weighted')
                 }

Due to data imbalance, I am using cross_validate:

cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
scores = cross_validate(model, X, y, cv=cv, scoring=custom_scorer, n_jobs=-1)

From the scores I am getting the metrics accuracy, balanced_accuracy, precision, recall etc. However, this gives me only the metrics, not the array containing predicted class. I want to get y_pred (predicted values of y) as well that might have been generated internally while executing the following line of code:

scores = cross_validate(model, X, y, cv=cv, scoring=custom_scorer, n_jobs=-1)

However, to get the prediction of y, I am using:

y_pred = cross_val_predict(model, X, y, cv=cv) 

This gives me an error:

ValueError: cross_val_predict only works for partitions

If I change the above code to

y_pred = cross_val_predict(model, X, y, cv=10)

then I can avoid the error, but cv=cv and cv=10 are not the same things, are they? If I calculate precision, recall etc. corresponding to each class by using:

precision, recall, fscore, support = score(y, y_pred)

those cannot correspond to the scores I calculated using cross_validate.

I also need the probability values calculated for y.

y_proba = cross_val_predict(model, X, y, cv=cv, method='predict_proba')

This also gives me an error

ValueError: cross_val_predict only works for partitions

Please help.

0 Answers
Related