[SciPy-user] Optimization Working only with a Specific Expression of the input Parameters

Lorenzo Isella lorenzo.isella at gmail.com
Sat Mar 3 15:23:16 EST 2007


Can you post your code, including the data?

Christian



Hi Cristian,
Actually I already posted this on the mailing list, but nevertheless I 
will give it another try.
Now, this is the code I fixed after posting on the mailing list:



#! /usr/bin/env python
from scipy import *
import pylab  # used to read the .csv file

# now I want to try reading some .csv file
data = pylab.load("120km-TPM.csv",delimiter=',')

vecdim=shape(data) # now I introduce a vector with the dimensions of 
data, the file I read
print "the dimensions of data are"
print vecdim  # now very careful! in Python the arrays start with index 
zero.
x=data[0:vecdim[0],0] # it means: slice the rows from the 1st one (0) to 
the last one (
# (vecdim[0]) for the first column (0)

#plot(x,data[:,1])
#show() # uncomment them to plot a distribution

# 1st problem: if uncomment the previous two lines, I get a warning and 
until I close
# the window, the script does not progress.


y_meas=data[0:vecdim[0],1]  # measured data, for example the 2nd column 
of the .csv file

# Here I define my own error function i.e. the function I want to minimize

def residuals(p, y, x):
    A1,mu1,myvar1 = p
    err = abs( 
y-log(10.0)*A1/sqrt(2.0*pi)/log(abs(myvar1))*exp(-((log(x/abs(mu1)))**2.0)/2.0/log(abs(myvar1))/log(abs(myvar1))))
    return err

#def peval(x, p):
#    return  
log(10.0)*p[0]/sqrt(2.0*pi)/log(abs(p[2]))*exp(-((log(x/abs(p[1])))**2.0)/2.0/log(abs(p[2]))/log(abs(p[2])))

# NB: I am using the mean as a mu1**2. and the std as myvar1**2.
# otherwise I run into problems (probably the optimizers tries some 
negative values of the
# mean and stops as it sees a complex number and it stops).


print "the initial guesses are"
p0 = [50000.0,90.0, 1.59]
print array(p0)

# now I try performing a least-square fitting
from scipy.optimize import leastsq

# now I try actually solving the problem
#print "ok up to here"
plsq = leastsq(residuals, p0, args=(y_meas, x))

#print "ok up to here2"

print "the optimized values of the parameters are:"

print plsq[0]

coeff=plsq[0]
#coeff[1]=coeff[1]**2.
#coeff[2]=coeff[2]**2.

print "so the amplitude is", coeff[0]

print "and the mean", coeff[1]

print "and the std", coeff[2]

print"and these are the same results as those provided by R!"

print "So far so good"




This way the code works, but if you try running it without the abs() 
around mu1 and myvar1, then you get again the error message concerning 
the floating point numbers.
I also (re)attach the data, which you will be able to access via a link.
Kind Regards

Lorenzo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 120km-TPM.csv
Type: text/csv
Size: 9696 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20070303/aa1139b1/attachment.csv>


More information about the SciPy-User mailing list