Python 类型错误:ufunc 'multiply' 不包含签名匹配类型 dtype('S32') dtype('S32') dtype('S32') 的循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42013903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
提问by A. Robinson
I am new to coding but I am trying to create a really simple program that will basically plot a line. The user will input values for v and a then v and a and x will determine y. I attempted to do this with this:
我是编码新手,但我正在尝试创建一个非常简单的程序,该程序基本上可以绘制一条线。用户将输入 v 和 a 的值,然后 v 和 a 和 x 将确定 y。我试图这样做:
x = np.linspace(0., 9., 10)
a = raw_input('Acceleration =')
v = raw_input('Velocity = ')
y=v*x-0.5*a*x**2.
basically this will represent a parabola where v is velocity, a is acceleration and x is time. But, I keep getting this error:
基本上这将代表一个抛物线,其中 v 是速度,a 是加速度,x 是时间。但是,我不断收到此错误:
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32'
) dtype('S32') dtype('S32')
采纳答案by ImportanceOfBeingErnest
From the documentation of raw_input
:
The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
然后该函数从输入中读取一行,将其转换为字符串(去除尾随换行符),然后返回该字符串。
So what happens is that you try to multiply a string with a float, something like y="3" * x - 0.5 * "3" *x**2
, which is not defined.
因此,您尝试将字符串与y="3" * x - 0.5 * "3" *x**2
未定义的浮点数相乘,例如。
The easiest way to circumvent this is to cast the input string to float first.
避免这种情况的最简单方法是首先将输入字符串转换为浮点数。
x = np.linspace(0., 9., 10)
a = float(raw_input('Acceleration ='))
v = float(raw_input('Velocity = '))
y=v*x-0.5*a*x**2
Mind that if you're using python 3, you'd need to use input
instead of raw_input
,
请注意,如果您使用的是 python 3,则需要使用input
代替raw_input
,
a = float(input('Acceleration ='))
回答by logicb0mb
I faced this problem recently, change the dtype of x to something specific by doing:
我最近遇到了这个问题,通过执行以下操作将 x 的 dtype 更改为特定的内容:
x = np.asarray(x, dtype='float64')