【问题标题】:Document clustering: DBSCAN and optics clustering not giving me any clusters文档集群:DBSCAN 和光学集群没有给我任何集群
【发布时间】:2018-08-29 02:49:31
【问题描述】:

我尝试使用 word2vec 加权 tfidf 向量进行 DBSCAN 聚类,并为 DBSCAN 使用不同的 epsilon 和 minpts 阈值。我还尝试了具有不同 minpts 的光学聚类方法,但它根本没有产生任何输出。

#Import libraries
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem import WordNetLemmatizer
from unidecode import unidecode # $ pip install unidecode
import gensim
import csv
import nltk
from sklearn.feature_extraction import text
import pandas as pd
import numpy as np
from collections import defaultdict
from string import lower

#read data
dat = pd.read_csv('D:\\data_800k.csv',encoding='latin',nrows=500000).Certi.tolist()

wnl = WordNetLemmatizer()
#nltk.download('punkt')
my_stop_words = text.ENGLISH_STOP_WORDSunion(['education','certification','certificate','certified'])


def tokenize_stop(row):
    az = []
    for j in nltk.word_tokenize(lower(unidecode(row))):
        if j not in my_stop_words:
            az.extend([j])
    return az

def preprocess(dat):
    return [tokenize_stop(row) for row in dat]


X = preprocess(dat)

#word2vec
model = gensim.models.Word2Vec(X, size=100)
w2v = dict(zip(model.wv.index2word, model.wv.syn0))


#
tfidf = TfidfVectorizer(analyzer=lambda x: x)
tfidf.fit(X)
max_idf = max(tfidf.idf_)

#train model
def fit(X):
    tfidf = TfidfVectorizer(analyzer=lambda x: x)
    tfidf.fit(X)
    # if a word was never seen - it must be at least as infrequent
    # as any of the known words - so the default idf is the max of 
    # known idf's
    max_idf = max(tfidf.idf_)
    return defaultdict(
        lambda: max_idf,
        [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])

#actual training//
word2weight = fit(X)

#multiply word2vec with tfidf
def transform_word2vec_tfidf(X,word2vec,word2weight):
    return np.array([
            np.mean([word2vec[w] * word2weight[w]
                     for w in words if w in word2vec] or
                    [np.zeros(dim)], axis=0)
            for words in X
        ])


export_data_w2v_Tfidf = transform_word2vec_tfidf(X,w2v,word2weight)
np.savetxt('D:\Azim\data_500k_w2v_tfidf.csv',export_data_w2v_Tfidf,delimiter=',',fmt=('%1.15e'))

以下是 ELKI 截图。如果他们能够使用 DBSCAN 或任何其他算法对文本数据进行有意义的聚类,任何人都可以分享见解吗?谢谢

【问题讨论】:

  • 您尝试过更大的 Epsilon 吗?一个 kdist 情节?光学图?
  • @Anony-Mousse 是的,在更大的 epsilon 下,它会耗尽内存。与光学相同
  • 先用一个子集做实验。在找到正确的方式之前无需解决可伸缩性问题。
  • 我为 50000 个文档做了,然后尝试对整个数据集使用 epsilon 值。这个 stackoverflow.com 问题的结果是针对整个数据的。
  • 另外,minpts 值太高了。

标签: python cluster-analysis dbscan elki


【解决方案1】:

我不认为 DBSCAN 是一种有前途的文本数据方法。选择参数会很困难(但正如 cmets 中所述,您的 minpts 可能太多太大了),而且显然您也遇到了可伸缩性问题。我也不确定你的“word2vec”有什么影响。 word2vec 在这里可能会使事情变得更加困难。

我宁愿选择 LDA。这通常是文本的最佳方法。

当您使用 OPTICS 时,请注意 OPTICS 不会产生分区。它产生光学绘图。你需要例如Xi 方法来提取分区,并且增加了另一个在高维数据中可能难以选择的参数。

【讨论】:

  • 你能推荐一个好的文本聚类方法,除了 Lda 吗?我尝试使用 Lda 并在那里遇到问题,因为 Lda 没有提供任何硬集群。 stackoverflow.com/q/49380258/4566277
  • 是的,LDA 提供了软聚类。对于文本数据,您需要软聚类以获得最佳结果。
猜你喜欢
  • 2018-11-22
  • 2018-09-01
  • 2014-10-20
  • 2015-08-15
  • 2021-07-07
  • 2021-03-28
  • 2011-11-28
  • 2013-08-20
  • 2018-09-21
相关资源
最近更新 更多