Python如何使用k-means方法將列表中相似的句子歸類
前言
由于今年暑假在學(xué)習(xí)一些自然語(yǔ)言處理的東西,發(fā)現(xiàn)網(wǎng)上對(duì)k-means的講解不是很清楚,網(wǎng)上大多數(shù)代碼只是將聚類結(jié)果以圖片的形式呈現(xiàn),而不是將聚類的結(jié)果表示出來(lái),于是我將老師給的代碼和網(wǎng)上的代碼結(jié)合了一下,由于網(wǎng)上有許多關(guān)于k-means算法基礎(chǔ)知識(shí)的講解,因此我在這里就不多講解了,想了解詳細(xì)內(nèi)容的,大家可以自行百度,在這里我只把我的代碼給大家展示一下。
k-means方法的缺點(diǎn)是k值需要自己找,大家可以多換換k值,看看結(jié)果會(huì)有什么不同
代碼
# coding: utf-8
import sys
import math
import re
import docx
from sklearn.cluster import AffinityPropagation
import nltk
from nltk.corpus import wordnet as wn
from nltk.collocations import *
import numpy as np
reload(sys)
sys.setdefaultencoding('utf8')
from sklearn.feature_extraction.text import CountVectorizer
#要聚類的數(shù)據(jù)
corpus = [
'This is the first document.',#0
'This is the second second document.',#1
'And the third one.',#2
'Is this the first document?',#3
'I like reading',#4
'do you like reading?',#5
'how funny you are! ',#6
'he is a good guy',#7
'she is a beautiful girl',#8
'who am i',#9
'i like writing',#10
'And the first one',#11
'do you play basketball',#12
]
#將文本中的詞語(yǔ)轉(zhuǎn)換為詞頻矩陣
vectorizer = CountVectorizer()
#計(jì)算個(gè)詞語(yǔ)出現(xiàn)的次數(shù)
X = vectorizer.fit_transform(corpus)#獲取詞袋中所有文本關(guān)鍵詞
word = vectorizer.get_feature_names()
#類調(diào)用
transformer = TfidfTransformer()
#將詞頻矩陣X統(tǒng)計(jì)成TF-IDF值
tfidf = transformer.fit_transform(X)
#查看數(shù)據(jù)結(jié)構(gòu) tfidf[i][j]表示i類文本中的tf-idf權(quán)重
weight = tfidf.toarray()
# print weight
# kmeans聚類
from sklearn.cluster import KMeans
# print data
kmeans = KMeans(n_clusters=5, random_state=0).fit(weight)#k值可以自己設(shè)置,不一定是五類
# print kmeans
centroid_list = kmeans.cluster_centers_
labels = kmeans.labels_
n_clusters_ = len(centroid_list)
# print "cluster centroids:",centroid_list
print labels
max_centroid = 0
max_cluster_id = 0
cluster_menmbers_list = []
for i in range(0, n_clusters_):
menmbers_list = []
for j in range(0, len(labels)):
if labels[j] == i:
menmbers_list.append(j)
cluster_menmbers_list.append(menmbers_list)
# print cluster_menmbers_list
#聚類結(jié)果
for i in range(0,len(cluster_menmbers_list)):
print '第' + str(i) + '類' + '---------------------'
for j in range(0,len(cluster_menmbers_list[i])):
a = cluster_menmbers_list[i][j]
print corpus[a]
運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)根據(jù)給定坐標(biāo)點(diǎn)生成多邊形mask的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)根據(jù)給定坐標(biāo)點(diǎn)生成多邊形mask的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02
一步步教你用python連接oracle數(shù)據(jù)庫(kù)
oracle作為最強(qiáng)大的數(shù)據(jù)庫(kù),Python也提供了足夠的支持。不過與其他數(shù)據(jù)庫(kù)略有不同,下面這篇文章主要給大家介紹了關(guān)于如何使用python連接oracle數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2023-04-04
基于Python的一個(gè)自動(dòng)錄入表格的小程序
這篇文章主要介紹了基于Python的一個(gè)自動(dòng)錄入表格的小程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實(shí)例教程
wxPython是一款集成了Python的圖形化類庫(kù)的工具,而py2exe是一款將Python程序轉(zhuǎn)換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來(lái)學(xué)習(xí)Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實(shí)例教程:2016-07-07
關(guān)于jupyter代碼自動(dòng)補(bǔ)全設(shè)置方式
這篇文章主要介紹了關(guān)于jupyter代碼自動(dòng)補(bǔ)全設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
python實(shí)現(xiàn)RSA加密(解密)算法
RSA是目前最有影響力的公鑰加密算法,它能夠抵抗到目前為止已知的絕大多數(shù)密碼攻擊,已被ISO推薦為公鑰數(shù)據(jù)加密標(biāo)準(zhǔn),下面通過本文給大家介紹python實(shí)現(xiàn)RSA加密(解密)算法,需要的朋友參考下2016-02-02
Python 實(shí)現(xiàn)字符串中指定位置插入一個(gè)字符
下面小編就為大家分享一篇Python 實(shí)現(xiàn)字符串中指定位置插入一個(gè)字符,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-05-05

