具有Scipy的数据框架最小化功能
我试图最大程度地减少与数据框架一起使用的Sum Square函数。 DF如下:
ds = pd.DataFrame({'t': [*np.linspace(0,300,7)], 'Ca': [0.05, 0.038, 0.0306, 0.0256, 0.0222, 0.0195, 0.0174]})
我使用Sum Square的我的模型是:
def model(params, t, ca0=0.05):
alpha = params[0]
k = params[1]
ca_pred = (ca0**(1-alpha) - (1-alpha)*k*t)**(1/(1-alpha))
return ca_pred
def sum_of_squares(params, t, ca, ca0=0.05):
ca_pred = model(params, t, ca0)
obj = ((ca - ca_pred)**2).sum()
return obj
Initial guess:
params = [1.5, 0.05]
我的具体疑问是在这里,我不知道如何通过sum_of_of_squares函数中的“ t”和“ ca”以最小化:
res = minimize(fun=sum_of_squares, x0=params, tol=1e-3, method="Powell")
Im trying to minimize sum square function that works with a dataframe. The df is as follows:
ds = pd.DataFrame({'t': [*np.linspace(0,300,7)], 'Ca': [0.05, 0.038, 0.0306, 0.0256, 0.0222, 0.0195, 0.0174]})
My model that Im using with sum square is:
def model(params, t, ca0=0.05):
alpha = params[0]
k = params[1]
ca_pred = (ca0**(1-alpha) - (1-alpha)*k*t)**(1/(1-alpha))
return ca_pred
def sum_of_squares(params, t, ca, ca0=0.05):
ca_pred = model(params, t, ca0)
obj = ((ca - ca_pred)**2).sum()
return obj
Initial guess:
params = [1.5, 0.05]
My specific doubt is here, I dont know how to pass dataframe to use "t" and "ca" in sum_of_squares function in minimize:
res = minimize(fun=sum_of_squares, x0=params, tol=1e-3, method="Powell")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
args
参数:或包装函数:
You can either use the
args
argument:or wrap the function: