简体   繁体   中英

ValueError: x and y must have same first dimension, but have shapes (6,) and (8,)

I've been following the step by step tutorial for this web app for plant disease detection and there's an error in this part where it suppose to show the line graph but there's an error at the line 3 which said "ValueError: x and y must have same first dimension, but have shapes (6,) and (8,)" I hope someone can help me to solve this thank you in advance Im just a beginner in coding so it will be a huge help for me.

n = 6
plt.figure(figsize=(8,5))
plt.plot(np.arange(1, n + 1),history.history['loss'], label = 'train_loss')
plt.plot(np.arange(1,n + 1), history.history['val_loss'], label = 'val_loss')
plt.plot(np.arange(1,n + 1), history.history['val_accuracy'], label = 'val_accuracy')
plt.grid(True)
plt.legend(loc = "best")
plt.savefig('/content/drive/My Drive/PlantDRecognition/performance.jpg')
plt.show()

The problem is that the the length of history.history['loss'] and n are not equal.

Actually, x values of matplotlib.pyplot.plot(x, y) are optional and default to range(len(y)) . You only need

plt.plot(history.history['loss'], label = 'train_loss')
plt.plot(history.history['val_loss'], label = 'val_loss')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')

If you want x to start with 1,

plt.plot(range(1, len(history.history['loss'])+1), history.history['loss'], label = 'train_loss')
plt.plot(range(1, len(history.history['val_loss'])+1), history.history['val_loss'], label = 'val_loss')
plt.plot(range(1, len(history.history['val_accuracy'])+1), history.history['val_accuracy'], label = 'val_accuracy')

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