插入“缺失年份”在 JavaScript 中?
之前:
id year value
SE 1950 67
SE 1960 71
SE 1965 82
NO 1975 65
NO 1985 75
之后:
data : {
SE : {
data : {
1950 : 67,
1951 : 67.4,
1952 : 67.8,
[...]
1965 : 82
},
min_year : 1950,
max_year : 1965
}
NO : {
data : {
[...]
},
[...]
}
}
那么基本上,根据 JS 中的相邻值填充间隙/插值的最有效方法是什么?
Before:
id year value
SE 1950 67
SE 1960 71
SE 1965 82
NO 1975 65
NO 1985 75
After:
data : {
SE : {
data : {
1950 : 67,
1951 : 67.4,
1952 : 67.8,
[...]
1965 : 82
},
min_year : 1950,
max_year : 1965
}
NO : {
data : {
[...]
},
[...]
}
}
So basically, what is the most effective way of filling the gaps/interpolating based on adjacent values in JS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JS 作为一种语言没有任何工具可以直接帮助你。
您可以非常轻松地实现线性或多项式插值。多项式(比如 3 次)可能会在中间提供稍微更好的数字,尽管端点可能有问题 - 取决于数据。
线性插值更容易,尽管总的来说,我认为它不会提供像更高次多项式插值那样准确的估计。
另一种选择是样条线(三次相对容易),它对于您的目的来说足够准确。这可能有点矫枉过正,尽管也许不是——不确定这个要求的范围。
您可能会考虑在服务器端执行此操作,并使用实现这些插值函数的其他语言的许多库之一。这将为您提供一种真正准确且通用的方法来有效地解决问题,而无需您自己实现。
这很难说,因为准确度、数据范围和项目范围都会影响您需要什么类型的插值、是否可以在服务器/客户端完成等。
JS as a language has no tools to help you directly.
You can implement linear or polynomial interpolation quite easily. Polynomial (of degree say 3) will probably provide slightly nicer numbers in the middle, although the endpoints can be problematic - depends on the data.
Linear interpolation is easier although on the whole I would presume it wouldn't give as accurate an estimation as a higher degree polynomial interpolation.
An alternative could be splines (cubic are relatively easy) which will be more than accurate enough for your purposes. It might be slight overkill, although maybe not - not sure the scope of this requirement.
You might consider doing this server-side and using one of many libraries for other languages that implement these interpolatory functions. That would give you a really accurate and general way to solve the problem efficiently without having to implement it yourself.
It's difficult to say since the degree of accuracy, the range of data and the scope of the project all factor in to what type of interpolation you need, if it can be done server/client side, etc.
如果您只想要直线插值,则将
n_0
和n_1
作为您确实知道其值的索引:给定 SE 数据,例如,1960 年的插值将为:
即大约
76.5
If you just want straight linear interpolation, then given
n_0
andn_1
as indices for which you do know the values:Given your data for SE, for example, the interpolated value for 1960 would be:
i.e. about
76.5