title: CNN- Trying to run a confusion matrix using seaborn.heatmap
body: <p>I've been trying to run a confusion matrix after my CNN model ran.</p>
  
  <p>My model is classifying dogs/bunnies.</p>
  
  <p>The following is what I did:</p>
  
  <p>I placed the photos of each class (dogs/bunnies) in separate folders inside two folders: training and testing.</p>
  
  <p>Training directory-> Bunny directory -> bunny images</p>
  
  <p>Training directory-> Puppy directory -> puppy images</p>
  
  <p>Testing directory-> Bunny directory -> bunny images</p>
  
  <p>Testing directory-> Puppy directory -> puppy images</p>
  
  <p>I used the following code to get the images from the folders:</p>
  
  <pre><code>training_data = train_datagen.flow_from_directory('./images/train',
                                               target_size = (28, 28),
                                               batch_size = 86,
                                               class_mode = 'binary',
                                               color_mode='rgb',
                                               classes=None)
  
  
  test_data = test_datagen.flow_from_directory('./images/test',
                                          target_size = (28, 28),
                                          batch_size = 86,
                                          class_mode = 'binary',
                                          color_mode='rgb',
                                          classes=None)
  </code></pre>
  
  <p>I used the following code to seperate the images into training/val.</p>
  
  <pre><code>data_generator = ImageDataGenerator(
      validation_split=0.2,
      rescale=1./255,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
  )
  
  train_generator = data_generator.flow_from_directory(
      './images/train',
      target_size = (28, 28),
      batch_size = 86,
      class_mode = 'binary',
      color_mode='rgb',
      classes=None, subset="training"
  )
  
  validation_generator = data_generator.flow_from_directory(
      './images/train',
      target_size = (28, 28),
      batch_size = 86,
      class_mode = 'binary',
      color_mode='rgb',
      classes=None, subset="validation"
  )
  
  history=classifier.fit_generator(
      train_generator,
      steps_per_epoch = (8000 / 86),
      epochs = 2,
      validation_data = validation_generator,
      validation_steps = 8000/86,
      callbacks=[learning_rate_reduction]
  )
  </code></pre>
  
  <p>When I tried to run  <code>confusion_matrix(validation_data)</code> I get this error:</p>
  
  <p><code>TypeError: confusion_matrix() missing 1 required positional argument: 'y_pred'</code></p>
  
  <p>And when I run</p>
  
  <pre><code>#Confusion matrix
  def plot_confusion_matrix(cm, classes,
                            normalize=False,
                            title='Confusion matrix',
                            cmap=plt.cm.Blues):
      """
      This function prints and plots the confusion matrix.
      Normalization can be applied by setting `normalize=True`.
      """
      plt.imshow(cm, interpolation='nearest', cmap=cmap)
      plt.title(title)
      plt.colorbar()
      tick_marks = np.arange(len(classes))
      plt.xticks(tick_marks, classes, rotation=45)
      plt.yticks(tick_marks, classes)
  
      if normalize:
          cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  
      thresh = cm.max() / 2.
      for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
          plt.text(j, i, cm[i, j],
                   horizontalalignment="center",
                   color="white" if cm[i, j] &gt; thresh else "black")
  
      plt.tight_layout()
      plt.ylabel('True label')
      plt.xlabel('Predicted label')
  
  # Predict the values from the validation dataset
  Y_pred = classifier.predict(training_data)
  # Convert predictions classes to one hot vectors 
  Y_pred_classes = np.argmax(Y_pred,axis = 1) 
  # Convert validation observations to one hot vectors
  Y_true = np.argmax(training_data,axis = 1) 
  # compute the confusion matrix
  confusion_mtx = confusion_matrix(Y_true, Y_pred_classes) 
  # plot the confusion matrix
  plot_confusion_matrix(confusion_mtx, classes = range(10))
  
  sns.heatmap(confusion_mtx, annot=True, fmt='d')
  </code></pre>
  
  <p>I get the following error</p>
  
  <p><code>AttributeError: 'DirectoryIterator' object has no attribute 'ndim'</code></p>