Alternative to Scipy mode function in Numpy?

后端 未结 2 926
遥遥无期
遥遥无期 2021-01-18 01:01

Is there another way in numpy to realize scipy.stats.mode function to get the most frequent values in ndarrays along axis?(without importing other modules) i.e.



        
2条回答
  •  无人及你
    2021-01-18 01:37

    The scipy.stats.mode function is defined with this code, which only relies on numpy:

    def mode(a, axis=0):
        scores = np.unique(np.ravel(a))       # get ALL unique values
        testshape = list(a.shape)
        testshape[axis] = 1
        oldmostfreq = np.zeros(testshape)
        oldcounts = np.zeros(testshape)
    
        for score in scores:
            template = (a == score)
            counts = np.expand_dims(np.sum(template, axis),axis)
            mostfrequent = np.where(counts > oldcounts, score, oldmostfreq)
            oldcounts = np.maximum(counts, oldcounts)
            oldmostfreq = mostfrequent
    
        return mostfrequent, oldcounts
    

    Source: https://github.com/scipy/scipy/blob/master/scipy/stats/stats.py#L609

提交回复
热议问题