TF-IDF词项权重计算

简介: 一、TF-IDF词项频率: df:term frequency。 term在文档中出现的频率.tf越大,词项越重要. 文档频率: tf:document frequecy。

一、TF-IDF

词项频率:

df:term frequency。 term在文档中出现的频率.tf越大,词项越重要.

文档频率:

tf:document frequecy。有多少文档包含此term,df越大词项越不重要.

词项权重计算公式:

tf-idf=tf(t,d)*log(N/df(t))
  • W(t,d):the weight of the term in document d
  • tf(t,d):the frequency of term t in document d
  • N:the number of documents
  • df(t):the number of documents that contain term t

二、JAVA实现

package com.javacore.algorithm;

import java.util.Arrays;
import java.util.List;

/**
 * Created by bee on 17/3/13.
 * @version 1.0
 * @author blog.csdn.net/napoay
 */
public class TfIdfCal {



    /**
     *calculate the word frequency
     * @param doc word vector of a doc
     * @param term  a word
     * @return the word frequency of a doc
     */
    public double tf(List<String> doc, String term) {

        double termFrequency = 0;
        for (String str : doc) {
            if (str.equalsIgnoreCase(term)) {
                termFrequency++;
            }
        }
        return termFrequency / doc.size();
    }


    /**
     *calculate the document frequency
     * @param docs the set of all docs
     * @param term a word
     * @return the number of docs which contain the word
     */

    public int df(List<List<String>> docs, String term) {
        int n = 0;
        if (term != null && term != "") {

            for (List<String> doc : docs) {
                for (String word : doc) {
                    if (term.equalsIgnoreCase(word)) {
                        n++;
                        break;
                    }
                }
            }
        } else {
            System.out.println("term不能为null或者空串");
        }

        return n;
    }


    /**
     *calculate the inverse document frequency
     * @param docs  the set of all docs
     * @param term  a word
     * @return  idf
     */

    public double idf(List<List<String>> docs, String term) {

        System.out.println("N:"+docs.size());
        System.out.println("DF:"+df(docs,term));
        return  Math.log(docs.size()/(double)df(docs,term));
    }


    /**
     * calculate tf-idf
     * @param doc a doc
     * @param docs document set
     * @param term a word
     * @return inverse document frequency
     */
    public double tfIdf(List<String> doc, List<List<String>> docs, String term) {

        return tf(doc, term) * idf(docs, term);
    }


    public static void main(String[] args) {

        List<String> doc1 = Arrays.asList("人工", "智能", "成为", "互联网", "大会", "焦点");
        List<String> doc2 = Arrays.asList("谷歌", "推出", "开源", "人工", "智能", "系统", "工具");
        List<String> doc3 = Arrays.asList("互联网", "的", "未来", "在", "人工", "智能");
        List<String> doc4 = Arrays.asList("谷歌", "开源", "机器", "学习", "工具");
        List<List<String>> documents = Arrays.asList(doc1, doc2, doc3,doc4);


        TfIdfCal calculator = new TfIdfCal();

        System.out.println(calculator.tf(doc2, "开源"));
        System.out.println(calculator.df(documents, "开源"));
        double tfidf = calculator.tfIdf(doc2, documents, "谷歌");
        System.out.println("TF-IDF (谷歌) = " + tfidf);
        System.out.println(Math.log(4/2)*1.0/7);

    }


}

运行结果:

0.14285714285714285
2
N:4
DF:2
TF-IDF (谷歌) = 0.09902102579427789
目录
相关文章
|
TensorFlow 算法框架/工具 Python
TensorFlow的历史版本与对应Python版本
TensorFlow的历史版本与对应Python版本
TensorFlow的历史版本与对应Python版本
|
2月前
|
算法 搜索推荐
经典的 TF-IDF 算法是什么?
TF-IDF是衡量词与文档相关性的经典算法,由词频(TF)和逆文档频率(IDF)相乘得出。TF反映词在文档中的重要性,IDF体现词的区分度。词频越高、文档频率越低的词,权重越大。通过累加各词项的TF-IDF值,可计算查询与文档的整体相关性,广泛应用于搜索引擎排序。
|
2月前
|
数据采集 人工智能 监控
GEO优化核心:高权重新闻信源筛选与AI收录实战指南
在 GEO(搜索引擎地理优化)实操中,新闻信源的质量直接影响内容的 AI 收录率、关键词排名及转化效果。多数开发者面临两大核心问题:1. 低价值信源浪费成本:部分新闻平台价格低廉(30-50 元 / 篇),但发布后未被 AI 抓取,无法为 GEO 排名提供权重支撑;2. 广告属性触发审核拒绝:含联系方式(电话、微信号)的软文易被平台判定为广告,导致审核驳回,影响发布效率。
|
4月前
|
机器学习/深度学习 人工智能 自然语言处理
34_GPT系列:从1到5的架构升级_深度解析
大型语言模型(LLM)的发展历程中,OpenAI的GPT系列无疑扮演着至关重要的角色。自2018年GPT-1问世以来,每一代GPT模型都在架构设计、预训练策略和性能表现上实现了质的飞跃。本专题将深入剖析GPT系列从1.17亿参数到能够处理百万级token上下文的技术演进,特别关注2025年8月8日发布的GPT-5如何引领大模型技术迈向通用人工智能(AGI)的重要一步。
|
机器学习/深度学习 PyTorch 算法框架/工具
【从零开始学习深度学习】30. 神经网络中批量归一化层(batch normalization)的作用及其Pytorch实现
【从零开始学习深度学习】30. 神经网络中批量归一化层(batch normalization)的作用及其Pytorch实现
|
11月前
|
人工智能 搜索推荐 机器人
详解:Grok 3 官网入口_Grok 3国内中文版在线使用
Grok是xAI于2023年11月推出的创新型语言模型,它可不是一般的聊天机器人
|
存储 缓存 应用服务中间件
微服务架构间数据传输,我坚决反对用缓存!
微服务架构间数据传输,我坚决反对用缓存!
411 0
|
机器学习/深度学习 PyTorch TensorFlow
conda、anaconda、pip、pytorch、tensorflow有什么关联?
conda、anaconda、pip、pytorch、tensorflow有什么关联?
439 3
|
机器学习/深度学习 自然语言处理 PyTorch
【机器学习】探索LSTM:深度学习领域的强大时间序列处理能力
【机器学习】探索LSTM:深度学习领域的强大时间序列处理能力
|
Oracle Java 关系型数据库
简单记录在Linux上安装JDK环境的步骤,以及解决运行Java程序时出现Error Could not find or load main class XXX问题
本文记录了在Linux系统上安装JDK环境的步骤,并提供了解决运行Java程序时出现的"Error Could not find or load main class XXX"问题的方案,主要是通过重新配置和刷新JDK环境变量来解决。
1135 0

热门文章

最新文章