bundles / scipy 1.17.1 / scipy / spatial / distance / jensenshannon
function
scipy.spatial.distance:jensenshannon
source: /scipy/spatial/distance.py :1205
Signature
def jensenshannon ( p , q , base = None , * , axis = 0 , keepdims = False ) Summary
Compute the Jensen-Shannon distance (metric) between two probability arrays. This is the square root of the Jensen-Shannon divergence.
Extended Summary
The Jensen-Shannon distance between two probability vectors p and q is defined as,
where is the pointwise mean of and and is the Kullback-Leibler divergence.
This routine will normalize p and q if they don't sum to 1.0.
Parameters
p: (N,) array_likeleft probability vector
q: (N,) array_likeright probability vector
base: double, optionalthe base of the logarithm used to compute the output if not given, then the routine uses the default base of scipy.stats.entropy.
axis: int, optionalAxis along which the Jensen-Shannon distances are computed. The default is 0.
keepdims: bool, optionalIf this is set to
True, the reduced axes are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. Default is False.
Returns
js: double or ndarrayThe Jensen-Shannon distances between
pandqalong theaxis.
Notes
Examples
from scipy.spatial import distance import numpy as np✓
distance.jensenshannon([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], 2.0) distance.jensenshannon([1.0, 0.0], [0.5, 0.5]) distance.jensenshannon([1.0, 0.0, 0.0], [1.0, 0.0, 0.0])✗
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) b = np.array([[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]])✓
distance.jensenshannon(a, b, axis=0) distance.jensenshannon(a, b, axis=1)✗
Aliases
-
scipy.spatial.distance.jensenshannon