【问题标题】:document clustering in pythonpython中的文档聚类
【发布时间】:2015-01-24 08:21:25
【问题描述】:

我是 python 和 scikit-learn 的新手,我将聚集一堆文本文件(新闻正文),我正在使用以下代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import nltk, sklearn, string, os
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.cluster import KMeans

# Preprocessing text with NLTK package
token_dict = {}
stemmer = PorterStemmer()

def stem_tokens(tokens, stemmer):
    stemmed = []
    for item in tokens:
        stemmed.append(stemmer.stem(item))
    return stemmed

def tokenize(text):
    tokens = nltk.word_tokenize(text)
    stems = stem_tokens(tokens, stemmer)
    return stems
###########################################################################
# Loading and preprocessing text data
print("\n Loading text dataset:")
path = 'n'

for subdir, dirs, files in (os.walk(path)):
    for i,f in enumerate(files):
        if f != '.DS_Store':
                file_path = subdir + os.path.sep + f
                shakes = open(file_path, 'r')
                text = shakes.read()
                lowers = text.lower()
                no_punctuation = lowers.translate(string.punctuation)
                token_dict[f] = no_punctuation
###########################################################################
true_k = 3 # *
print("\n Performing stemming and tokenization...")
vectorizer = TfidfVectorizer(tokenizer=tokenize, encoding='latin-1',
                              stop_words='english')
X = vectorizer.fit_transform(token_dict.values())
print("n_samples: %d, n_features: %d" % X.shape)
print()
###############################################################################
# Do the actual clustering
km = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1)
y=km.fit(X)
print(km)

print("Top terms per cluster:")
order_centroids = km.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
    print("Cluster %d:" % i, end='')
    for ind in order_centroids[i, :10]:
        print(' %s' % terms[ind], end='')
    print()

此代码正在获得最热门的单词。但是它是什么文档,我怎么知道哪些原始文本文件属于cluster0、cluster1或cluster2?

【问题讨论】:

  • 集群成员存储在km.labels_,也可以使用km.predict(X)获取。

标签: python-3.x scipy scikit-learn k-means


【解决方案1】:

再解释一下——您可以使用以下命令存储集群分配:

clusters = km.labels_.tolist()

此列表的排序将与您传递给矢量化器的 dict 相同。

我只是整理了一份文档聚类指南,您可能会觉得有帮助。如果我可以更详细地解释任何事情,请告诉我:http://brandonrose.org/clustering

【讨论】:

  • 好帖子!我看到您使用 K-means 进行聚类,使用余弦距离计算相似度。这不是问题吗?如果对向量化器结果进行归一化处理会不会更好,这会使 KMeans 表现得像球形 k-means?
  • 由于 python 2.7 代码响应 3.x 帖子,我投了反对票
  • @Schalton 很公平。随意分叉回购并将其更新为 3.whatever 如果你想帮助github.com/brandomr/document_cluster
猜你喜欢
  • 2013-02-01
  • 2012-03-01
  • 1970-01-01
  • 2015-05-13
  • 2015-04-27
  • 2012-09-05
  • 2014-10-13
  • 2013-08-13
  • 2011-12-24
相关资源
最近更新 更多