2016-11-24 46 views
0

當傳遞fitx,y,我收到以下錯誤:ValueError異常:類的數量必須大於一個(蟒蛇)更大

回溯(最近通話最後一個):

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) 
+0

scikit學習版本0.15.2 –

回答

6

您可能在訓練集目前只有一個唯一的類別標籤。正如錯誤消息所述,您需要在數據集中至少有兩個獨特的類。例如,您可以運行np.unique(y)以查看數據集中唯一的類標籤。

+1

哪裏以及如何使用np.unique(y)? –

+1

只是檢查它。無論在哪裏... – MMF

+0

只要看看你的代碼示例中已經有'labels = np.unique(Y)'。就在'print',例如'labels = np.unique(Y);打印(標籤)' – Sebastian

相關問題