【发布时间】:2021-12-20 11:14:33
【问题描述】:
制作语料库和词汇
K = 10
XYtr['description'] = XYtr['description'].fillna("nan")
Xte['description'] = Xte['description'].fillna("nan")
corpus = list(XYtr['description'])+list(Xte['description'])
vectorizer = CountVectorizer()
corpus = vectorizer.fit_transform(corpus)
lda = LatentDirichletAllocation(n_components = K)
lda.fit(corpus)
#There are no problems until here
# Create a list of (term, frequency) tuples sorted by their frequency
sum_words = corpus.sum(axis=0)
words_freq = [(word, sum_words[0, idx]) for word, idx in vectorizer.vocabulary_.items()]
words_freq = sorted(words_freq, key = lambda x: x[1])
# Keep only the terms in a list
vocabulary, _ = zip(*words_freq[:int(total_features * 0.2)])
vocabulary = list(vocabulary)
#Finally, we use the vocabulary to limit the model to the less frequent terms.
bottom_vect = CountVectorizer(vocabulary=vocabulary)
topics = bottom_vect.fit_transform(corpus)
这在代码的最后一行返回了“AttributeError: lower not found”。因此,我无法获得“主题”。
如果您能提出一些建议,我们将不胜感激。
这是我的数据集的几行
XYtr:
Xte:
【问题讨论】:
-
请同时提供几行数据,以便复制
标签: python pandas scikit-learn