python - 扩展二维数组并插入缺失值

标签 python interpolation

我有一个大小为 48x80 的数组,现在我想将数组扩展到一个大小为 117x192 的新数组。
我读过 scipy.interpolate , 但它没有提到扩展。

如何扩展数组并将值放入新数组?

例如: 给定数组 A [[1,2,3],[4,5,6],[7,8,9]]

[1 2 3]
[4 5 6]
[7 8 9]

现在我想将数组 A 扩展到数组 B,大小为 5x7

1 x 2 x 3
x x x x x
x x x x x
4 x 5 x 6
x x x x x
x x x x x
7 x 8 x 9

其中,用插值替换这些'x'。

示例 2: 在更一般的数组中

[4 2 6 4]
[4 34 6 2]
[2 11 3 4]
[2 4 22 4]
[2 1 35 255]
[1 3 4 54]
[22 1 4 5]

如果我想要一个大小为 20x30 的新数组,我该怎么办

更新: 我发现有一个不同之处使@nicoguaro 的回答在我的情况下不起作用:

他的解决方案:

pts = np.array([[i,j] for i in np.linspace(0,1,n) for j in np.linspace(0,1,m)] )
grid_x, grid_y = np.mgrid[0:1:m*2j, 0:1:n*2j]

我的解决方案:

pts = np.array([[i,j] for i in np.linspace(0,2*m-1,m) for j in np.linspace(0,2*n-1,n)] )
grid_x, grid_y = np.mgrid[0:m*2, 0:n*2]

这会导致不同的结果。事实上,他的解决方案在大多数情况下都有效,但我猜是 TIFF 文件

最佳答案

虽然 interpolate 没有用于此特定任务的函数,但您可以轻松地使用内置选项来完成它。使用您建议的相同示例

[1 2 3]
[4 5 6]
[7 8 9]

1 x 2 x 3
x x x x x
x x x x x
4 x 5 x 6
x x x x x
x x x x x
7 x 8 x 9

我们可以使用这段代码

import numpy as np
import scipy.interpolate as inter
import matplotlib.pyplot as plt

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
vals = np.reshape(A, (9))
pts = np.array([[i,j] for i in [0.0, 0.5, 1.0] for j in [0.0, 0.5, 1.0]] )
grid_x, grid_y = np.mgrid[0:1:7j, 0:1:5j]
grid_z = inter.griddata(pts, vals, (grid_x, grid_y), method='linear')

结果为

array([[ 1. ,  1.5,  2. ,  2.5,  3. ],
       [ 2. ,  2.5,  3. ,  3.5,  4. ],
       [ 3. ,  3.5,  4. ,  4.5,  5. ],
       [ 4. ,  4.5,  5. ,  5.5,  6. ],
       [ 5. ,  5.5,  6. ,  6.5,  7. ],
       [ 6. ,  6.5,  7. ,  7.5,  8. ],
       [ 7. ,  7.5,  8. ,  8.5,  9. ]])

或者,作为图像

Original image

Interpolated image

在这种情况下,我使用了 griddata 将在一组点 (pts) 上定义的集合函数 (vals) 插值到给定的直线网格(由 grid_xgrid_y 给出)。例如,如果您想对 $x$ 使用 nx 点,对 $y$ 使用 ny 点,则可以替换一行

grid_x, grid_y = np.mgrid[0:1:nx*1j, 0:1:ny*1j]

对于 nx=20ny=15 我们得到了这张图片

enter image description here

您可以在 documentation of the function 查看更多示例.

更新:包括示例2,其中矩阵为

A = np.array([[4, 2, 6, 4],
            [4, 34, 6, 2],
            [2, 11, 3, 4],
            [2, 4, 22, 4],
            [2, 1, 35, 255],
            [1, 3, 4, 54],
            [22, 1, 4, 5]])

和一个大小为 20x30 的新数组。代码如下

import numpy as np
import scipy.interpolate as inter
import matplotlib.pyplot as plt

A = np.array([[4, 2, 6, 4],
            [4, 34, 6, 2],
            [2, 11, 3, 4],
            [2, 4, 22, 4],
            [2, 1, 35, 255],
            [1, 3, 4, 54],
            [22, 1, 4, 5]])
vals = np.reshape(A, (28))
pts = np.array([[i,j] for i in np.linspace(0,1,4) for j in np.linspace(0,1,7)] )
grid_x, grid_y = np.mgrid[0:1:20j, 0:1:30j]
grid_z = inter.griddata(pts, vals, (grid_x, grid_y), method='linear')

plt.matshow(A)
plt.matshow(grid_z)
plt.show()

生成的图像是: Example 2 Interpolated matrix in example 2

关于python - 扩展二维数组并插入缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212122/

相关文章:

python - python3中有没有办法检查不止一件事?

Python 地理编码按距离过滤

python - 将 XYZ 文件中的不规则 3d 数据插值到规则网格

matlab - 不定大小-试图了解双三次插值

python - Pandas 使用其他不规则时间列表对不规则时间序列进行重新采样和插值

r - 如何将变量从一个数据帧插入到 R 中的另一个匹配时间

python - 使用 OpenCv python 库从内存中读取 base 64 编码图像

python - 算法逻辑——计算周日

python - 如何从当前应用程序运行独立应用程序?

matlab - 在圆形颜色图中插入阴影