# [SciPy-user] Optimization

Lorenzo Isella lorenzo.isella@gmail....
Sat Feb 24 11:44:54 CST 2007

```Dear All,
I am going through the Scipy manual and I am trying to reproduce, as an
exercise to learn Python and Scipy, some nonlinear least-square
optimization which I was able to carry out using another language.
I am collecting some questions which hopefully will help me understand
Python a bit better:
(1) What is the difference between
from pylab import *
and import pylab?
(2)  the 2nd question may be a non-problem: in running some of the
examples in the tutorial by Olinfant, I could not use the xplt module,
no matter whether trying import scipy.xplt, from scipy.xplt import * et
similia.
I bumped into
http://lists.debian.org/debian-science/2007/01/msg00007.html
which may be the answer (I am running Debian on my box as well and I
installed Scipy from the Debian repository). Can I install xplt by itself?
BTW, I also visited
http://www.scipy.org/Cookbook/xplt
but even  using
from scipy.sandbox import xplt
does not help.
(3) Is there a way to have arrays starting with index 1 rather than
zero in Python? As you can guess, I do not have a strong C background.
(4) This is the main question: I am trying to fit some experimental data
to a log-normal curve. I would like to follow the same steps as in the
tutorial, but something seems to be going wrong.
I cut and paste the code I am using and attach a .csv file so that one
can reproduce step by step my work.
(5) Finally, if I load scipy and then write, e.g., z=10.3, how is z
handled? Is it a floating point number?
What if, for instance, I need to have a very large number of significant
digits because they do matter for some computation I want to run? Can I
have the equivalent of format long [Matlab statement], so that every
non-integer number is by default treated with a certain precision?

Here is the code:

#! /usr/bin/env python
from pylab import plot, show, ylim, yticks
from scipy import *

import pylab

# now I want to try reading some .csv file

vecdim=shape(data) # now I introduce a vector with the dimensions of
print "the dimensions of data are"
print vecdim  # now very careful! in Python the arrays start with index
zero.
diam=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)
print "the dimensions of diam are"
print shape(diam)

#plot(diam,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.

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

x=diam # just a list of diameters

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

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

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

p0 = [50000.0,90.0, 1.59]
print array(p0)

# 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 plsq[0]

print array([A, k, theta])

print "So far so good"

which produces this output on my box:

the dimensions of data are
(104, 10)
the dimensions of diam are
(104,)
[  5.00000000e+04   9.00000000e+01   1.59000000e+00]
ok up to here
TypeError: array cannot be safely cast to required type
Traceback (most recent call last):
File "./read-and-plot.py", line 51, in ?
plsq = leastsq(residuals, p0, args=(y_meas, x))
File "/usr/lib/python2.4/site-packages/scipy/optimize/minpack.py",
line 266, in leastsq
retval =
_minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
minpack.error: Result from function call is not a proper array of floats.

Many thanks

Lorenzo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 120km-TPM.csv
Type: text/csv
Size: 9696 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20070224/e4ede6c7/attachment-0001.bin
```