在曲线拟合时,当y = 0.5时尝试查找/打印x值时出错

时间:2016-05-16 09:55:27

标签: python scipy curve-fitting

我正在尝试打印y = 0.50的x值以获得曲线拟合,并且我已尝试适应所提供的解决方案here但是我得到以下错误代码: -

TypeError:brentq()获取关键字参数'args'的多个值

sigmoidscaled()函数中的所有值都不应该在args中吗?还是我误解了什么?

import pylab
from scipy.optimize import curve_fit
from matplotlib.pyplot import *
from scipy.optimize import brentq
import numpy as np

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, y0=0):
    F = (1 + np.exp(-k*(x-x0))) 
    z = guess + (1-guess-lapse)/F + y0
    return z

p0=[1,1,0,0] 
popt, pcov = curve_fit(sigmoidscaled, xdata, ydata, p0, bounds=((-np.inf, -np.inf, 0.,0.), (np.inf, np.inf, 0.5, 0.5)))


#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() 

a = np.min(xdata)
b = np.max(xdata)
x0, k, guess, lapse = popt
y0 = 0.50

solution = brentq(sigmoidscaled, a, b, p0, args=(x0, k, guess, lapse, y0))

1 个答案:

答案 0 :(得分:0)

调用p0

时,您的问题是brentq
solution = brentq(sigmoidscaled, a, b, p0, args=(x0, k, guess, lapse, y0))

如果您查看parameters for brentq,则可以看到在ab间隔参数后没有参数接受列表。因此,args参数对于具有附加序列的函数签名来说是意外的。在任何情况下,我都不知道p0需要brentq,虽然我没有经验,但我认为只有在曲线拟合时才需要cfsqltype as cf_sql_date