【发布时间】:2016-02-11 21:36:57
【问题描述】:
我想根据给定的数据集调整模型的参数。
我正在尝试在 Sage 中使用 scipy 的函数 curve_fit,但我不断得到
TypeError: only length-1 arrays can be converted to Python scalars
这是我的代码:
from numpy import cos,exp,pi
f = lambda x: exp( - 1 / cos(x) )
import numpy as np
def ang(time): return (time-12)*pi/12
def temp(x,maxtemp):
cte=(273+maxtemp)/f(0)**(1/4)
if 6<x and x<18:
return float(cte*f(ang(x))**(1/4)-273)
else:
return -273
lT=list(np.linspace(15,40,1+24*2))
lT=[float(num) for num in lT] #list of y data
ltimes=np.linspace(0,24,6*24+1)[1:]
ltimes=list(ltimes) #list of x data
u0=lT[0]
def u(time,maxtemp,k): #the function I want to fit to the data
def integ(t): return k*exp(k*t)*temp(t,maxtemp)
return exp(-k*time)*( numerical_integral(integ, 0, time)[0] + u0 )
import scipy.optimize as optimization
print optimization.curve_fit(u, ltimes, lT,[1000,0.0003])
【问题讨论】:
-
尝试将参数放在元组而不是列表中:
optimization.curve_fit(u, ltimes, lT,(1000,0.0003)) -
我没有答案,但我有一个建议可以提高您获得答案的机会:将代码的大小减少到实际相关的部分。 Here is my example:只有 4 行,重现相同的问题。比起 20 行代码,更多的人愿意考虑 4 行代码,其中大多数是无关紧要的。
-
numerical_integral是什么,它返回什么。 -
numerical_integral返回答案元组和错误估计值。