Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 标准化双值时为NAN_Java_Double_Normalization - Fatal编程技术网

Java 标准化双值时为NAN

Java 标准化双值时为NAN,java,double,normalization,Java,Double,Normalization,我正在尝试计算文件的tfidf值并将其保存到矩阵中,首先要将tfidf值在0和1之间标准化。 但我有一个问题,归一化后计算的第一个值是NAN,如何解决这个问题 这就是我所做的 double tf; //term frequency double idf; //inverse document frequency double tfidf = 0; //term frequency inverse document frequency double minValu

我正在尝试计算文件的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都是零。我的建议是在规范化步骤之前放一个print语句——我猜你会在这里看到一些意外值。

我的第一个猜测是你将0.0除以0.0——也许maxValue、minValue和tfidf都是零。我的建议是在规范化步骤之前放一个print语句——我猜您会看到一些意外值。

您正在除以零。当添加到
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++;
}

你在除以零。当添加到
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++;
}

谢谢你,罗伯特。我已经打印了归一化步骤前后的结果,没有任何异常。请看一下我对问题所做的编辑谢谢你@Robert。我已经打印了归一化步骤前后的结果,没有任何异常。请看一下我对问题所做的编辑谢谢@Marco13。。请看一下编辑。我尝试了你的建议,但我仍然有同样的建议problem@user1488019在标准化之前,您是否更改代码以计算最小/最大值?只需添加行
System.out.println(“除以”+(maxValue-minValue))在进行规范化之前,请确保它永远不会除以0再次感谢@Marco13,但问题不在于操作本身。我注意到第一个数字没有被接受(忽略)@user1488019我不知道你的意思,也不知道这是否是一个可以在这里回答的问题。如果是这样,您可能会编辑原始问题,并添加实际问题的更详细描述…谢谢@Marco13。。请看一下编辑。我尝试了你的建议,但我仍然有同样的建议problem@user1488019在标准化之前,您是否更改代码以计算最小/最大值?只需添加行
System.out.println(“除以”+(maxValue-minValue))在进行规范化之前,请确保它永远不会除以0再次感谢@Marco13,但问题不在于操作本身。我注意到第一个数字没有被接受(忽略)@user1488019我不知道你的意思,也不知道这是否是一个可以在这里回答的问题。如果是这样,您可能会编辑原始问题并添加实际问题的更详细描述。。。