Matplotlib不显示def main中的图形

bkhjykvo  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(134)

我的代码仍然正常运行,但matplotlib的图形不显示。由于c_v一致性得分,必须使用def main,但使用def main不会显示matplotlib和pyLDAvis的图形。我不知道是什么错误,还是我以前的代码有问题

def main():
    df = pd.read_csv('ClearFunc_textana.csv', parse_dates=['date'])
    df_sorted = df.sort_values(by='like_count', ascending=False)
    df_top_10000 = df_sorted.iloc[:10000]

    stop_words = set(stopwords.words('english'))
    lemmatizer = WordNetLemmatizer()

    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    docs = df_top_10000['processed_content'].apply(lambda x: [lemmatizer.lemmatize(word) for word in nltk.word_tokenize(x.lower()) if
                   word.isalpha() and word not in stop_words])

    dictionary = corpora.Dictionary(docs)
    corpus = [dictionary.doc2bow(doc) for doc in docs]

    lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, random_state=100, update_every=1, chunksize=100, passes=10, alpha='auto', per_word_topics=True)

    for idx, topic in lda_model.print_topics(-1):
        print('Topic: {} \nWords: {}'.format(idx, topic))
        print('\n')

    cm = CoherenceModel(model=lda_model, corpus=corpus, coherence='c_v', texts=docs)
    coherence = cm.get_coherence()
    print('Coherence score:', coherence)
    print('\nPerplexity: ', lda_model.log_perplexity(corpus))

    def model_coherence_generator(corpus, dictionary, data, start_topic, end_topic, step, passes, workers):
        models = []
        coherence_scores = []
        for num in tqdm(range(start_topic, end_topic + 1, step)):
            ldamodel = LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num, passes=passes, workers=workers)
            cv_coherence_model = CoherenceModel(model=ldamodel, corpus=corpus, coherence='c_v', texts=data)
            score = cv_coherence_model.get_coherence()

            coherence_scores.append(score)
            models.append(ldamodel)
        return models, coherence_scores

        lda_models, coherence_scores = model_coherence_generator(corpus, dictionary, docs, start_topic=2, end_topic=40, step=2, passes=10, workers=10)

        x_ax = range(2, 41, 2)
        y_ax = coherence_scores
        plt.figure(figsize=(12, 6))
        plt.plot(x_ax, y_ax, c='r')
        plt.axhline(y=0.354, c='k', linestyle='--', linewidth=2)
        plt.rcParams['figure.facecolor'] = 'white'
        plt.title('Number of Topics vs Coherence Score')
        xl = plt.xlabel('Number of Topics')
        yl = plt.ylabel('Coherence Score')
        plt.savefig("figure.png")
        plt.show()

if __name__ == '__main__':
    main()

我已经搜索了很多相关的关键词,或者在页面的顶部写了matplotlib后端但是仍然没有解决这个问题

2o7dmzc5

2o7dmzc51#

我相信你有一个压痕的问题。正如您所发布的,绘图发生在model_coherence_generator函数内部,该函数从未被调用(尽管即使调用了它,也不会创建任何绘图,因为在绘图代码之前有一个return)。我认为如果您将缩进移到model_coherence_generator函数的return行后一级,您的问题将得到解决,如下所示。

def main():
    df = pd.read_csv('ClearFunc_textana.csv', parse_dates=['date'])
    df_sorted = df.sort_values(by='like_count', ascending=False)
    df_top_10000 = df_sorted.iloc[:10000]

    stop_words = set(stopwords.words('english'))
    lemmatizer = WordNetLemmatizer()

    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    docs = df_top_10000['processed_content'].apply(lambda x: [lemmatizer.lemmatize(word) for word in nltk.word_tokenize(x.lower()) if
                   word.isalpha() and word not in stop_words])

    dictionary = corpora.Dictionary(docs)
    corpus = [dictionary.doc2bow(doc) for doc in docs]

    lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, random_state=100, update_every=1, chunksize=100, passes=10, alpha='auto', per_word_topics=True)

    for idx, topic in lda_model.print_topics(-1):
        print('Topic: {} \nWords: {}'.format(idx, topic))
        print('\n')

    cm = CoherenceModel(model=lda_model, corpus=corpus, coherence='c_v', texts=docs)
    coherence = cm.get_coherence()
    print('Coherence score:', coherence)
    print('\nPerplexity: ', lda_model.log_perplexity(corpus))

    def model_coherence_generator(corpus, dictionary, data, start_topic, end_topic, step, passes, workers):
        models = []
        coherence_scores = []
        for num in tqdm(range(start_topic, end_topic + 1, step)):
            ldamodel = LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=num, passes=passes, workers=workers)
            cv_coherence_model = CoherenceModel(model=ldamodel, corpus=corpus, coherence='c_v', texts=data)
            score = cv_coherence_model.get_coherence()

            coherence_scores.append(score)
            models.append(ldamodel)
        return models, coherence_scores

    lda_models, coherence_scores = model_coherence_generator(corpus, dictionary, docs, start_topic=2, end_topic=40, step=2, passes=10, workers=10)

    x_ax = range(2, 41, 2)
    y_ax = coherence_scores
    plt.figure(figsize=(12, 6))
    plt.plot(x_ax, y_ax, c='r')
    plt.axhline(y=0.354, c='k', linestyle='--', linewidth=2)
    plt.rcParams['figure.facecolor'] = 'white'
    plt.title('Number of Topics vs Coherence Score')
    xl = plt.xlabel('Number of Topics')
    yl = plt.ylabel('Coherence Score')
    plt.savefig("figure.png")
    plt.show()

if __name__ == '__main__':
    main()

相关问题