Skip to content
Advertisement

ValueError: Classification metrics unable to handle multiclass

I am trying to build a object classification model, but when trying to print out the classification report it returned a value error. ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

This is my current code:

train_size = int(len(df) * 0.7,)
train_text = df['cleansed_text'][:train_size]
train_cat = df['category'][:train_size]

test_text = df['cleansed_text'][train_size:]
test_cat = df['category'][train_size:]

max_words = 2500
tokenize = text.Tokenizer(num_words=max_words, char_level=False)
tokenize.fit_on_texts(train_text) 


x_train = tokenize.texts_to_matrix(train_text)
x_test = tokenize.texts_to_matrix(test_text)


encoder = LabelEncoder()
encoder.fit(train_cat)
y_train = encoder.transform(train_cat)
y_test = encoder.transform(test_cat)


num_classes = np.max(y_train) + 1
y_train = utils.to_categorical(y_train, num_classes)
y_test = utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(256, input_shape=(max_words,)))
model.add(Dropout(0.5))
model.add(Dense(256,))
model.add(Dropout(0.5))
model.add(Activation('relu'))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train,
                    batch_size=32,
                    epochs=10,
                    verbose=1,
                    validation_split=0.1)
from sklearn.metrics import classification_report

y_test_arg=np.argmax(y_test,axis=1)
Y_pred = np.argmax(model.predict(x_test),axis=1)
print('Confusion Matrix')
print(confusion_matrix(y_test_arg, Y_pred))

print(classification_report(y_test_arg, y_pred, labels=[1,2,3,4,5]))


However, when I attempt to print out the classification report, it ran into this error:

21/21 [==============================] - 0s 2ms/step
Confusion Matrix
[[138   1   6   0   2]
 [  0 102   3   0   2]
 [  3   2 121   1   2]
 [  1   0   1 157   0]
 [  0   3   0   0 123]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [56], in <cell line: 8>()
      5 print('Confusion Matrix')
      6 print(confusion_matrix(y_test_arg, Y_pred))
----> 8 print(classification_report(y_test_arg, y_pred, labels=[1,2,3,4,5]))

File ~anaconda3libsite-packagessklearnmetrics_classification.py:2110, in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits, output_dict, zero_division)
   1998 def classification_report(
   1999     y_true,
   2000     y_pred,
   (...)
   2007     zero_division="warn",
   2008 ):
   2009     """Build a text report showing the main classification metrics.
   2010 
   2011     Read more in the :ref:`User Guide <classification_report>`.
   (...)
   2107     <BLANKLINE>
   2108     """
-> 2110     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
   2112     if labels is None:
   2113         labels = unique_labels(y_true, y_pred)

File ~anaconda3libsite-packagessklearnmetrics_classification.py:93, in _check_targets(y_true, y_pred)
     90     y_type = {"multiclass"}
     92 if len(y_type) > 1:
---> 93     raise ValueError(
     94         "Classification metrics can't handle a mix of {0} and {1} targets".format(
     95             type_true, type_pred
     96         )
     97     )
     99 # We can't have more than one value on y_type => The set is no more needed
    100 y_type = y_type.pop()

ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

y_test_arg
array([3, 3, 1, 0, 4, 1, 0, 4, 3, 4, 1, 1, 2, 2, 3, 0, 0, 4, 1, 3, 2, 0,
       4, 1, 2, 3, 1, 2, 2, 4, 3, 2, 0, 2, 1, 4, 3, 2, 1, 1, 0, 3, 4, 4,
       3, 1, 4, 2, 4, 3, 2, 2, 3, 1, 3, 2, 3, 4, 1, 3, 1, 0, 0, 1, 1, 1,
       4, 3, 0, 0, 2, 2, 0, 2, 1, 3, 3, 4, 2, 3, 0, 3, 0, 4, 3, 3, 0, 1,
       3, 3, 4, 3, 0, 2, 0, 1, 4, 1, 2, 0, 1, 2, 1, 2, 2, 0, 3, 3, 3, 4,
       4, 3, 2, 1, 4, 3, 1, 0, 1, 2, 0, 3, 4, 0, 3, 2, 0, 1, 1, 1, 2, 1,
       2, 1, 3, 1, 3, 2, 2, 0, 2, 4, 3, 4, 3, 0, 2, 4, 1, 1, 2, 1, 2, 3,
       3, 2, 0, 4, 3, 2, 2, 1, 3, 2, 2, 0, 4, 4, 0, 4, 3, 3, 0, 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4, 0, 4, 3, 0, 0, 2, 3, 4, 4, 4, 3, 1, 3, 2, 0, 2, 2, 1,
       4, 0, 4, 3, 1, 1, 3, 0, 1, 4, 4, 3, 1, 0, 2, 2, 2, 4, 4, 0, 2, 0,
       2, 2, 1, 3, 4, 0, 4, 1, 4, 4, 3, 2, 3, 3, 2, 1, 1, 0, 2, 2, 3, 0,
       0, 4, 0, 4, 4, 3, 0, 2, 3, 0, 0, 3, 4, 3, 4, 1, 3, 3, 1, 0, 4, 3,
       3, 2, 4, 0, 2, 3, 3, 2, 1, 4, 4, 4, 0, 3, 1, 1, 4, 0, 2, 4, 3, 3,
       4, 4, 2, 0, 3, 1, 1, 3, 1, 4, 4, 0, 0, 0, 3, 3, 4, 3, 0, 4, 0, 0,
       3, 0, 2, 0, 0, 4, 0, 4, 2, 4, 1, 2, 4, 1, 3, 2, 1, 0, 4, 0, 4, 1,
       4, 3, 0, 0, 2, 1, 2, 3], dtype=int64)
y_pred
array([[2.6148611e-05, 1.2884392e-06, 8.0136197e-06, 9.9993646e-01,
        2.8027451e-05],
       [1.1888630e-08, 1.9621881e-07, 6.0117927e-08, 9.9999917e-01,
        4.2087538e-07],
       [2.4368815e-06, 9.9999702e-01, 2.0465748e-07, 9.2730332e-08,
        2.5044619e-07],
       ...,
       [8.7212893e-04, 9.9891293e-01, 7.5106349e-05, 7.0842376e-05,
        6.8954141e-05],
       [1.2511186e-02, 5.9731454e-05, 9.8512655e-01, 3.0246837e-04,
        2.0000227e-03],
       [5.9550672e-07, 7.1766672e-06, 2.0012515e-06, 9.9999011e-01,
        1.1376539e-07]], dtype=float32)

Advertisement

Answer

Your problem is caused by the presence of continuous-multioutput target values in y_test_arg or Y_pred. I think this error was generated in the below code:

y_test_arg=np.argmax(y_test,axis=1)
Y_pred = np.argmax(model.predict(x_test),axis=1)

It would help if you rounded your predictions in Y_pred before calculating classification_report. You can see this question

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement