Jensen-Shannon Divergence

前端 未结 5 1497
醉话见心
醉话见心 2021-01-31 05:22

I have another question that I was hoping someone could help me with.

I\'m using the Jensen-Shannon-Divergence to measure the similarity between two probability distribu

5条回答
  •  忘了有多久
    2021-01-31 06:25

    Explicitly following the math in the Wikipedia article:

    def jsdiv(P, Q):
        """Compute the Jensen-Shannon divergence between two probability distributions.
    
        Input
        -----
        P, Q : array-like
            Probability distributions of equal length that sum to 1
        """
    
        def _kldiv(A, B):
            return np.sum([v for v in A * np.log2(A/B) if not np.isnan(v)])
    
        P = np.array(P)
        Q = np.array(Q)
    
        M = 0.5 * (P + Q)
    
        return 0.5 * (_kldiv(P, M) +_kldiv(Q, M))
    

提交回复
热议问题