2014-02-07 138 views
-1

我有一个函数,我想最小化。它是普通最小二乘的矢量化版本。使用数组作为参数最小化函数

import numpy as np 
from scipy import optimize 

def lr_cost_function(theta, x, y, derivative = False, hypotesis=linear_hypotesis, polynom = 1): 
    hyp = hypotesis(theta, x, polynom) 
    print("Hyp: ", hyp.shape) 
    dif = hyp - y 
    print("Dif:", dif.shape) 
    reuslt = dot(dif.T,dif) 
    print("RES", reuslt.shape) 
    return 1/len(y)*(dot(dif.T,dif)[0,0]) 

def linear_hypotesis(theta, x, polynom = 1): 
    print(x.shape, theta.shape, type(theta)) 
    return np.dot(x, theta) 

所以我打电话尽量减少这样的:

optimize.minimize(fun=lr_cost_function, x0=theta_copy, args=(x, y)) 

和我的代码无法完成,因为在optimize.py参数X0压扁我向量化被彻底打破(线822 0.13.2 scipy版本)。我甚至无法完成代码并查看结果,导致我没有足够的内存,并且一切都按照计算差异出现问题。

回答

0

我对数组的维度有点困惑,所以得到了这个错误。我的theta(它是二维数组)应该变平坦,代码中的所有向量(二维数组)也应该变平,这样代码就可以在最小变化的情况下工作。