Hierarchical Dirichlet Process Gensim topic number independent of corpus size

前端 未结 7 1708
余生分开走
余生分开走 2021-02-04 07:20

I am using the Gensim HDP module on a set of documents.

>>> hdp = models.HdpModel(corpusB, id2word=dictionaryB)
>>> topics = hdp.print_topics(         


        
7条回答
  •  遥遥无期
    2021-02-04 08:00

    @user3907335 is exactly correct here: HDP will calculate as many topics as the assigned truncation level. However, it may be the case that many of these topics have basically zero probability of occurring. To help with this in my own work, I wrote a handy little function that performs a rough estimate of the probability weight associated with each topic. Note that this is a rough metric only: it does not account for the probability associated with each word. Even so, it provides a pretty good metric for which topics are meaningful and which aren't:

    import pandas as pd
    import numpy as np 
    
    def topic_prob_extractor(hdp=None, topn=None):
        topic_list = hdp.show_topics(topics=-1, topn=topn)
        topics = [int(x.split(':')[0].split(' ')[1]) for x in topic_list]
        split_list = [x.split(' ') for x in topic_list]
        weights = []
        for lst in split_list:
            sub_list = []
            for entry in lst: 
                if '*' in entry: 
                    sub_list.append(float(entry.split('*')[0]))
            weights.append(np.asarray(sub_list))
        sums = [np.sum(x) for x in weights]
        return pd.DataFrame({'topic_id' : topics, 'weight' : sums})
    

    I assume that you already know how to calculate an HDP model. Once you have an hdp model calculated by gensim you call the function as follows:

    topic_weights = topic_prob_extractor(hdp, 500)
    

提交回复
热议问题