简体   繁体   中英

AttributeError: 'str' object has no attribute 'pop' in matplotlib

I am trying to learn Tensorflow and Deep Neural Networks. This error came up and i couldn't find an explanation. I am working on Pycharm and i tried on Anaconda command prompt also . Should i try on windows command prompt?

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
data = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

 train_images = train_images/255
 test_images = test_images/255
 model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # First layer
keras.layers.Dense(128, activation='relu'), #Second layer
keras.layers.Dense(10, activation='softmax') # Third layer
])


 model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])
 model.fit(train_images, train_labels, epochs=5)

 # test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

 # print('\nTest accuracy:', test_acc)

  prediction = model.predict(test_images)
 # print(class_names[np.argmax(prediction[1])])

 for i in range(5):
    plt.grid(False)
    plt.imshow(test_images[i], cmap=plt.cm.binary)
    plt.xlabel("Actual: ", class_names[test_labels[i]])
    plt.title("Prediction : ", class_names[np.argmax(prediction[i])])
    plt.show()

''' And I get this error '''

 Traceback (most recent call last):
   File "C:/Users/Kullanici/Desktop/MachineLearningProjects/neuralNetworkTutorial/TensorDeepNN.py", 
     line 40, in <module>
    plt.xlabel("Actual: ", class_names[test_labels[i]])
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\pyplot.py", line 
    3063, in xlabel
    xlabel, fontdict=fontdict, labelpad=labelpad, **kwargs)
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\axes\_axes.py", line 
    247, in set_xlabel
     return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
     File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\axis.py", line 
     1598, in set_label_text
    self.label.update(fontdict)
    File "C:\Users\Kullanici\Anaconda3\envs\tensor\lib\site-packages\matplotlib\text.py", line 176, 
     in update
    bbox = kwargs.pop("bbox", sentinel)
   **AttributeError: 'str' object has no attribute 'pop'**

I couldn't find a solution.

In function, matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs) the first argument should be string. However, you are passing

    plt.xlabel("Actual: ", class_names[test_labels[i]])
    plt.title("Prediction : ", class_names[np.argmax(prediction[i])])

which is not a string. You should call

    plt.xlabel("Actual: " + class_names[test_labels[i]])
    plt.title("Prediction : " + class_names[np.argmax(prediction[i])])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM