python 中来自 matlab 的等效插值是多少?
What is the equvalent interpolation from matlab in python?
我想将代码从 matlab 重写到 python。在 matlab 中我有以下内容:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'next') % matlab
'next'指的是什么插值?通常默认是线性的。有一个 numpy 等价物吗?例如:
numpy.interp(x, xp, fp, left=None, right=None, period=None) # python
这是 'linear' 插值...
Which interpolation is meant by 'next'
? Usually by default is linear. Is there a numpy equivalent?
插值方法'next'
插值到数据集中的下一个数据点(参见:https://www.mathworks.com/help/matlab/ref/interp1.html)。
查看 NumPy 的文档 (https://numpy.org/doc/stable/reference/generated/numpy.interp.html),它们似乎使用线性插值,因此如果您想要相同的输出,您只需在 MATLAB 命令中指定它,如下所示:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'linear')
也就是说,'linear'
是 interp1
的默认插值方法,因此您也可以简单地忽略该参数并使用命令:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time)
希望对您有所帮助!
编辑:我刚刚意识到你问的是向后的问题,你想在 Python 中使用 'next' 方法进行插值。以下是我的做法:
import numpy as np
import scipy as sp
# Generate data
x = np.linspace(0, 1, 10)
y = np.exp(x)
# Create interpolation function
f = sp.interpolate.interp1d(x, y, 'next')
# Create resampled range
x_resampled = np.linspace(x[0], x[-1], 100)
# Here's your interpolated data
y_interpolated = f(x_resampled)
我想将代码从 matlab 重写到 python。在 matlab 中我有以下内容:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'next') % matlab
'next'指的是什么插值?通常默认是线性的。有一个 numpy 等价物吗?例如:
numpy.interp(x, xp, fp, left=None, right=None, period=None) # python
这是 'linear' 插值...
Which interpolation is meant by
'next'
? Usually by default is linear. Is there a numpy equivalent?
插值方法'next'
插值到数据集中的下一个数据点(参见:https://www.mathworks.com/help/matlab/ref/interp1.html)。
查看 NumPy 的文档 (https://numpy.org/doc/stable/reference/generated/numpy.interp.html),它们似乎使用线性插值,因此如果您想要相同的输出,您只需在 MATLAB 命令中指定它,如下所示:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time, 'linear')
也就是说,'linear'
是 interp1
的默认插值方法,因此您也可以简单地忽略该参数并使用命令:
interp1(TMP.time_hor, TMP.lane_hor, TMP.travel_time)
希望对您有所帮助!
编辑:我刚刚意识到你问的是向后的问题,你想在 Python 中使用 'next' 方法进行插值。以下是我的做法:
import numpy as np
import scipy as sp
# Generate data
x = np.linspace(0, 1, 10)
y = np.exp(x)
# Create interpolation function
f = sp.interpolate.interp1d(x, y, 'next')
# Create resampled range
x_resampled = np.linspace(x[0], x[-1], 100)
# Here's your interpolated data
y_interpolated = f(x_resampled)