马哈拉诺比斯距离奇异矩阵误差

时间:2017-01-23 20:50:27

标签: python octave matrix-inverse mahalanobis

我有两组数据。两组都有25个变量和114个观察值。

目标是在两组中取一个变量,计算两组中的马哈拉诺比斯距离。距离最小的组是变量所属的组。

我已经在Octave中实现了它并且它“有效”。它给出了一个答案,但抛出了一个奇异的矩阵警告或“警告:矩阵奇异的机器精度”错误。由于某种原因,任一组的协变矩阵都以单数形式结束,或者它是精度误差。

我也在python中实现了这个。它会引发奇异的矩阵错误。我最终使用numpy伪逆函数(linalg.pinv)而不是采用协变矩阵的实数逆。

问题

1)当我采用它的倒数时,为什么我的协方差矩阵给我这个错误?

2)处理此应用程序的正确方法是什么?

3)使用numpy伪逆仍然计算接近正确的马哈拉诺比斯距离?我使用它的值与Octave吐出的值非常不同。

代码

Octave和python的代码可以在这里找到: Code

更新

而不是25个变量和114个观测值,我将数据减少到25个变量和25个观测值,所有内容都开始工作。

为什么重要?

这是常见的吗?

如何能够获得比变量更多的观察值,或者更重要的是比观察值更多的变量?我可以对数据做些什么来预处理它吗?

Python代码

这是工作的python代码,包含25个变量和25个观察编辑。

#Group 1
#25 Variables with 114 observations 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
#25 Variables with 114 observations 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];


#The variable to get the distance from the mean of each group
X = np.matrix(S25[:25]).transpose()
XSize = X.shape

#Group 1 
group1 = np.matrix([S0[:25],S1[:25],S2[:25],S3[:25],S4[:25],S5[:25],S6[:25],S7[:25],S8[:25],S9[:25],S10[:25],S11[:25],S12[:25],S13[:25],S14[:25],S15[:25],S16[:25],S17[:25],S18[:25],S19[:25],S20[:25],S21[:25],S22[:25],S23[:25],S24[:25]]) 
group1 = group1.transpose()
groupSize = group1.shape


#Group 2
group2 = np.matrix([S20[:25],S21[:25],S22[:25],S23[:25],S24[:25],S25[:25],S26[:25],S27[:25],S28[:25],S29[:25],S210[:25],S11[:25],S212[:25],S213[:25],S214[:25],S215[:25],S216[:25],S217[:25],S218[:25],S219[:25],S220[:25],S221[:25],S222[:25],S223[:25],S224[:25]])
group2 = group2.transpose()

#Group 1 Mean
g1_mean = np.mean(group1,1)
meanSize = g1_mean.shape

#Group 2 Mean
g2_mean = np.mean(group2,1)


#Group 1 COV
g1_cov = np.cov(group1, rowvar=1)
g1_cov = np.matrix(g1_cov) 
gcovSize = g1_cov.shape

#Group 2 COV
g2_cov = np.cov(group2, rowvar=1)
g2_cov = np.matrix(g2_cov)

#return XSize,groupSize,meanSize,gcovSize


#Fine the mahalanobis distance of X from group1 and group2
#The distance with the lowest value is the group X belongs to

#return g1_cov,g2_cov

#Mah for group1
Dm_g1 = maha(X,g1_mean,g1_cov)

#Mah for group2
Dm_g2 = maha(X,g2_mean,g2_cov)

#dm_c1 = mahalanobis(X, g1_mean,g1_cov.I)
#dm_c2 = mahalanobis(X, g2_mean,g2_cov.I)


return Dm_g1,Dm_g2



def maha(x,y,V):
    return np.sqrt((x-y).transpose() * V.I * (x-y)) 

1 个答案:

答案 0 :(得分:0)

问题在于Octave认为你有114个变量,每个变量有25个观测值,而不是25个变量,每个变量有114个观测值。它的cov function计算的协方差而不是行的协方差。因此,每个变量应该在它自己的列中,而不是在它自己的行中。

换句话说:在代码中使用cov(X')cov(X2')

我没有尝试过Python代码;可能问题是一样的。

您可以通过打印协方差矩阵的大小来自行诊断:disp(size(S))会告诉您它是114乘114,这显然是错误的。