使用 matplotlib 绘制的数据进行推断

发布于 2024-10-05 13:13:05 字数 165 浏览 3 评论 0原文

我的文件中有 10 个 x 和 y 值。

有什么方法可以推断图形,即使其成为连续函数并增加 matplotlib 中其他 x 值的范围?

如果有人能告诉我是否还有其他我可以使用的软件,我什至会很感激。我基本上希望这 10 个值近似为连续函数,以便我可以知道某个随机 x 点的 y 值。

I have 10 values of x and y in my file.

Is there any way that I can extrapolate the graph ie make it into a continous function and increasing its range for other x-values in matplotlib ??

I would even be thankful if anyone can tell me if there is any other software that I can use. I basically want that these 10 values get approximated to a continous function so that I can know the y-value at some random x point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-10-12 13:13:05

下面我使用Scipy,但是相同函数(polyvalpolyfit)也在NumPy< /em>; NumPy 是 Matplotlib 依赖项,因此如果您没有安装 SciPy,可以从那里导入这两个函数。

import numpy as NP
from scipy import polyval, polyfit
from matplotlib import pyplot as PLT

n=10   # 10 data points
# make up some data
x = NP.linspace(0, 1, n)
y = 7*x**2 - 5*x + 3
# add some noise 
noise = NP.random.normal(.5, .3, 10)
y += noise

# the shape of the data suggests a 2d polynomial, so begin there
# a, b, c are the polynomial coefficients: ax^2 + bx + c
a, b, c = polyfit(x, y, 2)
y_pred = polyval([a, b, c], x)    # y_pred refers to predicted values of y

# how good is the fit?
# calculate MSE:
MSE = NP.sqrt( NP.sum((y_pred-y)**2)/10 )
# MSE = .2

# now use the model polynomial to generate y values based on x values outside 
# the range of the original data:
x_out = NP.linspace(0, 2, 20)   # choose 20 points, 10 in, 10 outside original range
y_pred = polyval([a, b, c], x_out)

# now plot the original data points and the polynomial fit through them
fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.plot(x, y, 'g.', x_out, y_pred, 'b-' )

PLT.show()

替代文本

below i use Scipy, but the same functions (polyval and polyfit) are also in NumPy; NumPy is a Matplotlib dependency so you can import those two functions from there if you don't have SciPy installed.

import numpy as NP
from scipy import polyval, polyfit
from matplotlib import pyplot as PLT

n=10   # 10 data points
# make up some data
x = NP.linspace(0, 1, n)
y = 7*x**2 - 5*x + 3
# add some noise 
noise = NP.random.normal(.5, .3, 10)
y += noise

# the shape of the data suggests a 2d polynomial, so begin there
# a, b, c are the polynomial coefficients: ax^2 + bx + c
a, b, c = polyfit(x, y, 2)
y_pred = polyval([a, b, c], x)    # y_pred refers to predicted values of y

# how good is the fit?
# calculate MSE:
MSE = NP.sqrt( NP.sum((y_pred-y)**2)/10 )
# MSE = .2

# now use the model polynomial to generate y values based on x values outside 
# the range of the original data:
x_out = NP.linspace(0, 2, 20)   # choose 20 points, 10 in, 10 outside original range
y_pred = polyval([a, b, c], x_out)

# now plot the original data points and the polynomial fit through them
fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.plot(x, y, 'g.', x_out, y_pred, 'b-' )

PLT.show()

alt text

贪了杯 2024-10-12 13:13:05

如果您使用的是SciPy(科学Python),您可以尝试scipy.interp1d。有关示例,请参阅手册

否则,任何像样的电子表格软件都应该能够进行样条插值并为您提供一个漂亮的平滑图表。

不过,请注意外推。如果您没有良好的数据模型,则在推断输入范围之外时可能会得到完全不相关的数据。

示例(编辑):

from scipy.interpolate import interp1d

# the available data points
x = [1, 2, 3]
y = [10, 20, 30]

# return a function f, such that f(x) is the interpolated value at 'x'
f = interp1d(x, y, kind='cubic')

您现在可以在任意点 x 计算函数 f(x)。例如,print f(2.5) 将返回 x=2.5 的插值。

If you are using SciPy (Scientific Python) you can try scipy.interp1d. See the manual for an example.

Otherwise, any decent spreadsheet software should be able to do spline interpolation and give you a nice smooth graph.

Beware of extrapolation, though. If you don't have a good model for your data you might get completely unrelated data when extrapolating outside your input range.

Example (EDIT):

from scipy.interpolate import interp1d

# the available data points
x = [1, 2, 3]
y = [10, 20, 30]

# return a function f, such that f(x) is the interpolated value at 'x'
f = interp1d(x, y, kind='cubic')

You can now compute the function f(x) at any point x. For example print f(2.5) will return the interpolated value for x=2.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文