0

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.

Community
  • 1
  • 1
prashantitis
  • 1,797
  • 3
  • 23
  • 52
  • 1. Check your indentation - Python is whitespace sensitive. There is a HUGE difference in whether your `data = np.array(data)` line is in the loop or outside it. 2. Does the answer to the question you linked not help? What do you mean "didn't get anything?" Did you change it to – thegrinner Jun 03 '13 at 14:30
  • 1
    the indentation of your sample code is obviously wrong. did you indent the `data.append(row)` as well as the `data = np.array(data)`? Judging from your error I think so. please don't do that b/c you would create an array from your former list; and array does really not have `.append` – Faultier Jun 03 '13 at 14:30
  • 1
    @namit Please be careful editing indentation in a Python code sample - in this case the error implies your edit was incorrect. – thegrinner Jun 03 '13 at 14:31
  • @grinner: actually i am new to python and didn't understand the answer given there – prashantitis Jun 03 '13 at 14:32
  • in this line data = np.array(data) i need to convert a list to array and googled it and found the same code which i had used but don't know why it is giving error – prashantitis Jun 03 '13 at 14:35
  • @Vishu The (relative) indentation level of a statement is extremely important - it's similar to how brackets work in other languages for defining scope. Check out [this tutorial](http://www.diveintopython.net/getting_to_know_python/indenting_code.html) for a clearer explanation. – thegrinner Jun 03 '13 at 14:40
  • @thegrinner: yes i got your point but my code is still not working that last line is outside for loop – prashantitis Jun 03 '13 at 14:44
  • Are you getting the same error? Can you update the question with the indentation you're using? – thegrinner Jun 03 '13 at 14:48
  • i have provided the link to the tutorial i am following i am new to python and don't know much about indentation please use the above tutorial link and help me :) and yes i am getting the same error – prashantitis Jun 03 '13 at 14:51

3 Answers3

1

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/

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
0

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.

Community
  • 1
  • 1
Serial
  • 7,925
  • 13
  • 52
  • 71
0

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.

thegrinner
  • 11,546
  • 5
  • 41
  • 64