2012-05-08 64 views
3

我收到一個錯誤,我不太明白以下腳本。我想我應該能多兩個numpy的陣列happliy但我不斷收到此錯誤:錯誤乘以兩個numpy陣列

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

的腳本如下:

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 

打印Istack1 Istack2前乘出來的

['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'] 

我打電話使用

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

我在這裏錯過了什麼?

回答

6

轉換字符串數組,第一浮陣列:

Istack1 = np.array(Istack1, np.float) 
Istack2 = np.array(Istack2, np.float) 
2

在我看來,這些是ndarray s的字符串

>>> 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' 

你需要將它們轉換爲floats S或int■如果你想將它們相乘:

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

的一種方式做到這一點可能是這樣的。 (但是這取決於你傳遞給函數的東西。)

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

或者,使用列表理解:

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

或者,從HYRY偷(我忘了明顯的方法):

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

當然!非常感謝。列表理解方法拋出一個錯誤,但是浮點圖和HYRY的版本起作用。我傳遞一個從附加csv文件列生成的Tuple切片。我想我可以在將它傳遞給函數之前進行浮點轉換。 –