我正在尝试使用 PyLDAVis 可视化主题,但以下代码出错。不确定是什么问题。
import pyLDAvis.gensim_models
pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(lda_model, corpus, id2word)
pyLDAvis.show(vis)
File ~/PycharmProjects/KeyWordExtractor/venv/lib/python3.9/site-packages/pyLDAvis/_prepare.py:228, in _topic_info(topic_term_dists, topic_proportion, term_frequency, term_topic_freq, vocab, lambda_step, R, n_jobs)
225 saliency = term_proportion * distinctiveness
227 # Order the terms for the "default" view by decreasing saliency:
--> 228 default_term_info = pd.DataFrame({'saliency': saliency, 'Term': vocab, \
229 'Freq': term_frequency, 'Total': term_frequency, \
230 'Category': 'Default'}). \
231 sort_values(by='saliency', ascending=False). \
232 head(R).drop('saliency', 1)
233 # Rounding Freq and Total to integer values to match LDAvis code:
234 default_term_info['Freq'] = np.floor(default_term_info['Freq'])
TypeError: drop() takes from 1 to 2 positional arguments but 3 were given
我认为这可以通过将
pyLDAvis._prepare
模块的第 243 行更改为 read 来解决
default_term_info = default_term_info.sort_values(
by='saliency', ascending=False).head(R).drop('saliency', axis=1)
注意轴不再是位置参数。我是 pyLDAvis 的新手,但这对我有用。
即使使用 pyLDAvis 3.4.0、pandas 2.0.0、python 3.11.3,我也无法重现错误
.drop('saliency', 1)
import pyLDAvis.gensim_models
pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(lda, corpus, dictionary)
pyLDAvis.show(vis, local=False)
请更新你的 pyLDAvis 到 vs 3.4.0 https://github.com/bmabey/pyLDAvis/issues/247#issuecomment-1517214945
我在 pyLDAvis 3.4.0、pandas 2.0.0、python 3.11.3 中也遇到了这个错误。当我更新到 python 3.11 时才开始出现此错误。