from math import sin, cos, pi
import numpy as np
N=10
a=np.random.randint(0, 360+1, N)
print (a)
theta=a*pi/180
print(theta)
x=[cos(theta)]
print(x)
y=[sin(theta)]
print(y)
TypeError Traceback (most recent call last)
<ipython-input-47-632b45c2aba1> in <module>()
9 theta=a*pi/180
10 print(theta)
---> 11 x=[cos(theta)]
12 print(x)
13 y=[sin(theta)]
TypeError: only length-1 arrays can be converted to Python scalars
答案 0 :(得分:2)
尝试使用np.cos(theta)
代替cos(theta)
。同样适用于sin
。
只有NumPy函数可以应用于标量和数组。常规cos()
和sin()
只需要标量参数,并且在示例中失败,因为您尝试将它们应用于大小为10的NumPy数组。
NumPy cos
文档:http://docs.scipy.org/doc/numpy/reference/generated/numpy.cos.html
cos
文档:https://docs.python.org/2/library/math.html