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

在numpy2d数组中寻找元素索引的最有效方法

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

    1000x1000 )只有几个因素与我有关。假设这些元素是 >1000

    目前,我有两种不同的方法,它们的复杂性应该是相同的(我省略了可能的解决方案) for

    import numpy as np
    A = np.zeros((1000,1000))
    #do something with the matrix
    
    #first solution with np.where
    np.where(A > 999).T
    # array([[0, 0],[1, 20]....[785, 445]], dtype=int64) - made up numbers 
    
    #another solution with np.argwhere
    np.argwhere(A > 999)
    # array([[0, 0],[1, 20]....[785, 445]], dtype=int64) - outputs the same
    

    有没有任何可能的方法来加速这个搜索或者我的解决方案是最有效的?

    谢谢你的建议和建议!

    1 回复  |  直到 4 年前
        1
  •  0
  •   Cyril FRANCOIS    4 年前

    你可以试试这个,过滤器直接包含在numpy数组中!

    import numpy as np
    
    arr = np.array([998, 999, 1000, 1001])
    
    filter_arr = arr > 999
    
    newarr = arr[filter_arr]
    
    print(filter_arr)
    print(newarr) 
    

    https://www.w3schools.com/python/numpy_array_filter.asp