九州编程
numpy.interp正是您要寻找的。它采用表格中的函数,即一组 (x,y) 并计算任何新 x 的线性插值。考虑以下产生所需结果的代码:import numpy as npa = np.array([[1 , 0.1], [3 , 0.2], [5 , 0.4]])# a requested range - we fill in missing integer values.new_x = range(1,6)# perform interpolation. Origina array is sliced by columns.new_y = np.interp(new_x, a[:, 0], a[:, 1])# target array is zipped together from interpolated x a ynp.array(list(zip(new_x, new_y))).tolist()[[1.0, 0.1], [2.0, 0.15000000000000002], [3.0, 0.2], [4.0, 0.30000000000000004], [5.0, 0.4]]
HUX布斯
您可以使用np.interp。作为一个简单的例子:import numpy as npx = np.array([0,1,2,4])y = np.array([0.1,0.2,0.3,0.6])# do the linear interpolationnew_x = np.arange(0,5,1)new_y = np.interp(new_x, x, y)这给出了一个new_y:[0.1,0.2,0.3,0.45,0.6]