# How to get confusion matrix and precision of CNN model

**URL:** <https://discuss.ai.google.dev/t/how-to-get-confusion-matrix-and-precision-of-cnn-model/32136>\
**Category:** General Discussion\
**Tags:** keras, datasets, help\_request\
**Created:** [September 27, 2021, 5:03pm UTC](https://discuss.ai.google.dev/t/how-to-get-confusion-matrix-and-precision-of-cnn-model/32136 "2021-09-27T17:03:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Decena\_Castil](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Javier\_Decena\_Castil](https://discuss.ai.google.dev/u/Javier_Decena_Castil)\
**Post date:** [September 27, 2021, 5:03pm UTC](https://discuss.ai.google.dev/t/how-to-get-confusion-matrix-and-precision-of-cnn-model/32136/1 "2021-09-27T17:03:39Z")

</div>

I have trained a CNN with keras to detect if an image has a manhole inside or not. I want to get the confusion matrix and precision recall of my model but i dont know what is my real data and my predicted data.

This is the input I have:

```auto
Imports of datasets inside Drive
ds_negatives = np.loadtxt('/content/drive/MyDrive/Colab Notebooks/negative_depth.txt')
ds_positives = np.loadtxt('/content/drive/MyDrive/Colab Notebooks/positive_depth.txt')

#Labeled arrays for datasets
arrayceros = np.zeros(n_negatives_img)
arrayunos = np.ones(n_positives_img)

#Reshaping of datasets to convert separate them
arraynegativos= ds_negatives.reshape(( n_negatives_img, img_height, img_width,1))
arraypositivos= ds_positives.reshape((n_positives_img, img_height, img_width,1))

#Labeling datasets with the arrays
ds_negatives_target = tf.data.Dataset.from_tensor_slices((arraynegativos, arrayceros))
ds_positives_target = tf.data.Dataset.from_tensor_slices((arraypositivos, arrayunos))

#Concatenate 2 datasets and shuffle them
ds_concatenate = ds_negatives_target.concatenate(ds_positives_target)
datasetfinal = ds_concatenate.shuffle(n_total_img)

print("shape of positive data:",datasetfinal)
#24000 images for the training ds and 20000 for the validation ds
valid_ds_f = datasetfinal.take(10000)
train_ds_f = datasetfinal.skip(10000)

valid_ds = valid_ds_f.batch(12)
train_ds = train_ds_f.batch(12)

```

This is the fit of the model:

```auto
history= model.fit(train_ds, validation_data=valid_ds, batch_size=100, epochs=10)

```

BTW i leave here the link to the code.

> **[Google Colab](https://colab.research.google.com/drive/1KzoHXA8Y6lcyvq7K7segFfJp2AIw9P45?usp=sharing)**

---

<div class="post-metadata">

**Author:** ![lgusm](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lgusm/32/6230_2.png) [@lgusm](https://discuss.ai.google.dev/u/lgusm)\
**Post date:** [September 28, 2021, 6:03pm UTC](https://discuss.ai.google.dev/t/how-to-get-confusion-matrix-and-precision-of-cnn-model/32136/3 "2021-09-28T18:03:31Z")

</div>

your real data could be your test split with the labels  
the predicted data is what your trained model returns when you give the test split data.

is that it or maybe I didn’t understand the question

---

<div class="post-metadata">

**Author:** ![Javier\_Decena\_Castil](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Javier\_Decena\_Castil](https://discuss.ai.google.dev/u/Javier_Decena_Castil)\
**Post date:** [September 28, 2021, 6:26pm UTC](https://discuss.ai.google.dev/t/how-to-get-confusion-matrix-and-precision-of-cnn-model/32136/4 "2021-09-28T18:26:06Z")

</div>

Yes but how do i extract from my Real data and the predicted Just de labeled 0,1 arrays. I do now know how to split the datasets in x\_train and y\_train.

I have tried with train\_test\_split but i cant.
