CoherenceModel官网翻译
models.coherencemodel – Topic coherence pipeline
计算主题模型的主题一致性。这是论文四个阶段主题一致性管道的实现。MichaelRoeder,Andreas和Alexander Hinneburg:“Exploring the space of topic coherence measures"。典型的,CoherenceModel用于评价主题模型。
四段管道基本上是:
- 分割
- 概率估计
- 确认度量
- 聚集
该管道的实现允许用户通过在每个管道中选择一个方法,从本质上“做出”他/她选择的一致性度量。
另见
gensim.topic_coherence
管道的内部功能。
classgensim.models.coherencemodel.CoherenceModel(model=None, topics=None, texts=None, corpus=None, dictionary=None, window_size=None, keyed_vectors=None, coherence='c_v', topn=20, processes=-1)
Bases:gensim.interfaces.TransformationABC
这个类的对象允许建立和维护一个主题一致性模型。
实例
使用此特性的一种方法是提供经过训练的主题模型。如果模型尚未包含字典,则必须显式提供字典。
>>> from gensim.test.utils import common_corpus, common_dictionary
>>> from gensim.models.ldamodel import LdaModel
>>> from gensim.models.coherencemodel import CoherenceModel
>>>
>>> model = LdaModel(common_corpus, 5, common_dictionary)
>>>
>>> cm = CoherenceModel(model=model, corpus=common_corpus, coherence='u_mass')
>>> coherence = cm.get_coherence() # get coherence value
使用此功能的另一种方法是提供标记化的主题,如
>>> from gensim.test.utils import common_corpus, common_dictionary
>>> from gensim.models.coherencemodel import CoherenceModel
>>> topics = [
... ['human', 'computer', 'system', 'interface'],
... ['graph', 'minors', 'trees', 'eps']
... ]
>>>
>>> cm = CoherenceModel(topics=topics, corpus=common_corpus, dictionary=common_dictionary, coherence='u_mass')
>>> coherence = cm.get_coherence() # get coherence value
参数:
- model (BaseTopicModel(可选)-如果没有提供主题,则应提供预先训练过的主题模型。目前支持LdaModel, LdaMulticore, LdaMallet和LdaVowpalWabbit。使用主题参数插入一个尚未被支持的模型。
- topics(str列表, 任选)-标记化主题的列表,如果这比模型更好的话,字典应该提供。
- texts (str列表, 任选)-标记化文本,用于使用基于滑动窗口的一致性模型(即Coherence=`c_something‘)概率估计器。
- corpus (列表的可迭代性 (int, 数), 任选)-BoW语料库。
- dictionary (Dictionary,可选)-Gensim字典映射id字来创建语料库。如果model.id2word是存在的,这(dictionary)是不需要的。如果两者都提供了,则通过字典会被使用。
- window_size (int, 任选)-是使用布尔滑动窗口作为概率估计器用于一致性度量的窗口的大小。对于“u_mass”来说,这并不重要。如果没有-默认窗口大小是:‘c_v’-110,‘c_uci’-10,‘c_npmi’-10。
- 一致性 ({‘u_mass’, “c_v”, ‘c_uci’, ‘c_npmi’}, 任选)-将使用的一致性度量。最快的方法-‘u_mass’,‘c_uci’也称为c_pmi。对于“u_mass”语料库,如果提供texts,则使用字典将其转换为语料库。对于“c_v”、‘c_uci’和‘c_npmi’应提供文本(不需要语料库)
- topn (int, 任选)-整数对应于从每个主题中提取的顶级单词的数量。
- processes (int, 任选)-用于概率估计阶段的进程数,任何小于1的值都将被解释为num_cpu-1。
aggregate_measures(topic_coherence)
使用管道的聚合函数聚合单个主题一致性度量。使用self.measure.aggr(topic_coherence).
参数:topic_coherences (list of float)-分段主题中每个集合的计算确认度量值列表。
返回: 确认度量中包含的所有值的算术平均值。
返回类型:float
compare_model_topics(模型主题)
对每个模型执行一致性评估。
参数:model_topics (str列表)-用这个主题训练的模型的单词列表。
返回: 平均主题一致性和平均一致性对的序列。
返回类型: 列表(浮点数,浮点数)
注记
这首先预先计算概率一次,然后评估每个模型的一致性。
由于我们已经预先计算了概率,所以这只需使用CoherenceModel来执行评估,这应该是相当快的。
compare_models(models)
通过连贯值对主题模型进行比较。
参数:model (BaseTopicModel)-主题模型序列。
返回: 平均主题一致性和平均一致性对的序列。
返回类型: 列表(浮点数,浮点数)
estimate_probabilities(segmented_topics=None)
使用所选一致性度量的最优方法,从文本或语料库中积累单词出现和同时出现。
注记
基于滑动窗口的一致性方法可能需要相当长的时间。
参数: segmented_topics (list of list of pair, 任选)-分段主题,通常由segment_topics().
返回: Corpus accumulator。
返回类型: CorpusAccumulator
类方法 for_models(model, dictionary, topn=20, **kwargs)
用所有给定模型的估计概率初始化CoherenceModel。使用for_topics()方法。
参数:
- model(list of BaseTopicModel)-评估一致性的模型列表,每个模型都应该实现get_topics()方法。
- dictionary (Dictionary)-标识词的Gensim字典映射。
- topn(int,optinal)-整数对应于从每个主题中提取的顶级单词的数量。
- kwargs(object)-论点的顺序,见for_topics().
返回:所有给定模型的概率估计的CoherenceModel。
返回类型:CoherenceModel
例
>>> from gensim.test.utils import common_corpus, common_dictionary
>>> from gensim.models.ldamodel import LdaModel
>>> from gensim.models.coherencemodel import CoherenceModel
>>>
>>> m1 = LdaModel(common_corpus, 3, common_dictionary)
>>> m2 = LdaModel(common_corpus, 5, common_dictionary)
>>>
>>> cm = CoherenceModel.for_models([m1, m2], common_dictionary, corpus=common_corpus, coherence='u_mass')
类方法 for_topics(topics_as_topn_terms, **kwargs)
为所有给定主题初始化具有估计概率的CoherenceModel。
参数:topics_as_topn_terms (list of list of str)-顶层列表中的每个元素都应该是模型的主题列表。模型的主题应该是一个顶部N字的列表,每个主题一个。
返回: 所有给定模型的概率估计的CoherenceModel。
返回类型: CoherenceModel
get_coherence()
根据管道参数求出一致性值。
返回: 一致性的值。
返回类型: float
get_coherence_per_topic(segmented_topics=None, with_std=False, with_support=False)
根据管道参数获取每个主题的一致性值列表。
参数:
- segmented_topics (list of list of (int, number)) -主题。
- with_std (bool, optional) –除了每个主题的平均一致性外,还包括跨主题段集的标准差。
- with_support (bool, optional)-也包括跨主题段的支持。该支持被定义为使用成对相似性比较的次数来计算主题的整体一致性。
返回:每个主题的相似性度量序列。
返回类型:float
类方法 load(fname, mmap=None)
加载以前使用save()从file。
参数:
- fname (str) –包含所需对象的文件路径。
- mmap (str, optional) –内存映射选项。如果对象是用单独存储的大型数组保存的,则可以使用mmap(共享内存)加载这些数组。mmap=‘r’如果正在加载的文件被压缩(‘.gz’或‘.bz 2’),那么“mmap=None” 必须被设置好了。
另见
save()
将对象保存到文件中。
返回: 对象从fname加载.
返回类型: 对象
Raises: AttributeError-当调用对象实例而不是类时(这是一个类方法)。
measure
make pipeline,根据一致性参数值
返回: 包含计算一致性所需的函数/方法的管道。
返回类型:namedtuple
model
Get self._model field.
返回: Used model。
返回类型: BaseTopicModel
save(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset([]), pickle_protocol=2)
将对象保存到文件中。
参数:
fname_or_handle (str or file-like) -输出文件或已打开的类文件对象的路径。如果对象是文件句柄,则不会执行特殊的数组处理,所有属性都将保存到同一个文件中。
- separately (list of str or None, optional) -
如果没有,则自动检测存储对象中的大型numpy/sciy.稀疏数组,并将它们存储到单独的文件中。这防止了大型对象的内存错误,并且允许memory-mapping用于在多个进程之间高效地加载和共享RAM中的大数组的大型数组。
If list of str:的列表将这些属性存储到单独的文件中。在这种情况下,不执行自动大小检查。
- sep_limit (int, optional) – 不要单独存储比此更小的数组。以字节为单位。
- ignore (frozenset of str, optional) – 不应该存储的属性。
- pickle_protocol (int, optional) – Protocol number for pickle.
另见
load()
从文件加载对象。
segment_topics()
Segment topic, alias for self.measure.seg(self.topics).
返回: 分段的主题。
返回类型: list of list of pair
静态 top_topics_as_word_lists(model, dictionary, topn=20)
Get topn topics as list of words.
参数:
- model (BaseTopicModel)-预先训练过的主题模型。
- dictionary (Dictionary)-标识词的Gensim字典映射。
- topn(int,optional)-整数对应于从每个主题中提取的顶级单词的数量。
返回:Top topics in list-of-list-of-words format.
返回类型:list of list of str
topics
获取主题self_topics.
返回: 主题作为标记列表。
返回类型: str列表