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列表