[英]lagrange interpolation Python
I am trying to use scipy.interpolate.lagrange
to interpolate the following data, which is of degree 8: 我正在尝试使用
scipy.interpolate.lagrange
插值以下数据,其程度为8:
x_data = [1900., 1910., 1920., 1930., 1940., 1950., 1960., 1970., 1980.]
y_data = [76212168., 92228496., 106021537., 123202624., 132164569., 151325798., 179323175., 203302031., 226542199.]
with python code: poly = scipy.interpolate.lagrange(x_data, y_data)
使用python代码:
poly = scipy.interpolate.lagrange(x_data, y_data)
But the output looks not correct because even none of the (x_data[i], y_data[i])
pairs lies on the 'poly' I got from the scipy.interpolate.lagrange
call. 但是输出看起来不正确,因为
(x_data[i], y_data[i])
对甚至都不位于我从scipy.interpolate.lagrange
调用中获得的“ poly”上。
Could anybody give any hints or suggestion? 有人可以提供任何提示或建议吗? Thanks so much.
非常感谢。
Your values are poorly scaled, and, as the lagrange
docstring says, "Warning: This implementation is numerically unstable." 您的值缩放比例很差,并且,正如
lagrange
文档字符串所说,“警告:此实现在数值上是不稳定的。” Try applying lagrange
to, say, the "whitened" data (ie shift and scale the data to have mean 0 and standard deviation 1). 尝试将
lagrange
应用于“变白”的数据(即,对数据进行移位和缩放以使其平均值为0,标准差为1)。 For example, 例如,
xm = np.mean(x_data)
xscale = np.std(x_data)
ym = np.mean(y_data)
yscale = np.std(y_data)
x = (x_data - xm) / xscale
y = (y_data - ym) / yscale
poly = scipy.interpolate.lagrange(x, y)
( np
comes from import numpy as np
.) (
np
来自import numpy as np
。)
Then to use poly
on "raw" (ie unscaled) data, use the same transform on the x
input when you call poly
, and undo the y
transform on the values returned by poly
. 然后用
poly
对“生”(即未缩放)数据,使用在相同的转换x
输入,当你调用poly
,并撤消y
对返回的值转换poly
。 Eg if xx
is an array with values in the interval [1900, 1980]: 例如,如果
xx
是一个值在[1900,1980]之间的数组:
yy = poly((xx - xm)/xscale)*yscale + ym
Before you spend too much time on this, though, I have to ask: Why are you using Lagrange interpolation? 但是,在花太多时间之前,我必须问:为什么要使用Lagrange插值? It is an important theoretical tool, but it is not so good for practical data analysis (see http://en.wikipedia.org/wiki/Lagrange_polynomial#Notes ; in particular, note the occurrence of Runge's phenomenon).
它是一种重要的理论工具,但对实际数据分析却不太好(请参阅http://en.wikipedia.org/wiki/Lagrange_polynomial#Notes ;尤其要注意Runge现象的发生)。 Why do you need to interpolate at all?
为什么根本需要插值? What are you going to do with the interpolator?
您要如何使用插补器? If you have answers to those questions, you should include them as part of the question.
如果您对这些问题有答案,则应将其作为问题的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.