适合双极乙状Python

2024-05-19 22:26:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试拟合双极性乙状结肠曲线-我想要以下曲线:

enter image description here

但我需要它移动和伸展。我有以下输入:

x[0] = 8, x[48] = 2

所以在48个周期内,我需要用双极性的sigmoid函数从8降到2,以近似平滑的衰减。有什么想法可以推导出符合这些参数的曲线吗?在

到目前为止,我得到的结果是,我需要改变sigmoid函数:

^{pr2}$

Tags: 函数参数曲线极性pr2sigmoid
3条回答

你可以像这样重新定义sigmoid函数

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)

Severin的答案可能更为有力,但如果你只想快速而肮脏的解决方案,这应该没问题。在

^{pr2}$

enter image description here

或者,您也可以使用curve_fit,如果您有两个以上的数据点,这可能会很有用。输出如下:

enter image description here

如您所见,该图包含所需的数据点。我使用了@lanery的函数进行拟合;您当然可以选择任何您喜欢的函数。这是带有一些内联注释的代码:

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

从一般双极性乙状窦函数:

f(x,m,b)= 2/(1+exp(-b*(x-m))) - 1

有两个参数和两个未知数-shiftm和scaleb

你有两个条件:f(0)=8,f(48)=2

取第一个条件,表示bvsm,与第二个条件一起编写非线性函数求解,然后使用SciPy中的fsolve对其进行数值求解,并恢复b和{}。在

这里用相似的方法相关问答:How to random sample lognormal data in Python using the inverse CDF and specify target percentiles?

相关问题 更多 >