简体   繁体   中英

Tangent Inverse of an array

I want to get the tangent inverse of a set of array

import numpy as np
import math

For example (this is an array)

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

This still works fine, but when I get the tangent inverse of a:

b=math.atan(a)

I got this error: TypeError: only length-1 arrays can be converted to Python scalars

How should I solve this error where I can get the tangent inverse of the elements of array a?

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

you could use a list comprehension to apply the atan function to each element of the array:

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

您可以使用列表理解:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM