2014-07-23 66 views
0

我希望得到一組陣列切線逆

import numpy as np 
import math 

例如(這是一個數組)

x_value=[1 2 3 4 5 6] 
a= abs(x_value-125) 

這仍然工作正常,但是當我的切線逆得到的正切逆:

b=math.atan(a) 

我得到這個錯誤:類型錯誤:只有長度爲1的陣列可以被轉換到Python標量

我該如何解決這個錯誤,我可以得到數組a的正切反函數?

+0

你確定你的例子有效嗎?首先,它不解析。其次,你不能將列表傳遞給abs(從std) –

+0

爲什麼不使用'np.arctan'? – mgilson

回答

2

只需使用np.arctan

>>> import numpy as np 
>>> a = np.array([1,2,3,4,5,6]) 
>>> a = abs(a - 125) # could use np.abs. It does the same thing, but might be more clear that you expect to get an ndarray instance as a result. 
>>> a 
array([124, 123, 122, 121, 120, 119]) 
>>> np.arctan(a) 
array([ 1.56273199, 1.56266642, 1.56259979, 1.56253205, 1.56246319, 
     1.56239316]) 
1

您可以使用列表中理解到atan功能應用到陣列中的每個元素:

a = np.abs(np.array([1,2,3,4,5,6]) - 125) 
b = [np.math.atan(x) for x in a] 
0

您可以使用列表理解:

b = [math.atan(ele) for ele in a]