ValueError:操作数不能与形状(400,)(2,)一起广播

时间:2022-12-28 19:36:13

For some reason I'm getting a module object is not callable error on this code...and I don't really get why?

由于某种原因,我得到一个模块对象不是这个代码的可调用错误…我不明白为什么?

def func (t,a,x):
return
`plt.plot(t,(exp(a*t))*x(), label="lalala")` 

....and the Traceback

....和回溯

plt.plot(t,(exp(a*t))*x(), label="lalala") TypeError: 'numpy.ndarray' object is not callable

plt.plot(t(exp(* t))* x(),标签=“lalala”)TypeError:numpy。ndarray的对象是不可调用的。

after changing x() to x

把x()换成x。

x=np.array([1,100])
t=np.linspace(1,10,400)
def func (t,a,x):
    return plt.plot(t,(exp(a*t))*x, label="lalala")

Now I get:

现在我得到:

ValueError: operands could not be broadcast together with shapes (400,) (2,)

ValueError:操作数不能与形状(400,)(2,)一起广播

The goal is to sketch a graphic with the function: exp(a*t)x over the time t...=>y= exp(at)*x on the y-axis and t is the x-axis. a is the hole time constant, t(1,10,400) and x(1,100) changes

我们的目标是用函数绘制图形:exp(a*t)x除以时间t…= exp(at)*x在y轴上,t是x轴。a是孔时间常数,t(1, 10400)和x(1100)变化。

1 个解决方案

#1


0  

It's a little difficult to know for sure what you're after, but from your edits it seems you have two values of x you want to plot the graph of y = x.exp(a.t) for over the time range t=np.linspace(1,10,400). In which case:

要确切知道后面的内容有点困难,但是从编辑的角度来看,你似乎有两个x值,你想把y = x.exp(a.t)的图形绘制在时间范围t=np.linspace(1,10,400)上。在这种情况下:

import numpy as np
import matplotlib.pyplot as plt
a = 2
x = np.array([1,100])
t=np.linspace(1,10,400)
def func(t, a, z):
    return z * np.exp(a * t)

plt.plot(t,func(t, a, x[0]))
plt.plot(t,func(t, a, x[1]))

You're getting the error because x is an array of two values and NumPy doesn't (without some help) know how to "broadcast" these two values in relation to your time array. If you want func to produce an array of shape (2,400) in response to the input x, use np.newaxis to create the missing second axis in the x array:

你会得到错误,因为x是两个值的数组,而NumPy(没有一些帮助)知道如何“广播”这两个值与时间数组的关系。如果您想要func生成一个形状数组(2400),以响应输入x,使用np。在x数组中创建缺失的第二轴:

func(t, a, x[:,np.newaxis])

#1


0  

It's a little difficult to know for sure what you're after, but from your edits it seems you have two values of x you want to plot the graph of y = x.exp(a.t) for over the time range t=np.linspace(1,10,400). In which case:

要确切知道后面的内容有点困难,但是从编辑的角度来看,你似乎有两个x值,你想把y = x.exp(a.t)的图形绘制在时间范围t=np.linspace(1,10,400)上。在这种情况下:

import numpy as np
import matplotlib.pyplot as plt
a = 2
x = np.array([1,100])
t=np.linspace(1,10,400)
def func(t, a, z):
    return z * np.exp(a * t)

plt.plot(t,func(t, a, x[0]))
plt.plot(t,func(t, a, x[1]))

You're getting the error because x is an array of two values and NumPy doesn't (without some help) know how to "broadcast" these two values in relation to your time array. If you want func to produce an array of shape (2,400) in response to the input x, use np.newaxis to create the missing second axis in the x array:

你会得到错误,因为x是两个值的数组,而NumPy(没有一些帮助)知道如何“广播”这两个值与时间数组的关系。如果您想要func生成一个形状数组(2400),以响应输入x,使用np。在x数组中创建缺失的第二轴:

func(t, a, x[:,np.newaxis])