ValueError: The number of classes has to be greater than one; got 1 class in MultiLabel Classification problem I am working in Python in a Multilabel Classification problem. I have a dataset with texts and around 20k unique labels. I transformed the text to word embeddings and now I use that in a ChainClassifier to predict the tags that each texts might have.

from skmultilearn.problem_transform import LabelPowerset, ClassifierChain, BinaryRelevance
from sklearn.svm import SVC

df = pd.read_csv('sample_fos.csv')
df = df.dropna()
df.fos = df.fos.str.split(',')

mlb = MultiLabelBinarizer()

data = df.join(pd.DataFrame(mlb.fit_transform(df.pop('fos')),
                          columns=mlb.classes_,
                          index=df.index))

labels = mlb.classes_

X_train, X_test, y_train, y_test = train_test_split(data[features], data[labels], test_size=0.20, shuffle=True)

clf = ClassifierChain(classifier=SVC(gamma="auto"))
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)


I get the following error: 

  File "&lt;ipython-input-54-976064cc6433&gt;", line 30, in &lt;module&gt;
    clf.fit(X_train,y_train)

  File "/lib/python3.7/site-packages/skmultilearn/problem_transform/cc.py", line 155, in fit
    X_extended), self._ensure_output_format(y_subset))

  File "/lib/python3.7/site-packages/sklearn/svm/base.py", line 147, in fit
    y = self._validate_targets(y)

  File "/lib/python3.7/site-packages/sklearn/svm/base.py", line 521, in _validate_targets
    " class" % len(cls))

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


In my knowledge, this means that one of the label columns has one class only (either 0 or 1)

However, running the following snippet, I get that all of my columns has 2 unique values

b = []
for i in labels:
    b.append(len(data[i].unique()))
print(min(b),max(b))
&gt;&gt; 2, 2
Could you see how many classes are in training and in test?

y_train.unique()
y_test.unique()


It can happen that at some point you got a split that has only one label, if your data is small or heavily imbalanced there are more chances. One way to fix it is stratifying the target.

train_test_split(data[features], data[labels], test_size=0.20, shuffle=True,stratify=data[labels])