scipy.optimize.fmin_bfgs优化给出了简单函数调用的不同结果

时间:2014-01-10 22:50:50

标签: python numpy pandas scipy mathematical-optimization

我正在探索一些numpy / scipy函数,我注意到与直接函数调用相比,scipy.optimize.fmin_bfgs要求对正在调用的函数进行更改以提供正确的结果。我对fnRSS函数的第一个定义在调用函数时会返回正确的值,但拒绝在优化中工作;我的第二个定义在调用函数时给出了错误的结果,但在运行优化时给出了正确的结果。有人可以告诉我,为优化转置vY参数至关重要吗?它应该已经是164x1。

import numpy as np
import scipy as sp
import pandas as pd
from scipy import optimize

if __name__ == "__main__":
    urlSheatherData = "http://www.stat.tamu.edu/~sheather/book/docs/datasets/MichelinNY.csv"
    data = pd.read_csv(urlSheatherData)
    Xs = np.vstack(data[['Service','Decor', 'Food', 'Price']].values)
    Xs = np.concatenate((np.vstack(np.ones(Xs.shape[0])),Xs), axis=1)
    Ys = np.vstack(data[['InMichelin']].values)
    # optimal solution (given)
    vBeta = np.array([-1.49209249,  -0.01117662,  0.044193, 0.05773374, 0.00179794]).reshape(5,1)

    print Ys.shape, Xs.shape, vBeta.shape 
    # first definition of function
    def fnRSS(vBeta, vY, mX):
      return np.sum((vY - np.dot(mX, vBeta))**2) 
    print fnRSS(vBeta, Ys, Xs) # correct value
    print np.linalg.lstsq(Xs, Ys)[1] # confirm correct value
    print sp.optimize.fmin_bfgs(fnRSS, x0=vBeta, args=(Ys,Xs)) # wrong value
    # second definition
    def fnRSS(vBeta, vY, mX):
      return np.sum((vY.T - np.dot(mX, vBeta))**2) 
    print fnRSS(vBeta, Ys, Xs) # incorrect value
    print sp.optimize.fmin_bfgs(fnRSS, x0=vBeta, args=(Ys,Xs)) # correct convergence but simple call gives different value

我的输出:

(164, 1) (164, 5) (5, 1)
26.3239061505
[ 26.32390615]
Warning: Desired error not necessarily achieved due to precision loss.
         Current function value: 6660.000000
         Iterations: 39
         Function evaluations: 3558
         Gradient evaluations: 480
[  4.51220111e-01   1.32711255e-07   8.09143368e-08  -1.06633003e-07
  -5.18448332e-08]
9002.87916028
Warning: Desired error not necessarily achieved due to precision loss.
         Current function value: 26.323906
         Iterations: 29
         Function evaluations: 1954
         Gradient evaluations: 260
[-1.49209095 -0.0111764   0.04419313  0.05773347  0.00179789]

1 个答案:

答案 0 :(得分:0)

它不是关于vY.T,而是vBeta,即xfmin_bfgs传递给fnRSS,而不是2d矢量,而是1d阵列。因此,尽管您试图将x0=vBeta指定为形状数组(5,1),但它会在内部转换为1d数组形状(5,),最后会返回。< / p>