banner

短文本主题建模方法

作者: afenxi来源: afenxi时间:2017-05-08 15:47:48

摘要:许多数据分析应用都会涉及到从短文本中提取出潜在的主题,比如微博、短信、日志文件或者评论数据。

1. 引言

许多数据分析应用都会涉及到从短文本中提取出潜在的主题,比如微博、短信、日志文件或者评论数据。一方面,提取出潜在的主题有助于下一步的分析,比如情感评分或者文本分类模型。另一方面,短文本数据存在一定的特殊性,我们无法直接用传统的主题模型算法来处理它。短文本数据的主要难点在于:

短文本数据中经常存在多词一义的现象[1],比如 “dollar”, "$", "$$", "fee", "charges" 拥有相同的含义,但是受限于文本篇幅的原因,我们很难直接从短文本数据中提取出这些信息。 与长文档不同的地方在于,短文本数据中通常只包含一个主题。这看似很好处理,但是传统的主题模型算法都假设一篇文档中包含多个主题,这给建模分析带来了不小的麻烦。

主题提取模型通常包含多个流程,比如文本预处理、文本向量化、主题挖掘和主题表示过程。每个流程中都有多种处理方法,不同的组合方法将会产生不同的建模结果。

本文将主要从实际操作的角度来介绍不同的短文本主题建模算法的优缺点,更多理论上的探讨可以参考以下文章。

下文中我将自己创建一个数据集,并利用 Python scikit-learn 来拟合相应的主题模型。

2. 主题发现模型

本文主要介绍三个主题模型, LDA(Latent Dirichlet Allocation), NMF(Non-Negative Matrix Factorization)和SVD(Singular Value Decomposition)。本文主要采用 scikit-learn 来实现这三个模型。

除了这三个模型外,还有其他一些模型也可以用来发现文档的结构。其中最重要的一个模型就是 KMeans 聚类模型,本文将对比 KMeans 聚类模型和其他主题模型的拟合效果。

In [1]:

%matplotlib inline import numpy as np import matplotlib.pyplot as plt from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer from sklearn.decomposition import TruncatedSVD, NMF, LatentDirichletAllocation from sklearn.cluster import KMeans

首先,我们需要构建文本数据集。本文将以四个自己构建的文本数据集为例来构建主题模型:

clearcut topics: 该数据集中只包含两个主题—— "berger-lovers" 和 "sandwich-haters"。 unbalanced topics: 该数据集与第一个数据集包含的主题信息一致,但是此数据集的分布是有偏的。 semantic topics: 该数据集包含四个主题,分别是 "berger-lovers", "berger-haters","sandwich-lovers" 和 "sandwich-haters"。此外,该数据集中还包含了两个潜在的主题 “food” 和 “feelings”。 noisy topics: 正如前文所说的,短文本数据中经常存在多词一义的现象,该数据集主要用于模拟两个主题不同类型的文本。该数据集文本的篇幅小于其他三个数据集,这可以用来检验模型是否能够很好地处理短文本数据。

In [2]:

def generate_clearcut_topics(): ## for demostration purpose, dont take it personally : ) return np.repeat(["we love bergers", "we hate sandwiches"], [1000, 1000]) def generate_unbalanced_topics(): return np.repeat(["we love bergers", "we hate sandwiches"], [10, 1000]) def generate_semantic_context_topics(): return np.repeat(["we love bergers" , "we hate bergers" , "we love sandwiches" , "we hate sandwiches"], 1000) def generate_noisy_topics(): def _random_typos(word, n): typo_index = np.random.randint(0, len(word), n) return [word[:i]+"X"+word[i+1:] for i in typo_index] t1 = ["we love %s" % w for w in _random_typos("bergers", 15)] t2 = ["we hate %s" % w for w in _random_typos("sandwiches", 15)] return np.r_[t1, t2] sample_texts = topicfinder = topic_models[topic_model](n_topics, **kwargs).fit(text_vec) topic_dists = topicfinder.components_ if topic_model is not "kmeans" else topicfinder.cluster_centers_ topic_dists /= topic_dists.max(axis = 1).reshape((-1, 1)) ## 3. keywords for topics ## Unlike other models, LSA_SVD will generate both positive and negative values in topic_word distribution, ## which makes it more ambiguous to choose keywords for topics. The sign of the weights are kept with the ## words for a demostration here def _topic_keywords(topic_dist): keywords_index = np.abs(topic_dist) >= thr keywords_prefix = np.where(np.sign(topic_dist) > 0, "", "^")[keywords_index] keywords = " | ".join(map(lambda x: "".join(x), zip(keywords_prefix, words[keywords_index]))) return keywords topic_keywords = map(_topic_keywords, topic_dists) return "n".join("Topic %i: %s" % (i, t) for i, t in enumerate(topic_keywords)) 2.1 SVD: 正交分解

sklearn 中的 truncated SVD implementation 类似于主成分分析算法,它们都试图利用正交分解的方法选择出具有最大方差的变量信息。

对于 clearcut-topic 数据集来说,我们分别利用 TF 和 TFIDF方法来向量化文本数据,并构建 SVD 模型,模型的拟合结果如下所示。正如我们之前所提到的,SVD 模型所提取的关键词中包含正负向词语。为了简单起见, 我们可以理解为该主题包含正向词语,不包含负向的词语。

比如,对于 "Topic 1: bergers | ^hate | love | ^sandwiches" 来说,该文本的主题中包含 "love bergers" 但是不包含 "hate sandwiches"。

由于模型的随机效应,所以每次运行模型得到的结果都会存在细微的差异。在 SVD 的拟合结果中我们发现发现 Topic 3: bergers | ^hate | ^love | sandwiches 成功地提取了 “food” 的主题。

In [9]:

print(find_topic(sample_texts["clearcut topics"], "svd", 4, vec_model="tf"))

Topic 0: bergers | hate | love | sandwiches | we Topic 1: bergers | ^hate | love | ^sandwiches Topic 2: bergers | hate | love | sandwiches | ^we Topic 3: bergers | ^hate | ^love | sandwiches

In [10]:

print(find_topic(sample_texts["clearcut topics"], "svd", 4, vec_model="tfidf"))

Topic 0: bergers | hate | love | sandwiches | we Topic 1: bergers | ^hate | love | ^sandwiches Topic 2: bergers | hate | love | sandwiches | ^we Topic 3: bergers | ^hate | ^love | sandwiches

在上述的例子中,我们设定了过多的主题数量,这是因为大多数时候我们无法事先知道某个文档包含多少个主题。如果我们令主题个数等于2,可以得到下述结果:

In [11]:

print(find_topic(sample_texts["clearcut topics"], "svd", 2, vec_model="tfidf"))

Topic 0: bergers | hate | love | sandwiches | we Topic 1: bergers | ^hate | love | ^sandwiches

当我们在解释 SVD 模拟的拟合结果时,我们需要对比多个主题的信息。比如上述的模型拟合结果可以解释成:数据集中文档的主要差异是文档中包含 “love bergers” 但不包含 “hate sandwiches”。

接下来我们将利用 SVD 来拟合 unbalanced topic 数据集,检验该模型处理非平衡数据集的效果。

In [12]:

print(find_topic(sample_texts["unbalanced topics"], "svd", 3, vec_model="tf"))

Topic 0: hate | sandwiches | we Topic 1: bergers | ^hate | love | ^sandwiches | we Topic 2: bergers | hate | love | sandwiches | ^we

从下述结果中可以看出,SVD无法处理噪声数据,即无法从中提取出主题信息。

In [13]:

print(find_topic(sample_texts["noisy topics"], "svd", 2, vec_model="tf"))

Topic 0: bergerx | bergexs | bergxrs | berxers | bxrgers | hate | love | sandwichex | sandwixhes | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches | xergers Topic 1: ^bergerx | ^bergexs | ^bergxrs | ^berxers | ^bxrgers | hate | ^love | sandwichex | sandwixhes | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches | ^xergers 2.2 LDA: 根据词语的共现频率来提取主题

LDA 是最常用的主题提取模型之一,因为该模型能够处理多种类型的文本数据,而且模拟的拟合效果非常易于解释。

直观上来看,LDA 根据不同文档中词语的共现频率来提取文本中潜在的主题信息。另一方面,具有相同主题结构的文本之间往往非常相似,因此我们可以根据潜在的主题空间来推断词语之间的相似性和文档之间的相似性。

LDA 算法中主要有两类参数:

每个主题中各个关键词的分布参数 每篇文档中各个主题的分布参数

接下来我们将研究这些参数是如何影响 LDA 模型的计算过程,人们更多的是根据经验来选择最佳参数。

与 SVD 模型不同的是,LDA 模型所提取的主题非常好解释。以 clearcut-topics 数据集为例,LDA 模型中每个主题都有明确的关键词,它和SVD主要有以下两个区别:

LDA 模型中可能存在重复的主题 不同的主题可以共享相同的关键词,比如单词 “we” 在所有的主题中都出现了。

此外,对 LDA 模型来说,采用不同的文本向量化方法也会得到不同的结果。

In [14]:

print(find_topic(sample_texts["clearcut topics"], "lda", 4, vec_model="tf"))

Topic 0: bergers | love | we Topic 1: bergers | love | we Topic 2: hate | sandwiches | we Topic 3: bergers | love | we

In [15]:

print(find_topic(sample_texts["clearcut topics"], "lda", 4, vec_model="tfidf"))

Topic 0: bergers | love | we Topic 1: bergers | love | we Topic 2: bergers | love | we Topic 3: hate | sandwiches | we

在 sklearn 中,参数 topic_word_prior 和 doc_topic_prior 分别用来控制 LDA 模型的两类参数。

其中 topic_word_prior 的默认值是 1n_topics1n_topics,这意味着主题中的每个词语服从均匀分布。

In [16]:

print(find_topic(sample_texts["unbalanced topics"], "lda", 4, vec_model="tf"))

Topic 0: bergers | hate | love | sandwiches | we Topic 1: hate | sandwiches | we Topic 2: bergers | hate | love | sandwiches | we Topic 3: hate | sandwiches | we

选择更小的 topic_word_prior 参数值可以提取粒度更小的主题信息,因为每个主题中都会选择更少的词语。

In [17]:

print(find_topic(sample_texts["unbalanced topics"], "lda", 4, vec_model="tf", topic_word_prior=1e-5))

Topic 0: bergers | love | we Topic 1: hate | sandwiches | we Topic 2: hate | sandwiches | we Topic 3: hate | sandwiches | we

LDA 模型同样无法很好地处理 noisy topics 数据集,从下述结果中可以看出 LDA 模型提取的主题相当模糊:

In [18]:

print find_topic(sample_texts["noisy topics"],"lda",3, vec_model = "tfidf")

Topic 0: bergerx | bergexs | bergxrs | berxers | bxrgers | hate | love | sandwichex | sandwixhes | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches | xergers Topic 1: bergerx | bergexs | bergxrs | berxers | bxrgers | hate | love | sandwichex | sandwixhes | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches | xergers Topic 2: bergerx | bergexs | bergxrs | berxers | bxrgers | hate | love | sandwichex | sandwixhes | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches | xergers 2.3 NMF

NMF 可以视为 LDA模型的特例,从理论上来说,这两个模型之间的联系非常复杂。但是在实际应用中,NMF 经常被视为参数固定且可以获得稀疏解的 LDA 模型。虽然 NMF 模型的灵活性不如 LDA 模型,但是该模型可以很好地处理短文本数据集。

另一方面,NMF 最大的缺点是拟合结果的不一致——当我们设置过大的主题个数时,NMF 拟合的结果非常糟糕。相比之下,LDA模型的拟合结果更为稳健。

首先我们来看下 NMF 模型不一致的拟合结果。对于 clearcut topics 数据集来说,当我们设置提取5个主题时,其结果和真实结果非常相似:

In [19]:

print(find_topic(sample_texts["clearcut topics"], "nmf", 5, vec_model="tf"))

Topic 0: bergers | love | we Topic 1: hate | sandwiches | we Topic 2: bergers | love | we Topic 3: bergers | love | we Topic 4: hate | sandwiches | we

但是当我们增加主题个数时(远大于真实主题数2),NMF 模型将会得到一些奇异的结果:

In [20]:

print(find_topic(sample_texts["clearcut topics"], "nmf", 25, vec_model="tf"))

Topic 0: we Topic 1: bergers | love | we Topic 2: hate | sandwiches | we Topic 3: bergers | love | we Topic 4: hate | sandwiches | we Topic 5: bergers | love | we Topic 6: bergers | love | we Topic 7: bergers | love | we Topic 8: hate | sandwiches | we Topic 9: hate | sandwiches | we Topic 10: hate | sandwiches | we Topic 11: bergers | love | we Topic 12: we Topic 13: hate | sandwiches Topic 14: we Topic 15: we Topic 16: bergers | love | we Topic 17: sandwiches | we Topic 18: we Topic 19: bergers | love | we Topic 20: hate | sandwiches | we Topic 21: sandwiches | we Topic 22: bergers | love | we Topic 23: we Topic 24: bergers | love | we

相比之下,LDA模型的结果十分稳健。

In [21]:

print(find_topic(sample_texts["clearcut topics"], "lda", 25, vec_model="tf"))

Topic 0: bergers | love | we Topic 1: bergers | love | we Topic 2: hate | sandwiches | we Topic 3: bergers | hate | love | sandwiches | we Topic 4: bergers | love | we Topic 5: bergers | love | we Topic 6: bergers | love | we Topic 7: bergers | love | we Topic 8: bergers | hate | love | sandwiches | we Topic 9: bergers | hate | love | sandwiches | we Topic 10: bergers | hate | love | sandwiches | we Topic 11: hate | sandwiches | we Topic 12: bergers | hate | love | sandwiches | we Topic 13: bergers | love | we Topic 14: hate | sandwiches | we Topic 15: bergers | love | we Topic 16: bergers | love | we Topic 17: bergers | hate | love | sandwiches | we Topic 18: hate | sandwiches | we Topic 19: bergers | love | we Topic 20: bergers | love | we Topic 21: bergers | hate | love | sandwiches | we Topic 22: hate | sandwiches | we Topic 23: bergers | love | we Topic 24: hate | sandwiches | we

对于非平衡数据集,设置好合适的主题个数,NMF 可以很好地提取出文档中的主题信息。

In [22]:

print(find_topic(sample_texts["unbalanced topics"], "nmf", 5, vec_model="tfidf"))

Topic 0: hate | sandwiches | we Topic 1: hate | sandwiches | we Topic 2: hate | sandwiches | we Topic 3: hate | sandwiches | we Topic 4: bergers | love | we

值得注意的是,NMF 是本文提到的四个模型中唯一一个能够较好地处理 noisy topics 数据的模型:

In [23]:

print find_topic(sample_texts["noisy topics"],"nmf",5, vec_model = "tfidf",)

Topic 0: bergerx | bergexs | bergxrs | bxrgers | love | we Topic 1: bergxrs | hate | sandwichex | sandwxches | sandxiches | sanxwiches | saxdwiches | sxndwiches | we | xandwiches Topic 2: bergexs | bergxrs | berxers | bxrgers | love | we Topic 3: bergexs | bergxrs | bxrgers | love | we | xergers Topic 4: hate | sandwixhes | sandwxches | sxndwiches | we | xandwiches 2.4 KMeans

类似于 KMeans 模型的聚类方法能够根据文档的向量形式对其进行分组。然而这个模型无法看成是主题模型,因为我们很难解释聚类结果中的关键词信息。

但是如果结合 TF或TFIDF方法,我们可以将 KMeans 模型的聚类中心视为一堆词语的概率组合:

In [24]:

print find_topic(sample_texts["clearcut topics"],"kmeans",10, vec_model = "tf",)

Topic 0: hate | sandwiches | we Topic 1: bergers | love | we Topic 2: hate | sandwiches | we Topic 3: bergers | love | we Topic 4: bergers | love | we Topic 5: bergers | love | we Topic 6: bergers | love | we Topic 7: bergers | love | we Topic 8: bergers | love | we Topic 9: bergers | love | we

In [25]:

print find_topic(sample_texts["unbalanced topics"],"kmeans",10, vec_model = "tf",)

Topic 0: hate | sandwiches | we Topic 1: bergers | love | we Topic 2: hate | sandwiches | we Topic 3: hate | sandwiches | we Topic 4: hate | sandwiches | we Topic 5: hate | sandwiches | we Topic 6: hate | sandwiches | we Topic 7: hate | sandwiches | we Topic 8: hate | sandwiches | we Topic 9: hate | sandwiches | we

In [26]:

print find_topic(sample_texts["noisy topics"],"kmeans",10, vec_model = "tf",)

Topic 0: hate | sandwxches | sxndwiches | we | xandwiches Topic 1: bergexs | berxers | bxrgers | love | we Topic 2: bergerx | love | we Topic 3: hate | sandxiches | we Topic 4: love | we | xergers Topic 5: hate | sandwixhes | we Topic 6: hate | sandwichex | we Topic 7: bergxrs | love | we Topic 8: hate | sanxwiches | we Topic 9: hate | saxdwiches | we 2.5 寻找具有高语义相关的主题

最后,我将简单比较下不同的主题提取模型。大多数情况下,我们倾向于根据文档的主题分布情况对其进行分组,并根据关键词的分布情况来提取主题的信息。

大多数研究者都认为词语的语义信息是由其上下文信息所决定的,比如 “love” 和 “hate”可以看成是语义相似的词语,因为这两个词都可以用在 “I _ apples” 的语境中。事实上,词向量最重要的一个研究就是如何构建词语、短语或者文档的向量形式,使得新的向量空间中仍然保留着语义信息。

找寻语义相同的词语不同于计算词语的共现频率。从下述的结果中可以看出,大多数主题提取模型只涉及到词语的共现频率,并没有考虑词语的语义信息,只有 SVD 模型简单涉及到语义信息。

需要注意的是,本文所采用的数据集是根据一定的规则随机生成的,所以下述结果更多的是用于说明不同模型之间的区别:

In [27]:

print(find_topic(sample_texts["semantic topics"], "svd", 4, vec_model="tfidf"))

Topic 0: bergers | hate | love | sandwiches | we Topic 1: ^hate | love Topic 2: bergers | ^sandwiches Topic 3: ^bergers | ^hate | ^love | ^sandwiches | we

In [28]:

print(find_topic(sample_texts["semantic topics"], "nmf", 5, vec_model="tfidf"))

Topic 0: bergers | hate Topic 1: love | sandwiches | we Topic 2: hate | we Topic 3: hate | sandwiches | we Topic 4: bergers | love | we

In [29]:

print(find_topic(sample_texts["semantic topics"], "lda", 5, vec_model="tfidf"))

Topic 0: love | sandwiches | we Topic 1: bergers | love | we Topic 2: bergers | hate | we Topic 3: bergers | love | we Topic 4: bergers | love | we

In [30]:

print(find_topic(sample_texts["semantic topics"], "kmeans", 5, vec_model="tfidf"))

Topic 0: hate | sandwiches | we Topic 1: bergers | hate | we Topic 2: bergers | love | we Topic 3: love | sandwiches | we Topic 4: hate | sandwiches | we 3. 总结 短文本数据集具有其独特的性质,建模时需要特别注意。 模型的选择依赖于主题的定义(共现频率高或者语义相似性)和主题提取的目的(文档表示或者是异常值检验) 我们可以首先采用 KMeans 或者 NMF 模型来快速获取文档的结构信息: 主题中词语的分布情况 文档中主题的分布情况 主题个数 每个主题中词语的个数 LDA 模型具有很好的灵活性,可以处理多种类型的文本数据。但是调参过程需要很好地理解数据结构,因此如果你想构建 LDA 模型,你最好先构建一个基准模型(KMEANS 或 NMF) SVD 模型可以很好地提取出文本的主要信息。比如 SVD 模型可以很好地分析半结构化的数据(模板数据、截图或者html中的表格数据)。

原文链接:http://nbviewer.jupyter.org/github/dolaameng/tutorials/blob/master/topic-finding-for-short-texts/topics_for_short_texts.ipynb

原文作者:dolaameng

译者:Fibears

banner
看过还想看
可能还想看
最新文章
Yonghong Z-Suite一站式大数据分析平台 —— 以卓越的数据技术为客户创造价值,实现客户成功。