6

I have a training dataset of 8670 trials and each trial has a length of 125-time samples while my test set consists of 578 trials. When I apply SVM algorithm from scikit-learn, I get pretty good results.

However, when I apply logistic regression, this error occurs:

"ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 1.0" .

My question is why SVM is able to give predictions but logistic regression gives this error?

Could it be possible that something is wrong in the dataset or just that logistic regression was not able to classify because the training samples look similar to it?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • 1
    Please post minimal code that runs, including sample data to produce the error. – bakkal Jul 01 '16 at 06:09
  • There is something wrong with your code, neither SVM nor LR works with a single class and they both throw the same error. – lejlot Jul 01 '16 at 08:39
  • I'd love an upvote on my answer below! If it solved your problem! thank you! – Nico Sep 22 '16 at 13:39

1 Answers1

10

I read this in the following issue on a similar linear module:https://github.com/lensacom/sparkit-learn/issues/49

"Sadly this is a bug indeed. Sparkit trains sklearn's linear models in parallel, then averages them in a reduce step. There is at least one block, which contains only one of the labels. To check try the following:

train_Z[:, 'y']._rdd.map(lambda x: np.unique(x).size).filter(lambda x: x < 2).count()

To resolve You could randomize the train data to avoid blocks with one label, but this is still waiting for a clever solution."

EDIT: I found a solution, the above analysis of the error was correct. This would be a solution.

To Shuffle the arrays in the same order I used a scikitlearn utils module:

from sklearn.utils import shuffle
X_shuf, Y_shuf = shuffle(X_transformed, Y)

Then use those shuffled arrays to train your model again and it'll work!

Nico
  • 743
  • 7
  • 19