日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 文本聚类算法

發(fā)布時(shí)間:2025/4/5 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 文本聚类算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

三體下載
將下載的文件重命名為santi.txt,放在文件的目錄下

#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Aug 1 18:31:11 2018@author: luogan """import jieba import re from gensim.models import word2vec import multiprocessing import gensim import numpy as np import pandas as pd import collections import pandas def segment_text(source_corpus, train_corpus, coding, punctuation):'''切詞,去除標(biāo)點(diǎn)符號(hào):param source_corpus: 原始語(yǔ)料:param train_corpus: 切詞語(yǔ)料:param coding: 文件編碼:param punctuation: 去除的標(biāo)點(diǎn)符號(hào):return:'''with open(source_corpus, 'r', encoding=coding) as f, open(train_corpus, 'w', encoding=coding) as w:for line in f:# 去除標(biāo)點(diǎn)符號(hào)line = re.sub('[{0}]+'.format(punctuation), '', line.strip())# 切詞words = jieba.cut(line)w.write(' '.join(words))#if __name__ == '__main__':# 嚴(yán)格限制標(biāo)點(diǎn)符號(hào) strict_punctuation = '。,、':∶;?‘’“”〝〞?ˇ﹕︰﹔﹖﹑·¨….?;!′?!~—ˉ|‖"〃`@﹫??﹏﹋﹌︴々﹟#﹩$﹠&﹪%*﹡﹢﹦﹤‐ ̄ˉ―﹨??﹍﹎+=<--__-\ˇ~﹉﹊()〈〉??﹛﹜『』〖〗[]《》〔〕{}「」【】︵︷︿︹︽_﹁﹃︻︶︸﹀︺︾ˉ﹂﹄︼' # 簡(jiǎn)單限制標(biāo)點(diǎn)符號(hào) simple_punctuation = '’!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' # 去除標(biāo)點(diǎn)符號(hào) punctuation = simple_punctuation + strict_punctuation# 文件編碼 coding = 'utf-8'#coding ="gb18030" # 原始語(yǔ)料 source_corpus_text = 'santi.txt'# 是每個(gè)詞的向量維度 size = 10 # 是詞向量訓(xùn)練時(shí)的上下文掃描窗口大小,窗口為5就是考慮前5個(gè)詞和后5個(gè)詞 window = 5 # 設(shè)置最低頻率,默認(rèn)是5,如果一個(gè)詞語(yǔ)在文檔中出現(xiàn)的次數(shù)小于5,那么就會(huì)丟棄 min_count = 1 # 是訓(xùn)練的進(jìn)程數(shù),默認(rèn)是當(dāng)前運(yùn)行機(jī)器的處理器核數(shù)。 workers = multiprocessing.cpu_count() # 切詞語(yǔ)料 train_corpus_text = 'words.txt' # w2v模型文件 model_text = 'w2v_size_{0}.model'.format(size)# 切詞 @TODO 切詞后注釋 segment_text(source_corpus_text, train_corpus_text, coding, punctuation)# w2v訓(xùn)練模型 @TODO 訓(xùn)練后注釋 sentences = word2vec.Text8Corpus(train_corpus_text) model = word2vec.Word2Vec(sentences=sentences, size=size, window=window, min_count=min_count, workers=workers) model.save(model_text)# 加載模型 model = gensim.models.Word2Vec.load(model_text)g= open("words.txt","r") #設(shè)置文件對(duì)象 std= g.read() #將txt文件的所有內(nèi)容讀入到字符串str中 g.close() #將文件關(guān)閉cc=std.split(' ')dd=[] kkl=dict()''' 將每個(gè)詞語(yǔ)向量化,并且append 在dd中,形成一個(gè)二維數(shù)組 并形成一個(gè)字典,index是序號(hào),值是漢字 ''' for p in range(len(cc)):hk=cc[p]if hk in model:vec=list(model.wv[hk])dd.append(vec)kkl[p]=hk#將二維數(shù)組轉(zhuǎn)化成numpydd1=np.array(dd)from sklearn.cluster import KMeans estimator = KMeans(n_clusters=100) # 構(gòu)造聚類器 estimator.fit(dd1) # 聚類 label_pred = estimator.labels_ # 獲取聚類標(biāo)簽#index 是某條向量的序號(hào),值是分類號(hào) index1=list(range(len(dd1))) vc=pd.Series(label_pred,index=index1)aa = collections.Counter(label_pred) v = pandas.Series(aa) v1=v.sort_values(ascending=False)for n in range(10):vc1=vc[vc==v1.index[n]]vindex=list(vc1.index)kkp=pd.Series(kkl)print('第',n,'類的前10個(gè)數(shù)據(jù)')ffg=kkp[vindex][:10]ffg1=list(set(ffg))print(ffg1) 第 0 類的前10個(gè)數(shù)據(jù) ['商討', '合集\u200b', '連載', '劉慈欣簡(jiǎn)介', '無(wú)奈', '編輯', '事先', '題材', '一二三', '今年']第 1 類的前10個(gè)數(shù)據(jù) ['的'] 第 2 類的前10個(gè)數(shù)據(jù) ['本書', '活', '舉', '作者', '看做', '沒想到', '朋友', '見面', '主人公', '十分之一'] 第 3 類的前10個(gè)數(shù)據(jù) ['延續(xù)', '雖', '球狀', '更是', '占', '文革', '部', '冷酷'] 第 4 類的前10個(gè)數(shù)據(jù) [nan, '物理學(xué)家', '閃電', '怎樣', '一年', '一時(shí)', '往事', '關(guān)于', '卻是'] 第 5 類的前10個(gè)數(shù)據(jù) ['人類文明', '一段', '試圖', '光年', '重新', '星空', '死亡', '內(nèi)容'] 第 6 類的前10個(gè)數(shù)據(jù) ['不了', '大家', '寫', '仔細(xì)', '有時(shí)', '空靈', '許多', '如何', '966', '心中'] 第 7 類的前10個(gè)數(shù)據(jù) ['精神', '故事', '科幻', '方式', '永遠(yuǎn)', '之前'] 第 8 類的前10個(gè)數(shù)據(jù) ['里面', '冷兵器', '了', '自己', '立刻', '中', '已', '頂上'] 第 9 類的前10個(gè)數(shù)據(jù) ['在', '的']

總結(jié)

以上是生活随笔為你收集整理的python 文本聚类算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。