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

时间:2016-11-24 07:12:47

标签: python-2.7 scikit-learn classification svm

x,y中传递fit时,我收到以下错误:

追踪(最近一次呼叫最后一次):

  

文件“C:/Classify/classifier.py”,第95行,

     

train_avg,test_avg,cms = train_model(X,y,“ceps”,plot = True)
  文件“C:/Classify/classifier.py”,第47行,在train_model

中      

clf.fit(X_train,y_train)文件“C:\ Python27 \ lib \ site-packages \ sklearn \ svm \ base.py”,第676行,适合   提高ValueError(“类的数量必须大于”ValueError:类的数量必须大于一。

以下是我的代码:

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)

2 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:1)

完全正确。您的最后一列(标签)只有一种类型(分类)。您应该至少有两个。例如;如果您的标签决定是否要卸载,则标签列应具有卸载和不卸载或(0或1)。

相关问题