使用 Numpy 和 Scipy 进行一维三次插值不等数组
我需要对多个大型数组执行三次一维插值。我通常会使用 pandas,但我很难使用 pandas 来重复 x 值。据我了解,这可以使用 numpy 来实现。但是,我很难正确设置阵列。对于每个 x 值,有 50,000 个 y 值。我不能重复这个例子。下面的示例适用于较小的值,但我希望将其扩展到更大的值。
插值的第一个条目是 (x1, (y1,y0)),最后一个条目是 (x4,(y4,y10))。首先,我认为我没有正确设置数组,其次,我认为我没有正确使用插值函数。
import numpy as np
from scipy import interpolate
# setting up x-arrays
x1 = [10] * 10
x2 = [20] * 10
x3 = [30] * 10
x4 = [40] * 10
x_array_tuple = (x1,x2,x3,x4)
x_arrays = np. vstack(x_array_tuple)
# import y-arrays
y1 = [0,1,2,3,4,5,6,7,8,9]
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [0,1,2,3,4,5,6,7,8,9]
y4 = [0,1,2,3,4,5,6,7,8,9]
y_array_tuple = (y1,y2,y3,y4)
y_arrays = np. vstack(y_array_tuple)
# interpolation
i_func = interpolate.interp1d(x_arrays,y_arrays, kind='cubic', fill_value='extrapolation') # the first entry is (x1, (y1,y0)); the last entry is (x4,(y4,y10))
这是我收到的错误消息:
raise ValueError("x 和 y 数组沿长度必须相等"
ValueError: x 和 y 数组沿插值轴的长度必须相等。
如何执行此插值?
I need to perform a cubic 1D interpolation over a number of large arrays. I would normally use pandas but I am struggling to use pandas for repeating x-values. From what I understand this can be achieved using numpy. However, I am struggling to setup the arrays correctly. For each x-value there are 50,000 y-values. I cannot repeating this as an example. The example below is for less values but I am hoping to extend it to greater values.
The first entry for the interpolation is (x1, (y1,y0)) and the last entry is (x4,(y4,y10)). Firstly, I do not think I have set the arrays up correctly and secondly, I do not think I am using the interpolation function correctly.
import numpy as np
from scipy import interpolate
# setting up x-arrays
x1 = [10] * 10
x2 = [20] * 10
x3 = [30] * 10
x4 = [40] * 10
x_array_tuple = (x1,x2,x3,x4)
x_arrays = np. vstack(x_array_tuple)
# import y-arrays
y1 = [0,1,2,3,4,5,6,7,8,9]
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [0,1,2,3,4,5,6,7,8,9]
y4 = [0,1,2,3,4,5,6,7,8,9]
y_array_tuple = (y1,y2,y3,y4)
y_arrays = np. vstack(y_array_tuple)
# interpolation
i_func = interpolate.interp1d(x_arrays,y_arrays, kind='cubic', fill_value='extrapolation') # the first entry is (x1, (y1,y0)); the last entry is (x4,(y4,y10))
This is the error message I get:
raise ValueError("x and y arrays must be equal in length along "
ValueError: x and y arrays must be equal in length along interpolation axis.
How do I perform this interpolation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论