python - Python 中的可视化和聚类

标签 python nlp k-means

我想基于NLP算法(tf-idf)对评论进行分类。 我设法对这些簇进行分类,但我想以图形方式可视化它们(直方图、散点图...)

import collections
from nltk import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from sklearn.cluster import KMeans
from sklearn.feature_extraction.text import TfidfVectorizer
from pprint import pprint
import matplotlib.pyplot as plt
import pandas as pd
import nltk
import pandas as pd
import string
data = pd.read_excel (r'C:\Users\cra\One\intern\Book2.xlsx') 
def word_tokenizer(text):
        #tokenizes and stems the text
        tokens = word_tokenize(text)  
        stemmer = PorterStemmer() 
        tokens = [stemmer.stem(t) for t in tokens if t not in 
        stopwords.words('english')] 
        return tokens 

#tfidf convert text data to vectors 

def cluster_sentences(sentences, nb_of_clusters=5):
        tfidf_vectorizer = TfidfVectorizer(tokenizer=word_tokenizer,

        stop_words=stopwords.words('english'),#enlever stopwords
                                        max_df=0.95,min_df=0.05, 
           lowercase=True) 

        tfidf_matrix = tfidf_vectorizer.fit_transform(sentences) 
        kmeans = KMeans(n_clusters=nb_of_clusters)
        kmeans.fit(tfidf_matrix)
        clusters = collections.defaultdict(list)
        for i, label in enumerate(kmeans.labels_):
                clusters[label].append(i)
        return dict(clusters)
if __name__ == "__main__":
         sentences = data.Comment
        nclusters= 20
        clusters = cluster_sentences(sentences, nclusters) #dictionary of 
        #cluster and the index of the comment in the dataframe
        for cluster in range(nclusters):
                print ("cluster ",cluster,":")
                for i,sentence in enumerate(clusters[cluster]):
                        print ("\tsentence ",i,": ",sentences[sentence])

我得到的结果例如: 集群 6: 句子 0 : 26 RIH DP 标准 句子 1:32 RIH DP 标准 句子 2 : 68 RIH 内衬,孔内带 DP 标准 句子 3:105 RIH DP 标准 句子 4:118 RIH 标准孔中 DP 编号 句子 5:154 RIH DP 标准

请你帮帮我吧!谢谢

最佳答案

您将需要使用 t-SNE 来可视化集群 - 本文关于 visualizing and clustering US Laws使用 tf-idf 可以帮助您入门。

关于python - Python 中的可视化和聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556818/

相关文章:

python - coverity 工具可以扫描 python 代码库以查找 SCA 和安全问题吗?

具有大量 RAM 的 Python 2.7 MemoryError(64 位,Ubuntu)

用于删除互联网行话/俚语/首字母缩略词的 python 模块

python-3.x - k 表示聚类方法得分为负

用于数据流 kmeans 的 matlab 代码

python - numpy float的 "resolution"参数到底是什么

python - 无法使用系统分配的标识从 Azure 容器实例访问 Azure Vault

nlp - 使用Keras Tokenizer生成n-gram

algorithm - N-gram文本分类类别大小差异补偿

java - 如何获得weka簇质心的值(value)