Syntactic Confectionery Delight  
PerlMonks 
comment on 
( [id://3333] : superdoc . print w/replies, xml )  Need Help?? 
Many machine learning and data analysis tasks involve calculating distances between items. The Mahalanobis distance is a very popular distance because it is scale invariant. In this snippet, I present how to compute the Mahalanobis distance using the Perl Data Language. The inputs are two or three piddles (see comment below for a definition). The first piddle is a pdimensional vector. The second piddle could be either a pdimensional vector (when a third input is provided) or a matrix with N rows of pdimensional vectors. If the second piddle is a matrix, the distance is computed between the center of the second piddle and the first piddle (if only two inputs are provided, the second piddle is used to compute the covariance needed to determine the Mahalanobis distance). The third piddle, which is optional, represents the covariance matrix of the distribution from which the two other piddles were drawn. Note: to compute the covariance matrix, I use the snippet presented in Computing Covariance Matrices with PDL What are Piddles? They are a new data structure defined in the Perl Data Language. As indicated in RFC: Getting Started with PDL (the Perl Data Language): Piddles are numerical arrays stored in column major order (meaning that the fastest varying dimension represent the columns following computational convention rather than the rows as mathematicians prefer). Even though, piddles look like Perl arrays, they are not. Unlike Perl arrays, piddles are stored in consecutive memory locations facilitating the passing of piddles to the C and FORTRAN code that handles the element by element arithmetic. One more thing to note about piddles is that they are referenced with a leading $ Cheers,

Log In^{?}
Chatterbox^{?}
Other Users^{?}
Others rifling through the Monastery: (6)
As of 20240220 21:57 GMT
Sections^{?}
Information^{?}
Find Nodes^{?}
Leftovers^{?}
Voting Booth^{?}
My favourite way to spend a leap day ...
Results (21 votes). Check out past polls. 