limited number of classes for MultiOutputClassifier + SVC?

Viewed 144

I'm working on a machine learning problem and I met a limit on the number of classes I can run by using MultiOutputClassifier and SVC. In particular, the fit fails when the number of classes>14. I follow the example reported in the MultiOutputClassifier manual page, but using SVC and n_classes=15:

import numpy as np
from sklearn.datasets import make_multilabel_classification
from sklearn.multioutput import MultiOutputClassifier
from sklearn.svm import SVC

X, y = make_multilabel_classification(n_classes=15, random_state=0)
clf = MultiOutputClassifier(SVC()).fit(X, y)

This code fails with the following error message:

"ValueError: The number of classes has to be greater than one; got 1 class"

By using KNeighborsClassifier it does not fail. What's wrong? how can I make it work?

1 Answers

The problem is with the error message itself because it is misleading. Your problem is that the default value for n_samples is set to 100 and when using n_classes=15, some classes are not represented in the y output at all which is the source of the error. The classifier expects each class to be represented at least once.

Just by changing the n_samples to some bigger value, let's say 1000 will "solve" the error because now, every class will be represented.

X, y = make_multilabel_classification(n_classes=15, random_state=0, n_samples=1000)
clf = MultiOutputClassifier(SVC()).fit(X, y)

Here is simple reproduction case.

Given (5x5) input and (5x5) output (identity matrix), this will work just fine.

y = np.eye(5)
x = np.ones((5, 5))
clf = MultiOutputClassifier(SVC()).fit(x, y)

Now let's change the first row of the y to have 1 in the second column instead (the first class will not be represented anymore)

y = np.eye(5)
y[0, 0] = 0
y[0, 1] = 1
x = np.ones((5, 5))
clf = MultiOutputClassifier(SVC()).fit(x, y)

This will throw the same error

ValueError: The number of classes has to be greater than one; got 1 class

Related