Least-square-fit using python

This is something rather simple, a log-normal fit!

import sys,os
import numpy as np
import pylab as plt
import scipy.optimize

# function for fit - gauss curve - p = (mean,sigma)
func = lambda p, x: (1.0/ (p[1] * np.sqrt(2 * np.pi))) \
                                  * np.exp(-0.5 * ((x - p[0])/p[1])**2)
# residuals
res  = lambda p, x, y: func(p,x) - y

# initial parameters
pIni = [0,1.0]

# fetch the data
filename = '/home/jochen/somefile.data'
x,y = np.loadtxt(filename,usecols=(0,1),unpack=True)

# go log
y = np.log10(y)

# fit
p, sucess = scipy.optimize.leastsq(res,pIni,args=(x,y))

# plot
plt.plot(x,y,x,func(p,x))
plt.show()
1