3

I am getting an error that I don't quite understand with the following script. I thought I would be able to multiple the two numpy arrays happliy but I keep getting this error:

TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'

The script is below:

def currents_to_resistance(Istack1, Istack2, A_list, x_list):

    #Error Calcs
    Videal_R1 = (1.380648e-23 * (27+273.15)) / (1.6021766e-19)
    print Videal_R1
    Istack1 = np.array(Istack1)
    Istack2 = np.array(Istack2)
    print Istack1
    print Istack2   

    g = Istack1*Istack2
    print g 

The print Istack1 Istack2 before the multiply come out as

['0.0005789047' '0.0005743839' '0.0005699334' '0.000565551' '0.0005612346'
 '0.0005569839' '0.0005527969' '0.0005486719' '0.000544608' '0.0005406044'
 '0.0005366572' '0.000532768' '0.000528934' '0.0005251549' '0.0005214295'
 '0.0005177562' '0.0005141338' '0.0005105614' '0.000507039' '0.0005035643'
 '0.0005001368' '0.0004967555' '0.0004934193' '0.0004901279' '0.0004868796'
 '0.0004836736']
['0.000608027' '0.0006080265' '0.0006080267' '0.0006080267' '0.0006080261'
 '0.0006080261' '0.0006080262' '0.0006080261' '0.0006080263' '0.0006080272'
 '0.0006080262' '0.0006080262' '0.0006080257' '0.0006080256' '0.0006080258'
 '0.0006080256' '0.0006080252' '0.0006080247' '0.000608025' '0.0006080249'
 '0.000608025' '0.0006080251' '0.0006080249' '0.0006080254' '0.0006080251'
 '0.0006080247']

I call the function using

Re_list = currents_to_resistance(full_list[i][0],full_list[i][1], temp_A, temp_x)

What am I missing here?

2 Answers 2

6

convert string array to float array first:

Istack1 = np.array(Istack1, np.float)
Istack2 = np.array(Istack2, np.float)
Sign up to request clarification or add additional context in comments.

Comments

2

It looks to me like those are ndarrays of strings.

>>> numpy.array(['1', '2', '3']) * numpy.array(['1', '2', '3'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'

You need to convert them to floatss or ints if you want to multiply them:

>>> numpy.array([1, 2, 3]) * numpy.array([1, 2, 3])
array([1, 4, 9])

One way to do that might be something like this. (But it depends on what you're passing to the function.)

Istack1 = np.array(map(float, Istack1))

Or, using a list comprehension:

Istack1 = np.array([float(i) for i in Istack1])

Or, stealing from HYRY (I forgot about the obvious approach):

Istack1 = np.array(Istack1, dtype='f8')

1 Comment

Of course! Thanks alot. The list comprehension method throws an error, but the map to float and HYRY's version works. I am passing a slice of a Tuple generated from appending csv file columns. I guess I could just do the float conversion before I pass it to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.