Python ValueError:x和y必须具有相同的第一维度,但具有形状(6)和(8)

Python ValueError:x和y必须具有相同的第一维度,但具有形状(6)和(8),python,numpy,matplotlib,google-colaboratory,Python,Numpy,Matplotlib,Google Colaboratory,我一直在遵循这个植物疾病检测web应用程序的分步教程,这一部分有一个错误,它假设显示线图,但第3行有一个错误,它说“ValueError:x和y必须具有相同的第一维度,但具有形状(6,)和(8,)”我希望有人能帮我解决这个问题,谢谢你提前我只是一个初学者在编码,所以这将是一个巨大的帮助我 n = 6 plt.figure(figsize=(8,5)) plt.plot(np.arange(1, n + 1),history.history['loss'], label = 'train_loss

我一直在遵循这个植物疾病检测web应用程序的分步教程,这一部分有一个错误,它假设显示线图,但第3行有一个错误,它说“ValueError:x和y必须具有相同的第一维度,但具有形状(6,)和(8,)”我希望有人能帮我解决这个问题,谢谢你提前我只是一个初学者在编码,所以这将是一个巨大的帮助我

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()

问题是
history.history['loss']
n
的长度不相等

实际上,
x
的值是可选的,默认为
范围(len(y))
。你只需要

plt.plot(history.history['loss',标签='train_loss')
plt.plot(history.history['val\u loss'],label='val\u loss')
plt.plot(history.history['val\u accurity'],label='val\u accurity')
如果希望
x
以1开头

plt.plot(范围(1,len(history.history['loss'])+1),history.history['loss'],标签='train_loss')
plt.plot(范围(1,len(history.history['val_loss'])+1),history.history['val_loss'],label='val_loss'))
plt.plot(范围(1,len(history.history['val_-accurity'])+1),history.history['val_-accurity'],label='val_-accurity')

同时添加python代码。您提供输入值的部分。您可能需要使用
(1,6)
(1,8)
,因此请尝试使用numpyhy is
n=6来重塑这两个部分的形状?这和历史有什么关系吗?