一、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

TF-IDF词项权重计算的更多相关文章

  1. TF/IDF(term frequency/inverse document frequency)

    TF/IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明. 一. TF/IDF描述单个term与特定document的相 ...

  2. 文本分类学习(三) 特征权重(TF/IDF)和特征提取

    上一篇中,主要说的就是词袋模型.回顾一下,在进行文本分类之前,我们需要把待分类文本先用词袋模型进行文本表示.首先是将训练集中的所有单词经过去停用词之后组合成一个词袋,或者叫做字典,实际上一个维度很大的 ...

  3. ElasticStack学习(九):深入ElasticSearch搜索之词项、全文本、结构化搜索及相关性算分

    一.基于词项与全文的搜索 1.词项 Term(词项)是表达语意的最小单位,搜索和利用统计语言模型进行自然语言处理都需要处理Term. Term的使用说明: 1)Term Level Query:Ter ...

  4. 关键词权重计算算法:TF-IDF

    TF-IDF(Term Frequency–Inverse Document Frequency)是一种用于资讯检索与文本挖掘的常用加权技术.TF-IDF是一种统计方法,用以评估一字词对于一个文件集或 ...

  5. TF/IDF计算方法

    FROM:http://blog.csdn.net/pennyliang/article/details/1231028 我们已经谈过了如何自动下载网页.如何建立索引.如何衡量网页的质量(Page R ...

  6. 信息检索中的TF/IDF概念与算法的解释

    https://blog.csdn.net/class_brick/article/details/79135909 概念 TF-IDF(term frequency–inverse document ...

  7. (6)文本挖掘(三)——文本特征TFIDF权重计算及文本向量空间VSM表示

    建立文本数据数学描写叙述的过程分为三个步骤:文本预处理.建立向量空间模型和优化文本向量. 文本预处理主要採用分词.停用词过滤等技术将原始的文本字符串转化为词条串或者特点的符号串.文本预处理之后,每个文 ...

  8. tf-idf 词条权重计算

    在文本分类问题中,某些高频词一直出现,这样的词对区分文档的作用不大,例如: D1:  'Job was the chairman of Apple Inc.' D2:  'I like to use ...

  9. tf–idf算法解释及其python代码实现(下)

    tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...

随机推荐

  1. 转: springboot2.0下hystrix.stream 404

    springboot2.0下hystrix dashboard Unable to connect to Command Metric Stream解决办法https://blog.csdn.net/ ...

  2. BIO NIO AIO 简介

    原文: https://github.com/zhongmingmao/nio_demo 简介 NIO与AIO的简单使用 基本概念 同步与异步 同步和异步是针对应用程序和内核的交互而言的:同步指的是用 ...

  3. VS2005快捷键大全

    快捷键功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT + C显示类视图 ...

  4. Django基于正则表达式的URL(2)

    Django基于正则表达式的URL(2) 1. 关于正则的说明 url(r'^detail-(\d+)-(\d+).html',views.detail), 当客户端输入 127.0.0.1:8000 ...

  5. MySQL Metadata Lock详解

    Metadata Lock 的作用: 要直接说出Metadata Lock 的作用.以我目前的文字功底是不行的.好在我可以通过一个例子来说明. 假设session 1 在正在执行如下的SQL语句 se ...

  6. redis 3.2.3的源码安装

    Install necessary packages On CentOS : yum install wget make gcc tcl On CentOS yum install wget make ...

  7. Android 7.0 Dialog 无法显示的问题

    app 在 Android 7.0 上登录的时候, Dialog 不显示了,但是半透明背景显示 经过搜索和对比,发现出现该问题是因为重写了 getResources() 方法造成的 .重写该方法是为了 ...

  8. 基于Zynq平台的EtherCAT主站方案实现

    作者:陈秋苑 谢晓锋 陈海焕 广州虹科电子科技有限公司 摘 要:EtherCAT 是开放的实时以太网通讯协议,由德国倍福自动化有限公司研发.EtherCAT 具有高性能.低成本.容易使用等特点,目前在 ...

  9. 我的IT之路2013(二)

    严寒即将过去,温暖的春天正在向我们招手,欢呼吧,在迎接新的开始的同时,不要忘了回顾一下过去的这一年,总结一下过去的这一年有什么得失. 英语学习 13年下半年,最大的变化就是有很大一部分时间用来学英语. ...

  10. VS2015终极卸载方法

    今天打开VS2015发现出问题了,总是停止响应,去控制面板里卸载结果像下面这样,卸载出错!于是我有开始折腾了,重新安装一遍然后,还是有问题,在卸载还是出错于是我决定通过安装介质卸载,结果,悲剧的是,启 ...