Python - AttributeError: 'numpy.ndarray' 对象没有属性 'append'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42942781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - AttributeError: 'numpy.ndarray' object has no attribute 'append'
提问by Simplicity
This is related to my question, here.
这与我的问题有关,here。
I now have the updated code as follows:
我现在有更新的代码如下:
import numpy as np
import _pickle as cPickle
from PIL import Image
import sys,os
pixels = []
labels = []
traindata = []
i = 0
directory = 'C:\Users\abc\Desktop\Testing\images'
for root, dirs, files in os.walk(directory):
for file in files:
floc = file
im = Image.open(str(directory) + '\' + floc)
pix = np.array(im.getdata())
pixels.append(pix)
labels.append(1)
pixels = np.array(pixels)
labels = np.array(labels)
traindata.append(pixels)
traindata.append(labels)
traindata = np.array([traindata[i][i],traindata[1]], dtype=object)
i = i + 1
# do the same for validation and test data
# put all data and labels into 'data' array
cPickle.dump(traindata,open('data.pickle','wb'))
FILE = open("data.pickle", 'rb')
content = cPickle.load(FILE)
print (content)
When having only one image, the code runs fine. But, when I add another image or more, I get the following:
当只有一张图像时,代码运行良好。但是,当我添加另一个或更多图像时,我得到以下信息:
Traceback (most recent call last):
File "pickle_data.py", line 17, in <module>
pixels.append((pix))
AttributeError: 'numpy.ndarray' object has no attribute 'append'
How can I solve this issue?
我该如何解决这个问题?
Thanks.
谢谢。
采纳答案by hpaulj
for root, dirs, files in os.walk(directory):
for file in files:
floc = file
im = Image.open(str(directory) + '\' + floc)
pix = np.array(im.getdata())
pixels.append(pix)
labels.append(1) # append(i)???
So far ok. But you want to leave pixels
as a list until you are done with the iteration.
到目前为止还好。但是您希望pixels
在完成迭代之前将其保留为列表。
pixels = np.array(pixels)
labels = np.array(labels)
You had this indention right in your other question. What happened? previous
你在另一个问题中有这个缩进。发生了什么? 以前的
Iterating, collecting values in a list, and then at the end joining things into a bigger array is the right way. To make things clear I often prefer to use notation like:
迭代,收集列表中的值,然后最后将事物连接到更大的数组中是正确的方法。为了清楚起见,我通常更喜欢使用以下符号:
alist = []
for ..
alist.append(...)
arr = np.array(alist)
If names indicate something about the nature of the object I'm less likely to get errors like yours.
如果名称表明对象的性质,我不太可能遇到像您这样的错误。
I don't understand what you are trying to do with traindata
. I doubt if you need to build it during the loop. pixels
and labels
have the basic information.
我不明白你想要做什么traindata
。我怀疑您是否需要在循环期间构建它。 pixels
并labels
掌握基本信息。
That
那
traindata = np.array([traindata[i][i],traindata[1]], dtype=object)
comes from the previous question. I'm not sure you understand that answer.
来自上一个问题。我不确定你是否理解这个答案。
traindata = []
traindata.append(pixels)
traindata.append(labels)
if done outside the loop is just
如果在循环之外完成只是
traindata = [pixels, labels]
labels
is a 1d array, a bunch of 1s (or [0,1,2,3...] if my guess is right). pixels
is a higher dimension array. What is its shape?
labels
是一个一维数组,一堆 1(或者 [0,1,2,3...] 如果我猜对了)。 pixels
是一个更高维的数组。它的形状是什么?
Stop right there. There's no point in turning that list into an array. You can save the list with pickle
.
停在那儿。将该列表转换为数组是没有意义的。您可以使用 保存列表pickle
。
You are copying code from an earlier question, and getting the formatting wrong. cPickle very large amount of data
您正在复制先前问题中的代码,并且格式错误。 cPickle 非常大量的数据
回答by Robert Valencia
Numpy arrays do not have an append method. Use the Numpy append function instead:
Numpy 数组没有 append 方法。改用 Numpy append 函数:
import numpy as np
array_3 = np.append(array_1, array_2, axis=n)
# you can either specify an integer axis value n or remove the keyword argument completely
For example, if array_1 and array_2 have the following values:
例如,如果 array_1 和 array_2 具有以下值:
array_1 = np.array([1, 2])
array_2 = np.array([3, 4])
If you call np.append without specifying an axis value, like so:
如果您在不指定轴值的情况下调用 np.append ,如下所示:
array_3 = np.append(array_1, array_2)
array_3 will have the following value:
array_3 将具有以下值:
array([1, 2, 3, 4])
Else, if you call np.append with an axis value of 0, like so:
否则,如果您使用轴值为 0 调用 np.append,如下所示:
array_3 = np.append(array_1, array_2, axis=0)
array_3 will have the following value:
array_3 将具有以下值:
array([[1, 2],
[3, 4]])
More information on the append function here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
有关附加功能的更多信息,请访问:https: //docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
回答by Crispin
append
on an ndarray is ambiguous; to which axis do you want to append the data? Without knowing precisely what your data looks like, I can only provide an example using numpy.concatenate
that I hope will help:
append
在 ndarray 上是不明确的;您想将数据附加到哪个轴?在不确切知道您的数据是什么样子的情况下,我只能提供一个示例numpy.concatenate
,希望对您有所帮助:
import numpy as np
pixels = np.array([[3,3]])
pix = [4,4]
pixels = np.concatenate((pixels,[pix]),axis=0)
# [[3 3]
# [4 4]]
回答by Ashraful Islam
pixels = np.array(pixels)
in this line you reassign pixels
. So, it may not a list anyhow. Though pixels
is not a list it has no attributes append
. Does it make sense?
pixels = np.array(pixels)
在这一行中,您重新分配pixels
. 所以,无论如何它可能不是一个列表。虽然pixels
不是列表,但它没有属性append
。是否有意义?