Sum operation in numpy arrays

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 2 06:33:57 EDT 2013


On Thu, 02 May 2013 02:10:11 -0700, Ana Dionísio wrote:

> Hello! I have several numpy arrays in my script and i want to add them.
> For example:
> 
> a=[1,2,3,4,5]
> b=[1,1,1,1,1]
> c=[1,0,1,0,1]

These are not numpy arrays, they are lists of ints. Based on the error 
message you quote:

TypeError: unsupported operand type(s) for +: 'float' and 'numpy.string_'"

you have at least one numpy array of floats, and one numpy array of 
strings. Identify which array has got strings in it instead of floats, 
then identify how it got filled with strings, and change it to fill it 
with floats instead.

Start by printing this:

print(type(a), type(b), type(c))
print(type(a[0]), type(b[0]), type(c[0]))

and see what they say. 


My guess is that somewhere in your code you have a list of strings:

l = ['1', '2', '3']

(perhaps because you read them from a file), and then you converted it to 
a numpy array:

x = numpy.array(l)


without converting the strings to floats.


-- 
Steven



More information about the Python-list mailing list