From: D2Hitman on 20 Jul 2010 22:13 I want to fit an ndimensional distribution with an ndimensional gaussian. So far i have managed to do this in 2d (see below). I am not sure how to convert this to work in ndimensions. Using "ravel" on the arrays is not ideal, but optimize does not appear to work on multidimensional arrays. It seems "meshgrid" also does not translate to nd. import numpy as np from scipy import optimize #2D #random points N = 1000 nd = 2 a = np.random.normal(loc=[1.,2.], scale=[1.,2.], size=[N,nd]) #histogram points bins = [10, 20] H, e = np.histogramdd(a, bins=bins) #find center rather than left edge edges = [] for i in range(len(e)): edges.append( e[i][:1] + np.diff(e[i])/2. ) #not ndimensional x, y = np.meshgrid(edges[0], edges[1]) x, y = np.ravel(x), np.ravel(y) H = np.ravel(H) #2D gaussian gauss2d = lambda p, x, y: p[0] * np.exp( (0.5*(xp[1])**2/p[2]**2)  (0.5*(yp[3])**2/p[4]**2) ) + p[5] residuals = lambda p, x, y, H: H  gauss2d(p, x, y) #Fitting p0 = [1., 0., 1., 0., 1., 0] plsq, cov_x, infodict, mesg, ier = optimize.leastsq(residuals, p0, args=(x, y, H), full_output=True) #Reading off _x, _y = 0.091, 1.293 print '%.3f %.3f %.4f' % (_x, _y, gauss2d(plsq, _x,_y) / plsq[0])  View this message in context: http://old.nabble.com/MultidimensionalFittingtp29221343p29221343.html Sent from the Python  pythonlist mailing list archive at Nabble.com.
From: Robert Kern on 20 Jul 2010 22:22 On 7/20/10 10:13 PM, D2Hitman wrote: > > I want to fit an ndimensional distribution with an ndimensional gaussian. > So far i have managed to do this in 2d (see below). I am not sure how to > convert this to work in ndimensions. Using "ravel" on the arrays is not > ideal, but optimize does not appear to work on multidimensional arrays. It > seems "meshgrid" also does not translate to nd. You will want to ask scipy questions on the scipy mailing list: http://www.scipy.org/Mailing_Lists Don't try to fit a Gaussian to a histogram using leastsquares. It's an awful way to estimate the parameters. Just use np.mean() and np.cov() to estimate the mean and covariance matrix directly.  Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco
From: D2Hitman on 20 Jul 2010 23:56 Robert Kern2 wrote: > > Don't try to fit a Gaussian to a histogram using leastsquares. It's an > awful > way to estimate the parameters. Just use np.mean() and np.cov() to > estimate the > mean and covariance matrix directly. > Ok, what about distributions other than gaussian? Would you use leastsq in that case? If yes, i will post that to the scipy mailing list.  View this message in context: http://old.nabble.com/MultidimensionalFittingtp29221343p29221776.html Sent from the Python  pythonlist mailing list archive at Nabble.com.
From: Robert Kern on 21 Jul 2010 00:31 On 7/20/10 11:56 PM, D2Hitman wrote: > > > Robert Kern2 wrote: >> >> Don't try to fit a Gaussian to a histogram using leastsquares. It's an >> awful >> way to estimate the parameters. Just use np.mean() and np.cov() to >> estimate the >> mean and covariance matrix directly. >> > > Ok, what about distributions other than gaussian? Would you use leastsq in > that case? If yes, i will post that to the scipy mailing list. No, you would use a maximum likelihood method.  Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco

Pages: 1 Prev: How to treat the first or last item differently Next: SQLalchemy+py2exe+pymssql error 