Fit a Gaussian to each of two feature sets and return the squared Frechet distance between the two Gaussians.
d2=∥μa−μb∥2+Tr(Σa)+Tr(Σb)−2Tr((ΣaΣb)1/2)Each row of features_a and features_b is one feature vector of length D. The mean μa is the average row of features_a, and Σa is its sample covariance with the unbiased divisor Na−1, where Na is the number of rows. The same definitions give μb and Σb from features_b. Tr is the matrix trace.
The product ΣaΣb is usually not symmetric, so compute the trace of its square root through the symmetric matrix below, which has the same eigenvalues λi.
Tr((ΣaΣb)1/2)=i∑λi,λi=eigenvalues of Σa1/2ΣbΣa1/2Here Σa1/2 is the symmetric square root of Σa. Return the value d2 as a Python float (a NumPy floating scalar is also accepted). A tiny negative result from rounding, such as -1e-8 for identical sets, is accepted.
Input: features_a = [[0.0,0.0],[1.0,0.0],[0.0,1.0],[1.0,1.0]], features_b = [[1.0,1.0],[3.0,1.0],[1.0,2.0],[3.0,2.0]]
Output: 3.583333
Explanation: Set a has mean (0.5, 0.5) and covariance diag(1/3, 1/3), set b has mean (2, 1.5) and covariance diag(4/3, 1/3). The mean term is 3.25, the traces add 7/3, and the cross term subtracts 2 times 1, giving about 3.583333.
Input: features_a = [[0.5,-1.0,2.0],[1.5,0.0,1.0],[-0.5,1.0,0.0],[1.0,2.0,-1.0]], features_b = [[0.5,-1.0,2.0],[1.5,0.0,1.0],[-0.5,1.0,0.0],[1.0,2.0,-1.0]]
Output: 0.0
Input: features_a = [[1.0],[2.0],[3.0]], features_b = [[0.0],[4.0],[8.0],[12.0],[16.0]]
Output: 64.350889
The function np.linalg.eigh returns the eigenvalues and orthonormal eigenvectors of a symmetric matrix, which is enough to build its symmetric square root.
Similar matrices share eigenvalues, so a symmetric matrix built from the two covariances can stand in for the non-symmetric product.
Pass rowvar=False to np.cov when each row is one observation.
np.cov returns a 0-d array when there is one feature, so np.atleast_2d keeps a 1 by 1 matrix.
Accepts: array
Accepts: array
Fit a Gaussian to each of two feature sets and return the squared Frechet distance between the two Gaussians.
d2=∥μa−μb∥2+Tr(Σa)+Tr(Σb)−2Tr((ΣaΣb)1/2)Each row of features_a and features_b is one feature vector of length D. The mean μa is the average row of features_a, and Σa is its sample covariance with the unbiased divisor Na−1, where Na is the number of rows. The same definitions give μb and Σb from features_b. Tr is the matrix trace.
The product ΣaΣb is usually not symmetric, so compute the trace of its square root through the symmetric matrix below, which has the same eigenvalues λi.
Tr((ΣaΣb)1/2)=i∑λi,λi=eigenvalues of Σa1/2ΣbΣa1/2Here Σa1/2 is the symmetric square root of Σa. Return the value d2 as a Python float (a NumPy floating scalar is also accepted). A tiny negative result from rounding, such as -1e-8 for identical sets, is accepted.
Input: features_a = [[0.0,0.0],[1.0,0.0],[0.0,1.0],[1.0,1.0]], features_b = [[1.0,1.0],[3.0,1.0],[1.0,2.0],[3.0,2.0]]
Output: 3.583333
Explanation: Set a has mean (0.5, 0.5) and covariance diag(1/3, 1/3), set b has mean (2, 1.5) and covariance diag(4/3, 1/3). The mean term is 3.25, the traces add 7/3, and the cross term subtracts 2 times 1, giving about 3.583333.
Input: features_a = [[0.5,-1.0,2.0],[1.5,0.0,1.0],[-0.5,1.0,0.0],[1.0,2.0,-1.0]], features_b = [[0.5,-1.0,2.0],[1.5,0.0,1.0],[-0.5,1.0,0.0],[1.0,2.0,-1.0]]
Output: 0.0
Input: features_a = [[1.0],[2.0],[3.0]], features_b = [[0.0],[4.0],[8.0],[12.0],[16.0]]
Output: 64.350889
The function np.linalg.eigh returns the eigenvalues and orthonormal eigenvectors of a symmetric matrix, which is enough to build its symmetric square root.
Similar matrices share eigenvalues, so a symmetric matrix built from the two covariances can stand in for the non-symmetric product.
Pass rowvar=False to np.cov when each row is one observation.
np.cov returns a 0-d array when there is one feature, so np.atleast_2d keeps a 1 by 1 matrix.
Accepts: array
Accepts: array