代码之家  ›  专栏  ›  技术社区  ›  guyguyguy12345

检测多维数据中的空洞

  •  1
  • guyguyguy12345  · 技术社区  · 4 年前

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

    一个简单的例子:

    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])
    

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  1
  •   Ananda    4 年前

    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宽度和使用不同的阈值来获得更好的结果。