【问题标题】:AttributeError: 'str' object has no attribute 'reshape'AttributeError:“str”对象没有属性“reshape”
【发布时间】:2021-07-15 15:34:08
【问题描述】:

https://gist.github.com/akashp1712/446ffaa5d87e0110404c75f0b0cff157#filemnist_augmentation_random_forest-py

当我运行这段代码时,我得到了这个错误:

AttributeError: 'str' 对象没有属性 'reshape'

进程以退出代码 1 结束

def shift_image(image, dx, dy):
  image = image.reshape((28, 28))

【问题讨论】:

  • 好的,您对这个错误信息有什么问题?您阅读错误信息了吗?你明白了吗?
  • 它模棱两可。上面代码 URL 中的第 18/19 行
  • 我不明白。你觉得它有什么模棱两可的地方?它说''str'对象没有属性'reshape'. Is there more than one 'str'对象'你认为它可能在谈论吗?不止一个attribute 'reshape'have no 此类属性的方法不止一种?
  • 这条线不明确。输入来自 MNIST 数据集。当您调试并悬停时,它会显示图像。当这段代码命中它时,是什么将它转换为 STR?

标签: python mnist


【解决方案1】:

请您在使用它的地方添加一个代码块。

另外,我相信你在调用这个函数时传递了错误的参数,我错误地认为你传递了一个字符串作为参数

【讨论】:

  • 在代码 URL 的第 18/19 行调用调整图像大小
【解决方案2】:

试试:

for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
         for image, label in zip(X_train.values, y_train):
                 X_train_augmented.append(shift_image(image, dx, dy))
                 y_train_augmented.append(label)

代替:

for dx, dy in ((1,0), (-1,0), (0,1), (0,-1)):
     for image, label in zip(X_train, y_train):
             X_train_augmented.append(shift_image(image, dx, dy))
             y_train_augmented.append(label)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    • 2014-03-04
    • 2013-09-22
    相关资源
    最近更新 更多