Reputation: 1795
I am working with this tutorial. On the example
import csv as csv
import numpy as np
csv_file_object = csv.reader(open('train.csv', 'rb'))
header = csv_file_object.next()
data = []
for row in csv_file_object:
data.append(row)
data = np.array(data)
I encountered the following error:
Traceback (most recent call last):
File "C:/Users/Prashant/Desktop/data mining/demo.py", line 7,
in module data.append(row)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
I googled this and found this question/answer on append
, but I didn't get anything.
Upvotes: 0
Views: 5495
Reputation: 8043
Well, looking at the link to the other question you asked, it looks like numpy.ndarray
has no attribute called append
, but it looks like NumPy does.
So instead use:
numpy.append()
Or you can try to concatenate.
Take a look at Stack Overflow question Append a NumPy array to a NumPy array.
Upvotes: 0
Reputation: 32542
Have a look at the example at the linked location:
#The first thing to do is to import the relevant packages
# that I will need for my script,
#these include the Numpy (for maths and arrays)
#and csv for reading and writing csv files
#If i want to use something from this I need to call
#csv.[function] or np.[function] first
import csv as csv
import numpy as np
#Open up the csv file in to a Python object
csv_file_object = csv.reader(open('../csv/train.csv', 'rb'))
header = csv_file_object.next() #The next() command just skips the
#first line which is a header
data=[] #Create a variable called 'data'
for row in csv_file_object: #Run through each row in the csv file
data.append(row) #adding each row to the data variable
data = np.array(data) #Then convert from a list to an array
#Be aware that each item is currently
#a string in this format
Python is indentation-sensitive. That is, the indentation level will determine the body of the for loop, and according to the comment by thegrinner:
There is a HUGE difference in whether your data = np.array(data) line is in the loop or outside it.
That being said the following should demonstrate the difference:
>>> import numpy as np
>>> data = []
>>> for i in range(5):
... data.append(i)
...
>>> data = np.array(data) # re-assign data after the loop
>>> print data
array([0, 1, 2, 3, 4])
vs.
>>> data = []
>>> for i in range(5):
... data.append(i)
... data = np.array(data) # re-assign data within the loop
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'
As a side-note, I'd doubt the quality of the tutorial you are apparantly following is appropriate for bloody Python starters. I think this more basic (official) tutorial should be more appropriate for a quick first overview of the language: http://docs.python.org/2/tutorial/
Upvotes: 1
Reputation: 12241
Check your indentation. If data = np.array(data)
is in your for loop (ie indented the same amount as data.append(row)
), you'll turn data
into a Numpy array before you've finished appending items to a list.
This will cause the error you see because lists have an append()
method, while numpy arrays do not. Your for loop should look something like
data = [] # Make data a list
for row in csv_file_object: #iterate through rows in the csv and append them to the list
data.append(row)
# Turn the list into an array. Notice this is NOT indented! If it is, the data
# list will be overwritten!
data = np.array(data)
Check Dive Into Python for a more extensive explanation of how indentation works in Python.
Upvotes: 0