微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python-2.7 – ValueError:类的数量必须大于一(python)

当传递x,y in fit时,我收到以下错误

Traceback(最近一次调用最后一次):

File “C:/Classify/classifier.py”,line 95,in

train_avg,test_avg,cms = train_model(X,y,“ceps”,plot=True)
File “C:/Classify/classifier.py”,line 47,in train_model

clf.fit(X_train,y_train) File “C:\Python27\lib\site-packages\sklearn\svm\base.py”,line 676,in fit
raise ValueError(“The number of classes has to be greater than” ValueError: The number of classes has to be greater than one.

以下是我的代码

def train_model(X,Y,name,plot=False):
"""
    train_model(vector,vector,name[,plot=False])

    Trains and saves model to disk.
"""
labels = np.unique(Y)

cv = ShuffleSplit(n=len(X),n_iter=1,test_size=0.3,indices=True,random_state=0)

train_errors = []
test_errors = []

scores = []
pr_scores = defaultdict(list)
precisions,recalls,thresholds = defaultdict(list),defaultdict(list),defaultdict(list)

roc_scores = defaultdict(list)
tprs = defaultdict(list)
fprs = defaultdict(list)

clfs = []  # for the median

cms = []

for train,test in cv:
    X_train,y_train = X[train],Y[train]
    X_test,y_test = X[test],Y[test]

    clf = LogisticRegression()
    clf.fit(X_train,y_train)
    clfs.append(clf)

解决方法

您可能在训练集中只有一个唯一的类标签.如上所述,您需要在数据集中至少有两个唯一的类.例如,您可以运行np.unique(y)来查看数据集中的唯一类标签.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐