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

时间:2017-12-25 21:12:08

标签: python arrays list numpy reshape

我有以下Python脚本部分:

X_to_text_file = np.savetxt('x.txt', X.reshape(np.shape(X)), fmt='%5f')

我收到此错误:

AttributeError: 'list' object has no attribute 'reshape'

前提是XNumpy数组,我按如下方式获取:

for img in range(len(names)):
    for name in names:
        img = np.array(Image.open(name))
        X.append(img)

为什么我会收到此错误以及如何解决此问题?

感谢。

1 个答案:

答案 0 :(得分:0)

我在这里看到两个问题。首先,您的代码段不完整,因为没有X的定义。没有它,在最小的示例中,您会看到如下内容:

>>> import numpy as np
>>> X.append(np.array())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'X' is not defined

因此,您必须先在某处创建X,并根据您的错误消息判断,它是一个简单的Python列表。

现在,关于如何将numpy数组连接在一起,我认为this answer会帮助你。