[SciPy-user] Using SciPy/NumPy optimization

Robert Kern robert.kern@gmail....
Wed Feb 28 11:43:58 CST 2007


Brandon Nuttall wrote:
> Folks,
> 
> I've gotten further, but still am not there. I have the following code:
> 
> import numpy as np
> from scipy.optimize import fmin_tnc
> 
> class hyp_func:
>      def __init__(self,*args):
>          self.x, self.y = args
>          self.avg = y.mean()
>      def rmsd(self,*args):
>          """A function suitable for passing to the fmin() minimizers
>          """
>          a, b, c = args[0]
>          sse = 0.0
>          sst = 0.0
>          # minimize the RMSD
>          for i in range(len(x)):
>              y = a*(1.0+b*c*x[i])**(-1.0/b)
>              sse += (self.y[i]-y)**2
>              sst += (self.avg-y)**2

Don't do this looping. Instead, just make sure that self.x and self.y are arrays
and use array math.

y = a*(1.0 + b*c*self.x) ** (-1.0/b)
dy = self.y - y
sse = (dy*dy).sum()
sst = ((y - avg)**2).sum()

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list