Home > Software design >  MultiOutputClassifier ValueError: The number of classes has to be greater than one
MultiOutputClassifier ValueError: The number of classes has to be greater than one

Time:10-29

I am solving a multilabel classification task using SVM with data representing features of processed images in X and the presence of 6 natural elements (like hills, clouds, etc) represented by binary variables(0 if absent/1 if present), present in Y. Here's the train and the test data:

Train: https://s3.amazonaws.com/istreet-questions-us-east-1/418844/train.csv
Test: https://s3.amazonaws.com/istreet-questions-us-east-1/418844/test.csv

Number of features: 294 Number of labels for each instance: 6

This is the code I am using to train my model:

import csv
import numpy as np

train = []
test = []

with open('/home/keerat/Desktop/train.csv') as trainfile:
    reader = csv.reader(trainfile)
    for row in reader:
        train.append(row)

with open('/home/keerat/Desktop/test.csv') as testfile:
    reader = csv.reader(testfile)
    for row in reader:
        test.append(row)

X = []
y = []
X_test = []
# split data into X and y
for i in range(len(train)):
    X.append(train[i][0:294])
    y.append(train[i][294:300])
for i in range(len(test)):
    X_test.append(test[i][0:294])

# convert list of strings to list of num
for i in range(len(X)):
    X[i] = [float(x) for x in X[i]]
for j in range(len(y)):
    y[j] = [int(yy) for yy in y[i]]
for i in range(len(X_test)):
    X_test[i] = [float(x) for x in X_test[i]]

X = np.array(X)
y = np.array(y)
X_test = np.array(X_test)
# define svm model for multi label classification
from sklearn.svm import SVC
from sklearn import metrics
from sklearn.multioutput import MultiOutputClassifier
svc=SVC() #Default hyperparameters
n_samples, n_features = X.shape
n_outputs = y.shape[1]
multi_target_svc = MultiOutputClassifier(svc, n_jobs=-1)
multi_target_svc.fit(X[:],y)

Here's how X and y look like:

X:
[[0.826575 0.843082 0.805944 ... 0.010919 0.011375 0.015069]
 [0.766867 0.669694 0.636238 ... 0.055661 0.079765 0.097522]
 [0.962784 0.975387 0.96395  ... 0.195177 0.221791 0.201402]
 ...
 [0.527828 0.588172 0.639713 ... 0.030422 0.004995 0.002626]
 [0.574357 0.598345 0.63484  ... 0.039915 0.075365 0.056335]
 [0.698135 0.732643 0.724918 ... 0.014463 0.04427  0.041442]]
y: 
[[1 0 0 0 0 1]
 [1 0 0 0 0 1]
 [1 0 0 0 0 1]
 ...
 [1 0 0 0 0 1]
 [1 0 0 0 0 1]
 [1 0 0 0 0 1]]

The model.fit() line throws the error mentioned in the main heading. I have already checked numpy.unique(y)-->[0 1]which means I have more than 1 (2 precisely) class available.

Could anyone pls give some insight into what's going wrong here?

CodePudding user response:

The training and testing go ahead smoothly if the n_jobs parameter in MultiOutputClassifier() is set to 1 instead of -1. Not sure what the reason is but, after this modification, the problem is solved for all classifiers in sklearn.

  • Related