One dimensional Mahalanobis Distance in Python

后端 未结 2 1103
梦毁少年i
梦毁少年i 2021-02-10 11:09

I\'ve been trying to validate my code to calculate Mahalanobis distance written in Python (and double check to compare the res

2条回答
  •  广开言路
    2021-02-10 11:31

    One-dimensional Mahalanobis distance is really easy to calculate manually:

    import numpy as np
    s = np.array([[20], [123], [113], [103], [123]])
    std = s.std()
    print np.abs(s[0] - s[1]) / std
    

    (reducing the formula to the one-dimensional case).

    But the problem with scipy.spatial.distance is that for some reason np.cov returns a scalar, i.e. a zero-dimensional array, when given a set of 1d variables. You want to pass in a 2d array:

    >>> covar = np.cov(s, rowvar=0)
    
    >>> covar.shape
    ()
    
    >>> invcovar = np.linalg.inv(covar.reshape((1,1)))
    
    >>> invcovar.shape
    (1, 1)
    
    >>> mahalanobis(s[0], s[1], invcovar)
    2.3674720531046645
    

提交回复
热议问题