【问题标题】:Detect voids in the multidimensional data检测多维数据中的空隙
【发布时间】:2020-12-25 05:11:41
【问题描述】:

如何检测多维(包括一维案例)数据中的空白?通过检测我的意思是找到它们的边界。

一个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(-1,1,(500,2))
x = x[np.apply_along_axis(lambda t: np.linalg.norm(t) > 0.5, 1, x), :]
plt.scatter(x[:,0], x[:,1])

【问题讨论】:

    标签: python numpy pattern-matching computational-geometry


    【解决方案1】:

    一种简单的方法是使用直方图。

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.random.uniform(-1,1,(500,2))
    x = x[np.apply_along_axis(lambda t: np.linalg.norm(t) > 0.5, 1, x), :]
    
    bins, hist = np.histogramdd(x)
    
    th = 0
    axis0_M, axis0_m = hist[0][1:][np.bitwise_or.reduce(bins<=th, axis=1)][0], hist[0][1:][np.bitwise_or.reduce(bins<=th, axis=1)][-1]
    axis1_M, axis1_m = hist[1][1:][np.bitwise_or.reduce(bins<=th, axis=0)][0], hist[1][1:][np.bitwise_or.reduce(bins<=th, axis=0)][-1]
    
    plt.vlines(x=[axis0_M, axis0_m], ymin=-x[:, 0].max(), ymax=x[:, 0].max())
    plt.hlines(y=[axis1_M, axis1_m], xmin=-x[:, 1].max(), xmax=x[:, 1].max())
    
    plt.scatter(x[:,0], x[:,1])
    plt.show()
    

    您可以通过调整直方图的 bin 宽度并使用不同的阈值来获得更好的结果。

    【讨论】:

    • 这是一个很好的答案。
    • 有没有自动猜测垃圾箱大小的方法?
    • 我不确定,但您可以尝试根据邻域周围的点对之间的平均距离来计算它。这是我能想到的最直接的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2015-04-25
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多