Confusion Matrix using logistic-regression python

Viewed 37

I need help creating a confusion matrix from my model. Ive seen multiple sources use yhat or y_prediction, I thought weight was that. But nothing i do is working and Ive tried several things. My model doesnt use scikit-learn but im open to using it for the confusion matirx.

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 55)
x_train

x_train = x_train.T
x_test = x_test.T
y_train = y_train.T
y_test = y_test.T

def sigmoid(x):
  return 1/(1+np.exp(-x))
def model(X, Y, learningRate, iterations):
  m= x_train.shape[1]
  n = x_train.shape[0]
  weight = np.zeros((n,1)) # initialize weights with zeros 
  bias = 0.0 

  costlist = []

  for i in range(iterations):

    z = np.dot(weight.T, X) + bias    #dot is vector multiplication & .T tranpose data 
    activation = sigmoid(z) 

    #cost function (propagate)
    cost = -(1/m)*np.sum(Y*np.log(activation) + (1-Y)*np.log(1-activation))
   

    #gradient (backward propagate)
    dw = (1/m)*np.dot(activation-Y, X.T) 
    db = (1/m)*np.sum(activation - Y)
    
    
    weigt = weight - learningRate*dw.T
    bias = bias - learningRate*db

    costlist.append(cost)

    if (i%(iterations/10)==0):
    #if i % 100 == 0:

      print("cost after", i, "iterations is:", cost)
      

  return weight, bias, costlist 
def accuracy(X, Y, weight, bias):
    
    z = np.dot(weight.T, X) + bias
    activation = sigmoid(z)
    
    activation = activation > 0.5
    
    activation = np.array(activation, dtype = 'int64')
    
    accuracy = (1 - np.sum(np.absolute(activation - Y))/Y.shape[1])*100
    ```
1 Answers

I've got this one from my classes anotations (credits to my Professor Carla), it uses scikit, hopefuly it will come in handy for you!

import sklearn.metrics as skm
import numpy as np
import pandas as pd

def mat_conf(Xmeasured, ymeasured, model,data_name): #data name is a string which you would like to entitle your confusion matrix
    Y_pred_prob = model.predict(Xmeasured)
    Y_pred = np.round(Y_pred_prob) # 50% limiar
    cmat=skm.confusion_matrix(ymeasured,Y_pred)
    cm_df = pd.DataFrame(cmat) 

    ax= plt.subplot()
    sns.heatmap(cm_df,annot=True, cmap = sns.color_palette("light:firebrick", as_cmap=True),fmt="d",cbar=False)
    plt.title('Test')


    ax.set_xlabel('Predicted');ax.set_ylabel('Real'); 
    ax.set_title('Confusion Matrix: '+data_name); 
    ax.xaxis.set_ticklabels(['label 1', 'label 2']); ax.yaxis.set_ticklabels(['with something', 'without that']);
    return

#mat_conf(x_train, y_train, model,'Train Data')
Related