在Weka中,类StringToWordVector定义了一个名为setNormalizeDocLength的方法。它用于标准化文档的词频。我的问题是:
- 什么是“标准化文档的词频”?
- Weka是如何实现这个功能的?
一个实际的例子将对我最有帮助。提前感谢。
回答:
查看Weka的源代码,这是执行标准化操作的方法:
private void normalizeInstance(Instance inst, int firstCopy) throws Exception { double docLength = 0; if (m_AvgDocLength < 0) { throw new Exception("Average document length not set."); } // 计算文档向量的长度 for(int j=0; j<inst.numValues(); j++) { if(inst.index(j)>=firstCopy) { docLength += inst.valueSparse(j) * inst.valueSparse(j); } } docLength = Math.sqrt(docLength); // 标准化文档向量 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
。