ValueError:类数必须大于一(python)

编程入门 行业动态 更新时间:2024-10-23 20:18:12
本文介绍了ValueError:类数必须大于一(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 fit 中传递 x,y 时,出现以下错误:

When passing x,y in fit, I am getting the following error:

跟踪(最近一次通话):

Traceback (most recent call last):

文件 C:/Classify/classifier.py ,在

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

train_avg,test_avg,cms = train_model(X,y, ceps,plot = True)中,行95, 文件 C :/Classify/classifier.py,第47行,位于train_model

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)文件 C:\Python27\lib\site -packages\sklearn\svm\base.py,行676,适合提高ValueError(类数必须大于 ValueError:类数必须大于一。

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)来查看数据集中的唯一类标签是什么。

You probably have only one unique class label in the training set present. As the error messages noted, you need to have at least two unique classes in the dataset. E.g., you can run np.unique(y) to see what the unique class labels in your dataset are.

更多推荐

ValueError:类数必须大于一(python)

本文发布于:2023-11-15 16:55:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ValueError   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!