0

我使用 org.apache.mahout.common.distance.MahalanobisDistanceMeasure 来计算矩阵中的行与平均向量之间的距离,但它有时会返回 NaN。我尝试调试,似乎在 Object 类中抛出了 NullPointerException。但对于其他行,一切都很好。如果有人能给我一些指导,我将不胜感激。

import com.opencsv.CSVReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import com.opencsv.CSVWriter;
import org.apache.commons.math.MathException;
import org.apache.mahout.common.distance.MahalanobisDistanceMeasure;
import org.apache.mahout.math.*;
import org.apache.mahout.math.Matrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;

public class FindMultivariateOutliers {
    public static void main(String[] args) {
        String url = "VIC_20160401_201606301.csv";
        double[][] data = extractRealData(readCSV(url), 3);
        double[][] dataSet = new double[30][49];
        for (int i = 30; i < 60; i++) {
            dataSet[i-30] = data[i];
        }

        double[] mean = calculateMeanVector(dataSet);
        Vector meanVector = new DenseVector(mean);
        Matrix covarianceMatrix = covarianceMatrix(dataSet);

        MahalanobisDistanceMeasure measure = new MahalanobisDistanceMeasure();
        measure.setMeanVector(meanVector);
        measure.setCovarianceMatrix(covarianceMatrix);

        for (int i = 0; i < dataSet.length; i++) {
            DenseVector ve = new DenseVector(dataSet[i]);
            double x = measure.distance(dataSet[centroid(dataSet)[0]][centroid(dataSet)[1]],meanVector,ve);
            System.out.println(i+" "+x);
        }
}

输出:

0 NaN
1 NaN
2 1.3382137932701006
3 5.140281428741069
4 5.448118335171329
5 4.658774790167001
6 3.055235041048766
7 5.577659807980593
8 2.9899726295069784
9 6.095988936666251
10 5.188517209151716
11 3.2929774499538014
12 5.090550175124932
13 5.801822265633947
14 4.714239296215186
15 5.02905587450129
16 4.981122780626051
17 5.195044166268684
18 5.325097238194922
19 4.7899888250142375
20 5.506442897174045
21 5.266585564849615
22 5.403384368592266
23 4.110229775894713
24 5.960687924915147
25 4.5745629099807745
26 5.0580441561885205
27 5.146058878694013
28 5.1375323540721425
29 3.7919178679466015

centroid() 是一种计算矩阵质心的方法,返回 int[2](第一个元素是 x 坐标,第二个元素是 y)。dataSet 是我关注的矩阵。

4

1 回答 1

0

“NaN”代表“不是数字”。如果浮点运算(Double/Float)有一些输入参数会导致运算产生一些未定义的结果,则会产生“Nan”。例如,0.0 除以 0.0 在算术上是未定义的。取负数的平方根也是未定义的。

NaN 是 Double 和 Float 包装类中的静态变量。它不是数值,所以在您的场景中,当您尝试测量两个坐标之间的距离时,它会返回 Double.NaN,然后​​它会尝试将其转换为原始数字,所以它会给你一个 NullPointerException。

于 2017-09-11T05:46:48.653 回答