python 2.7 - ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0 -


i have applied logistic regression on train set after splitting data set test , train sets, got above error. tried work out, , when tried print response vector y_train in console prints integer values 0 or 1. when wrote file found values float numbers 0.0 , 1.0. if thats problem, how can on come it.

lenreg = logisticregression()  print y_train[0:10] y_train.to_csv(path='ytard.csv')  lenreg.fit(x_train, y_train) y_pred = lenreg.predict(x_test) print metics.accuracy_score(y_test, y_pred) 

stracktrace follows,

traceback (most recent call last):

file "/home/amey/prog/pd.py", line 82, in

lenreg.fit(x_train, y_train) 

file "/usr/lib/python2.7/dist-packages/sklearn/linear_model/logistic.py", line 1154, in fit

self.max_iter, self.tol, self.random_state) 

file "/usr/lib/python2.7/dist-packages/sklearn/svm/base.py", line 885, in _fit_liblinear

" class: %r" % classes_[0]) 

valueerror: solver needs samples of @ least 2 classes in data, data contains 1 class: 0.0

meanwhile i've gone across link unanswered. there solution.

the problem here y_train vector, whatever reason, has zeros. not fault, , kind of bug ( think ). classifier needs 2 classes or else throws error.

it makes sense. if y_train vector has zeros, ( ie 1 class ), classifier doesn't need work, since predictions should 1 class.

in opinion classifier should still complete , predict 1 class ( zeros in case ) , throw warning, doesn't. throws error in stead.

a way check condition this:

lenreg = logisticregression()  print y_train[0:10] y_train.to_csv(path='ytard.csv')  if len(np.sum(y_train)) in [len(y_train),0]:     print "all 1 class"     #do else else:     #ok proceed     lenreg.fit(x_train, y_train)     y_pred = lenreg.predict(x_test)     print metics.accuracy_score(y_test, y_pred) 

to overcome problem more recommend including more samples in test set, 100 or 1000 instead of 10.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -