计算余弦值在Python数组 [英] Calculating cosine values for an array in Python
本文介绍了计算余弦值在Python数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的阵列名为 1242号的
。我需要得到的余弦值Python中所有的数字。
I have this array named a
of 1242 numbers. I need to get the cosine value for all the numbers in Python.
当我使用: cos_ra = math.cos(一)
我得到一个错误,说明:
When I use : cos_ra = math.cos(a)
I get an error stating:
类型错误:仅长度-1阵列可以转换到Python标量
TypeError: only length-1 arrays can be converted to Python scalars
我该如何解决这个问题?
How can I solve this problem??
在此先感谢
推荐答案
问题是你使用 numpy.math.cos
在这里,它希望你传递一个标量。如果你想 COS
适用于可迭代使用 numpy.cos
。
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 ])
错误:
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数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文