Plotting conusion Matrix

Viewed 34

I am trying to plot a confusion matrix and heatmap that contains the percentages and numbered values, I use the code in this link https://gist.github.com/mesquita/f6beffcc2579c6f3a97c9d93e278a9f1

The Error message :

cm = confusion_matrix(y_true, y_pred, labels=labels)
File "C:\Users\XX\anaconda3\envs\yamnet\lib\site- 
packages\sklearn\metrics\_classification.py", line 316, in confusion_matrix
raise ValueError("At least one label specified must be in y_true")
ValueError: At least one label specified must be in y_true

This is my code:

encoder = LabelBinarizer()
print("encoder", encoder)
labels = encoder.fit_transform(y)
print("label ", labels)
print("y", y)
# Save the names of the classes for future using.
np.save(fname, encoder.classes_)
num_classes = len(np.unique(y))

# Generate the model
general_model = generate_model(num_classes, num_hidden=num_hidden,
                               activation=activation)
general_model.compile(optimizer=optimizer, loss='categorical_crossentropy',
                      metrics=['accuracy'])

# Create some callbacks
callbacks = [tf.keras.callbacks.ModelCheckpoint(filepath=fname, monitor='val_loss',
                                                save_best_only=True),
             tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.9,
                                                  patience=15, verbose=1,
                                                  min_lr=0.000001)]

X, labels = shuffle(X, labels)

X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.20)

history = general_model.fit(X_train, y_train, epochs=epochs, validation_split=0.20,
                            batch_size=batch_size, callbacks=callbacks, verbose=1)
score = general_model.evaluate(X_test, y_test, verbose=0)
print(f'Test loss: {score[0]} / Test accuracy: {score[1]}')
y_pred = general_model.predict(X_test)
y_pred = (y_pred.argmax(axis=1))
y_test = (y_test.argmax(axis=1))
cm_analysis(y_test, y_pred, "ConfusionMatrix", y, X, ymap=None, figsize=(17, 17))
0 Answers
Related