'Plotting classification results with confusion matrices on python

I am performing least squares classification on my data and I was able to obtain my weights and I decided to plot a decision boundary line. However I require to use a confusion matrix to show my classification results. I was going to use from sklearn.metrics import confusion_matrix and I was going to assign t as my prediction however I am not sure how to obtain my actual results to work out the matrix. I have never plotted one so I might be getting all this wrong.

import numpy as np
import matplotlib.pyplot as plt

data=np.loadtxt("MyData_A.txt")
x=data[:,0:2] #the data points 
t=data[:,2] #class which data points belong to either 1s or 0s

x0=np.ones((len(x),1)) # creat array of ones as matrix (nx1) where n is number of points
X=np.append(x, x0, axis=1) # add column x0 to data

# w= ( (((X^T)X)^-1 )X^T )t           
XT_X=np.dot(X.T, X)              # (X^T)X
inv_XT_X=np.linalg.inv(XT_X)     # (X^T)X)^-1
X_tot=np.dot(inv_XT_X, X.T)      # ((X^T)X)^-1 )X^T
w=np.dot(X_tot, t)               # ( (((X^T)X)^-1 )X^T )t

x1_line = np.array([-1, 2])
x2_line = -w[2] / w[1] - (w[0] / w[1]) * x1_line

color_cond=['r' if t==1 else 'b' for t in t]
plt.scatter(x[:,0],x[:,1],color=color_cond)
plt.plot(x1_line,x2_line,color='k')
plt.xlabel('X1')
plt.ylabel('X2')
plt.ylim(-2,2)
plt.title('Training Data (X1,X2)')
plt.show()

The following is the plot obtained.



Solution 1:[1]

from sklearn.metrics import confusion_matrix
import seaborn as sns

def predict(x1_line, x2_line, x):
    d = (x[0] - x1_line[0]) * (x2_line[1] - x2_line[0]) - (x[1] - x2_line[0]) * (x1_line[1] - x1_line[0])
    pred = 0 if d > 0 else 1
    return pred

preds = np.array([predict(x1_line, x2_line, x12) for x12 in x])

conf_mat = confusion_matrix(t, preds)
sns.heatmap(conf_mat, annot=True);
plt.show()

Solution 2:[2]

LogisticRegression, confusion_matrix and ConfusionMatrixDisplay get the job done:

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

data = np.loadtxt("MyData_A.txt")
X = data[:, :-1]
y = data[:, -1].astype(int)
clf = LogisticRegression().fit(X, y)
pred = clf.predict(X)
cm = confusion_matrix(y, pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Hodel
Solution 2