def sigmoid(x, a, b, c, d):
""" General sigmoid function
a adjusts amplitude
b adjusts y offset
c adjusts x offset
d adjusts slope """
y = ((a-b) / (1 + np.exp(x-(c/2))**d)) + b
return y
x = np.arange(49)
y = sigmoid(x, 8, 2, 48, 0.3)
plt.plot(x, y)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def sigmoid(x, a, b, c, d):
return ((a - b) / (1. + np.exp(x - (c / 2)) ** d)) + b
# one needs at least as many data points as parameters, so I just duplicate the data
xdata = [0., 48.] * 2
ydata = [8., 2.] * 2
# plot data
plt.plot(xdata, ydata, 'bo', label='data')
# fit the data
popt, pcov = curve_fit(sigmoid, xdata, ydata, p0=[1., 1., 50., 0.5])
# plot the result
xdata_new = np.linspace(0, 50, 100)
plt.plot(xdata_new, sigmoid(xdata_new, *popt), 'r-', label='fit')
plt.legend(loc='best')
plt.show()
你可以像这样重新定义sigmoid函数
Severin的答案可能更为有力,但如果你只想快速而肮脏的解决方案,这应该没问题。在
^{pr2}$或者,您也可以使用
curve_fit
,如果您有两个以上的数据点,这可能会很有用。输出如下:如您所见,该图包含所需的数据点。我使用了@lanery的函数进行拟合;您当然可以选择任何您喜欢的函数。这是带有一些内联注释的代码:
从一般双极性乙状窦函数:
有两个参数和两个未知数-shift
m
和scaleb
你有两个条件:f(0)=8,f(48)=2
取第一个条件,表示}。在
b
vsm
,与第二个条件一起编写非线性函数求解,然后使用SciPy中的fsolve
对其进行数值求解,并恢复b
和{这里用相似的方法相关问答:How to random sample lognormal data in Python using the inverse CDF and specify target percentiles?
相关问题 更多 >
编程相关推荐