Handson-ml: Chapter 3: ValueError: The number of classes has to be greater than one; got 1 class

Created on 21 Feb 2019  路  10Comments  路  Source: ageron/handson-ml

Hello,

When I run the following:

from sklearn.linear_model import SGDClassifier
sgd_clf = SGDClassifier(tol=None, max_iter=5, random_state=42)
sgd_clf.fit(X_train, y_train_5)

I get the error ValueError: The number of classes has to be greater than one; got 1 class

I'm sure both classes y_train_5 and y_test_5 are set up correctly as I have run in the course notebook and my own.

Can anyone point me in the right direction for a solution?

Most helpful comment

Hi @pin0S ,

The error message says that there's just one class in y_train_5. Could you please print the output of:

np.unique(y_train_5)

There should be more than one value in the resulting array.
If not, then perhaps what happened is that you used fetch_openml() to download MNIST, and it returns labels as strings so when you defined y_train_5 = (y_train == 5), it results an array full of False. A solution is to cast y_train to int8: y_train = y_train.astype(np.int8) (which is what I do in the notebook, just after loading the data).

Hope this helps,
Aur茅lien

All 10 comments

Hi @pin0S ,

The error message says that there's just one class in y_train_5. Could you please print the output of:

np.unique(y_train_5)

There should be more than one value in the resulting array.
If not, then perhaps what happened is that you used fetch_openml() to download MNIST, and it returns labels as strings so when you defined y_train_5 = (y_train == 5), it results an array full of False. A solution is to cast y_train to int8: y_train = y_train.astype(np.int8) (which is what I do in the notebook, just after loading the data).

Hope this helps,
Aur茅lien

Hi @ageron

Thanks for coming back so quick and apologies I have only got around to it now.

You were correct I had missed the to int8. I was looking at the old notebook and not the newer one.

Cheers,

Pete

Hi @ageron

I did the same mistake, thought that "We prefer numbers" was something optional, skipped and then forgot that I did it. Maybe different wording will help people like us in the next edition.

P.S. The book is amazing btw, thanks!

image

Hi @eapotapov ,
Thanks for your feedback and your kind words. Indeed "We prefer numbers" was not a great way to phrase this, I'll fix it.
Edit: I changed it to "Most ML algorithms expect numbers".
Cheers!

I also started working with machine learning of recent(real programming in general) and I missed the conversion to integer part:).
i am working with the book, I must say it is wonderful, I don't regret a cent of my money. Thanks a lot, @ageron. God bless

Thanks for your kind words Fritz! :)

ddd
The number of classes has to be greater than one; got 1 class
Please help me sir

@umeshhello12 @ageron
Hi guys, The targets of 'MNIST' dataset downloaded are string digit. So you just need to change to
y_train_5 = (y_train == '5')
y_test_5 = (y_test == '5')

Hi @pin0S ,

The error message says that there's just one class in y_train_5. Could you please print the output of:

np.unique(y_train_5)

There should be more than one value in the resulting array.
If not, then perhaps what happened is that you used fetch_openml() to download MNIST, and it returns labels as strings so when you defined y_train_5 = (y_train == 5), it results an array full of False. A solution is to cast y_train to int8: y_train = y_train.astype(np.int8) (which is what I do in the notebook, just after loading the data).

Hope this helps,
Aur茅lien

yeah it worked!!

@umeshhello12 @ageron
Hi guys, The targets of 'MNIST' dataset downloaded are string digit. So you just need to change to
y_train_5 = (y_train == '5')
y_test_5 = (y_test == '5')

love you babe

Was this page helpful?
0 / 5 - 0 ratings