程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Nearest neighbor filling substitution of Python matrix

編輯:Python

        Matrix nearest neighbor filling refers to replacing the value of the specified element in the matrix with the value of the surrounding nearest neighbor element . Here are three implementation methods . The first two methods are suitable for smaller input matrices , The third method is the fastest .

1 Nearest neighbor substitution nearest_fill

        use for The way of circulation , Calculate the distance between the position of the element to be replaced and the position of the remaining non replaced elements one by one , Select the element with the smallest distance as the replacement target . This method is used to adopt for The form of the cycle , So the video memory takes up less .mei The function is defined as follows :
   def nearest_fill(x, a, b):
    """
    Parameters
    ----------
    x : array, Input matrix
    a : float, The lower value limit of the element to be replaced
    b : float, The upper value limit of the element to be replaced
    Returns
    -------
    x : array, Matrix after replacement
    """
    x = x.copy()
    index_r = np.array(np.where((x>=a) & (x <=b))).T
    index_t = np.array(np.where((x<a) | (x >b))).T
    for ir in index_r:
        d = (ir - index_t) ** 2
        d = d[:, 0] + d[:, 1]
        p = index_t[np.argmin(d)]
        x[ir[0], ir[1]] = x[p[0], p[1]]
    return x

2  Nearest neighbor substitution nearest_fill_batch

         In the form of a matrix , Calculate the distance between the position of the element to be replaced and the position of the remaining non replaced elements one by one , Select the element with the smallest distance as the replacement target . This method stores all the distance matrices , Occupy a large amount of memory or video memory . Can be set by batch To reduce memory or video memory usage . The function is defined as follows :
    x : array, Input matrix
    a : float, The lower value limit of the element to be replaced
    b : float, The upper value limit of the element to be replaced
    batch: Set the maximum number of padding for a distance matrix operation , Set a smaller value to make the memory or video memory smaller

def nearest_fill_batch(x, a, b, batch=4):
"""
Parameters
----------
x : array, Input matrix
a : float, The lower value limit of the element to be replaced
b : float, The upper value limit of the element to be replaced
batch: Set the maximum number of padding for a distance matrix operation , Set a smaller value to make the memory or video memory smaller
Returns
-------
x : array, Matrix after replacement
"""
x = x.copy()
index_r = np.array(np.where((x>=a) & (x <=b))).T
index_t = np.array(np.where((x<a) | (x >b))).T
index_r1 = np.expand_dims(index_r, 1)
N = index_r.shape[0] // batch
for i in range(N):
index_r2 = index_r1[i*batch:(i+1)*batch]
distances = index_r2 - index_t
distances = distances ** 2
distances = np.sum(distances, -1)
pos = np.argmin(distances, 1)
x[index_r2[:, 0, 0], index_r2[:, 0, 1]] = x[index_t[pos][:, 0], index_t[pos][:, 1]]
if index_r.shape[0] % batch > 0:
index_r2 = index_r1[(index_r.shape[0]- index_r.shape[0] % batch):]
distances = index_r2 - index_t
distances = distances ** 2
distances = np.sum(distances, -1)
pos = np.argmin(distances, 1)
x[index_r2[:, 0, 0], index_r2[:, 0, 1]] = x[index_t[pos][:, 0], index_t[pos][:, 1]]
return x

3 Nearest neighbor substitution nearest_fill_fast

        The first two methods calculate the distance between each element to be replaced and all remaining non replacement elements , Large amount of computation , And the memory or video memory is large . This method is suitable for the replacement of matrix elements with smaller dimensions . For matrices with larger dimensions , The calculation time of the above two methods will also increase significantly . The third method , Only the distance matrix of several elements with small position distance is calculated , The running speed is greatly improved . The function is defined as follows :
    x : array, Input matrix
    a : float, The lower value limit of the element to be replaced
    b : float, The upper value limit of the element to be replaced

def nearest_fill_fast(x, a, b):
"""
Parameters
----------
x : array, Input matrix
a : float, The lower value limit of the element to be replaced
b : float, The upper value limit of the element to be replaced
Returns
-------
y : array, Matrix after replacement
"""
y = x.copy()
h, w = x.shape[:2]
index_r = np.array(np.where((x>=a) & (x <=b))).T
index_t = np.array(np.where((x<a) | (x >b))).T
for ir in index_r:
i, j = ir
for k in range(3, max(w, h)):
kernel = [k, k]
x1 = max(j-kernel[0]//2, 0)
x2 = min(x1 + kernel[0], w)
y1 = max(i-kernel[1]//2, 0)
y2 = min(y1 + kernel[1], h)
x_tmp = x[y1:y2, x1:x2]
index_t = np.array(np.where((x_tmp<a) | (x_tmp >b))).T
if len(index_t) < 1:
continue
index_t += [y1, x1]
d = (ir - index_t) ** 2
d = d[:, 0] + d[:, 1]
p = index_t[np.argmin(d)]
y[ir[0], ir[1]] = x[p[0], p[1]]
break
return y

4 test result

Time consuming test results :

  More 3D 、 Please pay attention to two-dimensional perception algorithm and financial quantitative analysis algorithm “ Lele perception school ” WeChat official account , And will continue to update .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved