How to calculate the Cholesky decomposition of a non-square matrix in order to calculate the Mahalanobis distance using `numpy`?

How to calculate the Cholesky decomposition of a non-square matrix in order to calculate the Mahalanobis distance with numpy?

def get_fitting_function(G):
    print(G.shape) #(14L, 11L) --> 14 samples of dimension 11
    g_mu = G.mean(axis=0) 
    #Cholesky decomposition uses half of the operations as LU
    #and is numerically more stable.
    L = np.linalg.cholesky(G)

    def fitting_function(g):
        x = g - g_mu
        z = np.linalg.solve(L, x)
        #Mahalanobis Distance 
        MD = z.T*z
        return math.sqrt(MD)
    return fitting_function  


C:\Users\Matthias\CV\src\fitting_function.py in get_fitting_function(G)
     22     #Cholesky decomposition uses half of the operations as LU
     23     #and is numerically more stable.
---> 24     L = np.linalg.cholesky(G)
     25 
     26     def fitting_function(g):

C:\Users\Matthias\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\linalg\linalg.pyc in cholesky(a)
    598     a, wrap = _makearray(a)
    599     _assertRankAtLeast2(a)
--> 600     _assertNdSquareness(a)
    601     t, result_t = _commonType(a)
    602     signature = 'D->D' if isComplexType(t) else 'd->d'

C:\Users\Matthias\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\linalg\linalg.pyc in _assertNdSquareness(*arrays)
    210     for a in arrays:
    211         if max(a.shape[-2:]) != min(a.shape[-2:]):
--> 212             raise LinAlgError('Last 2 dimensions of the array must be square')
    213 
    214 def _assertFinite(*arrays):

LinAlgError: Last 2 dimensions of the array must be square 


    LinAlgError: Last 2 dimensions of the array must be square 

Based on Matlab implementation: Mahalanobis distance modifying the covariance matrix

Edit: chol(a) = linalg.cholesky(a).T cholesky factorization of the matrix ( chol(a)in matlab returns the upper triangular matrix, but linalg.cholesky(a)returns the lower triangular matrix) (source: http://wiki.scipy.org/NumPy_for_Matlab_Users )

Edit2:

G -= G.mean(axis=0)[None, :]
C = (np.dot(G, G.T) / float(G.shape[0]))
#Cholesky decomposition uses half of the operations as LU
#and is numerically more stable.
L = np.linalg.cholesky(C).T

therefore, if D = x ^ tS ^ -1.x = x ^ t. (LL ^ t) ^ - 1.x = x ^ tLL ^ tx = z ^ tz

+4
1

, . , , . LU, , L = U '. , . . Wikipedia.

, , cholesky .

: , C=np.dot(G, G.T) , , , - . C = ( C + C.T) /2.0 chol(C).

+1

Source: https://habr.com/ru/post/1540754/


All Articles