生成流失率和猜测率的自由运行列表以进行心理测度曲线拟合(Scipy) [英] Generation of free running list of lapse rate and guess rate for psychometric curve fitting (Scipy)
问题描述
作为scipy曲线拟合函数的新用户和python的相对较新的用户,我对* popt和p0究竟生成了什么感到困惑(参考此)
As a new user to the curve fitting function from scipy and a relatively new user of python, I am a little confused as to what *popt and p0 exactly generates (with reference to this)
我正在尝试根据自定义的S形函数公式在此处绘制心理测验拟合,该公式考虑了猜测和失败率(两个值都介于0和1之间,以说明实验中参与者的猜测和表现失败率。这些值将定义拟合度
So I am trying to plot a psychometric fitting here based of a customized sigmoid function formula that accounts for a guess and lapse rate (Both with values between 0 and 1 to account for participant guessing and performance lapse rates in an experiment. These values would define the fit on the lower and upper end of the curve fit respectively.)
我可以得到函数,以在固定猜测率时生成最适合曲线的失效率在功能之外设置。但是,当我希望该函数同时生成最佳的失败率和猜测率时,它无法生成并给出以下错误:-
I can get the function to generate the best lapse rate it could fit to the curve when a fixed guess rate was set outside the function. But when I want the function to generate both the best lapse rate and guess rate, it couldn't, and gave the following error:-
文件 C:\Users\Aaron\Anaconda2\lib\site-packagesscipy\optimize\minpack.py,行447,在_general_function
中返回函数(xdata, params)-ydata
TypeError:sigmoidscaled()恰好接受5个参数(给定4个)
现在我知道这意味着没有值来自猜测比率变量,因此没有该错误。那么函数在这里如何生成失败率而不是猜测率呢?
Now I am aware that this means that there was no value coming from the 'guess rate' variable and thus this error. So how is it that the function was able to generate a 'lapse rate' but not a 'guess rate' here?
这些是当猜测率是a时的代码。前缀值和曲线拟合成功:-
These are the codes when the guess rate is a prefixed value and the curve fit is successful:-
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最低机会水平'被删除,并且在sigmoid函数中插入了一个guess变量。):-
Whereas when I tried to have the formula try to find the best 'guess' value, it couldn't. (Here, 'guess = 0.05 #Set the minimum chance level' was removed and a guess variable inserted into the sigmoid function.) :-
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()
推荐答案
p0
是拟合过程的起点。 popt
是参数的最佳拟合值。
p0
is the starting point for the fit procedure. popt
is the resulting best-fit values of the parameters.
请注意, curve_fit
假设 f(x,* parameters)
时函数的签名:第一个参数是您拥有<$ c $的自变量c> xdata ,其余的都是您要优化的参数。
Note that curve_fit
assumes that the signature of your function if f(x, *parameters)
: the first argument is an independent variable for which you have xdata
, and the rest are parameters that you want optimized.
在第一个示例中, sigmoidscaled
接受四个参数,并且为 p0
提供了一个长度为3的列表。这样,拟合开始于 x0 = 1; k = 1; lapse = -10
。
In your first example, sigmoidscaled
takes four arguments, and you provide a length-three list for p0
. This way, the fitting starts with x0 = 1; k = 1; lapse = -10
.
在第二个示例中, sigmoidscaled
包含五个参数,表示您正在为四个参数设置初始值。
In your second example, sigmoidscaled
takes five arguments, meaning you're fitting four parameters for which you need initial values.
快速检查:
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])
这篇关于生成流失率和猜测率的自由运行列表以进行心理测度曲线拟合(Scipy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!