Python如何使用k-means方法將列表中相似的句子歸類
前言
由于今年暑假在學(xué)習(xí)一些自然語言處理的東西,發(fā)現(xiàn)網(wǎng)上對(duì)k-means的講解不是很清楚,網(wǎng)上大多數(shù)代碼只是將聚類結(jié)果以圖片的形式呈現(xiàn),而不是將聚類的結(jié)果表示出來,于是我將老師給的代碼和網(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
]
#將文本中的詞語轉(zhuǎn)換為詞頻矩陣
vectorizer = CountVectorizer()
#計(jì)算個(gè)詞語出現(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é)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬蟲requests模塊之URL地址中的參數(shù)解讀
這篇文章主要介紹了Python爬蟲requests模塊之URL地址中的參數(shù)解讀,在你拿到數(shù)據(jù)所在的url地址之后,發(fā)送網(wǎng)絡(luò)請(qǐng)求時(shí),請(qǐng)求的url中包含兩種地址參數(shù):查詢參數(shù)和請(qǐng)求參數(shù),需要的朋友可以參考下2023-08-08
使用python找出list列表中相同元素(指定元素)的所有索引
這篇文章主要給大家介紹了關(guān)于使用python找出list列表中相同元素(指定元素)的所有索引,在平時(shí)開發(fā)過程中經(jīng)常遇到需要在數(shù)據(jù)中獲取特定的元素索引的信息,需要的朋友可以參考下2023-08-08
Jmeter通過OS進(jìn)程取樣器調(diào)用Python腳本實(shí)現(xiàn)參數(shù)互傳
這篇文章主要介紹了Jmeter通過OS進(jìn)程取樣器調(diào)用Python腳本實(shí)現(xiàn)參數(shù)互傳,描述在cmd中調(diào)用上面的Python腳本并傳入兩個(gè)參數(shù)展開主題,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03
Matplotlib多子圖使用一個(gè)圖例的實(shí)現(xiàn)
多子圖是Matplotlib中的一個(gè)功能,可以在同一圖形中創(chuàng)建多個(gè)子圖,本文主要介紹了Matplotlib多子圖使用一個(gè)圖例的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
python使用paramiko執(zhí)行服務(wù)器腳本并拿到實(shí)時(shí)結(jié)果
這篇文章主要介紹了python使用paramiko執(zhí)行服務(wù)器腳本并拿到實(shí)時(shí)結(jié)果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

