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:
JavaScript
x
27
27
1
train_size = int(len(df) * 0.7,)
2
train_text = df['cleansed_text'][:train_size]
3
train_cat = df['category'][:train_size]
4
5
test_text = df['cleansed_text'][train_size:]
6
test_cat = df['category'][train_size:]
7
8
max_words = 2500
9
tokenize = text.Tokenizer(num_words=max_words, char_level=False)
10
tokenize.fit_on_texts(train_text)
11
12
13
x_train = tokenize.texts_to_matrix(train_text)
14
x_test = tokenize.texts_to_matrix(test_text)
15
16
17
encoder = LabelEncoder()
18
encoder.fit(train_cat)
19
y_train = encoder.transform(train_cat)
20
y_test = encoder.transform(test_cat)
21
22
23
num_classes = np.max(y_train) + 1
24
y_train = utils.to_categorical(y_train, num_classes)
25
y_test = utils.to_categorical(y_test, num_classes)
26
27
JavaScript
1
18
18
1
model = Sequential()
2
model.add(Dense(256, input_shape=(max_words,)))
3
model.add(Dropout(0.5))
4
model.add(Dense(256,))
5
model.add(Dropout(0.5))
6
model.add(Activation('relu'))
7
model.add(Dense(num_classes, activation='softmax'))
8
9
model.compile(loss='categorical_crossentropy',
10
optimizer='adam',
11
metrics=['accuracy'])
12
model.summary()
13
history = model.fit(x_train, y_train,
14
batch_size=32,
15
epochs=10,
16
verbose=1,
17
validation_split=0.1)
18
JavaScript
1
11
11
1
from sklearn.metrics import classification_report
2
3
y_test_arg=np.argmax(y_test,axis=1)
4
Y_pred = np.argmax(model.predict(x_test),axis=1)
5
print('Confusion Matrix')
6
print(confusion_matrix(y_test_arg, Y_pred))
7
8
print(classification_report(y_test_arg, y_pred, labels=[1,2,3,4,5]))
9
10
11
However, when I attempt to print out the classification report, it ran into this error:
JavaScript
1
45
45
1
21/21 [==============================] - 0s 2ms/step
2
Confusion Matrix
3
[[138 1 6 0 2]
4
[ 0 102 3 0 2]
5
[ 3 2 121 1 2]
6
[ 1 0 1 157 0]
7
[ 0 3 0 0 123]]
8
---------------------------------------------------------------------------
9
ValueError Traceback (most recent call last)
10
Input In [56], in <cell line: 8>()
11
5 print('Confusion Matrix')
12
6 print(confusion_matrix(y_test_arg, Y_pred))
13
----> 8 print(classification_report(y_test_arg, y_pred, labels=[1,2,3,4,5]))
14
15
File ~anaconda3libsite-packagessklearnmetrics_classification.py:2110, in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits, output_dict, zero_division)
16
1998 def classification_report(
17
1999 y_true,
18
2000 y_pred,
19
( )
20
2007 zero_division="warn",
21
2008 ):
22
2009 """Build a text report showing the main classification metrics.
23
2010
24
2011 Read more in the :ref:`User Guide <classification_report>`.
25
( )
26
2107 <BLANKLINE>
27
2108 """
28
-> 2110 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
29
2112 if labels is None:
30
2113 labels = unique_labels(y_true, y_pred)
31
32
File ~anaconda3libsite-packagessklearnmetrics_classification.py:93, in _check_targets(y_true, y_pred)
33
90 y_type = {"multiclass"}
34
92 if len(y_type) > 1:
35
---> 93 raise ValueError(
36
94 "Classification metrics can't handle a mix of {0} and {1} targets".format(
37
95 type_true, type_pred
38
96 )
39
97 )
40
99 # We can't have more than one value on y_type => The set is no more needed
41
100 y_type = y_type.pop()
42
43
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets
44
45
JavaScript
1
48
48
1
y_test_arg
2
array([3, 3, 1, 0, 4, 1, 0, 4, 3, 4, 1, 1, 2, 2, 3, 0, 0, 4, 1, 3, 2, 0,
3
4, 1, 2, 3, 1, 2, 2, 4, 3, 2, 0, 2, 1, 4, 3, 2, 1, 1, 0, 3, 4, 4,
4
3, 1, 4, 2, 4, 3, 2, 2, 3, 1, 3, 2, 3, 4, 1, 3, 1, 0, 0, 1, 1, 1,
5
4, 3, 0, 0, 2, 2, 0, 2, 1, 3, 3, 4, 2, 3, 0, 3, 0, 4, 3, 3, 0, 1,
6
3, 3, 4, 3, 0, 2, 0, 1, 4, 1, 2, 0, 1, 2, 1, 2, 2, 0, 3, 3, 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,
22
3, 0, 3, 0, 0, 0, 0, 3, 2, 3, 4, 4, 3, 4, 0, 1, 0, 3, 0, 4, 4, 2,
23
1, 0, 1, 0, 4, 2, 1, 2, 1, 1, 4, 0, 4, 4, 0, 2, 3, 1, 0, 2, 1, 0,
24
4, 3, 4, 2, 3, 2, 0, 2, 2, 0, 0, 0, 4, 2, 0, 2, 0, 1, 2, 3, 2, 2,
25
3, 1, 4, 4, 0, 4, 3, 0, 0, 2, 3, 4, 4, 4, 3, 1, 3, 2, 0, 2, 2, 1,
26
4, 0, 4, 3, 1, 1, 3, 0, 1, 4, 4, 3, 1, 0, 2, 2, 2, 4, 4, 0, 2, 0,
27
2, 2, 1, 3, 4, 0, 4, 1, 4, 4, 3, 2, 3, 3, 2, 1, 1, 0, 2, 2, 3, 0,
28
0, 4, 0, 4, 4, 3, 0, 2, 3, 0, 0, 3, 4, 3, 4, 1, 3, 3, 1, 0, 4, 3,
29
3, 2, 4, 0, 2, 3, 3, 2, 1, 4, 4, 4, 0, 3, 1, 1, 4, 0, 2, 4, 3, 3,
30
4, 4, 2, 0, 3, 1, 1, 3, 1, 4, 4, 0, 0, 0, 3, 3, 4, 3, 0, 4, 0, 0,
31
3, 0, 2, 0, 0, 4, 0, 4, 2, 4, 1, 2, 4, 1, 3, 2, 1, 0, 4, 0, 4, 1,
32
4, 3, 0, 0, 2, 1, 2, 3], dtype=int64)
33
y_pred
34
array([[2.6148611e-05, 1.2884392e-06, 8.0136197e-06, 9.9993646e-01,
35
2.8027451e-05],
36
[1.1888630e-08, 1.9621881e-07, 6.0117927e-08, 9.9999917e-01,
37
4.2087538e-07],
38
[2.4368815e-06, 9.9999702e-01, 2.0465748e-07, 9.2730332e-08,
39
2.5044619e-07],
40
,
41
[8.7212893e-04, 9.9891293e-01, 7.5106349e-05, 7.0842376e-05,
42
6.8954141e-05],
43
[1.2511186e-02, 5.9731454e-05, 9.8512655e-01, 3.0246837e-04,
44
2.0000227e-03],
45
[5.9550672e-07, 7.1766672e-06, 2.0012515e-06, 9.9999011e-01,
46
1.1376539e-07]], dtype=float32)
47
48
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:
JavaScript
1
3
1
y_test_arg=np.argmax(y_test,axis=1)
2
Y_pred = np.argmax(model.predict(x_test),axis=1)
3
It would help if you rounded your predictions in Y_pred before calculating classification_report. You can see this question