nullpointerexception - Java - sometimes return NaN when calculating Mahalanobis distance -
i use org.apache.mahout.common.distance.mahalanobisdistancemeasure calculate distance between rows in matrix , mean vector, returns nan. tried debug , seems nullpointerexception thrown in object class. other rows fine. i'd grateful if give me guidance.
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]; (int = 30; < 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); (int = 0; < 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); } }
outputs:
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()is method calculate centroid of matrix, returns int[2](first element x coordinate , second y). dataset matrix i'm focusing on.
"nan" stands "not number". "nan" produced if floating point operation(double/float) has input parameters cause operation produce undefined result. example, 0.0 divided 0.0 arithmetically undefined. taking square root of negative number undefined.
nan static variable in double , float wrapper classes.it not numeric value in scenario, when try measure distance between 2 coordinate return double.nan, tries convert primitive number give nullpointerexception.
Comments
Post a Comment