I have this array named a of 1242 numbers. I need to get the cosine value for all the numbers in Python.
When I use : cos_ra = math.cos(a) I get an error stating:
TypeError: only length-1 arrays can be converted to Python scalars
How can I solve this problem??
Thanks in advance
解决方案
Problem is you're using numpy.math.cos here, which expects you to pass a scalar. Use numpy.cos if you want to apply cos to an iterable.
In [30]: import numpy as np
In [31]: np.cos(np.array([1, 2, 3]))
Out[31]: array([ 0.54030231, -0.41614684, -0.9899925 ])
Error:
In [32]: np.math.cos(np.array([1, 2, 3]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 np.math.cos(np.array([1, 2, 3]))
TypeError: only length-1 arrays can be converted to Python scalars