Python ';numpy.int64';对象在使用潜在dirichlet分配时不可iterable
我试图将潜在的dirichlet分配算法应用于从twitter数据检索的.csv文件 目前我遇到了以下错误:Python ';numpy.int64';对象在使用潜在dirichlet分配时不可iterable,python,pandas,numpy,lda,Python,Pandas,Numpy,Lda,我试图将潜在的dirichlet分配算法应用于从twitter数据检索的.csv文件 目前我遇到了以下错误: Traceback (most recent call last): File "...Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__ return self.func(*args) File "...\src\project.py", line 26
Traceback (most recent call last):
File "...Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "...\src\project.py", line 262, in topic-modelling-lda
for i in top_topic_words:
TypeError: 'numpy.int64' object is not iterable
我使用的库:
import pandas as pd
import numpy as np
import re, random
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
这是我为该函数编写的代码
def topic-modelling-lda(self):
df = pd.read_csv('sample_dataset.csv')
vect = CountVectorizer(max_df=0.6, min_df=4, stop_words='english')
matrix = vect .fit_transform(df)
LDA = LatentDirichletAllocation(n_components=5, random_state=50)
LDA.fit(matrix)
first_topic = LDA.components_[0]
top_topic_words = first_topic.argsort()[-10]
for i in top_topic_words:
print(count_vect.get_feature_names()[i])
第262行是第一个主题中i的。\u单词:
我不确定该如何解决这个问题。我相信您想选择前10个单词,但使用了错误的语法。你只选择了排名第10的单词,这是不合适的。将第261行更改为该行以选择前10行,而不是仅选择第10行:
top_topic_words = first_topic.argsort()[-10:]
我相信你想要选择前10个单词,你使用了错误的语法。你只选择了排名第10的单词,这是不合适的。将第261行更改为该行以选择前10行,而不是仅选择第10行:
top_topic_words = first_topic.argsort()[-10:]