曲线拟合scipy.stats.multivariate_normal
我试图将曲线拟合2D多元正态分布与图像。方法在这里有效,但是我想用协方差矩阵来参数化结果,例如scipy.stats。 Multivariate_normal
。
这是我的代码:
def mvnorm(data_tuple, mean1, mean2, cov1, cov2, cov3):
(x, y) = data_tuple
pos = numpy.dstack((x, y))
mean = [mean1,mean2]
cov = [[cov1,cov3],[cov3,cov2]]
result = scipy.stats.multivariate_normal.pdf(pos, mean, cov)
return result
def fit_mvnorm(array, coords, initial_guess):
shp = numpy.shape(array)
step1 = abs(coords[0]-coords[1])/shp[1]
step2 = abs(coords[2]-coords[3])/shp[1]
x, y = numpy.mgrid[coords[0]:coords[1]:step1, coords[2]:coords[3]:step2]
popt,pcov = scipy.optimize.curve_fit(mvnorm, (x,y), array, p0=initial_guess)
return popt, pcov
我的数据numpy.ndarray
包含图像的亮度值(float)并具有形状(548,548)。 坐标
是轴限制坐标的列表。当我运行该功能时,我会收到以下错误:
ValueError: object too deep for desired array
...
error: Result from function call is not a proper array of floats.
您对尝试什么有任何想法吗?
I am trying to curve fit a 2D multivariate normal distribution to an image. The approach here works, however I want to parametrize the result with a covariance matrix, like in scipy.stats.multivariate_normal
.
Here is my code:
def mvnorm(data_tuple, mean1, mean2, cov1, cov2, cov3):
(x, y) = data_tuple
pos = numpy.dstack((x, y))
mean = [mean1,mean2]
cov = [[cov1,cov3],[cov3,cov2]]
result = scipy.stats.multivariate_normal.pdf(pos, mean, cov)
return result
def fit_mvnorm(array, coords, initial_guess):
shp = numpy.shape(array)
step1 = abs(coords[0]-coords[1])/shp[1]
step2 = abs(coords[2]-coords[3])/shp[1]
x, y = numpy.mgrid[coords[0]:coords[1]:step1, coords[2]:coords[3]:step2]
popt,pcov = scipy.optimize.curve_fit(mvnorm, (x,y), array, p0=initial_guess)
return popt, pcov
My data numpy.ndarray
contains the image's brightness values (float) and has shape (548,548). coords
is a list of the axis limit coordinates. When I run the function, I get the following error:
ValueError: object too deep for desired array
...
error: Result from function call is not a proper array of floats.
Do you have any idea on what to try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论