Search code examples
machine-learningscikit-learnldalatent-semantic-analysis

Is it possible to set the initial topic assignments for scikit-learn LDA?


Instead of setting the topic_word_prior as a parameter, I would like to initialize the topics according to a pre-defined distribution over words. How would I set this initial topic distribution in sklearn's implementation? If it's not possible, is there a better implementation to consider?


Solution

  • If you have a predefined distribution of words in a pre-trained model you can just pass a bow_corpus through that distribution as a function. Gensims LDA and LDAMallet can both be trained once then you can pass a new data set through for allocation without changing the topics.

    Steps:

    1. Import your data
    2. Clean your data: nix punctuation, numbers, lemmatize, remove stop-words, and stem
    3. Create a dictionary

      dictionary = gensim.corpora.Dictionary(processed_docs[:])
      dictionary.filter_extremes(no_below=15, no_above=0.5, keep_n=100000)
      
    4. Define a bow corpus

      bow_corpus = [dictionary.doc2bow(doc) for doc in processed_docs]
      
    5. Train your model - skip if it's already trained

      ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, 
                  corpus=bow_corpus, num_topics=15, id2word=dictionary)
      
    6. Import your new data and follow steps 1-4

    7. Pass your new data through your model like this:

        ldamallet[bow_corpus_new[:len(bow_corpus_new)]]
      
    8. Your new data is allocated now and you can put it in a CSV