“只有大小为 1 的数组可以转换为 Python 标量"当我使用 sin 函数时 [英] "only size-1 arrays can be converted to Python scalars" when I use the sin function
问题描述
如果此错误的解决方案微不足道,我很抱歉,但我已经进行了广泛的研究,但没有运气.互联网上的每个答案都说当您尝试将数组用作标量时会出现此错误,但我无法理解如何在此处应用此信息.代码如下:
将 numpy 导入为 npfrom math import e, sin从 scipy 导入杂项导入 matplotlib.pyplot 作为 plt定义 f(x):返回 x-e**-x一 = 0乙 = 1通过 = 0.1x = np.arange(a, b+pas, pas)plt.plot(x, f(x), 'r-')plt.show()
它没有问题,直到我尝试以某种方式使用 sin 函数:
def f(x):返回 x-e**-x+sin(x)
在这种情况下,我收到以下错误:
<块引用>回溯(最近一次调用最后一次):文件a.py",第 14 行,在
我不明白为什么它认为它不是标量.我试图计算 f(0), f(0.5), f(1) 并且输出总是一个标量.这里发生了什么事?sin函数有什么特别之处?
定义 f(x):返回 x-e**-x
<块引用>
而且它没有问题
这是因为:对于 numpy
,**
是一个 一元 ufunc 对应于 np.power
.所以 numpy
可以用它执行向量化操作,即你可以将向量传递给函数.
直到我尝试以某种方式使用 sin 函数:
然而,在这里,您使用的是 math.sin
,它是 unknown 到 numpy
- 它是一个常规"代码.对它起作用,并且那里不会发生矢量化*.由于 math.sin
是期望一个 Python 标量,你会得到错误.
要解决这些问题,我建议您使用 numpy
的正弦函数,即 np.sin
(以及 np.exp
代替 math.e
).它将是已知的"到 numpy 并将立即进行矢量化.
我试图计算 f(0), f(0.5), f(1) 并且输出总是一个标量
你正在传递 Python 标量,所以每个人都很高兴 :)
<小时>*:有 np.vectorize
并且您可以用它包装一个常规函数(例如 math.sin
此处)以对其进行矢量化,但它本质上是一个 for 循环,因此当存在已经矢量化的版本,即 np.sin
时,它根本不是可取的.
I'm sorry if the solution to this error is trivial, but I've extensively reserchead with no luck. Every answer on the internet says that this error appears when you try to use an array as a scalar, but I can't understand how to apply this information here. Here is the code:
import numpy as np
from math import e, sin
from scipy import misc
import matplotlib.pyplot as plt
def f(x):
return x-e**-x
a = 0
b = 1
pas = 0.1
x = np.arange(a, b+pas, pas)
plt.plot(x, f(x), 'r-')
plt.show()
And it works with no problem, until I try to use the sin function in some way:
def f(x):
return x-e**-x+sin(x)
In this case, I get the following error:
Traceback (most recent call last): File "a.py", line 14, in <module> plt.plot(x, f(x), 'r-') File "a.py", line 7, in f return 10x-9(e**-x)+int(sin(x)) TypeError: only size-1 arrays can be converted to Python scalars
I can't understand why it thinks it's not a scalar. I tried to calculate f(0), f(0.5), f(1) and the output is always a scalar. What's happening here? What's special about the sin function?
def f(x): return x-e**-x
And it works with no problem
This is because: to numpy
, **
is a unary ufunc that corresponds to np.power
. So numpy
can execute vectorize operations with it i.e. you can pass a vector to the function.
until I try to use the sin function in some way:
Here, however, you are using math.sin
which is unknown to numpy
- it's a "regular" function to it and no vectorization can happen there*. Since math.sin
is expecting a single Python scalar, you get the error.
To fix these, I'd suggest you use numpy
's sine function i.e. np.sin
(and also np.exp
in lieu of math.e
). It will be "known" to numpy and will be vectorized right away.
I tried to calculate f(0), f(0.5), f(1) and the output is always a scalar
There you are passing Python scalars so everyone is happy :)
*: there is np.vectorize
and you can wrap a regular function (e.g. math.sin
here) with it to vectorize it but it's essentially a for loop so it is not at all preferable when an already-vectorized version exists i.e. np.sin
.
这篇关于“只有大小为 1 的数组可以转换为 Python 标量"当我使用 sin 函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!