基于Pyspark的TF-IDF英文關鍵詞確定

      網友投稿 993 2025-03-31

      文章目錄

      一、TF-IDF回顧

      二、PySpark注意事項

      三、具體代碼

      四、結果分析

      一、TF-IDF回顧

      TF-IDF(Term Frequency/Inverse Document Frequency,詞頻-逆文檔頻率)算法,可以找出文檔中的關鍵詞,

      顧名思義,TF-IDF 分數由兩部分組成:

      第一部分是TF詞語頻率(Term Frequency),

      第二部分是IDF逆文檔頻率(Inverse Document Frequency)。

      其中計算語料庫中文檔總數除以含有該詞語的文檔數量,然后再取對數就是逆文檔頻率。

      TF(t)= 該詞語在當前文檔出現的次數 / 當前文檔中詞語的總數

      IDF(t)= log_e(文檔總數 / 出現該詞語的文檔總數)即:

      I D F ( x ) = log ? N N ( x ) I D F(x)=\log \frac{N}{N(x)} IDF(x)=logN(x)N

      IDF反應了一個詞在所有文本中出現的頻率,如果一個詞在很多的文本中出現,那么它的IDF值應該低,比如I come to China to travel中的“to”。而反過來如果一個詞在比較少的文本中出現,那么它的IDF值應該高。一個極端的情況,如果一個詞在所有的文本中都出現,那么它的IDF值應該為0。

      二、PySpark注意事項

      如果之前跑過數據,需要刪除緩存中的rdd數據再跑:normalized_document_tfidf_rdd.unpersist()。

      三、具體代碼

      from pyspark import SparkConf, SparkContext import math #以下為計算過程中需要用到的幾個函數 # 該函數主要是統計一個文檔中包含哪些單詞 def word_contains(words_list): words_set=set(words_list)#將列表轉為set,去除重復的單詞 return list(words_set)#再將set轉為列表返回 # 計算每個單詞的逆文檔頻率idf def computeIDF(word_df_tuple,num_document): word=word_df_tuple[0] df=word_df_tuple[1] #根據逆文檔頻率計算公式計算idf值 word_idf = math.log(float(num_document+1) / float(df+1), 2) return (word, word_idf)#以一個元組tuple的形式返回一個單詞的dif值 # 計算每個文檔中單詞的tf值,并將文檔轉成向量 def computeTF(words_list, all_words_list): words_num=len(words_list)#獲取文檔中出現的單詞的個數 words_dic={} for word in words_list:#統計文檔中每個單詞出現的次數 if word in words_dic.keys(): words_dic[word]+=1 else: words_dic[word]=1 tf_vector=[] for word in all_words_list:#將文檔轉為一個tf值向量并返回 if word in words_dic.keys(): tf=float(words_dic[word])/words_num tf_vector.append(tf) else: tf_vector.append(0) return tf_vector # 計算每個文檔向量中每個單詞的tfidf值 def computeTFIDF(tf_vector, words_idf_dic,all_words_list): i=0 tfidf_vector=[] for word in all_words_list:#將每個單詞的tf值和idf值相乘 tfidf=tf_vector[i]*words_idf_dic[word] tfidf_vector.append(tfidf) i+=1 return tfidf_vector # 對每個tfidf向量進行歸一化 def nomoralize(tfidf_vector): new_vector=[] sum=0 for item in tfidf_vector: sum+=math.pow(item,2) sqrt_sum=math.sqrt(sum) for item in tfidf_vector: new_item=item/sqrt_sum new_vector.append(new_item) return new_vector #主程序 if __name__ == "__main__": #conf = SparkConf().setAppName("tfidf") #sc = SparkContext(conf=conf) # 刪除緩存中的rdd數據 # normalized_document_tfidf_rdd.unpersist() #示例文檔數據,每個文檔是一個單詞列表 documents_list=[["hello","world","china","good","spark","good"], ["hello","china","china","great","love","china"], ["love","spark","spark","good","hello","spark"]] #documents_list=[["hello","friends","today","is","my","holiday"], # ["hello","china","china","great","love","china"], # ["love","spark","spark","good","hello","spark"]] #創建RDD并進行緩存 tokenized_document_rdd=sc.parallelize(documents_list).cache() print ("*************************** compute idf************************************") #這個階段的主要操作是計算單詞的idf值 #獲取文檔的個數用來計算逆文檔頻率 num_document=tokenized_document_rdd.count() #計算每個單詞的文檔支持度 #實現思路是,針對每個文本文檔,通過將單詞列表轉成set來獲取每個文檔中出現的單詞,然后 #通過flatMap操作,將每個文檔出現的單詞合并成一個新的集合。在新的集合中,一個單詞出現 #的次數即是其文檔支持度。因此,我們可以在flatMap操作之后應用map和reducebykey操作來統 #計每個單詞的文檔支持度。 words_df_rdd=tokenized_document_rdd.flatMap(lambda words_list:word_contains(words_list)) \ .map(lambda word:(word,1)) \ .reduceByKey(lambda a,b:a+b) #根據單詞的文檔頻率和文檔的總數計算每個單詞的idf # computeIDF函數實現的是具體計算idf的值 words_idf_rdd=words_df_rdd.map(lambda word_df_tuple: computeIDF(word_df_tuple, num_document)) print ("*********************************** compute tf *******************************") #計算每個文本中每個單詞出現的頻次,進而計算tf值 #返回包含所有單詞的列表 #flatMap是將所有文檔中的單詞合并成一個大的列表,distinct是將列表中重復的單詞去除 all_words_list= tokenized_document_rdd.flatMap(lambda words_list:words_list) \ .distinct() \ .collect() #考慮到單詞可能很多,我們將包含所有單詞的all_words_list變量做出廣播變量,使得一個executor #上的多個Task可以共享該變量 all_words_broadcast=sc.broadcast(all_words_list) #計算單詞的tf,得到文檔的tf向量 document_tf_rdd= tokenized_document_rdd.map(lambda words_list: computeTF(words_list, all_words_broadcast.value)) print ("******************************* compute tfidf*********************************") #提取從rdd中提取每個單詞的idf值,并將提取的列表變量轉成字典變量,進而轉成廣播變量,以 #供發送給各個executor計算每個文檔中每個單詞的tfidf值 words_idf_list= words_idf_rdd.collect() words_idf_dic={} for item in words_idf_list:#將單詞的idf值列表轉為字典易于獲取每個單詞的idf值 words_idf_dic[item[0]]=item[1] words_idf_broadcast=sc.broadcast(words_idf_dic) #計算每個文本中每個單詞的tfidf值 document_tfidf_rdd= document_tf_rdd.map(lambda words_tf_list:computeTFIDF(words_tf_list, words_idf_broadcast.value,all_words_broadcast.value)) #將每個文本對應的列表向量進行歸一化 normalized_document_tfidf_rdd= document_tfidf_rdd.map(lambda tfidf_vector: nomoralize(tfidf_vector)) print ("************************** print tfidf vectors*********************************") #打印輸出每個tfidf向量 tfidf_vectors= normalized_document_tfidf_rdd.collect() num = 0 for item in tfidf_vectors: print (item) num = num + 1 print("第%d條文本:" % num) print("當前文本的tfidf向量: \n", item) print(documents_list[num - 1]) print("最大值是:", p.max(item), "所在的下標是:", item.index(p.max(item))) # tf-idf值最大的單詞 print("tfidf值最大的單詞", documents_list[num - 1][ item.index(p.max(item)) ], "\n")

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      基于Pyspark的TF-IDF英文關鍵詞確定

      112

      113

      114

      115

      116

      117

      118

      119

      120

      121

      122

      123

      124

      125

      126

      127

      128

      129

      130

      131

      132

      133

      134

      135

      136

      137

      138

      139

      140

      141

      四、結果分析

      每條句子(文檔)的tf-idf最大的單詞也打印出來了:

      *************************** compute idf************************************ *********************************** compute tf ******************************* ******************************* compute tfidf********************************* ************************** print tfidf vectors********************************* [0.5820915838854853, 0.0, 0.29104579194274266, 0.0, 0.29104579194274266, 0.7012517964002163, 0.0] 第1條文本: 當前文本的tfidf向量: [0.5820915838854853, 0.0, 0.29104579194274266, 0.0, 0.29104579194274266, 0.7012517964002163, 0.0] ['hello', 'china', 'china', 'great', 'love', 'china'] 最大值是: 0.7012517964002163 所在的下標是: 5 tfidf值最大的單詞 china [0.0, 0.0, 0.0, 0.6060537877905645, 0.7546051455392007, 0.0, 0.2515350485130669] 第2條文本: 當前文本的tfidf向量: [0.0, 0.0, 0.0, 0.6060537877905645, 0.7546051455392007, 0.0, 0.2515350485130669] ['hello', 'china', 'china', 'great', 'love', 'china'] 最大值是: 0.7546051455392007 所在的下標是: 4 tfidf值最大的單詞 love [0.30151134457776363, 0.0, 0.9045340337332908, 0.0, 0.0, 0.0, 0.30151134457776363] 第3條文本: 當前文本的tfidf向量: [0.30151134457776363, 0.0, 0.9045340337332908, 0.0, 0.0, 0.0, 0.30151134457776363] ['hello', 'china', 'china', 'great', 'love', 'china'] 最大值是: 0.9045340337332908 所在的下標是: 2 tfidf值最大的單詞 china

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:OCR技術專題系列二:TensorRT入門實踐
      下一篇:wps office怎么統計數量?
      相關文章
      亚洲爆乳成av人在线视菜奈实 | 亚洲第一视频网站| 亚洲国产av无码精品| 久久精品九九亚洲精品| 亚洲午夜精品第一区二区8050| 亚洲老熟女五十路老熟女bbw| 小说专区亚洲春色校园| 婷婷亚洲久悠悠色悠在线播放| 国产亚洲精品影视在线| 亚洲阿v天堂在线| 亚洲日本乱码在线观看| 亚洲av中文无码| 亚洲无人区码一二三码区别图片| 亚洲成a人无码亚洲成www牛牛| 亚洲性天天干天天摸| 亚洲七七久久精品中文国产| 亚洲成a人片在线不卡一二三区| 亚洲精品字幕在线观看| 亚洲人成人77777在线播放| 伊人久久亚洲综合| 亚洲AV永久无码精品网站在线观看| 亚洲av色福利天堂| 伊人久久大香线蕉亚洲| 色偷偷亚洲女人天堂观看欧| 女bbbbxxxx另类亚洲| 亚洲另类春色校园小说| 亚洲日韩欧洲乱码AV夜夜摸| 毛片亚洲AV无码精品国产午夜| 亚洲VA中文字幕不卡无码| 亚洲国产精品成人综合色在线| 久久精品国产亚洲av麻豆蜜芽| 色老板亚洲视频免在线观| 亚洲成人中文字幕| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲中文字幕一二三四区| 亚洲国产另类久久久精品黑人| 亚洲国产精品一区| 亚洲真人无码永久在线| 狠狠亚洲狠狠欧洲2019| 亚洲国产欧洲综合997久久| 亚洲精品高清国产一久久|