1

so I'm having trouble with this piece of code here:

import numpy as np
import matplotlib.pyplot as plt
import scipy as sy
import pylab as plb

def oscDecay(x, A, B, C, tau, omega):
    return A*(1+B*np.cos(omega*x))*np.exp(-1*x**2/(2*tau**2))+C


def LMfit(func, x, y, p0, sig):
    #Fits data to non-linear curve using Levenberg-Marquart Method
    #Inputs: func = the function you are fitting data to
    #        x, y = data set
    #       p0 = tuple containing inital guesses for fitting  parameters
    #       sig = uncertainty in values
    #Outputs: nlfit = array containing optimal values for fitting parameters
    #       nlcov = two dimensional array (square root of diagonals contain
    #               uncertainty in fitting parameters)
    nlfit, nlpcov = sy.optimize.curve_fit(func, x, y, p0, sig)
    return nlfit, nlpcov

data=np.loadtxt('testing.txt', skiprows=4)

x=data[:, 0]
y=data[:, 1]
sig=data[:, 2]

#intial parameters
A0=16.5
B0=0.57
C0=17
tau0=30
omega0=7
p0=(A0, B0, C0, tau0, omega0)

nlfit, nlpcov = LMfit(oscDecay, x, y, p0, sig)

When I try to run it, I get this error message:

nlfit, nlpcov = sy.optimize.curve_fit(oscDecay, x, y, p0, sig). AttributeError: 'module' object has no attribute 'optimize'

I'm not sure what it means since my software provided me with scipy.optimize already to use.

1
  • 1
    You need to do import scipy.optimize, not just import scipy. Commented May 9, 2016 at 4:29

1 Answer 1

1

In your case the import seems to not have worked as noted by the commenter. I had to make a synthetic sample to test your code. I just left out the sigmas and it worked all right (using Ipython notebooks).

import numpy as np
import matplotlib.pyplot as p
from scipy import optimize as opt
%matplotlib inline

def oscDecay(x, A, B, C, tau, omega):
    return A*(1+B*np.cos(omega*x))*np.exp(-1*x**2/(2*tau**2))+C

# make an example of data with some experimental noise
def oscDecayexp(x, A, B, C, tau, omega):
    rand=np.random.rand(len(x))
    return A*(1+(B+rand/10)*np.cos(omega*x))*np.exp(-1*x**2/(2*tau**2))+C


def LMfit(func, x, y, p0 ,sig=None):
    #Fits data to non-linear curve using Levenberg-Marquart Method
    #Inputs: func = the function you are fitting data to
    #        x, y = data set
    #       p0 = tuple containing inital guesses for fitting  parameters
    #       sig = uncertainty in values
    #Outputs: nlfit = array containing optimal values for fitting parameters
    #       nlcov = two dimensional array (square root of diagonals contain
    #               uncertainty in fitting parameters)
    nlfit, nlpcov = opt.curve_fit(func, x, y, p0, sig)
    return nlfit, nlpcov


#intial parameters
A0=16.5
B0=0.57
C0=17
tau0=30
omega0=7

x=np.arange(0,80,0.02)
y=oscDecayexp(x,A0,B0,C0,tau0,omega0)

p.figure(figsize=(12,8))

p.plot(x, oscDecay(x,A0,B0,C0,tau0,omega0)) # blue
p.plot(x, oscDecayexp(x,A0,B0,C0,tau0,omega0),lw=0.5) #green

p0=(A0, B0, C0, tau0, omega0)
# scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, **kw)  
nlfit, nlpcov = LMfit(oscDecay, x, y, p0 )

print nlfit

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

from scipy import optimize as opt does the trick. upvoted :)
@ Joe: +1 as well helped me, though it is a strange behavior, but exactly such a trick helped in simple example... though no problems at colab.research.google.com/notebooks/welcome.ipynb at all with such a code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.