在curve_fit(scipy)期间将边界应用于特定变量会导致错误

时间:2016-05-06 01:37:17

标签: python matplotlib scipy curve-fitting

我正在尝试在曲线拟合期间将边界应用于某些参数,但是当我尝试这样做时,我收到以下错误消息:

ValueError:解压缩的值太多

在绑定命令中的每个2元组是否分别对应于我的情况下的sigmoidcaled函数中的x0,k,lapse,guess(也就是对应于p0)?

然后,我试着通过减少对以下内容的绑定命令来试图弄清楚它是如何工作的,以消除太多的值':

  

bounds =(( - np.inf,np.inf),(0,1))

然后我收到错误消息:

ValueError:边界与x0之间的形状不一致。

我在这里弄错了什么?

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,0,0] 
popt, pcov = curve_fit(sigmoidscaled, xdata, ydata, p0, bounds=((-np.inf,np.inf), (-np.inf,np.inf), (0,1), (0,1))

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

1 个答案:

答案 0 :(得分:7)

问题在于:

text

documentation开始,<ab>需要是2元组的数组。因此,不是指定每个点的下限和上限,而是需要指定第一个数组中每个点的下限,然后指定第二个数组中每个点的上限,如下所示:

for i in doc.iter(tag='{http://www.tei-c.org/ns/1.0}ab'): 
    innerText = i.text+''.join((text.tail or '') for text in i.iter()).strip()  
    print(i.tag, innerText)

在此更改之后,情节会立即弹出!