如果有必要,我有Theta的起始值,可以从中开始搜索功能(0.01604 = Theta2,1.143 = Theta1,1.537 = Theta0)
我实现了Huber功能
def Huber(l,delta):
if m.fabs(l)<=delta:
return 1/2*(l)**2
else:
return delta*m.fabs(l)-1/2*delta**2
然后我尝试
def my_function(start, X, y):
Theta0, Theta1, Theta2 = start
u = Huber((y - (Theta0 + Theta1*X+Theta2*X*X)),7)
return u
optimized = minimize(my_function(start, X, y), start)
有
TypeError: cannot convert the series to <class 'float'>
如何使其正确?
答案 0 :(得分:3)
只需提供一个列表,其中包含您的起点:
Date a = new Date(); a.toString();
然后使用SciPy的start = [1.537, 1.143, 0.01604]
(docs):
optimize
在上面,from scipy.optimize import minimize
def my_function(args):
Theta0, Theta1, Theta2 = args
# here return the value of the function
optimized = minimize(my_function, start)
,Theta0
和Theta1
是变量。从问题尚不清楚它们是否为常数。如果是这种情况,只需将上面的代码用作模板,然后将起点解压缩到变量中即可,例如
Theta2
注意:您应该仅传递函数名称,而不是函数调用,即
from scipy.optimize import minimize
start = [1, 1]
def my_function(args):
X, y = args # Thetas constant
# here return the value of the function
optimized = minimize(my_function, start)