繁体   English   中英

标准化双精度值时为NAN

[英]NAN when Normalize double values

我正在尝试计算文件的tfidf值并将其保存到矩阵中,我想先将tfidf值标准化在0和1之间。 但是我有一个问题,归一化后计算的第一个值是NAN,我该如何解决这个问题。

这就是我所做的

    double tf; //term frequency
    double idf; //inverse document frequency
    double tfidf = 0; //term frequency inverse document frequency 
    double minValue=0.0;
    double maxValue=0;
    File output = new File("E:/hsqldb-2.3.2/hsqldb-2.3.2/hsqldb/hsqldb/matrix.txt");
    FileWriter out = new FileWriter(output); 
    mat= new String[termsDocsArray.size()][allTerms.size()];
    int c=0; //for files
    for (String[] docTermsArray : termsDocsArray) {
        int count = 0;//for words
        for (String terms : allTerms) {
            tf = new TfIdf().tfCalculator(docTermsArray, terms);
            idf = new TfIdf().idfCalculator(termsDocsArray, terms);
            tfidf = tf * idf;           
            //System.out.print(terms+"\t"+tfidf+"\t");
            //System.out.print(terms+"\t");

            tfidf = Math.round(tfidf*10000)/10000.0d;
            tfidfList.add(tfidf);
            maxValue=Collections.max(tfidfList);
            tfidf=(tfidf-minValue)/(maxValue-minValue);  //Normalization here
            mat[c][count]=Double.toString(tfidf);
            count++;   
        }     
    c++;
    }

这是我得到的输出

NaN 1.0  0.0  0.021
0.0 1.0 0.0 0.365 ... and others

只有第一个数字是NAN,这个数字最初也是在矩阵中重复多次的数字,但其值不是NAN

请给我一些想法来解决此问题。

谢谢

我的第一个猜测是您将0.0除以0.0-也许maxValue,minValue和tfidf都为零。 我的建议是在规范化步骤之前放置一条打印语句-我猜您在那里会看到一些意外值。

您被零除。 当添加到tfidflist的第一个值为0.0时,将发生这种情况。

为了执行真正的标准化 ,您可能必须首先计算所有可能的值,然后计算这些值的最小值/最大值,然后再根据这些最小值/最大值对所有值进行标准化。 大致:

// First collect all values and compute min/max on the fly
double minValue=Double.MAX_VALUE;
double maxValue=-Double.MAX_VALUE;
double values = new String[termsDocsArray.size()][allTerms.size()];
int c=0; //for files
for (String[] docTermsArray : termsDocsArray) {
    int count = 0;//for words
    for (String terms : allTerms) {
        double tf = new TfIdf().tfCalculator(docTermsArray, terms);
        double idf = new TfIdf().idfCalculator(termsDocsArray, terms);
        double tfidf = tf * idf;           
        tfidf = Math.round(tfidf*10000)/10000.0d;
        minValue = Math.min(minValue, tfidf);
        maxValue = Math.max(maxValue, tfidf);
        values[c][count]=tfidf;
        count++;   
    }     
    c++;
}

// Then, create the matrix containing the strings of the normalized 
// values (although using strings here seems like a bad idea)
c=0; //for files
for (String[] docTermsArray : termsDocsArray) {
    int count = 0;//for words
    for (String terms : allTerms) {
        double tfidf = values[c][count];
        tfidf=(tfidf-minValue)/(maxValue-minValue);  //Normalization here
        mat[c][count]=Double.toString(tfidf);
        count++;   
    }     
    c++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM