我最近正在尝试执行一些数据清理算法。当我尝试计算数据集中的点与平均向量之间的马氏距离时,它似乎是一样的。
例如,我有一个数据集,如:
{{2,2,3},{4,5,9},{7,8,9}}
平均向量为:
{13/3,5,7}
协方差矩阵为:
{{6.333333333333333,7.5,7.0},{7.5,9.0,9.0},{7.0,9.0,12.0}}
那么{2,2,3},{4,5,9},{7,8,9}和均值向量之间的距离都是8290542,比较奇怪。在纸上计算后,结果是一样的。
有谁知道我的代码或想法有什么问题?如果有人可以帮助我,我将不胜感激。以下是我在处理此问题时使用的一些代码。
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.mahout.math.*;
import org.apache.mahout.common.distance.MahalanobisDistanceMeasure;
public class Test {
public static void main(String[] args) {
double[] a = {2,2,3};
Vector aVector = new DenseVector(a);
double[] b = {4,5,9};
Vector bVector = new DenseVector(b);
double[] c = {7,8,9};
Vector cVector = new DenseVector(b);
double[] mean = {13/3,5,7};
Vector meanVector = new DenseVector(mean);
MahalanobisDistanceMeasure measure = new MahalanobisDistanceMeasure();
double[][] ma = {{2,2,3},{4,5,9},{7,8,9}};
RealMatrix matrix = new Covariance(ma).getCovarianceMatrix();
Matrix math = new DenseMatrix(matrix.getData());
measure.setCovarianceMatrix(math);
measure.setMeanVector(meanVector);
System.out.println(matrix.toString());
System.out.println(measure.distance(meanVector,cVector));
}
}