【问题标题】:How to load our own text data to scikit for MeanShift clustering?如何将自己的文本数据加载到 scikit 以进行 Mean Shift 聚类?
【发布时间】:2014-05-10 14:05:43
【问题描述】:

我打算加载我自己的一组非结构化文本数据,可以看到如下:

64.242.88.10 - - [07/Mar/2004:16:05:49 -0800] "GET /twiki/bin/edit/Main/Double_bounce_sender?topicparent=Main.ConfigurationVariables HTTP/1.1" 401 12846

或者也可以采用以下形式:

/usr/local/etc/snmp/snmpd.conf: line 68: Error: Blank line following agentaddress token.
NET-SNMP version 5.3.1

基本上,程序并不关心给定数据的结构。

我已经在 scikit 中编辑了 MeanShift 示例中给出的代码,以便我的代码加载我自己的数据集。在这种情况下,输入文件被命名为 input

for idx, line in enumerate(input):
   if(len(line) == ''):
       continue;
   line = line.strip()
   tmpNumPy = np.array([line])
   print tmpNumPy
   example = np.append(example, tmpNumPy)

# Compute clustering with MeanShift

# The following bandwidth can be automatically detected using
bandwidth = estimate_bandwidth(example, quantile=0.2, n_samples=500)

ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_

labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)

print(labels_unique)
print("number of estimated clusters : %d" % n_clusters_)

但是,当我运行此代码时,它给了我以下错误:

ValueError: data type not understood

我想知道如何使用 scikit 将一组文本数据而不是数值加载到 MeanShift 聚类中,或者我可以采取任何其他方法吗?

注意:我已经通过以下链接没有任何运气:

Meanshift in scikit learn (python) doesn't understand datatype

Loading your own text dataset to scikit-learn

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    如果您想形成文本集群,首先您需要在向量空间中表示您的文本数据集。执行此操作的标准方法称为Latent Semantic Analysis (LSA)。基本要点是您计算一个矩阵,其中每行代表一个文档,每列代表一个术语。矩阵的元素是每个文档中每个术语的术语频率 - 逆文档频率 (tfidf) 值。这个矩阵将是巨大而稀疏的。您将使用奇异值分解 (SVD) 将此矩阵的维数减少为您选择的多个“主题”。标准是主题数量为 200-500。

    用于 LSA 的优秀 Python 包称为 Gensim

    将文本表示为向量后,您可以根据它们之间的余弦距离对它们进行聚类,余弦距离表示任意两个文档在语义上的相似程度。 sklearn MeanShift 实现不允许您指定余弦距离,但如果您在聚类之前对向量进行归一化,您仍然可以很好地衡量语义相似度。

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 2017-03-22
      • 2015-02-24
      • 1970-01-01
      • 2019-12-25
      • 2014-10-02
      • 2015-01-08
      • 2016-11-23
      相关资源
      最近更新 更多