Make numpy matrix more sparse

前端 未结 2 402
栀梦
栀梦 2021-01-23 02:25

Suppose I have a numpy array

np.array([
    [3, 0, 5, 3, 0, 1],
    [0, 1, 2, 1, 5, 2],
    [4, 3, 5, 3, 1, 4],
    [2, 5, 2, 5, 3, 1],
    [0, 1, 2, 1, 5, 2],
]         


        
2条回答
  •  情歌与酒
    2021-01-23 02:39

    We can use np.random.choice(..., replace=False) to randomly select a number of unique non-zero flattened indices and then simply index and reset those in the input array.

    Thus, one solution would be -

    def make_more_sparsey(a, n):
        # a is input array
        # n is number of non-zero elements to be reset to zero
        idx = np.flatnonzero(a) # for performance, use np.flatnonzero(a!=0)
        np.put(a, np.random.choice(idx, n, replace=False),0)
        return a
    

    Sample run -

    In [204]: R = np.array([
         ...:     [3, 0, 5, 3, 0, 1],
         ...:     [0, 1, 2, 1, 5, 2],
         ...:     [4, 3, 5, 3, 1, 4],
         ...:     [2, 5, 2, 5, 3, 1],
         ...:     [0, 1, 2, 1, 5, 2],
         ...: ])
    
    In [205]: make_more_sparsey(R, n=5)
    Out[205]: 
    array([[3, 0, 5, 3, 0, 1],
           [0, 1, 0, 0, 5, 2],
           [4, 3, 5, 3, 1, 4],
           [2, 5, 0, 5, 3, 1],
           [0, 1, 0, 1, 0, 2]])
    

提交回复
热议问题