使用Python中的最近邻方法,根据给定的缩放因子来扩大2D数组

huangapple go评论56阅读模式
英文:

Enlarging 2D array with a given rescaling factor using nearest-neighbour in Python

问题

我正在尝试使用Python中的最近邻算法编写一个以给定缩放因子放大2D数组的代码。

例如,我有一个如下所示的数组:

[[1, 2],
[3, 4]]

我想要使用最近邻算法和给定的缩放因子来放大这个数组。

让我一步一步来解释。假设缩放因子是3。放大后的数组应该如下所示:

[[1, 0, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[3, 0, 0, 4, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]

填充空元素后,它应该如下所示:

[[1, 1, 2, 2, 2, 2],
[1, 1, 2, 2, 2, 2],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4]]

这就是输出应该看起来的样子。 (0,2) 的值是 2 而不是 1,因为它的最近邻是 (0,3) 处的 2,而不是 (0,0) 处的 1

如何实现这个?很容易创建以下类似的数组:

[[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2], 
[1, 1, 1, 2, 2, 2], 
[3, 3, 3, 4, 4, 4], 
[3, 3, 3, 4, 4, 4], 
[3, 3, 3, 4, 4, 4]]

但这不是我想要的。

英文:

I'm trying to write a code that enlarges 2D array with a given rescaling factor in Python using nearest-neighbour algorithm.

For example, I have an array that looks like below.

[[1, 2],
[3, 4]]

And what I want to do is enlarging this array with NN algorithm and a given rescaling factor.

Let me explain step by step. Let's assume that the rescaling factor is 3. The enlarged array should look like below:

[[1, 0, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[3, 0, 0, 4, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]

And after filling the empty elements, it should look like below.

[[1, 1, 2, 2, 2, 2],
[1, 1, 2, 2, 2, 2],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4],
[3, 3, 4, 4, 4, 4]]

This is what output should look like. (0,2) is 2 instead of 1 because its nearest neighbour is 2 at (0,3) not 1 at (0,0).

How can I achieve this?

It was easy to create an array like below:

[[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2], 
[1, 1, 1, 2, 2, 2], 
[3, 3, 3, 4, 4, 4], 
[3, 3, 3, 4, 4, 4], 
[3, 3, 3, 4, 4, 4]]

But It is not what I wanted.

答案1

得分: 0

首先需要创建填充数组,但我们将使用 np.nan 填充数组,以便进行下一步的插值。因为如果在填充之前已经有元素 0,那么当我们使用 0 计算 mask 时,这将导致错误的 mask。以下是填充函数:

def pad_data(arr, padlen):
    m, n = arr.shape
    out = np.empty((m * padlen, n * padlen)) * np.nan
    for i in range(m):
        for j in range(n):
            out[i * padlen, j * padlen] = arr[i, j]
    return out

然后,我们需要使用 scipy 中的 NearestNDInterpolator 进行最近邻插值。完整的代码如下:

import numpy as np
from scipy.interpolate import NearestNDInterpolator

def pad_data(arr, padlen):
    m, n = arr.shape
    out = np.empty((m * padlen, n * padlen)) * np.nan
    for i in range(m):
        for j in range(n):
            out[i * padlen, j * padlen] = arr[i, j]
    return out

arr = np.array([[1, 2], [3, 4]])
arr_pad = pad_data(arr, 3)

mask = np.where(~np.isnan(arr_pad))
interp = NearestNDInterpolator(np.transpose(mask), arr_pad[mask])
filled_data = interp(*np.indices(arr_pad.shape))
filled_data

这将给出以下结果:

array([[1., 1., 2., 2., 2., 2.],
       [1., 1., 2., 2., 2., 2.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.]])
英文:

First need to create the padded array, but we will pad the array with np.nan for the interpolation of the next step. Cause if you already have element 0 before padding, then when we calculate the mask with 0s, this will give us a wrong mask. Here is the function for padding :

def pad_data(arr,padlen):
    m,n = arr.shape
    out= np.empty((m*padlen, n*padlen)) * np.nan
    for i in range(m):
        for j in range(n):
            out[i*padlen, j*padlen] = arr[i,j]
    return out

Then we need to use the NearestNDInterpolator in scipy for the nearest interpolation. The full code as below:

import numpy as np
from scipy.interpolate import NearestNDInterpolator

def pad_data(arr,padlen):
    m,n = arr.shape
    out= np.empty((m*padlen, n*padlen)) * np.nan
    for i in range(m):
        for j in range(n):
            out[i*padlen, j*padlen] = arr[i,j]
    return out



arr = np.array([[1, 2],[3, 4]])
arr_pad = pad_data(arr,3)

mask = np.where(~np.isnan(arr_pad))
interp = NearestNDInterpolator(np.transpose(mask), arr_pad[mask])
filled_data = interp(*np.indices(arr_pad.shape))
filled_data

Gives you :

array([[1., 1., 2., 2., 2., 2.],
       [1., 1., 2., 2., 2., 2.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.],
       [3., 3., 4., 4., 4., 4.]])

huangapple
  • 本文由 发表于 2023年4月4日 17:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定