# [SciPy-User] fmin_l_bfgs_b pass ndarray with an array

Warren Weckesser warren.weckesser@enthought....
Sun Jul 22 11:18:14 CDT 2012

```On Sun, Jul 22, 2012 at 11:01 AM, ilan barak <ibarak2000@yahoo.com> wrote:

>  Hello,
> I apologize for the long description, this is the best I can do...
>
> I have two functions defined as:
>
> def hypothesis(params):
>     # build a series of length params[0], with starting gap params[1] ,
> repitition params[2] with sclae params[3]
>     # using tt as the basic shape
>     result=np.zeros(params[0])
>     for index in np.arange(params[1],params[0]-tt.shape[0],params[2]):
>         result[int(round(index)):int(round(index))+tt.shape[0]]=tt
>     return params[3]*result
>
> def Cost(params,sig): #  starting gap params[0] , repitition params[1]
> with sclae params[2]
>     # sig is signa;s to compare to
>     # calculate error cost
>     result=np.linalg.norm(sig-hypothesis([sig.shape[0]]+params))
>     return result
>
> The Cost function requires a 3 parameter list and a signal that is a 800
> long ndarray
> Running the Cost with:
> params=([start,gap,0.04])
> Cost(params,mysig5) , where mysig 5 is length 800 ndarray works fine.
> However:
> p0 = np.array([10.,20.,0.01]) # Initial guess for the parameters
> start,gap,scale
> mybounds = [(0,20), (10,25), (0.001,0.1)]
> x, f, d= optimize.fmin_l_bfgs_b(Cost, p0[:],fprime=None, args=mysig5,
> bounds=mybounds, approx_grad=True)
>
> complains:
>
> Traceback
>     <module>    C:\Users\ilan\Documents\python\opt_detection_danny1.py
> 130
>     fmin_l_bfgs_b
> C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py    199
>     func_and_grad
> C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py    145
> TypeError: Cost() takes exactly 2 arguments (801 given)
>
> Where am I wrong
>
>

Use a *tuple* containing the single argument mysig5 for the `args` argument:

x, f, d = optimize.fmin_l_bfgs_b(Cost, p0[:],fprime=None, args=(mysig5,),
bounds=mybounds, approx_grad=True)

Warren

thanks
>
> Ilan
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120722/b5c5330d/attachment-0001.html
```

More information about the SciPy-User mailing list