I have a script that I wrote on my machine using Python 3.2, because of the size of the data files involved I need to run it on another machine that runs version 2.5 . When I run the following bit of code that converts a python list to a numpy array I get the following error
Traceback (most recent call last):
File "count_code2.py", line 119, in <module>
cell_centers()
File "count_code2.py", line 58, in cell_centers
read_file(F)
File "count_code2.py", line 44, in read_file
Xa = numpy.array(X, dtype=float) #Converts lists to NumPy arrays
ValueError: setting an array element with a sequence.
the bit of code the error is referring to is
Xa = numpy.array(X, dtype=float) #Converts lists to NumPy arrays
Ya = numpy.array(Y, dtype=float)
Za = numpy.array(Z, dtype=float)
Where X, Y and Z are the following Python lists(test lists)
X= ['0', '0', '0', '1.5', '0', '0', '0', '0', '0', '0']
Y= ['0', '0', '0', '1', '0', '0', '0', '0', '0', '0']
Z= ['0', '0', '0', '1', '0', '0', '0', '0', '0', '0']
I have tried recasting the dtype as object as I have read in similar posts that this can fix the problem. It does stop the error from being thrown, but another function that sums the data in the arrays returns zeros on the python 2.x machine and gives this error on the 3.2 machine
File "C:\Documents and Settings\My Documents\Count_Code.py", line 103, in counter
((ymin <= Ya) & (Ya <= ymax))&
TypeError: unorderable types: float() <= str()
I am assuming that dtype object turns the list to an array of strings and that is why I get this error. I would like to ask if someone can explain why the differences in versions is causing the errors, and what I can do to work around it. Thanks in advance
X = [ float(p) for p in X ]
to convert list elements to numeric first. – yosukesabai May 3 '12 at 4:10numpy.[ float(p) for p in X ]
or just as it is? Thanks – Surfcast23 May 3 '12 at 4:16float
, which converts string to number, and pack them into new list, and names it X. – yosukesabai May 3 '12 at 4:19Xa = numpy.array(X, dtype=float)
works for me. The error "Setting array ELEMENT with a SEQUENCE" would seem to imply that X isn't what you think it is. – tom10 May 3 '12 at 4:19X
(andY
andZ
) are strings (rather than numbers). So the problem is when Numpy attempts to implicitly convert them to floats, which correctly doesn't work. This is not a problem with either Python or Numpy. – huon May 3 '12 at 4:51