gpt4 book ai didi

python - 如何从多项式拟合中排除值?

转载 作者:行者123 更新时间:2023-12-01 02:39:01 25 4
gpt4 key购买 nike

我给我的数据拟合了一个多项式,如图:enter image description here

使用脚本:

from scipy.optimize import curve_fit
import scipy.stats
from scipy import asarray as ar,exp

xdata = xvalues
ydata = yvalues

fittedParameters = numpy.polyfit(xdata, ydata + .00001005 , 3)
modelPredictions = numpy.polyval(fittedParameters, xdata)

axes.plot(xdata, ydata, '-')
xModel = numpy.linspace(min(xdata), max(xdata))
yModel = numpy.polyval(fittedParameters, xModel)

axes.plot(xModel, yModel)

我想从 3.4 到 3.55 um 中排除该区域。我怎么能在我的脚本中做到这一点?此外,我试图在原始 .fits 文件中删除 NaN。帮助将受到重视。

最佳答案

您可以屏蔽排除区域内的值,然后将此屏蔽应用于拟合函数

# Using random data here, since you haven't provided sample data
xdata = numpy.arange(3,4,0.01)
ydata = 2* numpy.random.rand(len(xdata)) + xdata

# Create mask (boolean array) of values outside of your exclusion region
mask = (xdata < 3.4) | (xdata > 3.55)

# Do the fit on all data (for comparison)
fittedParameters = numpy.polyfit(xdata, ydata + .00001005 , 3)
modelPredictions = numpy.polyval(fittedParameters, xdata)
xModel = numpy.linspace(min(xdata), max(xdata))
yModel = numpy.polyval(fittedParameters, xModel)

# Do the fit on the masked data (i.e. only that data, where mask == True)
fittedParameters1 = numpy.polyfit(xdata[mask], ydata[mask] + .00001005 , 3)
modelPredictions1 = numpy.polyval(fittedParameters1, xdata[mask])
xModel1 = numpy.linspace(min(xdata[mask]), max(xdata[mask]))
yModel1 = numpy.polyval(fittedParameters1, xModel1)

# Plot stuff
axes.plot(xdata, ydata, '-')
axes.plot(xModel, yModel) # orange
axes.plot(xModel1, yModel1) # green



enter image description here

绿色曲线现在与 3.4 < xdata 3.55 匹配排除在外。橙色曲线是没有排除的拟合(用于比较)

如果你想在你的 xdata 中排除可能的 nans您可以增强 masknumpy.isnan()功能如
# Create mask (boolean array) of values outside of your exclusion AND which ar not nan
xdata < 3.4) | (xdata > 3.55) & ~numpy.isnan(xdata)

关于python - 如何从多项式拟合中排除值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com