Three dimensional Gaussian fit

277 views
Skip to first unread message

Peter Stahlecker

unread,
Aug 22, 2021, 10:26:25 AM8/22/21
to lmfit-py
Expanding on an example given, I tried to get a 3D fit.
I wrote a program, see attached:

I get an error, saying the matrix (I guess, it meant my covariance matrix C) must be two dimensional.

This is not urgent or important, I am just playing around with lmfit, but I would surely appreciate any help!

Untitled.ipynb

Mark Dean

unread,
Aug 22, 2021, 2:44:19 PM8/22/21
to lmfi...@googlegroups.com
Hi Peter,

Always post your error message in plain text. This is very often helpful and basically a rule for using the message board. 

I can see a couple of problems.
-- The values passed into parameters should be float,s not single-valued arrays i.e. 0.7 not np.array([0.7])
-- The function would normally return a np.array and not a np.matrix
-- The function can return NaN for certain parameter values

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/0c8c0bec-090c-4e8a-b612-013a6c208f21n%40googlegroups.com.


--
--------------------------------------------
Mark P. M. Dean
Brookhaven National Laboratory
Condensed Matter Physics Bldg 734
Upton NY 11973
USA
Phone: 001 631 344 7847

Peter Stahlecker

unread,
Aug 23, 2021, 6:59:58 AM8/23/21
to lmfi...@googlegroups.com
Dear Mark,

Thanks for the hints!

I rewrote the program to avoid what you mentioned:

#Estimating 3_D Gaussian distribution
import numpy as np
import lmfit
from lmfit import Model

x = np.linspace(-1., 1., 100)
y = x
z = x

c00 = 1.   #parameters must have names
c01 = 0.
c02 = 0.
c11 = 1.
c12 = 0.
c22 = 1.

C = np.matrix(((c00, c01, c02), (c01, c11, c12), (c01, c12, c22)))
print(C)  #just to verify, that it C the unit matric


#three dimensional Gaussian
def gaussian_nd(x, y, z, c00, c01, c02, c11, c12, c22):
    C = np.array(((c00, c01, c02), (c01, c11, c12), (c01, c12, c22)))
    X = np.array(((x), (y), (z)))
    return np.array(1./np.sqrt(np.pi**3 / np.linalg.det(C)) * np.exp(X.T @ C @ X))

gauss = gaussian_nd(x, y, z, c00, c01, c02, c11, c12, c22)    # true values
Z = gauss + np.random.normal(size=(100,100), scale=0.1)       #measured values
print(Z.shape, type(Z))
model = lmfit.Model(gaussian_nd, independent_vars=['x', 'y', 'z'])
params = model.make_params()

params['c00'].set(value=1. + 0.01 * np.random.normal(size=1, scale=0.1))
params['c01'].set(value=0. + 0.01 * np.random.normal(size=1, scale=0.1))
params['c02'].set(value=0. + 0.01 * np.random.normal(size=1, scale=0.1))
params['c11'].set(value=1. + 0.01 * np.random.normal(size=1, scale=0.1))
params['c12'].set(value=0. + 0.01 * np.random.normal(size=1, scale=0.1))
params['c22'].set(value=1. + 0.01 * np.random.normal(size=1, scale=0.1))

result = model.fit(Z, x=x, y=y, z=z, params=params)

Now I get this error message:

LinAlgError: Last 2 dimensions of the array must be square




You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/fu9ZQxS3TgU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CABMFFfocXN5zsSGYac-nUJHpFcr1TLKHk07qoT82U%2BjK1VazUg%40mail.gmail.com.
--
Best regards,

Peter Stahlecker

Peter Stahlecker

unread,
Aug 23, 2021, 7:06:56 AM8/23/21
to lmfi...@googlegroups.com
Here is a screen shot of the error message if it helps.
Thanks again!


Mark Dean

unread,
Aug 23, 2021, 11:44:01 AM8/23/21
to lmfi...@googlegroups.com
The line
params['c00'].set(value=1. + 0.01 * np.random.normal(size=1, scale=0.1))
should be
params['c00'].set(value=1. + 0.01 * np.random.normal(size=1, scale=0.1)[0])


Peter Stahlecker

unread,
Aug 23, 2021, 12:00:37 PM8/23/21
to lmfi...@googlegroups.com
Dear Mark,

This did the trick!!
Now my model function returns NaN values (mostly, not always), so I must have made a mistake there.
I think, you may close this.
Thanks again!
Peter

Peter Stahlecker

unread,
Aug 24, 2021, 2:59:21 AM8/24/21
to lmfi...@googlegroups.com
After I corrected my 3D Gaussian, it all works well.
Thanks a lot for your help!
Reply all
Reply to author
Forward
0 new messages