2014-02-07 111 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(它是二維數組)應該變平坦,代碼中的所有向量(二維數組)也應該變平,這樣代碼就可以在最小變化的情況下工作。