Python 3.x 确定二维高斯拟合的起始参数
我正在编写一些代码,这些代码需要能够进行二维高斯拟合。我的代码主要基于以下问题:。现在的问题是,我对需要使用的不同参数并没有一个初步的猜测 我试过这个:Python 3.x 确定二维高斯拟合的起始参数,python-3.x,scipy-optimize,Python 3.x,Scipy Optimize,我正在编写一些代码,这些代码需要能够进行二维高斯拟合。我的代码主要基于以下问题:。现在的问题是,我对需要使用的不同参数并没有一个初步的猜测 我试过这个: def twoD_Gaussian(x_data_tuple, amplitude, xo, yo, sigma_x, sigma_y, theta, offset): (x,y) = x_data_tuple xo = float(xo) yo = float(yo) a = (np.cos(theta
def twoD_Gaussian(x_data_tuple, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
(x,y) = x_data_tuple
xo = float(xo)
yo = float(yo)
a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo)
+ c*((y-yo)**2)))
return g.ravel()
重塑数据(201201)只是我从前面提到的问题中得到的
mean_gauss_x = sum(x * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_x = np.sqrt(sum(data.reshape(201,201) * (x - mean_gauss_x)**2) / sum(data.reshape(201,201)))
mean_gauss_y = sum(y * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_y = np.sqrt(sum(data.reshape(201,201) * (y - mean_gauss_y)**2) / sum(data.reshape(201,201)))
initial_guess = (np.max(data), mean_gauss_x, mean_gauss_y, sigma_gauss_x, sigma_gauss_y,0,10)
popt, pcov = curve_fit(twoD_Gaussian, (x, y), data, p0=initial_guess)
data_fitted = twoD_Gaussian((x, y), *popt)
如果我尝试这样做,我会得到以下错误消息:ValueError:使用序列设置数组元素
关于begin参数的推理正确吗?
为什么会出现此错误?如果我使用中的可运行代码并替换您对
初始值的定义
:
mean_gauss_x = sum(x * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_x = np.sqrt(sum(data.reshape(201,201) * (x - mean_gauss_x)**2) / sum(data.reshape(201,201)))
mean_gauss_y = sum(y * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_y = np.sqrt(sum(data.reshape(201,201) * (y - mean_gauss_y)**2) / sum(data.reshape(201,201)))
initial_guess = (np.max(data), mean_gauss_x, mean_gauss_y, sigma_gauss_x, sigma_gauss_y,0,10)
然后
屈服
(13.0, array([...]), array([...]), array([...]), array([...]), 0, 10)
请注意,initial\u guess
中的一些值是数组。optimize.curve\u fit
函数期望initial\u guess
是一个标量元组。这就是问题的根源
错误消息
ValueError: setting an array element with a sequence
通常在需要标量值时提供类似于的数组时出现。这表明问题的根源可能与数组的维数错误有关。例如,如果将一维数组传递给需要标量的函数,可能会出现这种情况
让我们看看这段代码取自:
x
和y
是一维数组,而x
和y
是二维数组。(我已将所有二维数组大写,以帮助区分它们与一维数组)
现在请注意,Pythonsum
和NumPy的sum
方法在应用于2D数组时表现不同:
In [146]: sum(X)
Out[146]:
array([ 0., 201., 402., 603., 804., 1005., 1206., 1407.,
1608., 1809., 2010., 2211., 2412., 2613., 2814., 3015.,
...
38592., 38793., 38994., 39195., 39396., 39597., 39798., 39999.,
40200.])
In [147]: X.sum()
Out[147]: 4040100.0
Pythonsum
函数相当于
total = 0
for item in X:
total += item
由于X
是一个二维数组,因此X中项的循环在X的行上迭代。因此,每个项
都是一个1D数组,表示一行X
。因此,total
最终成为1D数组
相反,X.sum()
对X
中的所有元素求和并返回标量
因为initial\u guess
应该是一个标量元组,
无论您在哪里使用sum
都应该使用NumPysum
方法。例如,替换
mean_gauss_x = sum(x * data) / sum(data)
与
嘿非常感谢您的回答,代码似乎很有效!我会记住sum()和np.sum()之间的区别
total = 0
for item in X:
total += item
mean_gauss_x = sum(x * data) / sum(data)
mean_gauss_x = (X * DATA).sum() / (DATA.sum())
import numpy as np
import scipy.optimize as optimize
import matplotlib.pyplot as plt
# define model function and pass independant variables x and y as a list
def twoD_Gaussian(data, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
X, Y = data
xo = float(xo)
yo = float(yo)
a = (np.cos(theta) ** 2) / (2 * sigma_x ** 2) + (np.sin(theta) ** 2) / (
2 * sigma_y ** 2
)
b = -(np.sin(2 * theta)) / (4 * sigma_x ** 2) + (np.sin(2 * theta)) / (
4 * sigma_y ** 2
)
c = (np.sin(theta) ** 2) / (2 * sigma_x ** 2) + (np.cos(theta) ** 2) / (
2 * sigma_y ** 2
)
g = offset + amplitude * np.exp(
-(a * ((X - xo) ** 2) + 2 * b * (X - xo) * (Y - yo) + c * ((Y - yo) ** 2))
)
return g.ravel()
# Create x and y indices
x = np.linspace(0, 200, 201)
y = np.linspace(0, 200, 201)
X, Y = np.meshgrid(x, y)
# create data
data = twoD_Gaussian((X, Y), 3, 100, 100, 20, 40, 0, 10)
data_noisy = data + 0.2 * np.random.normal(size=data.shape)
DATA = data.reshape(201, 201)
# add some noise to the data and try to fit the data generated beforehand
mean_gauss_x = (X * DATA).sum() / (DATA.sum())
sigma_gauss_x = np.sqrt((DATA * (X - mean_gauss_x) ** 2).sum() / (DATA.sum()))
mean_gauss_y = (Y * DATA).sum() / (DATA.sum())
sigma_gauss_y = np.sqrt((DATA * (Y - mean_gauss_y) ** 2).sum() / (DATA.sum()))
initial_guess = (
np.max(data),
mean_gauss_x,
mean_gauss_y,
sigma_gauss_x,
sigma_gauss_y,
0,
10,
)
print(initial_guess)
# (13.0, 100.00000000000001, 100.00000000000001, 57.106515650488404, 57.43620227324201, 0, 10)
# initial_guess = (3,100,100,20,40,0,10)
popt, pcov = optimize.curve_fit(twoD_Gaussian, (X, Y), data_noisy, p0=initial_guess)
data_fitted = twoD_Gaussian((X, Y), *popt)
fig, ax = plt.subplots(1, 1)
ax.imshow(
data_noisy.reshape(201, 201),
cmap=plt.cm.jet,
origin="bottom",
extent=(X.min(), X.max(), Y.min(), Y.max()),
)
ax.contour(X, Y, data_fitted.reshape(201, 201), 8, colors="w")
plt.show()