NumPy最近邻线拟合跨移动窗口

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

NumPy Nearest Neighbor Line Fitting Across Moving Window

问题

我已经加载了两个二维数组到NumPy中,它们的大小都是80i x 80j。我想要在这些数组上进行一个移动窗口的多项式拟合计算,我已经确定如何进行多项式拟合,但卡在了具体的移动窗口方法上。我想要实现以下目标:

1) 在Array 1(a1)的每个索引处,代码会将该索引及其最近的8个邻居的所有值隔离到一个单独的一维数组中,然后在Array 2(a2)上重复相同的窗口操作。

2) 使用NumPy的polyfit执行线性回归线拟合,方法如下: model = np.polyfit(a1slice, a2slice, 1)

3) 将得到的回归系数和截距(手动执行示例输出为:array([-0.02114911, 10.02127152]))放入另外两个数组的相同索引位置,其中model[0] 放入第一个新数组,model[1] 放入第二个新数组的这个索引位置。

4) 代码然后顺序移动到下一个索引并再次执行步骤1-3,或者a1(i+1, j+0, 等等)。

我提供了一个图形示例,展示了我尝试实现的目标,针对Array 1中的两个随机索引选择以及在最近八个邻居索引上的计算。这样做可能更容易理解所期望的结果。

NumPy最近邻线拟合跨移动窗口

NumPy最近邻线拟合跨移动窗口

英文:

I have two two-dimensional arrays loaded into NumPy, both of which are 80i x 80j in size. I'm looking to do a moving window polyfit calculation across these arrays, I've nailed down how to conduct the polyfit but am stuck on the specific moving window approach I'm looking to accomplish. I'm aiming for:

1) At each index of Array 1 (a1), code isolates all the values of the index & its closest 8 neighbors into a separate 1D array, and repeats over the same window at Array 2 (a2)

2) With these two new arrays isolated, perform linear regression line-fitting using NumPy's polyfit in the following approach: model = np.polyfit(a1slice, a2slice, 1)

3) Cast the resulting regression coefficient and intercept (example output doing this manually: array([-0.02114911, 10.02127152])) to the same index of two other arrays, where model[0] would be placed into the first new array and model[1] into the second new array at this index.

4) The code then moves sequentially to the next index and performs steps 1-3 again, or a1(i+1, j+0, etc.)

I've provided a graphical example of what I'm trying achieve for two random index selections across Array 1 and the calculation across the index's eight nearest neighbors, should this make the desired result easier to understand:

NumPy最近邻线拟合跨移动窗口

NumPy最近邻线拟合跨移动窗口

答案1

得分: 1

我已经编写了一个函数来获取窗口,但存在一个麻烦的边缘情况,当索引位于边缘并距离角落一定距离时。您可能需要修改函数以精确获取您想要的内容。

def get_neighbors(arr, origin, num_neighbors=8):
    
    coords = np.array([[i, j] for (i, j), value in np.ndenumerate(arr)]).reshape(arr.shape + (2,))
    distances = np.linalg.norm(coords - origin, axis=-1)
    neighbor_limit = np.sort(distances.ravel())[num_neighbors]
    
    window = np.where(distances <= neighbor_limit)
    exclude_window = np.where(distances > neighbor_limit)
    
    return window, exclude_window, distances


test = np.zeros((5, 5))

plt.close('all')

cases = [[0, 0], [0, 1], [0, 2]]

for case in cases:
    
    window, exclude, distances = get_neighbors(test, case)
    distances[exclude] = 0
    
    plt.figure()
    plt.imshow(distances)

图像输出:

NumPy最近邻线拟合跨移动窗口
NumPy最近邻线拟合跨移动窗口
NumPy最近邻线拟合跨移动窗口

英文:

I've written a function to get the window, but there is a troublesome edge case when the index is on the edge and one distance away from the corner. You'll probably want to modify the function to get exactly what you're looking for.

def get_neighbors(arr, origin, num_neighbors = 8):

    coords = np.array([[i,j] for (i,j),value in np.ndenumerate(arr)]).reshape(arr.shape + (2,))
    distances = np.linalg.norm(coords - origin, axis = -1)
    neighbor_limit = np.sort(distances.ravel())[num_neighbors]

    window = np.where(distances &lt;= neighbor_limit)
    exclude_window = np.where(distances &gt; neighbor_limit)
    
    return window, exclude_window, distances


test = np.zeros((5, 5))

plt.close(&#39;all&#39;)

cases = [[0,0], [0, 1], [0, 2]]

for case in cases:
    
    window, exclude, distances = get_neighbors(test, case)
    distances[exclude] = 0
    
    plt.figure()
    plt.imshow(distances)

Image Outputs:

NumPy最近邻线拟合跨移动窗口
NumPy最近邻线拟合跨移动窗口
NumPy最近邻线拟合跨移动窗口

huangapple
  • 本文由 发表于 2023年2月13日 23:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438217.html
匿名

发表评论

匿名网友

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

确定