Warrior
Warrior

Reputation: 87

Confusion Matrix error : Mismatched Datatypes between Actual and Predicted Values

enter image description here

Original Dataset is as shown above. CO(ppm) is the dependent variable.

model = Sequential()
#First Hidden Layer
model.add(Dense(64, activation='relu', kernel_initializer='random_normal', input_dim=19))
#Second  Hidden Layer
model.add(Dense(4, activation='relu', kernel_initializer='random_normal'))
#model.add(Dropout(0.3))
#Output Layer
model.add(Dense(1, activation='sigmoid', kernel_initializer='random_normal'))

For a binary classification problem shown above, I am trying to obtain the confusion matrix. I have arrays resulting from y_pred and y_test and the datatypes are mismatched because the y_pred outputs a range of values from 0 to 1(sigmoid activation function) and the y_test has an array consisting of only 0s and 1s.

enter image description here

I would greatly appreciate it if someone can help me figure a method to plot the confusion matrix for this.

Thank you.

Upvotes: 0

Views: 362

Answers (1)

Chicodelarose
Chicodelarose

Reputation: 882

Supposing any predicted class is greater or equal to 0.5 and given that the final activation maps any value between 0 and 1 non-inclusive you could do the following:

y_pred = np.where(y_pred >= 0.5, 1, 0)

Any value greater and equal to 0.5 will be mapped to 1, otherwise 0. After this you can obtain the confusion matrix using sklearn's confusion_matrix method:

sklearn.metrics.confusion_matrix(y_true, y_pred)

Example:

y_true = np.array([1, 0, 0, 1, 1, 1, 0, 0])

y_pred = np.array([0.8, 0.1, 0.4, 0.6, 0.9, 0.69, 0.46, 0.21])
y_pred = np.where(y_pred >= 0.5, 1, 0)

sklearn.metrics.confusion_matrix(y_true, y_pred)

Output:

array([[4, 0],
       [0, 4]])

Upvotes: 2

Related Questions