# [SciPy-user] Bug(?) in optimize.leastsq when Jacobian matrix is provided (Complete) <Ignore my previous mail>

Abhiman Chatra abhiman at gmail.com
Thu Jun 23 09:47:40 CDT 2005

Skipped content of type multipart/alternative-------------- next part --------------
from scipy import *
import random
import pprint

inputs = [
[ 1.0 , 2.0 ],
[ 2.0 , 2.0 ],
[ 3.0 , 1.0 ],
[ 2.0 , 4.0 ]]

answers = [ 4.05 , 6.1 , 6.9 , 8.0 ]

def residual_values(betas, inp , ans) :
ret = [0.0] * len(inp)
for i in range(len(inp)) :
ret[i] = 1.0 * betas[len(inp[i])] - ans[i]
for j in range(len(inp[0])) :
ret[i] += betas[j] * inp[i][j]
print "In residual_values"
#print "Betas : "
#pprint.pprint(betas)
#print "return value : "
#pprint.pprint(ret)
return ret

def jacobian(betas, inp , ans) :
print "In jacobian"
J =  [[0.0] * len(betas)] * len(inp)
for i in range(len(inp)) :
J[i][0] = inp[i][0]
J[i][1] = inp[i][1]
J[i][2] = 1.0
return J

try:
for i in range(100):
print("---------------- %d -----------------" % (i))
initial = [0.0] * 3
#random.shuffle(initial)
for i in range(3) :
initial[i] = 1.0
#opt = optimize.leastsq(residual_values, initial, args = (inputs, answers), Dfun=jacobian, maxfev=10000)
#opt = optimize.leastsq(residual_values, initial, args = (inputs, answers), maxfev=10000)

pprint.pprint(opt)

except:
print "Error regression fitting model..."

pprint.pprint(locals())

raise