使用 Python 沿列插入二维矩阵

Interpolate 2D matrix along columns using Python

我正在尝试将维度为 (5, 3) 的 2D numpy 矩阵沿轴 1(列)插值到维度为 (7, 3) 的矩阵。显然,错误的做法是在原始矩阵之间的任意位置随机插入行,请参见以下示例:

Source:
 [[0, 1, 1]
  [0, 2, 0]
  [0, 3, 1]
  [0, 4, 0]
  [0, 5, 1]]

Target (terrible interpolation -> not wanted!):
 [[0, 1, 1]
  [0, 1.5, 0.5]
  [0, 2, 0]
  [0, 3, 1]
  [0, 3.5, 0.5]
  [0, 4, 0]
  [0, 5, 1]]

正确的方法是考虑每一行并在所有行之间进行插值以将源矩阵扩展为 (7, 3) 矩阵。我知道 scipy.interpolate.interp1d 或 scipy.interpolate.interp2d 方法,但无法使其与其他 Stack Overflow 帖子或网站一起使用。我希望收到任何类型的提示或技巧。

更新 #1:预期值应等距。

更新 #2: 我想要做的基本上是使用原始矩阵的单独列,将列的长度扩展到 7 并在原始列的值之间进行插值。请参阅以下示例:

Source:
 [[0, 1, 1]
  [0, 2, 0]
  [0, 3, 1]
  [0, 4, 0]
  [0, 5, 1]]

Split into 3 separate Columns:
 [0    [1    [1
  0     2     0
  0     3     1
  0     4     0
  0]    5]    1] 

Expand length to 7 and interpolate between them, example for second column:
 [1
  1.66
  2.33
  3
  3.66
  4.33
  5]   

似乎每一列都可以完全独立地处理,但对于每一列,你需要基本上定义一个 "x" 坐标,以便你可以适合一些函数 "f(x)" 从中生成你的输出矩阵。 除非矩阵中的行与其他一些数据结构相关联(例如时间戳向量),否则一组明显的 x 值就是行号:

x = numpy.arange(0, Source.shape[0])

然后你可以构造一个插值函数:

fit = scipy.interpolate.interp1d(x, Source, axis=0)

并使用它来构建输出矩阵:

Target = fit(numpy.linspace(0, Source.shape[0]-1, 7)

产生:

array([[ 0.        ,  1.        ,  1.        ],
       [ 0.        ,  1.66666667,  0.33333333],
       [ 0.        ,  2.33333333,  0.33333333],
       [ 0.        ,  3.        ,  1.        ],
       [ 0.        ,  3.66666667,  0.33333333],
       [ 0.        ,  4.33333333,  0.33333333],
       [ 0.        ,  5.        ,  1.        ]])

默认情况下,scipy.interpolate.interp1d 使用分段线性插值。 scipy.interpolate 中有许多基于高阶多项式等的奇异选项。插值本身就是一个大话题,除非矩阵的行具有某些特定属性(例如,是信号的常规样本已知频率范围),可能没有 "truly correct" 插值方式。所以,某种程度上,插值方案的选择会有些随意。

您可以按如下方式进行:

from scipy.interpolate import interp1d
import numpy as np


a = np.array([[0, 1, 1],
               [0, 2, 0],
               [0, 3, 1],
               [0, 4, 0],
               [0, 5, 1]])

x = np.array(range(a.shape[0]))

# define new x range, we need 7 equally spaced values
xnew = np.linspace(x.min(), x.max(), 7)

# apply the interpolation to each column
f = interp1d(x, a, axis=0)

# get final result
print(f(xnew))

这将打印

[[ 0.          1.          1.        ]
 [ 0.          1.66666667  0.33333333]
 [ 0.          2.33333333  0.33333333]
 [ 0.          3.          1.        ]
 [ 0.          3.66666667  0.33333333]
 [ 0.          4.33333333  0.33333333]
 [ 0.          5.          1.        ]]