1
$\begingroup$

I'm trying to make a minimum Mahalanobis distance classifier. I have two groups of data.

The goal is to take one of the datasets in either group, calculate the Mahalanobis distance from both of the groups' means. The group with the smallest distance is the one that the dataset belongs to.

Details about what the data is. S0-S24 below are scans of a material. Each scan has 114 data points along the material collected by a sensor.

When a new scan is collected I need to compute the Mahalanobis distance of that new scan from each group's mean scan to determine which group it most likely belongs to.

I'm trying to figure out what are the variables and what are the observations to use for the covariance calculation.

In my head, each group is one variable. This is because each scan is an observation of a group/variable. Which makes 2 variables (2 groups) with 25 observations of each variable and each observations has 114 data points. I want to see how a scan with an unknown group compares with each group. Then the lowest Mahalanobis distance is the group it belongs to. Does the Mahalanobis equation handle this 3D data [2x25x114]?

Questions

1) What are the variables and what are the observations in my case?

2) In wiki the equation is this with the description. enter image description here X would be one observation (which has 114 data points) and U is the mean of the group I'm comparing X to which also has 114 points. To me this reads that S is the covariance matrix of the "set of observations" of the group I'm comparing to. Or is S the covariance matrix of the variables that the observations are of?

3) For the classifier here is a good form of my question, enter image description here

If X is S0 below, and the classes are group 1 and group 2 below, what are C1 and M1 and what are C2 and M2?

#Group 1 (Variable 1?)
#25 observations with 114 data points each
S0 = [131, 133, 123, 123, 133, 135, 137, 136, 136, 140, 139, 137, 136, 134, 135, 136, 136, 137, 135, 133, 135, 136, 136, 139, 137, 132, 115, 80, 69, 95, 91, 60, 70, 94, 99, 79, 55, 71, 93, 108, 109, 107, 105, 102, 102, 107, 113, 116, 116, 118, 120, 115, 109, 110, 107, 91, 88, 101, 102, 105, 106, 101, 98, 100, 104, 105, 104, 103, 99, 88, 83, 86, 86, 88, 89, 99, 100, 98, 100, 99, 94, 90, 91, 99, 103, 103, 101, 96, 88, 81, 75, 69, 72, 82, 82, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S1 = [127, 134, 125, 119, 126, 133, 134, 137, 140, 140, 140, 139, 136, 135, 135, 135, 136, 136, 135, 134, 134, 135, 135, 136, 136, 136, 134, 127, 103, 68, 75, 92, 79, 57, 74, 99, 97, 71, 57, 82, 105, 105, 103, 103, 101, 101, 105, 112, 119, 120, 121, 118, 111, 105, 109, 106, 89, 89, 101, 101, 105, 105, 100, 97, 101, 105, 105, 105, 103, 99, 89, 83, 85, 86, 87, 88, 96, 102, 99, 100, 101, 98, 91, 91, 96, 101, 103, 103, 99, 95, 88, 81, 74, 72, 78, 85, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S2 = [130, 134, 123, 120, 127, 136, 137, 140, 141, 141, 141, 139, 137, 135, 135, 136, 136, 136, 136, 132, 134, 134, 135, 136, 135, 135, 132, 123, 98, 67, 76, 92, 77, 59, 78, 100, 93, 63, 59, 85, 105, 105, 105, 103, 101, 102, 108, 113, 115, 115, 119, 118, 111, 106, 111, 105, 87, 90, 102, 103, 105, 105, 101, 99, 102, 105, 105, 103, 104, 101, 91, 85, 86, 87, 88, 88, 97, 102, 100, 101, 101, 98, 91, 91, 96, 101, 104, 103, 101, 96, 86, 80, 73, 70, 76, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S3 = [129, 133, 124, 121, 130, 136, 136, 137, 140, 140, 140, 140, 136, 135, 135, 135, 135, 134, 134, 132, 133, 134, 136, 136, 136, 135, 133, 123, 95, 62, 79, 93, 73, 60, 86, 103, 92, 62, 62, 89, 105, 105, 104, 102, 101, 103, 109, 115, 117, 118, 121, 119, 110, 106, 110, 101, 85, 92, 101, 103, 105, 105, 101, 98, 101, 105, 106, 105, 104, 100, 91, 85, 88, 88, 89, 91, 100, 102, 101, 102, 99, 96, 89, 91, 98, 101, 103, 101, 98, 93, 85, 79, 72, 70, 79, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S4 = [126, 134, 127, 120, 126, 135, 136, 137, 139, 140, 139, 140, 139, 139, 137, 135, 135, 134, 133, 132, 132, 133, 135, 137, 139, 137, 133, 119, 89, 65, 86, 94, 68, 60, 89, 103, 85, 55, 70, 97, 105, 103, 103, 101, 101, 103, 109, 117, 119, 119, 121, 118, 109, 105, 108, 98, 85, 91, 99, 98, 101, 103, 97, 91, 99, 104, 105, 105, 103, 99, 88, 85, 90, 92, 92, 94, 101, 101, 100, 102, 102, 98, 96, 98, 101, 103, 103, 102, 100, 95, 86, 79, 73, 74, 85, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S5 = [123, 131, 131, 122, 123, 131, 136, 137, 139, 139, 140, 140, 139, 136, 136, 135, 134, 135, 134, 133, 134, 136, 137, 139, 139, 139, 135, 131, 106, 70, 69, 92, 83, 59, 76, 99, 90, 63, 65, 91, 105, 104, 102, 102, 101, 103, 105, 113, 121, 121, 122, 121, 113, 106, 109, 110, 96, 86, 94, 99, 97, 101, 101, 91, 89, 100, 105, 106, 105, 102, 92, 86, 89, 91, 93, 92, 99, 103, 101, 101, 102, 101, 98, 96, 99, 101, 101, 101, 101, 98, 92, 85, 77, 75, 85, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S6 = [125, 131, 131, 123, 125, 133, 135, 137, 137, 139, 140, 137, 136, 135, 135, 135, 137, 137, 139, 136, 137, 137, 139, 139, 139, 139, 135, 129, 103, 68, 74, 91, 73, 59, 83, 102, 87, 60, 63, 91, 106, 105, 105, 101, 101, 103, 109, 117, 119, 119, 121, 121, 114, 107, 108, 111, 101, 85, 90, 99, 98, 101, 103, 95, 94, 102, 105, 105, 105, 101, 91, 86, 89, 91, 93, 92, 99, 103, 102, 103, 103, 101, 97, 96, 98, 101, 102, 101, 101, 98, 91, 83, 74, 75, 87, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S7 = [125, 133, 128, 121, 126, 133, 134, 136, 137, 137, 137, 136, 135, 135, 135, 136, 137, 137, 137, 136, 136, 136, 136, 137, 139, 139, 134, 118, 79, 68, 92, 89, 62, 68, 95, 101, 77, 55, 77, 101, 104, 105, 105, 102, 101, 104, 111, 116, 118, 118, 120, 120, 115, 107, 108, 108, 98, 84, 91, 95, 96, 101, 95, 87, 96, 105, 105, 105, 103, 99, 88, 85, 91, 93, 94, 94, 100, 103, 101, 101, 101, 99, 97, 98, 101, 103, 103, 103, 100, 94, 89, 79, 75, 83, 87, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S8 = [127, 133, 123, 120, 128, 134, 134, 136, 137, 139, 140, 140, 137, 137, 136, 136, 139, 139, 137, 136, 135, 136, 137, 137, 137, 134, 126, 101, 68, 79, 98, 89, 59, 76, 100, 92, 67, 55, 84, 103, 105, 105, 105, 101, 101, 104, 111, 117, 119, 117, 119, 121, 115, 107, 107, 105, 91, 90, 101, 102, 105, 105, 101, 99, 103, 106, 106, 105, 103, 97, 86, 85, 89, 88, 87, 91, 100, 101, 99, 100, 97, 91, 87, 91, 99, 102, 102, 99, 91, 84, 78, 71, 72, 80, 84, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S9 = [130, 133, 124, 120, 129, 133, 134, 136, 139, 140, 139, 137, 135, 135, 135, 135, 136, 136, 136, 136, 136, 136, 137, 137, 136, 133, 125, 100, 69, 80, 97, 76, 60, 84, 102, 93, 63, 57, 85, 105, 105, 105, 105, 101, 101, 102, 109, 113, 115, 113, 118, 121, 113, 105, 109, 100, 86, 91, 101, 100, 103, 103, 98, 98, 103, 105, 105, 105, 103, 95, 85, 85, 87, 88, 87, 93, 101, 99, 101, 101, 98, 92, 92, 99, 103, 104, 103, 99, 91, 85, 79, 72, 73, 84, 85, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S10 = [125, 132, 126, 121, 128, 134, 135, 137, 139, 140, 140, 139, 136, 136, 136, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 136, 131, 113, 74, 67, 92, 96, 67, 62, 91, 103, 91, 63, 55, 80, 101, 105, 105, 104, 100, 99, 99, 107, 111, 112, 114, 119, 115, 108, 106, 107, 93, 84, 96, 99, 101, 104, 102, 96, 97, 103, 105, 105, 103, 100, 89, 85, 89, 89, 91, 95, 102, 100, 101, 102, 99, 94, 93, 98, 101, 103, 103, 101, 98, 91, 84, 76, 71, 75, 85, 81, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S11 = [127, 131, 124, 122, 131, 135, 135, 137, 139, 140, 140, 139, 137, 136, 136, 135, 135, 133, 134, 133, 133, 135, 136, 137, 139, 136, 130, 105, 70, 70, 93, 91, 65, 63, 92, 103, 88, 65, 59, 82, 103, 105, 104, 103, 101, 103, 107, 115, 118, 118, 120, 121, 114, 105, 108, 105, 88, 85, 96, 96, 98, 102, 97, 90, 97, 105, 105, 104, 101, 95, 85, 88, 91, 93, 93, 100, 102, 101, 102, 102, 99, 97, 98, 100, 103, 104, 102, 101, 97, 89, 84, 74, 72, 80, 86, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S12 = [128, 132, 124, 123, 133, 135, 136, 139, 141, 141, 139, 136, 134, 134, 134, 135, 134, 134, 134, 135, 135, 136, 137, 137, 137, 134, 128, 106, 73, 69, 92, 94, 72, 60, 81, 100, 91, 65, 60, 86, 105, 105, 103, 102, 101, 102, 108, 114, 117, 118, 121, 119, 112, 104, 107, 105, 89, 85, 98, 100, 102, 104, 99, 96, 102, 106, 106, 105, 101, 91, 85, 87, 87, 89, 91, 101, 103, 101, 102, 102, 98, 93, 92, 98, 102, 103, 103, 101, 95, 89, 83, 76, 72, 79, 88, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S13 = [125, 133, 128, 121, 130, 135, 135, 137, 137, 139, 137, 135, 133, 134, 133, 133, 136, 136, 135, 134, 134, 136, 136, 137, 137, 137, 134, 127, 103, 68, 72, 95, 92, 63, 67, 96, 100, 73, 55, 79, 104, 105, 105, 105, 101, 101, 105, 113, 117, 117, 118, 120, 116, 108, 105, 109, 99, 83, 91, 99, 99, 103, 99, 94, 100, 105, 105, 105, 103, 93, 85, 86, 90, 92, 92, 98, 103, 101, 101, 102, 100, 96, 93, 95, 101, 103, 103, 102, 100, 93, 86, 80, 73, 74, 85, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S14 = [124, 133, 127, 123, 131, 134, 135, 136, 137, 139, 139, 137, 137, 137, 137, 137, 137, 140, 137, 135, 134, 137, 139, 139, 139, 140, 137, 127, 101, 68, 76, 101, 90, 59, 81, 104, 93, 63, 67, 88, 105, 105, 105, 105, 101, 101, 103, 109, 116, 119, 119, 121, 119, 110, 103, 103, 90, 85, 97, 93, 99, 101, 92, 92, 103, 107, 107, 106, 101, 91, 87, 91, 93, 95, 94, 99, 103, 103, 103, 103, 101, 98, 98, 101, 103, 105, 105, 103, 101, 95, 87, 79, 73, 80, 87, 85, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S15 = [129, 132, 123, 121, 131, 134, 134, 136, 137, 140, 139, 137, 137, 139, 139, 137, 137, 139, 139, 135, 135, 136, 137, 137, 137, 137, 136, 129, 105, 70, 77, 97, 80, 60, 82, 103, 92, 63, 60, 90, 106, 105, 104, 103, 101, 101, 105, 113, 117, 117, 119, 119, 112, 104, 107, 104, 88, 90, 101, 100, 104, 103, 98, 99, 105, 107, 106, 105, 101, 91, 87, 88, 89, 90, 90, 97, 103, 101, 101, 101, 100, 96, 92, 95, 101, 103, 103, 103, 99, 92, 84, 79, 72, 73, 85, 85, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S16 = [127, 131, 123, 124, 133, 135, 136, 139, 139, 141, 140, 137, 135, 135, 136, 137, 139, 137, 139, 137, 136, 136, 139, 140, 139, 137, 131, 122, 91, 67, 85, 98, 70, 60, 84, 101, 87, 55, 60, 88, 105, 105, 105, 105, 101, 100, 103, 110, 115, 115, 118, 119, 111, 104, 107, 100, 85, 94, 101, 101, 105, 103, 98, 101, 105, 107, 107, 105, 101, 93, 85, 86, 87, 89, 90, 97, 103, 101, 101, 102, 100, 95, 92, 97, 101, 105, 105, 103, 100, 93, 85, 78, 72, 75, 87, 85, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S17 = [119, 129, 131, 120, 121, 131, 133, 135, 137, 137, 139, 137, 137, 134, 134, 134, 135, 136, 136, 137, 136, 135, 134, 135, 136, 137, 136, 133, 126, 98, 67, 80, 95, 76, 60, 78, 101, 97, 69, 57, 79, 103, 104, 105, 104, 99, 99, 105, 112, 114, 115, 119, 117, 109, 105, 109, 102, 83, 91, 99, 100, 103, 101, 96, 97, 103, 107, 107, 105, 103, 97, 85, 86, 90, 91, 92, 97, 103, 102, 101, 103, 101, 98, 94, 97, 101, 104, 103, 103, 100, 93, 86, 79, 73, 74, 83, 85, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S18 = [119, 128, 131, 122, 125, 134, 136, 136, 139, 140, 141, 140, 140, 136, 137, 136, 136, 137, 139, 137, 137, 137, 139, 139, 140, 139, 137, 135, 121, 94, 67, 85, 98, 73, 60, 79, 102, 98, 68, 57, 82, 104, 105, 105, 104, 101, 100, 106, 115, 117, 117, 121, 118, 110, 105, 107, 96, 83, 94, 99, 99, 104, 102, 96, 97, 103, 107, 106, 105, 104, 96, 86, 85, 91, 92, 93, 98, 103, 103, 103, 103, 102, 98, 97, 99, 103, 105, 105, 103, 100, 94, 86, 80, 73, 73, 84, 86, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S19 = [128, 131, 122, 121, 131, 135, 135, 137, 137, 141, 141, 141, 139, 136, 135, 134, 136, 135, 133, 133, 134, 134, 136, 139, 139, 137, 133, 123, 98, 67, 77, 97, 80, 60, 74, 98, 98, 71, 57, 85, 104, 105, 104, 102, 100, 102, 108, 115, 117, 118, 119, 114, 106, 106, 109, 98, 83, 92, 101, 101, 105, 104, 99, 98, 103, 106, 106, 105, 104, 98, 87, 85, 90, 89, 90, 94, 101, 101, 100, 103, 102, 97, 92, 93, 99, 102, 103, 102, 99, 94, 87, 81, 74, 73, 83, 87, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S20 = [128, 131, 125, 123, 132, 136, 137, 139, 140, 140, 140, 139, 136, 135, 133, 133, 134, 135, 135, 134, 135, 136, 139, 140, 140, 139, 131, 118, 90, 67, 81, 98, 76, 60, 79, 99, 86, 59, 65, 91, 105, 104, 104, 101, 99, 103, 111, 117, 117, 119, 121, 114, 106, 104, 107, 97, 84, 95, 102, 101, 105, 104, 100, 99, 104, 107, 107, 106, 103, 95, 85, 85, 88, 88, 90, 96, 103, 101, 101, 101, 99, 93, 91, 94, 100, 104, 105, 103, 101, 96, 86, 79, 72, 73, 85, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S21 = [132, 131, 122, 121, 131, 136, 137, 137, 136, 137, 139, 135, 133, 133, 133, 136, 137, 137, 137, 135, 136, 137, 139, 140, 140, 136, 131, 107, 73, 69, 96, 93, 59, 69, 95, 96, 67, 55, 84, 107, 109, 109, 107, 103, 103, 108, 113, 114, 115, 118, 120, 116, 109, 109, 105, 91, 88, 101, 102, 103, 105, 102, 98, 100, 105, 105, 105, 104, 99, 87, 84, 88, 87, 89, 91, 98, 101, 98, 100, 99, 96, 89, 88, 92, 99, 101, 101, 99, 92, 85, 79, 73, 70, 79, 84, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S22 = [126, 134, 127, 121, 127, 135, 137, 139, 139, 140, 140, 139, 137, 137, 135, 136, 137, 139, 139, 137, 137, 139, 140, 140, 141, 139, 133, 119, 80, 67, 92, 96, 55, 68, 96, 100, 71, 55, 84, 107, 108, 108, 107, 103, 103, 107, 114, 117, 117, 118, 120, 119, 115, 110, 108, 97, 86, 98, 103, 101, 106, 103, 99, 99, 104, 106, 106, 104, 99, 89, 84, 88, 89, 90, 91, 97, 101, 100, 99, 101, 99, 94, 90, 93, 99, 102, 102, 101, 96, 88, 81, 74, 70, 76, 84, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S23 = [128, 135, 127, 121, 131, 134, 135, 136, 137, 141, 140, 139, 135, 135, 135, 135, 136, 139, 140, 139, 137, 139, 140, 140, 140, 137, 129, 105, 73, 73, 99, 89, 62, 73, 100, 90, 59, 65, 94, 107, 108, 109, 106, 104, 105, 107, 113, 116, 115, 117, 119, 117, 111, 108, 109, 96, 85, 97, 103, 103, 106, 103, 99, 101, 103, 102, 102, 103, 99, 92, 86, 89, 90, 91, 90, 94, 100, 99, 99, 101, 99, 94, 91, 92, 100, 103, 103, 100, 95, 86, 80, 74, 71, 80, 85, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S24 = [129, 131, 123, 125, 131, 134, 137, 139, 139, 141, 141, 140, 137, 136, 137, 139, 141, 141, 139, 137, 137, 139, 139, 140, 137, 130, 116, 87, 67, 91, 102, 68, 62, 90, 94, 74, 54, 74, 98, 108, 111, 110, 107, 102, 101, 103, 109, 111, 108, 109, 113, 113, 110, 105, 105, 94, 87, 101, 104, 105, 108, 105, 103, 103, 105, 107, 103, 101, 97, 87, 82, 84, 85, 85, 84, 92, 100, 99, 99, 101, 98, 89, 88, 95, 101, 103, 103, 99, 91, 83, 77, 71, 72, 81, 83, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];


#Group 2 (Variable 2?)
#25 observations with 114 data points each
S20 = [115, 114, 125, 130, 131, 132, 132, 132, 131, 130, 130, 130, 128, 128, 129, 128, 128, 128, 129, 130, 130, 130, 124, 98, 76, 75, 80, 80, 84, 96, 104, 107, 108, 110, 109, 108, 107, 104, 105, 107, 105, 101, 98, 99, 98, 96, 97, 96, 94, 93, 97, 105, 111, 110, 101, 90, 82, 76, 72, 67, 63, 65, 77, 100, 108, 105, 94, 78, 70, 77, 86, 90, 86, 89, 95, 94, 94, 95, 92, 92, 96, 97, 96, 91, 84, 76, 73, 81, 84, 85, 88, 86, 89, 99, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S21 = [122, 112, 119, 129, 129, 130, 131, 131, 131, 131, 131, 131, 130, 130, 130, 131, 131, 131, 131, 130, 130, 130, 130, 128, 109, 80, 75, 80, 82, 85, 96, 105, 108, 109, 110, 109, 108, 105, 105, 107, 108, 105, 101, 99, 101, 99, 96, 98, 98, 95, 94, 97, 105, 111, 111, 101, 90, 80, 73, 69, 65, 63, 70, 88, 106, 108, 105, 93, 78, 70, 76, 85, 88, 86, 87, 94, 94, 94, 94, 93, 92, 96, 98, 97, 92, 85, 76, 72, 82, 84, 86, 89, 87, 92, 100, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S22 = [120, 112, 119, 128, 130, 131, 131, 132, 131, 131, 130, 130, 130, 129, 129, 131, 132, 132, 132, 131, 131, 131, 131, 128, 107, 75, 74, 81, 84, 86, 94, 101, 105, 107, 108, 108, 106, 103, 103, 105, 106, 104, 99, 96, 96, 94, 92, 94, 95, 94, 94, 99, 107, 112, 109, 98, 87, 81, 76, 71, 67, 65, 69, 85, 105, 108, 105, 95, 81, 71, 75, 84, 88, 86, 88, 94, 92, 92, 94, 93, 92, 95, 96, 94, 88, 82, 74, 73, 82, 84, 85, 87, 86, 92, 101, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S23 = [120, 111, 115, 127, 129, 130, 131, 131, 131, 131, 130, 130, 129, 128, 128, 128, 128, 128, 127, 128, 128, 128, 128, 128, 116, 90, 74, 79, 81, 81, 86, 96, 102, 105, 107, 107, 105, 103, 102, 104, 105, 104, 101, 97, 96, 96, 92, 92, 95, 92, 90, 92, 100, 108, 111, 108, 98, 86, 76, 71, 68, 65, 65, 70, 90, 105, 107, 101, 88, 73, 68, 76, 83, 86, 83, 88, 94, 92, 92, 91, 89, 90, 92, 95, 92, 87, 80, 72, 75, 80, 80, 85, 88, 86, 93, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S24 = [119, 112, 121, 130, 131, 131, 131, 131, 131, 130, 130, 130, 128, 127, 128, 130, 131, 131, 130, 129, 130, 130, 130, 128, 107, 81, 74, 78, 77, 76, 83, 97, 102, 105, 108, 108, 107, 104, 102, 102, 105, 103, 96, 94, 94, 92, 91, 94, 94, 93, 94, 99, 109, 113, 108, 94, 88, 90, 86, 79, 69, 65, 68, 82, 100, 108, 105, 96, 82, 70, 73, 82, 86, 84, 88, 94, 91, 92, 94, 94, 95, 97, 97, 94, 88, 81, 73, 73, 80, 84, 86, 87, 86, 90, 99, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S25 = [119, 111, 121, 130, 131, 132, 132, 131, 131, 131, 130, 130, 130, 130, 130, 131, 131, 130, 130, 130, 130, 131, 131, 128, 104, 82, 75, 78, 78, 77, 84, 99, 107, 108, 110, 110, 109, 107, 105, 105, 107, 105, 101, 98, 99, 98, 96, 97, 96, 95, 95, 99, 107, 113, 108, 96, 89, 93, 92, 82, 72, 67, 68, 81, 101, 109, 107, 99, 84, 72, 75, 86, 88, 86, 91, 96, 93, 92, 95, 96, 96, 98, 98, 94, 88, 81, 74, 72, 80, 88, 88, 88, 86, 88, 98, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S26 = [113, 114, 126, 128, 130, 132, 131, 131, 132, 131, 130, 130, 130, 128, 129, 130, 130, 131, 131, 131, 131, 130, 130, 125, 105, 77, 74, 80, 81, 83, 93, 102, 107, 108, 108, 108, 107, 105, 104, 106, 107, 105, 101, 99, 99, 97, 94, 96, 97, 93, 92, 98, 105, 111, 110, 101, 91, 82, 76, 71, 67, 63, 68, 86, 106, 108, 101, 86, 72, 72, 82, 89, 88, 87, 94, 94, 93, 94, 93, 92, 93, 96, 96, 92, 87, 80, 72, 74, 82, 84, 86, 88, 87, 89, 98, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S27 = [116, 116, 128, 130, 131, 132, 131, 131, 131, 131, 130, 129, 128, 130, 131, 132, 131, 131, 131, 131, 131, 130, 130, 121, 96, 74, 77, 82, 82, 83, 92, 100, 103, 105, 105, 105, 105, 102, 102, 105, 104, 99, 94, 94, 93, 92, 94, 95, 94, 93, 97, 104, 111, 113, 107, 94, 85, 82, 76, 70, 65, 67, 78, 100, 109, 105, 95, 78, 70, 75, 85, 88, 84, 89, 94, 92, 92, 93, 93, 92, 94, 96, 94, 90, 84, 76, 70, 75, 83, 84, 87, 88, 86, 94, 101, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S28 = [125, 116, 116, 128, 130, 131, 131, 131, 131, 131, 130, 131, 130, 130, 130, 131, 131, 130, 130, 131, 130, 130, 130, 130, 121, 96, 74, 77, 81, 84, 88, 97, 105, 108, 109, 108, 107, 106, 105, 105, 107, 107, 104, 101, 100, 100, 97, 95, 97, 95, 91, 92, 97, 105, 111, 110, 100, 86, 76, 70, 67, 63, 67, 81, 102, 108, 104, 91, 75, 70, 80, 86, 88, 84, 88, 94, 92, 93, 94, 91, 90, 92, 96, 96, 92, 86, 77, 71, 76, 82, 83, 87, 88, 86, 94, 102, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S29 = [119, 110, 119, 128, 130, 131, 131, 131, 131, 130, 130, 131, 130, 129, 130, 130, 130, 130, 130, 131, 131, 131, 131, 130, 119, 96, 74, 76, 81, 82, 86, 96, 104, 107, 108, 107, 105, 103, 103, 105, 105, 104, 99, 97, 97, 95, 92, 95, 96, 92, 91, 96, 104, 111, 113, 107, 94, 82, 74, 70, 67, 63, 68, 88, 107, 107, 99, 82, 70, 70, 79, 85, 86, 84, 90, 94, 92, 92, 93, 91, 92, 94, 96, 94, 90, 86, 77, 71, 76, 84, 85, 88, 88, 86, 94, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S210 = [114, 114, 127, 130, 130, 131, 131, 131, 130, 131, 131, 131, 130, 131, 131, 130, 131, 132, 131, 131, 131, 131, 131, 130, 115, 89, 75, 78, 80, 79, 88, 103, 108, 110, 110, 108, 108, 106, 105, 107, 107, 106, 101, 99, 101, 99, 95, 97, 97, 93, 92, 98, 105, 111, 108, 96, 86, 82, 77, 70, 67, 65, 77, 99, 108, 105, 96, 81, 71, 75, 84, 90, 87, 86, 93, 95, 92, 93, 94, 93, 94, 97, 96, 94, 88, 82, 74, 72, 80, 86, 88, 89, 88, 88, 98, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S211 = [114, 119, 128, 130, 130, 131, 131, 131, 131, 130, 130, 130, 130, 130, 131, 132, 132, 131, 131, 131, 131, 130, 130, 125, 98, 75, 76, 82, 82, 86, 95, 102, 104, 107, 107, 107, 105, 102, 102, 105, 105, 101, 96, 95, 94, 92, 92, 95, 94, 93, 96, 104, 111, 110, 98, 88, 86, 81, 74, 68, 65, 75, 94, 108, 108, 103, 90, 76, 70, 77, 86, 88, 84, 88, 93, 92, 92, 93, 94, 93, 95, 97, 95, 90, 85, 77, 71, 74, 81, 84, 85, 86, 85, 90, 99, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S212 = [120, 111, 119, 128, 128, 130, 130, 131, 131, 131, 130, 129, 128, 129, 129, 130, 131, 131, 131, 130, 130, 130, 130, 130, 125, 99, 74, 78, 83, 85, 92, 100, 107, 108, 109, 110, 108, 107, 105, 105, 108, 107, 105, 102, 101, 101, 99, 95, 98, 96, 92, 94, 101, 108, 111, 105, 93, 80, 72, 68, 65, 63, 73, 92, 108, 108, 102, 88, 75, 71, 78, 86, 88, 85, 88, 94, 93, 93, 94, 92, 91, 94, 97, 96, 93, 86, 80, 72, 76, 83, 84, 86, 88, 86, 91, 99, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S213 = [116, 115, 125, 128, 130, 131, 131, 131, 131, 131, 130, 130, 130, 129, 129, 130, 131, 130, 131, 130, 131, 130, 130, 127, 105, 78, 74, 80, 82, 84, 92, 99, 104, 105, 107, 107, 107, 104, 103, 104, 106, 105, 99, 95, 95, 93, 92, 94, 94, 93, 95, 102, 111, 111, 102, 88, 84, 80, 74, 68, 65, 68, 83, 104, 108, 105, 98, 82, 72, 73, 82, 88, 88, 86, 91, 93, 92, 94, 94, 94, 94, 97, 97, 94, 89, 82, 75, 70, 78, 84, 86, 87, 87, 87, 95, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S214 = [116, 113, 124, 130, 130, 131, 132, 132, 131, 131, 131, 131, 131, 130, 130, 131, 131, 131, 131, 131, 130, 130, 130, 130, 118, 91, 74, 78, 82, 83, 90, 100, 107, 109, 110, 110, 109, 108, 107, 106, 108, 107, 105, 101, 100, 101, 96, 98, 97, 94, 93, 98, 107, 112, 109, 99, 86, 78, 72, 68, 67, 65, 71, 90, 107, 109, 104, 91, 75, 70, 78, 87, 90, 86, 90, 96, 94, 94, 94, 92, 92, 94, 97, 96, 92, 87, 80, 72, 74, 82, 84, 86, 88, 87, 91, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S215 = [119, 111, 119, 129, 130, 131, 131, 131, 131, 131, 130, 130, 130, 129, 128, 130, 130, 131, 130, 130, 130, 130, 130, 130, 125, 104, 76, 74, 81, 83, 86, 94, 104, 107, 108, 108, 106, 105, 102, 102, 105, 105, 101, 97, 97, 96, 92, 94, 96, 92, 92, 98, 107, 111, 110, 103, 90, 79, 73, 70, 67, 63, 67, 79, 102, 108, 104, 93, 78, 68, 72, 81, 86, 85, 85, 94, 94, 92, 93, 92, 91, 92, 94, 96, 95, 91, 85, 77, 72, 78, 83, 83, 87, 88, 89, 98, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S216 = [115, 112, 124, 130, 131, 133, 133, 133, 132, 132, 131, 130, 131, 130, 129, 130, 130, 131, 131, 131, 131, 131, 131, 130, 117, 88, 74, 80, 84, 84, 90, 99, 107, 108, 109, 109, 109, 107, 105, 107, 107, 105, 101, 101, 101, 98, 98, 99, 96, 94, 96, 102, 110, 113, 110, 99, 88, 78, 72, 69, 67, 65, 72, 91, 107, 108, 102, 86, 74, 71, 80, 88, 90, 86, 90, 96, 94, 93, 94, 92, 90, 92, 96, 97, 94, 88, 81, 73, 75, 83, 84, 87, 88, 86, 94, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S217 = [119, 110, 118, 129, 130, 131, 131, 131, 131, 131, 131, 130, 130, 130, 130, 131, 130, 131, 131, 131, 131, 132, 132, 132, 122, 94, 74, 79, 83, 84, 90, 99, 107, 108, 108, 106, 105, 103, 103, 106, 105, 104, 101, 99, 98, 94, 94, 97, 96, 94, 93, 95, 101, 110, 111, 105, 94, 79, 71, 68, 68, 67, 71, 88, 105, 106, 101, 86, 73, 70, 74, 81, 85, 84, 84, 92, 92, 91, 92, 91, 88, 89, 92, 96, 94, 88, 81, 72, 75, 82, 82, 86, 87, 87, 96, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S218 = [120, 112, 118, 128, 130, 131, 130, 131, 131, 131, 131, 131, 130, 130, 131, 131, 131, 131, 130, 130, 131, 131, 131, 129, 107, 80, 74, 79, 80, 84, 96, 104, 106, 107, 109, 108, 107, 105, 106, 107, 105, 101, 98, 98, 97, 95, 97, 96, 96, 95, 98, 104, 111, 109, 98, 88, 87, 84, 77, 68, 63, 68, 87, 106, 109, 106, 97, 82, 73, 76, 84, 88, 86, 86, 93, 94, 91, 91, 93, 94, 95, 96, 96, 92, 87, 80, 72, 74, 82, 86, 88, 86, 85, 92, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S219 = [119, 111, 120, 129, 130, 131, 131, 130, 130, 130, 130, 129, 129, 129, 131, 131, 133, 133, 132, 131, 131, 130, 130, 127, 108, 79, 74, 81, 83, 86, 96, 104, 106, 108, 107, 105, 104, 104, 105, 105, 104, 100, 97, 98, 97, 93, 95, 96, 94, 92, 94, 102, 109, 111, 107, 96, 81, 72, 68, 67, 67, 68, 81, 102, 108, 104, 94, 78, 69, 73, 79, 85, 88, 84, 88, 94, 93, 94, 93, 90, 89, 92, 95, 94, 90, 83, 73, 72, 80, 81, 83, 87, 85, 92, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S220 = [124, 113, 114, 126, 130, 131, 131, 132, 131, 131, 131, 131, 131, 130, 130, 131, 132, 133, 132, 132, 132, 131, 131, 131, 127, 101, 74, 76, 82, 84, 91, 101, 108, 109, 110, 109, 108, 106, 105, 108, 107, 105, 102, 100, 101, 101, 97, 98, 98, 94, 93, 97, 104, 111, 112, 106, 91, 81, 74, 70, 67, 65, 68, 84, 105, 109, 106, 95, 80, 70, 73, 82, 87, 88, 86, 92, 94, 93, 94, 92, 90, 92, 95, 96, 94, 88, 80, 72, 74, 82, 82, 86, 88, 87, 96, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S221 = [120, 111, 120, 128, 128, 129, 130, 130, 130, 131, 130, 130, 130, 129, 129, 130, 131, 131, 131, 131, 131, 131, 131, 131, 119, 88, 74, 80, 82, 84, 93, 102, 106, 108, 107, 105, 104, 102, 104, 105, 105, 102, 99, 97, 98, 95, 92, 95, 95, 92, 92, 98, 106, 111, 110, 102, 89, 78, 72, 69, 67, 65, 68, 84, 105, 108, 103, 92, 78, 69, 73, 80, 86, 86, 85, 92, 94, 92, 94, 92, 91, 92, 96, 96, 93, 88, 79, 72, 78, 84, 84, 87, 88, 88, 97, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S222 = [116, 113, 124, 129, 130, 131, 131, 131, 131, 131, 131, 131, 131, 130, 130, 131, 132, 132, 131, 130, 131, 131, 131, 125, 98, 73, 78, 83, 83, 92, 101, 107, 107, 108, 107, 106, 104, 104, 105, 106, 105, 101, 98, 98, 98, 93, 94, 96, 94, 92, 94, 102, 110, 113, 107, 99, 84, 74, 70, 68, 67, 67, 78, 97, 108, 107, 99, 85, 72, 70, 76, 82, 87, 84, 89, 93, 92, 93, 91, 88, 88, 92, 96, 94, 88, 80, 72, 74, 81, 81, 84, 87, 86, 93, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S223 = [119, 111, 118, 128, 130, 131, 131, 131, 131, 131, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 131, 125, 100, 75, 78, 80, 80, 84, 98, 105, 108, 109, 110, 110, 109, 107, 105, 107, 108, 106, 101, 99, 99, 98, 97, 98, 97, 96, 96, 102, 110, 113, 106, 91, 88, 88, 84, 74, 67, 65, 70, 90, 105, 108, 107, 99, 84, 72, 76, 86, 90, 86, 90, 96, 93, 92, 94, 95, 96, 98, 99, 95, 89, 81, 74, 72, 80, 86, 87, 88, 86, 90, 99, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
S224 = [113, 120, 129, 130, 131, 131, 132, 132, 132, 131, 131, 131, 130, 129, 130, 131, 131, 131, 130, 129, 130, 130, 122, 96, 74, 78, 81, 81, 85, 96, 104, 107, 108, 109, 110, 109, 106, 105, 106, 108, 105, 101, 98, 98, 96, 96, 98, 97, 95, 96, 101, 110, 113, 109, 94, 89, 87, 83, 75, 68, 65, 67, 78, 100, 110, 107, 97, 82, 71, 76, 86, 89, 86, 89, 95, 93, 92, 94, 94, 95, 97, 97, 95, 90, 83, 74, 72, 80, 86, 86, 88, 86, 90, 99, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

Python Code

I implemented some code in python. But i'm getting a singular matrix error when computing the inverse of the cov. I was able to fix the error if I went down to 25 observations and 25 variables. What am I doing wrong?

The data i'm populating into 'class1' is the same data as above. Only this time I have 158 scans in 1 group. Each scan as 114 data points. This is to not be rank deficient and have more observations (Scans) than variables (data points in a scan)

Here are the matrix sizes

class1 = 158x114

class1Mean = 1x114

scan = 1x114

class1Cov = 114x114

def mahal(c1, c2, scan):


    #Get class 1 data in a matrix
    #Observations are each scan 
    #A variable is each datapoint
    #Class one will be (Observations by variables)

    class1 = np.zeros([len(c1),len(c1[0])])

    #Populate the data
    for scan in range(0,len(c1)):
        for dp in range(0,len(c1[0])):
            class1[scan][dp] = c1[scan][dp]

    class1 = np.matrix(class1)
    classSize = class1.shape



    class1Mean = np.matrix(np.mean(class1, 0)) #Get the column (variable) mean for class 1

    cMeanSize = class1Mean.shape


    #Calculate the covariance for class 1
    class1Cov = np.matrix(np.cov(class1, rowvar = 0)) #the class has variables in columbs not rows
    cCovSize = class1Cov.shape

    #Get the scan to classify
    scan = np.matrix(c1[scan])
    sSize = scan.shape

    #return classSize, cMeanSize, cCovSize, sSize

    #Calculate the mahalanobis distance of scan from class1Mean
    Dm1 = maha(scan,class1Mean, class1Cov)


return Dm1

def maha(x,y,V):
    return np.sqrt((x-y).transpose() * V.I * (x-y)) 
$\endgroup$
1
$\begingroup$

I would put the Group 1 data into a matrix that is 25 x 114, and Group 2 into another matrix of the same size. The means will be the colMeans (in R) of these two matrices, each a vector of length 114. The covariances can be computed by cov in R. (Though it looks like you are using matlab. So compute the means by mean(thematrix,1) and the covariance via cov.)

You are going to have a hard time computing Mahalanobis distances here because your covariance estimates will be rank-deficient: you have fewer observations than variables. That means that if you do something like Sigma1 \ (newx - mean1') in matlab, it will go beyond simple matrix inversion. (I forget what exactly it does, but seem to remember it will give you the solution with the smallest $\ell_2$ norm.)

$\endgroup$
  • $\begingroup$ I tried what you said. To not be rank-deficient I went to 158 observations with 114 variables. But the same data format. See my edited post for the code. Do you know why i'm still having a hard time getting the inverse covariance? $\endgroup$ – guitardenver Jan 24 '17 at 21:02
  • $\begingroup$ If I change to 158 observations (scans) and 100 variables (data points in a scan) then it works...... But if I go anywhere above 100 variables it fails with singular matrix error. 101 variables fails.... $\endgroup$ – guitardenver Jan 24 '17 at 21:33
  • $\begingroup$ Maybe it's something to do with all the zeros in the right hand columns. Are they adding any real information? $\endgroup$ – steveo'america Jan 24 '17 at 22:04
  • $\begingroup$ That does work. I put random data in place of the zeros just for a quick test and it worked. But if I put any other constant in place of the values it did the same error. Does this have to do with the fact that mahalanobis distance must be used with Standard Distributions? Does my line of zeros or any other constant break this rule? I have not wrapped by head around why it only works with standard/Gaussian distributions yet. $\endgroup$ – guitardenver Jan 25 '17 at 14:28
  • $\begingroup$ Remember that the covariance computation is invariant with respect to the addition of a constant, meaning you will have rank deficient covariance if you just replace the zeros with some constant (even a different constant per column). What exactly is the point of those columns if they add no real information at all? $\endgroup$ – steveo'america Jan 25 '17 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.