我对python(比方说一周)和numpy/scipy是个新手,但对编程一点也不陌生,但我想知道如何正确地完成以下操作(最好是使用numpy/scipy):
假设我有一个150x200ndarray的浮点值。我想插一条从20:40到100:150的线,中间有500个点。
使用以下命令获取x:y插值:
xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)但现在我如何使用numpy/scipy获得插值的值(仅在该行上)?
PS I使用python3
发布于 2016-01-27 23:25:36
查看Scipy.interpolate
import numpy as np
from scipy.interpolate import interp1d
xValues = numpy.linspace(20,100,500)
yValues = numpy.linspace(40,150,500)
f = interpolate.interp1d(x, y)
xnew = np.arange(20, 30, 0.1)
ynew = f(xnew)发布于 2016-01-28 16:08:58
我目前是这样做的:
我想知道我这样做是否正确:
def get_interpolated_slice(self, grid_start_x, grid_start_y, grid_end_x, grid_end_y, resolution=100):
x = np.arange(self.z_data.shape[1])
y = np.arange(self.z_data.shape[0])
interpolated_z_ft = interpolate.interp2d(x, y, self.z_data)
xvalues = np.linspace(grid_start_x, grid_end_x, resolution)
yvalues = np.linspace(grid_start_y, grid_end_y, resolution)
interpolated_y_values = interpolated_y_m(xvalues, yvalues)
z_to_return = [None] * resolution
for i in range(resolution):
z_to_return[i] = interpolated_z_values[i][i]
return z_to_return那么我该做什么呢:我对2个点之间的范围完全插值2维数组(矩阵)。因为它将是一个分辨率X分辨率网格,所以我可以在矩阵中进行一次对角线‘遍历’,这将导致a线。
有人能确认这是否正确和/或这是正确的方法吗?
https://stackoverflow.com/questions/35041020
复制相似问题