【问题标题】:ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) [duplicate]ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) [重复]
【发布时间】:2021-06-30 12:22:56
【问题描述】:

我一直在按照此 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()

【问题讨论】:

  • 也添加 python 代码。您提供输入值的部分。
  • 你可能需要 (1,6)(1,8) 代替,所以尝试用 numpy 重塑它们
  • 为什么是n=6?这和history.history有什么关系?

标签: python numpy matplotlib google-colaboratory


【解决方案1】:

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

实际上,matplotlib.pyplot.plot(x, y)x 值是可选的,默认为 range(len(y))。你只需要

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

如果你想让x以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')

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多