python - 在 Python 中计算数组的余弦值

标签 python arrays math numpy trigonometry

我有一个名为 a 的数组,包含 1242 个数字。我需要获取 Python 中所有数字的余弦值。

当我使用:cos_ra = math.cos(a) 我收到一条错误消息:

TypeError: only length-1 arrays can be converted to Python scalars

我该如何解决这个问题??

提前致谢

最佳答案

问题是您在这里使用 numpy.math.cos,它希望您传递一个标量。如果您想将 cos 应用于可迭代对象,请使用 numpy.cos

In [30]: import numpy as np

In [31]: np.cos(np.array([1, 2, 3]))                                                             
Out[31]: array([ 0.54030231, -0.41614684, -0.9899925 ])

错误:

In [32]: np.math.cos(np.array([1, 2, 3]))                                                        
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-8ce0f3c0df04> in <module>()
----> 1 np.math.cos(np.array([1, 2, 3]))

TypeError: only length-1 arrays can be converted to Python scalars

关于python - 在 Python 中计算数组的余弦值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043644/

相关文章:

arrays - 基于 NSData 子集的 Swift NSData 字符串转换

使用 LaTeX 重构文本中的数学

python - 重复的措施让 Pandas 发生了转变

php - 如何使用 implode 查询数据库?

python - 创建成员(member) ID 的下拉菜单

javascript - ASP.NET MVC - Javascript 数组总是作为 null 传递给 Controller

math - RGB 和 RYB 颜色空间之间的转换

iphone - 如何在没有陀螺仪的设备上创建 CMRotationMatrix

python - 禁用 Django 中所有表单字段的语法

Python3.3 : ImportError with textmining-1. 0