当曲线拟合上 y = 0.5 时尝试 find/print x 值时出错

Error trying to find/print the x-value when y = 0.5 on a curve fit

我正在尝试为曲线拟合打印出 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))

您的问题是 p0 调用 brentq

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

如果您查看 parameters for brentq,您会发现在 ab 间隔参数之后没有接受列表的参数。因此,args 参数对于具有附加序列的函数签名而言变得意外。在任何情况下,我都看不到 brentq 是如何需要 p0 的,尽管我没有这方面的经验,但我认为它只是在曲线拟合时才需要。