使用numpy进行平滑线性插值

wbrvyc0a  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(193)

bounty将在11小时后到期。此问题的答案有资格获得+50声望奖励。carlitador正在寻找来自可靠来源的**答案 *:

这是我想解决的一个重要问题
我需要重新编码一个等价的numpy interp函数。为了做到这一点,我想建立一个成对距离矩阵,然后使用这个矩阵来定义“权重”,我用它来乘以目标值。

  1. def interp_diff(x, xp, fp):
  2. dxp = np.diff(xp)
  3. dxp = np.concatenate(([dxp[0]], dxp))
  4. pairwise_distance = np.abs(x[:, np.newaxis] - xp) # Calculate the absolute differences between x and xp
  5. # Add a small epsilon to pairwise distances to avoid division by zero
  6. epsilon = 1e-8
  7. pairwise_distance += epsilon
  8. weights = 1 / pairwise_distance # Calculate the weights based on the differences
  9. # Normalize the weights
  10. weights /= np.sum(weights, axis=1)[:, np.newaxis]
  11. # Apply hardness threshold to the weights
  12. weights = np.where(weights > 0, weights, 0)
  13. return np.dot(weights, fp)

当xp值放置在规则栅格上时,此操作会产生预期结果,但如果xp值的间距不均匀,则此操作不起作用。有没有人有一个想法,使这一工作的任何间距xp?我的限制是我不能使用索引相关的方法(argsort,argwhere,searchsorted等...)。这就是为什么它有点挑战性
常规网格上的用法示例:

  1. x_np = np.linspace(0,5,4)
  2. y_np = np.sin(x_np)
  3. x_i = x_np
  4. x_i = np.linspace(0,5,10)
  5. y_i = interp_diff(x_i,xp=x_np,fp=y_np)
  6. ax = plt.figure().gca()
  7. ax.scatter(x_np,y_np)
  8. ax.scatter(x_i,y_i,marker='+',s=30,alpha=0.7)
  9. ax.plot(x_i,y_i)
  10. plt.show()

产生预期结果:

但是,切换到非均匀间隔网格:

  1. def sinspace(start,stop,num):
  2. ones = 0 * start + 1
  3. return start + (stop - start) * (1 - np.cos(np.linspace(
  4. 0 * ones,
  5. np.pi / 2 * ones,
  6. num
  7. )))
  8. x_np = sinspace(0,5,4)
  9. y_np = np.sin(x_np)
  10. x_i = x_np
  11. x_i = np.linspace(0,5,10)
  12. y_i = interp_diff(x_i,xp=x_np,fp=y_np)
  13. ax = plt.figure().gca()
  14. ax.scatter(x_np,y_np)
  15. ax.scatter(x_i,y_i,marker='+',s=30,alpha=0.7)
  16. ax.plot(x_i,y_i)
  17. plt.show()

这就是我得到的

谢谢

r6hnlfcb

r6hnlfcb1#

我无法用你提供的代码的稍微修改版本复制你的结果:

  1. import numpy
  2. from matplotlib import pyplot
  3. def interp_diff(x, xp, fp):
  4. dxp = numpy.diff(xp)
  5. dxp = numpy.concatenate(([dxp[0]], dxp))
  6. pairwise_distance = numpy.abs(x[:, numpy.newaxis] - xp) # Calculate the absolute differences between x and xp
  7. # Add a small epsilon to pairwise distances to avoid division by zero
  8. epsilon = 1e-8
  9. pairwise_distance += epsilon
  10. weights = 1 / pairwise_distance # Calculate the weights based on the differences
  11. # Normalize the weights
  12. weights /= numpy.sum(weights, axis=1)[:, numpy.newaxis]
  13. # Apply hardness threshold to the weights
  14. weights = numpy.where(weights > 0, weights, 0)
  15. return numpy.dot(weights, fp)
  16. # 1 : 1 grid
  17. x_np = numpy.linspace(0,5,4)
  18. y_np = numpy.sin(x_np)
  19. x_i = x_np
  20. y_i = interp_diff(x_i,xp=x_np,fp=y_np)
  21. ax = pyplot.figure().gca()
  22. ax.scatter(x_np,y_np)
  23. ax.scatter(x_i,y_i,marker='+',s=30,alpha=0.7)
  24. ax.plot(x_i,y_i)
  25. pyplot.show()

  1. # 1 : 2.5 grid
  2. x_np = numpy.linspace(0,5,4)
  3. y_np = numpy.sin(x_np)
  4. x_i = numpy.linspace(0,5,10)
  5. y_i = interp_diff(x_i,xp=x_np,fp=y_np)
  6. ax = pyplot.figure().gca()
  7. ax.scatter(x_np,y_np)
  8. ax.scatter(x_i,y_i,marker='+',s=30,alpha=0.7)
  9. ax.plot(x_i,y_i)
  10. pyplot.show()

您使用的是哪个版本的numpymatplotlib库?

展开查看全部

相关问题