[SciPy-user] Optimization Working only with a Specific Expression of the input Parameters
Sat Mar 3 14:23:16 CST 2007
Can you post your code, including the data?
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
x=data[0:vecdim,0] # it means: slice the rows from the 1st one (0) to
the last one (
# (vecdim) for the first column (0)
#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,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(
#def peval(x, p):
# 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]
# 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 "so the amplitude is", coeff
print "and the mean", coeff
print "and the std", coeff
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 9696 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20070303/aa1139b1/attachment.bin
More information about the SciPy-user