【问题标题】:Error trying to find/print the x-value when y = 0.5 on a curve fit当曲线拟合上的 y = 0.5 时,尝试查找/打印 x 值时出错
【发布时间】:2016-05-16 18:04:14
【问题描述】:

我正在尝试打印 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))

【问题讨论】:

  • 我们需要完整的回溯,可能还需要 brentq 函数的签名......

标签: python scipy curve-fitting


【解决方案1】:

拨打brentq时,您的问题是p0

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

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

【讨论】:

    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多