Confusion matrix
by: Chris Cave
(Feyn version 3.4.0 or newer)
A confusion matrix provides a summary or all correct and incorrect classifications made by a binary classifier Model
.
This plot only works for classifiers, and will return a type error if you try to use it on a regressor.
Example
import feyn
import pandas as pd
from sklearn.datasets import load_breast_cancer
# Load into a pandas dataframe
breast_cancer = load_breast_cancer(as_frame=True)
data = breast_cancer.frame
# Train/test split
train, test = feyn.tools.split(data, ratio=[0.6, 0.4], stratify='target', random_state=666)
ql = feyn.QLattice()
models = ql.auto_run(
data=train,
output_name = 'target'
)
best = models[0]
Plotting the confusion matrix
best.plot_confusion_matrix(train, threshold=0.5)
This provides all the True Positives, True Negatives, False Positives and False Negatives at the given threshold. The default threshold is 0.5.
Saving the plot
You can save the plot using the filename
parameter. The plot is saved in the current working directory unless another path specifed.
best.plot_confusion_matrix(data=train, filename="feyn-plot")
If the extension is not specified then it is saved as a png file.
Feyn
Location in This function can also be found in feyn.plots
module.
from feyn.plots import plot_confusion_matrix
y_true = train['target']
y_pred = best.predict(train).round()
plot_confusion_matrix(y_true, y_pred)