3
$\begingroup$

I'm using Classifier Chain with logistic regression and when i try to use fit, i get

This solver needs samples of at least 2 classes in the data, but the data contains only one class: 1

but I'm pretty sure I have two classes in my data, this is my X_test

<5825x2000 sparse matrix of type '<class 'numpy.float64'>'
    with 219990 stored elements in Compressed Sparse Row format>

and I have this as my y_train:

array([[1, 0, 0, ..., 0, 1, 0],
       [1, 0, 0, ..., 1, 1, 0],
       [1, 0, 0, ..., 0, 1, 0],
       ...,
       [1, 0, 0, ..., 1, 1, 0],
       [1, 0, 0, ..., 0, 1, 0],
       [1, 0, 0, ..., 0, 1, 0]])

and this is the part I'm trying to run

cc_clf = ClassifierChain(LogisticRegression(penalty='l1', C=1, dual=False, solver='liblinear'))
cc_clf.fit(X_train, y_train)
cc_y_pred = cc_clf.predict(X_test)

Am i using the classifier chain incorrectly? When I pass in the same data into one vs rest, it works.

clf = OneVsRestClassifier(classifier1)
clf.fit(X_train, y_train)
$\endgroup$
3
  • $\begingroup$ Check whether every column of y_train has both 0 and 1? $\endgroup$
    – Ben Reiniger
    Dec 8, 2019 at 14:14
  • $\begingroup$ ...erm, except that should also break OneVsRestClassifier, I think. $\endgroup$
    – Ben Reiniger
    Dec 8, 2019 at 20:05
  • $\begingroup$ How many classes do you have in the output data? Any reason to use Classifier Chain if you have only 2 classes? $\endgroup$
    – hssay
    Dec 9, 2019 at 8:44

1 Answer 1

0
$\begingroup$

To use ClassifierChain every column of y_train has both 0 and 1. Here is a valid and invalid example:

import numpy as np
from sklearn.multioutput  import ClassifierChain
from sklearn.linear_model import LogisticRegression

X_train = np.array([[1], [2]])

# Runs 
y_train = np.array([[0, 1], [1, 0]]) 
cc_clf = ClassifierChain(LogisticRegression(penalty='l1', C=1, dual=False, solver='liblinear'))
cc_clf.fit(X_train, y_train)

# ValueError
y_train = np.array([[0, 1], [0, 0]])   
cc_clf = ClassifierChain(LogisticRegression(penalty='l1', C=1, dual=False, solver='liblinear'))
cc_clf.fit(X_train, y_train)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.