杰卡德相似系数
两个集合A和B的交集元素在A和B的并集中所占的比例
J(A,B) = |A^B| / |A V B|
杰卡德相似系数是衡量2个集合相似度的一种指标
使用词频-逆文档频率(TF-IDF)实现关键词提取
词频-逆文档频率(Term Frequency - Inverse Document Frequency,TF-IDF)是一种用于资讯检索和文本挖掘的常用加权技术。
TF-IDF 算法的主要思想:如果某个词或短语在一篇文章中出现的频率TF高,并且在其他文章中很少出现,则认为此词或短语具有很好的类别区分能力,适合用于分类。
TF-IDF = TF * IDF
1.TF 的计算公式
TF(Term Frequency ) 表示词条 t 在文档 D 中出现的频率。TF的计算公式见式
TF = count(t) / D
式中,count(t)表示词条t出现的次数,D表示文档D中所有词条的个数。
2. IDF 的计算公式
IDF( Inverse Document Frequency)表示词条t在整个语料库中的区分能力
IDF = lg( N / 1(t,D) )
式中,N为所有文档的总数, I(t, D) 表示文档 D 是否包含词条t, 若包含则为1,不包含则为0。
但此处有一个问题:如果词条t在所有文档中都没有出现,则式中的分母为0,此时就需要对IDF做平滑处理。改善后的IDF计算公式见式。
IDF = lg( N / 1 + 1(t,D) )
3. TF-IDF = TF * IDF
最终词条t 在文档中的TF-IDF 值为:
TF-IDF = TF * IDF
从TF-IDF = TF * IDF值的计算过程中看出:一个词条在文档中出现的频率越高,且新鲜度越低(即普遍度低),则其对应的TF-IDF 值越高。
4. 举例
比如,现在有一个语料库,包含了100篇(N)论文,其中涉及包含推荐系统t的有20篇,在第一篇论文D中总共有200个技术词汇,其中推荐系统出现了15次,则词条推荐词条在第一篇论文D中的TF-IDF值为:
TF-IDF 推荐系统 = (15/200) * lg (100 / 20+ 1) = 0.05
词频-逆文档频率(Term Frequency - Inverse Document Frequency,TF-IDF)是一种用于资讯检索和文本挖掘的常用加权技术。
TF-IDF是一种统计方法,用于评估一个字词对于一个文件集或一个语料库中的一份文件的重要程度。字词的重要性随着他在文件中出现的次数成正比增加,但同时会随着他在语料库中出现频率成反比下降。
TF-IDF = TF * IDF
TF-IDF算法示例
0. 引入依赖
import numpy as np
import pandas as pd
1. 定义数据和预处理
docA = "The cat sat on my bed"
docB = "The dog sat on my knees"
# 磁带
bowA = docA.split(" ")
bowB = docB.split(" ")
bowA
# 构建词库
wordSet = set(bowA).union(set(bowB))
wordSet
{'The', 'bed', 'cat', 'dog', 'knees', 'my', 'on', 'sat'}
2. 进行词数统计
# 用统计字典来保存词出现的次数
wordDictA = dict.fromkeys( wordSet, 0 )
wordDictB = dict.fromkeys( wordSet, 0 )
# 遍历文档,统计词数
for word in bowA:
wordDictA[word] += 1
for word in bowB:
wordDictB[word] += 1
pd.DataFrame([wordDictA, wordDictB])

wordDictA
{'knees': 0,
'cat': 1,
'The': 1,
'dog': 0,
'sat': 1,
'on': 1,
'bed': 1,
'my': 1}
3. 计算词频TF
def computeTF( wordDict, bow ):
# 用一个字典对象记录tf,把所有的词对应在bow文档里的tf都算出来
tfDict = {}
nbowCount = len(bow)
for word, count in wordDict.items():
tfDict[word] = count / nbowCount
return tfDict
tfA = computeTF( wordDictA, bowA )
tfB = computeTF( wordDictB, bowB )
tfA
{'cat': 0.16666666666666666,
'on': 0.16666666666666666,
'sat': 0.16666666666666666,
'knees': 0.0,
'bed': 0.16666666666666666,
'The': 0.16666666666666666,
'my': 0.16666666666666666,
'dog': 0.0}
4. 计算逆文档频率idf
def computeIDF( wordDictList ):
#wordDictList:[{'knees': 0, 'cat': 1, 'The': 1, 'dog': 0, 'sat': 1, 'on': 1, 'bed': 1, 'my': 1}, {'knees': 1, 'cat': 0, 'The': 1, 'dog': 1, 'sat': 1, 'on': 1, 'bed': 0, 'my': 1}]
# 用一个字典对象保存idf结果,每个词作为key,初始值为0
idfDict = dict.fromkeys(wordDictList[0], 0)
#idfDict:{'knees': 0, 'cat': 0, 'The': 0, 'dog': 0, 'sat': 0, 'on': 0, 'bed': 0, 'my': 0}
N = len(wordDictList)
import math
for wordDict in wordDictList:
# 遍历字典中的每个词汇,统计Ni
for word, count in wordDict.items():
if count > 0:
# 先把Ni增加1,存入到idfDict
idfDict[word] += 1
# 已经得到所有词汇i对应的Ni,现在根据公式把它替换成为idf值
for word, ni in idfDict.items():
idfDict[word] = math.log10( (N+1)/(ni+1) )
return idfDict
idfs = computeIDF( [wordDictA, wordDictB] )
idfs
{'cat': 0.17609125905568124,
'on': 0.0,
'sat': 0.0,
'knees': 0.17609125905568124,
'bed': 0.17609125905568124,
'The': 0.0,
'my': 0.0,
'dog': 0.17609125905568124}
5. 计算TF-IDF
def computeTFIDF( tf, idfs ):
tfidf = {}
for word, tfval in tf.items():
tfidf[word] = tfval * idfs[word]
return tfidf
tfidfA = computeTFIDF( tfA, idfs )
tfidfB = computeTFIDF( tfB, idfs )
pd.DataFrame( [tfidfA, tfidfB] )

