作为scipy和相对较新的python用户的曲线拟合函数的新用户,我对* popt和p0的确切生成(参考this)感到有点困惑
所以我试图根据一个定制的S形函数公式绘制一个心理测量拟合,该公式考虑了猜测和失效率(两者的值都在0和1之间,以说明参与者猜测和实验中的性能失效率。值将分别定义曲线拟合的下端和上端的拟合。)
当在函数外部设置固定的猜测率时,我可以获得能够生成适合曲线的最佳失效率的功能。但是,当我希望函数生成最佳失效率和猜测率时,它不能,并给出以下错误: -
文件“C:\ Users \ Aaron \ Anaconda2 \ lib \ site-packages \ scipy \ optimize \ minpack.py”,第447行,在_general_function中 返回函数(xdata, params) - ydata TypeError:sigmoidscaled()只需要5个参数(给定4个)
现在我知道这意味着没有来自'guess rate'变量的值,因此这个错误。那么该功能如何能够产生“失效率”而不是“猜测率”呢?
当猜测率是一个前缀值并且曲线拟合成功时,这些是代码: -
import numpy as np
import pylab
from scipy.optimize import curve_fit
from matplotlib.pyplot import *
n = 20 #20 trials
ydata = [0/n, 9.0/n, 9.0/n, 14.0/n, 17.0/n] #Divided by n to fit to a plot of y =1
xdata = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])
guess = 0.05 #Set the minimum chance level
#The scaled sigmoid function
def sigmoidscaled(x, x0, k, lapse):
F = (1 + np.exp(-k*(x-x0)))
z = guess + (1-guess-lapse)/F
return z
p0=[1,1,-10]
popt, pcov = curve_fit(sigmoidscaled, xdata, ydata, p0, maxfev = 3000)
#Start and End of x-axis, in spaces of n. The higher the n, the smoother the curve.
x = np.linspace(1,5,20)
#The sigmoid values along the y-axis, generated in relation to the x values and the 50% point.
y = sigmoidscaled(x, *popt)
pylab.plot(xdata, ydata, 'o', label='Psychometric Raw', color = 'blue')
pylab.plot(x,y, label='Psychometric Fit', color = 'blue')
#y axis range.
pylab.ylim(0, 1)
#Replace x-axis numbers as labels and y-axis numbers as percentage
xticks([1., 2., 3., 4., 5.], ['C1','CN2','N3','CN4','S5'])
yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], ['0%','20%','40%','60%','80%','100%'])
pylab.legend(loc='best')
xlabel('Conditions')
ylabel('% perceived more sin like')
pylab.show()
然而,当我试图找到最佳'猜测'值时,我试图找到最好的“猜测”价值,但事实并非如此。 (这里,'guess = 0.05 #Set the minimum chance level'被删除,一个猜测变量被插入sigmoid函数。): -
import pylab
from scipy.optimize import curve_fit
from matplotlib.pyplot import *
n = 20 #20 trials
ydata = [0/n, 9.0/n, 9.0/n, 14.0/n, 17.0/n] #Divided by n to fit to a plot of y =1
xdata = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0])
#The scaled sigmoid function
def sigmoidscaled(x, x0, k, lapse, guess):
F = (1 + np.exp(-k*(x-x0)))
z = guess + (1-guess-lapse)/F
return z
p0=[1,1,-10]
popt, pcov = curve_fit(sigmoidscaled, xdata, ydata, p0, maxfev = 3000)
#Start and End of x-axis, in spaces of n. The higher the n, the smoother the curve.
x = np.linspace(1,5,20)
#The sigmoid values along the y-axis, generated in relation to the x values and the 50% point.
y = sigmoidscaled(x, *popt)
pylab.plot(xdata, ydata, 'o', label='Psychometric Raw', color = 'blue')
pylab.plot(x,y, label='Psychometric Fit', color = 'blue')
#y axis range.
pylab.ylim(0, 1)
#Replace x-axis numbers as labels and y-axis numbers as percentage
xticks([1., 2., 3., 4., 5.], ['C1','CN2','N3','CN4','S5'])
yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], ['0%','20%','40%','60%','80%','100%'])
pylab.legend(loc='best')
xlabel('Conditions')
ylabel('% perceived more sin like')
pylab.show()
答案 0 :(得分:0)
p0
是适合程序的起点。 popt
是参数的最佳拟合值。
请注意,curve_fit
假设您的函数的签名为f(x, *parameters)
:第一个参数是您拥有xdata
的自变量,其余的是您想要优化的参数
在您的第一个示例中,sigmoidscaled
接受四个参数,并为p0
提供长度为三的列表。这样,拟合从x0 = 1; k = 1; lapse = -10
开始。
在你的第二个例子中,sigmoidscaled
有五个参数,这意味着你需要拟合四个你需要初始值的参数。
快速检查:
In [22]: p0 = [1, 1, -10, 0] # add the 4th element
In [23]: popt, pcov = curve_fit(sigmoidscaled, xdata, ydata, p0, maxfev = 3000)
In [24]: popt
Out[24]: array([ -1.97865387e+01, 3.31731590e-01, -1.03275740e-01,
-1.05595226e+03])