Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python function to calculate the confusion matrix for the prediction results of a binary classifier. This function should take the form: def func_calConfusionMatrix(predY, trueY) where

Python function to calculate the confusion matrix for the prediction results of a binary classifier. This function should take the form:

def func_calConfusionMatrix(predY, trueY) where predY is the vector of the binary predicted classes (1 being positive, 0 being negative) and

trueY is the vector of binary true classes (1 being positive, 0 being negative). This function should return accuracy, per-class precision, and per-class recall rate.

I have the following codes and the error seems to tell me that my range(K) is only 2x2 and my trueY I try to loop to is not 2x2 matrix. I wonder if need to be fix since I can't find an appoarch to it.

Iimage text in transcribed

image text in transcribed

04): step 3: Use the model to get class labels of testing samples. #PLACEHOLDER3 #starti sklearn prd_elf - elf.predict(test) #Do I need to do this here #YHatLinear - 1*(prd_elf > 0.5) #model length len(test) yHat - 1 for i in range (length)] for i in rangel length): yHat[i] - round( Prediction (theta, testX[1])) #PLACEHOLDER#end *******PLACEHOLDER 3 #end ### 05): testy 05): array([[1.), [1.), [1.), [1.), [1.), [1.), [1.), [1.), [1.), [1.), [1.), (1.) [1.), [1.), [1.), [1.), [1.), (1.), [1.), [1.), 06]yllat 06] [1, 1, 18): def fune_calConfusionMatrix(truey, predy): Function to return accuracy, precison and recall of both the classes #creating a matrix of zeroes of size 2 x 2 K - len(np.unique(true)) result - np.zeros((K, K)) #calculate the values in the confusion matrix for i in range(K): result[truey[1]](pred[1]] +- 1 #the error occurs here #storing the four values into four different variables, namely tp, tn, fp, en tn - result[0][0) fp - result[o](1) fn - result(110) tp - result[1][1] accuracy - ((n + tp)/(tn + tp + En + fp))*100 #accuracy precision_postive - (tp/(fp + tp))*100 #precision of positive class precision negative - (tn/(tn + fn))*100 #precision of negative class recall_positive - (tp/(En + tp))*100 #recall of positive class recall_negative - (tn/(tn + fp))*100 #recall of negative class #return the values return accuracy, precision_postive, precision_negative, recall_positive, recall_negative 9): testy - testy.astype (int) 10): fyllat - map/int, yat) 12]: model_result - func_calConfusionMatrix(testy, yhat) IndexError Traceback (most recent call last) in ----> 1 model_result - func_calConfusionMatrix(testy, Hat) in fune_calConfusionMatrix(truey, predy) 11 #calculate the values in the confusion matrix 12 for i in range(K) --> 13 result[truey[i]](pred[1]] +- 1 #the error occurs here 10 15 #storing the four values into four different variables, namely tp, tn, fp, En IndexError: index 1 is out of bounds for axis O with size 1 I)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago