我想获得每个值的余弦结果。
import numpy as np
import math
t = np.arange(0, 20.5, 0.5)
print(math.cos(t))
我得到' TypeError:只有length-1数组可以转换为Python标量'错误。
答案 0 :(得分:2)
您希望将操作应用于数组的每个元素,而不是整个数组。
>>> import numpy as np
>>> import math
>>> t = np.arange(0, 20.5, 0.5)
>>> print([math.cos(element) for element in t])
[1.0, 0.8775825618903728, 0.5403023058681398, 0.0707372016677029, -0.4161468365471424, -0.8011436155469337, -0.9899924966004454, -0.9364566872907963, -0.6536436208636119, -0.2107957994307797, 0.28366218546322625, 0.70866977429126, 0.960170286650366, 0.9765876257280235, 0.7539022543433046, 0.3466353178350258, -0.14550003380861354, -0.6020119026848236, -0.9111302618846769, -0.9971721561963784, -0.8390715290764524, -0.4755369279959925, 0.004425697988050785, 0.4833047587530059, 0.8438539587324921, 0.9977982791785807, 0.9074467814501962, 0.594920663309892, 0.1367372182078336, -0.354924266788705, -0.7596879128588213, -0.9784534628188842, -0.9576594803233847, -0.7023970575027135, -0.27516333805159693, 0.2194399632114593, 0.6603167082440802, 0.939524893748256, 0.9887046181866692, 0.7958149698139441, 0.40808206181339196]
然而,更好的解决方案是使用np.cos
,它也接受一个数组:
>>> np.cos(t)
array([ 1. , 0.87758256, 0.54030231, 0.0707372 , -0.41614684,
-0.80114362, -0.9899925 , -0.93645669, -0.65364362, -0.2107958 ,
0.28366219, 0.70866977, 0.96017029, 0.97658763, 0.75390225,
0.34663532, -0.14550003, -0.6020119 , -0.91113026, -0.99717216,
-0.83907153, -0.47553693, 0.0044257 , 0.48330476, 0.84385396,
0.99779828, 0.90744678, 0.59492066, 0.13673722, -0.35492427,
-0.75968791, -0.97845346, -0.95765948, -0.70239706, -0.27516334,
0.21943996, 0.66031671, 0.93952489, 0.98870462, 0.79581497,
0.40808206])
第二种解决方案在速度方面具有巨大优势:
In [9]: %timeit [math.cos(element) for element in t]
The slowest run took 5.19 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.54 µs per loop
In [10]: %timeit np.cos(t)
The slowest run took 21.73 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.1 µs per loop
答案 1 :(得分:0)
你可以使用允许数组的东西(更快!),或使用列表理解。
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/redmond/jquery-ui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.5/css/ui.jqgrid.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.5/js/jquery.jqgrid.min.js"></script>
</head>
<body>
<div id="outerDiv" style="margin:5px;">
<table id="grid"></table>
</div>
</body>
</html>
: 1000000次循环,最佳3:每循环1.81μs np.cos(t)
: 100000次循环,最佳3:每循环11.2μs 答案 2 :(得分:0)
您可以使用np.cos
:
t = np.arange(0, 20.5, 0.5)
print np.cos(t) # which accepts np.arrays
或者您可以使用math.cos
:
t = np.arange(0, 20.5, 0.5)
print([math.cos(element) for element in t])