Term frequency vs TF-IDF

ferandi
ferandi New Altair Community Member
edited November 5 in Community Q&A
Hi friends,

I'm processing documents from files. Anyone know the diference between term frequency vector and tf-idf vector?

Thanks in advance!
Tagged:

Answers

  • IngoRM
    IngoRM New Altair Community Member
    Hi,

    term frequency is simply the normalized number of occurrences of a term divided by the number of terms of the document (from the souce code):

            int numTerms = wordList.size();
            double totalTermNumber = 0;
            for (float value: frequencies)
            totalTermNumber += value;
           
            // Create the result structure
            double[] wv = new double[numTerms];

            // If document contains at least one term
            if (totalTermNumber > 0) {
                // Create the vector
                double length = 0.0;
                for (int i = 0; i < wv.length; i++) {
                    wv = frequencies / totalTermNumber;
                    length += wv * wv;
                }

                length = Math.sqrt(length);

                // Normalize the vector
                if (length > 0.0)
                    for (int i = 0; i < wv.length; i++)
                        wv = wv / length;
            }



    In case of TDIDF, the term frequency of each term is normalized with the inverse document frequency (from the source code):

          // Obtain the total number of documents and the document frequencies
            int numDocuments = wordList.getNumberOfDocuments();
            int[] docFrequencies = wordList.getDocumentFrequencies();
            double totalTermNumber = 0;
            for (float value: frequencies)
            totalTermNumber += value;
           
           
            // Create the result structure
            double[] wv = new double[docFrequencies.length];

            // Create the vector

            // If the document contains at least one term
            if (totalTermNumber > 0) {
                double length = 0.0;
                for (int i = 0; i < wv.length; i++) {
                    // Note: docFrequencies is always > 0 as otherwise the word
                    // would not be in the word list, it is also always smaller as
                    // the total number of documents

                    double idf = Math.log(((double) numDocuments) / ((double) docFrequencies));
                    wv = (frequencies / totalTermNumber) * idf;
                    length += wv * wv;
                }
                length = Math.sqrt(length);

                // Normalize the vector
                if (length > 0.0)
                    for (int i = 0; i < wv.length; i++)
                        wv = wv / length;
            }


    You can find a discussion about the normalization somewhere in the forum if this is interesting for you.

    Cheers,
    Ingo

  • ferandi
    ferandi New Altair Community Member
    Thank you so much Ingo!

    On Text classification with one are more suitable? Thanks in advance!!
  • IngoRM
    IngoRM New Altair Community Member
    Hi,

    as always in data mining: just test it. If one of both preprocessings works better for your data then just go for it. In general, however, TFIDF has outperformed term frequency alone on all practical settings I have experienced so far.

    Cheers,
    Ingo