如何在Weka中规范文档的词频 [英] How to Normalize word frequencies of document in Weka

查看:101
本文介绍了如何在Weka中规范文档的词频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Weka中,类 StringToWordVector 定义一种名为 setNormalizeDocLength 的方法.它将文档的词频归一化.我的问题是:

In Weka, class StringToWordVector defines a method called setNormalizeDocLength. It normalizes word frequencies of a document. My questions are:

  1. 标准化文档的词频"是什么意思?
  2. Weka是如何做到的?

一个实际的例子将对我有最大的帮助.预先感谢.

A practical example will help me best. Thanks in advance.

推荐答案

在Weka源代码中,这是进行标准化的方法:

Looking in the Weka source, this is the method that does the normalising:

private void normalizeInstance(Instance inst, int firstCopy) throws Exception 
{
    double docLength = 0;

    if (m_AvgDocLength < 0) 
    {
        throw new Exception("Average document length not set.");
    }

    // Compute length of document vector
    for(int j=0; j<inst.numValues(); j++) 
    {
        if(inst.index(j)>=firstCopy) 
        {
            docLength += inst.valueSparse(j) * inst.valueSparse(j);
        }
    }     
    docLength = Math.sqrt(docLength);

    // Normalize document vector
    for(int j=0; j<inst.numValues(); j++) 
    {
        if(inst.index(j)>=firstCopy) 
        {
            double val = inst.valueSparse(j) * m_AvgDocLength / docLength;
            inst.setValueSparse(j, val);
            if (val == 0)
            {
                System.err.println("setting value "+inst.index(j)+" to zero.");
                j--;
            }
        }
    }
}

看起来最相关的部分是

double val = inst.valueSparse(j) * m_AvgDocLength / docLength;
inst.setValueSparse(j, val);

所以看起来规范化是value = currentValue * averageDocumentLength / actualDocumentLength.

这篇关于如何在Weka中规范文档的词频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆