Python中的极限指数回归

2024-04-23 22:30:04 发布

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

我已经根据一个实验的数据建立了一个指数回归。但是,我希望在y值开始稳定(大约x=42000秒)时停止回归。见附图。你知道吗

这是目前为止的代码:

import matplotlib.pyplot as plt;
import numpy as np;
import pandas as pd
import scipy.optimize as opt;

# This is the function we are trying to fit to the data.
def func(x, a, b, c):
     return a * b**x

dataC = pd.read_csv("yeastdata1cropped.txt")
data = pd.read_csv("yeastdata1.txt")

xdata = np.array(data.iloc[:,1])
ydata = np.array(data.iloc[:,0])

xdatac = np.array(dataC.iloc[:,1])
ydatac = np.array(dataC.iloc[:,0])

# Plot the actual data
plt.plot(xdata, ydata, ".", label="Data");

# The actual curve fitting happens here
optimizedParameters, pcov = opt.curve_fit(func, xdatac, ydatac);

# Use the optimized parameters to plot the best fit
plt.plot(xdata, func(xdata, *optimizedParameters), label="fit");

# Show the graph
plt.legend();
plt.show();

enter image description here


Tags: thetoimportdataplotasnpplt
1条回答
网友
1楼 · 发布于 2024-04-23 22:30:04

您只需将相关/感兴趣的值传递给fit,如下所示。您可以使用NumPy索引来只传递低于42000的x值。使用[xdatac<42000]将返回此条件所在的索引/位置True。其余代码保持不变。你知道吗

optimizedParameters, pcov = opt.curve_fit(func, xdatac[xdatac<42000],
                                          ydatac[xdatac<42000]);

这样,拟合最多只能在42000处执行,您以后仍可以通过传递完整的x数据来绘制拟合线。你知道吗

相关问题 更多 >