ValueError:此求解器需要数据中至少2个类的样本,但数据仅包含一个类:0.0 [英] ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0

查看:1545
本文介绍了ValueError:此求解器需要数据中至少2个类的样本,但数据仅包含一个类:0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将数据集分为测试集和火车集后,我在火车集上应用了Logistic回归,但是出现了以上错误.我试图解决这个问题,当我尝试在控制台中打印我的响应向量y_train时,它会打印诸如0或1之类的整数值.但是当我将其写入文件时,我发现这些值是诸如0.0和1.0之类的浮点数.如果那是问题,我该如何解决.

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如下,

Traceback (most recent call last):

  File "/home/amey/prog/pd.py", line 82, in <module>

    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: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0

与此同时,我遇到了链接(未回答).有解决办法吗?

解决方案

此处的问题是,无论出于何种原因,您的y_train向量都只有零.实际上,这不是您的错,而是一种错误(我认为).分类器需要2个类,否则它将引发此错误.

这很有道理.如果您的y_train向量只有零(即只有1个类),则分类器实际上不需要做任何工作,因为所有预测都应该只是一个类.

在我看来,分类器仍应完成并仅预测一个类(在这种情况下为全零),然后发出警告,但事实并非如此.它将引发错误.

检查这种情况的方法如下:

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 one class"
    #do something else
else:
    #OK to proceed
    lenreg.fit(X_train, y_train)
    y_pred = lenreg.predict(X_test)
    print metics.accuracy_score(y_test, y_pred)

为了更轻松地解决该问题,我建议您在测试集中仅包含更多样本,例如100或1000,而不是10.

I have applied Logistic Regression on train set after splitting the data set into test and train sets, but I got the above error. I tried to work it out, and when i tried to print my response vector y_train in the console it prints integer values like 0 or 1. But when i wrote it into a file I found the values were float numbers like 0.0 and 1.0. If thats the problem, how can I over 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 is as follows,

Traceback (most recent call last):

  File "/home/amey/prog/pd.py", line 82, in <module>

    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: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0

Meanwhile I've gone across the link which was unanswered. Is there a solution.

解决方案

The problem here is that your y_train vector, for whatever reason, only has zeros. It is actually not your fault, and its kind of a bug ( I think ). The classifier needs 2 classes or else it throws this error.

It makes sense. If your y_train vector only has zeros, ( ie only 1 class ), then the classifier doesn't really need to do any work, since all predictions should just be the one class.

In my opinion the classifier should still complete and just predict the one class ( all zeros in this case ) and then throw a warning, but it doesn't. It throws the error in stead.

A way to check for this condition is like 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 one class"
    #do something else
else:
    #OK to proceed
    lenreg.fit(X_train, y_train)
    y_pred = lenreg.predict(X_test)
    print metics.accuracy_score(y_test, y_pred)

TO overcome the problem more easily i would recommend just including more samples in you test set, like 100 or 1000 instead of 10.

这篇关于ValueError:此求解器需要数据中至少2个类的样本,但数据仅包含一个类:0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆