ValueError: The number of classes has to be greater than one (python)
ValueError: The number of classes has to be greater than one (python)
在 fit
中传递 x,y
时,出现以下错误:
追溯(最近调用最后):
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)
您可能在训练集中只有一个唯一的 class 标签。如错误消息所述,您需要在数据集中至少有两个唯一的 classes。例如,您可以 运行 np.unique(y)
查看数据集中唯一的 class 标签是什么。
没错。您的最后一列(标签)只有一种类型(分类)。你应该至少有两个。例如;如果您的标签决定您是否必须卸载,则标签列应该有卸载和不卸载或(0 或 1)。
在 fit
中传递 x,y
时,出现以下错误:
追溯(最近调用最后):
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_modelclf.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)
您可能在训练集中只有一个唯一的 class 标签。如错误消息所述,您需要在数据集中至少有两个唯一的 classes。例如,您可以 运行 np.unique(y)
查看数据集中唯一的 class 标签是什么。
没错。您的最后一列(标签)只有一种类型(分类)。你应该至少有两个。例如;如果您的标签决定您是否必须卸载,则标签列应该有卸载和不卸载或(0 或 1)。