def lda_model_evaluation(): """ This function loops through a number of parameters for an LDA model, creates the model, computes the coherenece score, and saves the results in a pandas dataframe. The outputed dataframe contains the values of the parameters tested and the resulting coherence score. """ #define empty lists to save results topic_number, decay_rate_list, score = [], [], [] #loop through a number of parameters for topics in range(5,12): for decay_rate in [0.5, 0.6, 0.7]: #build LDA model lda_model = LdaModel(corpus = corpus, id2word = id2word, num_topics = topics, decay = decay_rate, random_state = 0, chunksize = 100, alpha = 'auto', per_word_topics = True) #compute coherence score coherence_model_lda = CoherenceModel(model = lda_model, texts = texts, dictionary = id2word, coherence = 'c_v') coherence_score = coherence_model_lda.get_coherence() #append parameters to lists topic_number.append(topics) decay_rate_list.append(decay_rate) score.append(coherence_score) print("Model Saved") #gather result into a dataframe results = {"Number of Topics": topic_number, "Decay Rate": decay_rate_list, "Score": score} results = pd.DataFrame(results) return(results)