获得 scipy.optimize.leastsq 的正确用法
Get the correct ussage of scipy.optimize.leastsq
所以根据对问题的回答[python nonlinear least squares fitting
我调整了答案来估计三个参数kd,p0,l0
N = 10
kd_guess = 7.0 # <-- You have to supply a guess for kd
p0_guess = 8.0
l0_guess = 15.0
p0 = np.linspace(0,10,N)
l0 = np.linspace(0,10,N)
PLP = func(4.0,5.0,6.0)+(np.random.random(N)-0.5)*2.0
# The target should be (4.0,5.0,6.0)
kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])
我想避免以下错误,
Traceback (most recent call last):
File "Main.py", line 40, in <module>
kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])
File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 380, in leastsq
x0 = asarray(x0).flatten()
File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
这是一个使用 scipy 的 curve_fit() 例程的图形示例,它调用 leastsq() - 我个人认为 scipy curve_fit 例程更容易使用比 leastsq.
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7])
yData = numpy.array([1.1, 20.2, 30.3, 60.4, 50.0, 60.6, 70.7])
def func(x, a, b, c): # simple quadratic example
return (a * numpy.square(x)) + b * x + c
# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])
# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
modelPredictions = func(xData, *fittedParameters)
absError = modelPredictions - yData
SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()
##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')
# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
所以根据对问题的回答[python nonlinear least squares fitting
我调整了答案来估计三个参数kd,p0,l0
N = 10
kd_guess = 7.0 # <-- You have to supply a guess for kd
p0_guess = 8.0
l0_guess = 15.0
p0 = np.linspace(0,10,N)
l0 = np.linspace(0,10,N)
PLP = func(4.0,5.0,6.0)+(np.random.random(N)-0.5)*2.0
# The target should be (4.0,5.0,6.0)
kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])
我想避免以下错误,
Traceback (most recent call last):
File "Main.py", line 40, in <module>
kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])
File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 380, in leastsq
x0 = asarray(x0).flatten()
File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
这是一个使用 scipy 的 curve_fit() 例程的图形示例,它调用 leastsq() - 我个人认为 scipy curve_fit 例程更容易使用比 leastsq.
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7])
yData = numpy.array([1.1, 20.2, 30.3, 60.4, 50.0, 60.6, 70.7])
def func(x, a, b, c): # simple quadratic example
return (a * numpy.square(x)) + b * x + c
# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])
# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
modelPredictions = func(xData, *fittedParameters)
absError = modelPredictions - yData
SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()
##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')
# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)