数组的正切逆

2024-05-01 21:48:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我要求一组数组的正切逆

import numpy as np
import math

例如(这是一个数组)

^{pr2}$

这仍然很好,但是当我得到a的正切逆时:

b=math.atan(a)

我得到了这个错误:TypeError:只有length-1数组可以转换成Python标量

我应该如何解决这个错误,我可以得到数组a的元素的正切逆?在


Tags: importnumpy元素as错误npmath数组
3条回答

只需使用^{}

>>> 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])

您可以使用列表理解:

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

可以使用列表理解将atan函数应用于数组的每个元素:

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

相关问题 更多 >