[SciPy-user] Using SciPy/NumPy optimization

lechtlr lechtlr@yahoo....
Tue Mar 13 13:46:44 CDT 2007

Hi there:

I am looking for a non-linear, constrained optimization tool, and thought fmin_tnc would do the job that I wanted to do.
As a starter, I tried the attached script posted here recently. However, I get the following error when I run this script: 
"TypeError: only length-1 arrays can be converted to Python scalars" 
Can any one help me to figure out what I was doing wrong ?  



Traceback (most recent call last):
 File "C:\Python24\lib\site-packages\scipy\optimize\tnc.py", line 200, in fmin_tnc
    fmin, ftol, rescale)
  File "C:\Python24\lib\site-packages\scipy\optimize\tnc.py", line 165, in func_and_grad
    g = approx_fprime(x, func, epsilon, *args)
  File "C:\Python24\lib\site-packages\scipy\optimize\optimize.py", line 555, in approx_fprime
    grad[k] = (apply(f,(xk+ei,)+args) - f0)/epsilon
TypeError: only
 length-1 arrays can be converted to Python scalars

from numpy import *
from scipy.optimize import fmin_tnc

class LossFunction(object):
    def __init__(self, x, y):
      self.x = x
      self.y = y

    def __call__(self, abc):
      """ A function suitable for passing to the fmin() minimizers.
      a, b, c = abc
      y = a*(1.0 + b*c*self.x) ** (-1.0/b)
      dy = self.y - y
      return dy*dy

x = array([1502.0, 1513.7,1517.5,1545.5,1578.9,1587.3,1600.4,1636.1,1682.9,1697.6,1813.4,1907.5])
y = array([0.28,0.22,0.26,0.18,0.12,0.13,0.09,0.07,0.06,0.05,0.01,0.01])

lf = LossFunction(x, y)
abc0 = [10., 2.5, 0.0]  
retcode, nfeval, abc_optimal = fmin_tnc(lf, abc0,  approx_grad=True, bounds=None, epsilon=1e-008)
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070313/c76f2558/attachment.html 

More information about the SciPy-user mailing list