1
我想調整模型的參數給一組給定的數據。「只能將長度爲1的數組轉換爲Python標量」在Sage中使用scipy.optimize
我試圖用scipy
的功能curve_fit
在聖人,但我不斷收到
TypeError: only length-1 arrays can be converted to Python scalars
Here's我的代碼:
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))' – hpaulj
我沒有答案,但我有一個建議,可以提高你獲得一個機會n答案:將代碼的大小減小到實際相關的部分。 [這是我的例子](https://gist.github.com/normalhuman/2b3778eb4660a7cfa258):它只是4行,重現相同的問題。更多的人願意考慮4行代碼,而不是20行,其中大多數是不相關的。 – 2016-02-12 04:46:05
什麼是'numerical_integral',它返回什麼。 –