[GRASS-dev] Compute mahalanobis distance using Scipy

Moritz Lennert mlennert at club.worldonline.be
Fri Jan 30 01:59:50 PST 2015


On 29/01/15 18:30, Paulo van Breugel wrote:
> I would like to compute a raster layer with for each raster cell the
> mahalanobis distance to the centre of the environmental space// formed
> by all reference data points (raster cells). In R this can be done as
> explained here [1].
>
> . I would like to do this using python only (no dependency on R). I came
> up with the following, which works, but is very slow. I guess this is
> because it loops over every raster cell to compute the mahalanobis
> distance? Any idea how this can be done faster (I am very new to python,
> so bear with me if I am making stupid mistakes)

There's probably ways to accelerate this in Python (maybe you can try 
rewriting your for-loops as map() calls), but on the Wikipedia page on 
mahalanobis distance that you reference [1], it says that:

"Along each principal component axis, it measures the number of standard 
deviations from P to the mean of D. If each of these axes is rescaled to 
have unit variance, then Mahalanobis distance corresponds to standard 
Euclidean distance in the transformed space."

Couldn't you use i.pca to calculate principal components and then 
calculate distances of points in that space ?

Just brainstorming...


Moritz


[1] http://en.wikipedia.org/wiki/Mahalanobis_distance



More information about the grass-dev mailing list