Scikit-learn 中可重现的 LDA 模型
Reproducible LDA Model in Scikit-learn
我正在使用 LDA 进行主题建模。
从 sklearn.decomposition 导入 LatentDirichletAllocation
使用一组 10 个文件,我制作了模型。现在,我尝试将其聚类为 3。
类似于以下内容:
'''
import numpy as np
data = []
a1 = " a word in groupa doca"
a2 = " a word in groupa docb"
a3 = "a word in groupb docc"
a4 = "a word in groupc docd"
a5 ="a word in groupc doce"
data = [a1,a2,a3,a4,a5]
del a1,a2,a3,a4,a5
NO_DOCUMENTS = len(data)
print(NO_DOCUMENTS)
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer
NUM_TOPICS = 2
vectorizer = CountVectorizer(min_df=0.001, max_df=0.99998,
stop_words='english', lowercase=True,
token_pattern='[a-zA-Z\-][a-zA-Z\-]{2,}')
data_vectorized = vectorizer.fit_transform(data)
# Build a Latent Dirichlet Allocation Model
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10, learning_method='online')
lda_Z = lda_model.fit_transform(data_vectorized)
vocab = vectorizer.get_feature_names()
text = "The economy is working better than ever"
x = lda_model.transform(vectorizer.transform([text]))[0]
print(x, x.sum())
for iDocIndex,text in enumerate(data):
x = list(lda_model.transform(vectorizer.transform([text]))[0])
maxIndex = x.index(max(x))
if TOPICWISEDOCUMENTS[maxIndex]:
TOPICWISEDOCUMENTS[maxIndex].append(iDocIndex)
else:
TOPICWISEDOCUMENTS[maxIndex] = [iDocIndex]
print(TOPICWISEDOCUMENTS)
'''
每当我 运行 系统时,即使对于同一组输入数据,我也会得到不同的集群。
或者,LDA 不可重现。
如何使其可重现..?
为了在 scikit 中的可重复性,请在您在代码中看到的任何位置设置 random_state
参数。
你的情况是 LatentDirichletAllocation(...)
使用这个:
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10,
learning_method='online'
random_state = 42)
检查这个 link:
如果你想让你的整个脚本可重现并且不想搜索放在哪里random_state
,你可以设置一个全局 numpy 随机种子。
import numpy as np
np.random.seed(42)
看到这个:http://scikit-learn.org/stable/faq.html#how-do-i-set-a-random-state-for-an-entire-execution
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10,
learning_method='online'
random_state = 42)
成功了...!!!
非常感谢
另外,这个我试过了
import numpy as np
np.random.seed(42)
但是没有效果。
感谢解决
我正在使用 LDA 进行主题建模。
从 sklearn.decomposition 导入 LatentDirichletAllocation
使用一组 10 个文件,我制作了模型。现在,我尝试将其聚类为 3。
类似于以下内容:
'''
import numpy as np
data = []
a1 = " a word in groupa doca"
a2 = " a word in groupa docb"
a3 = "a word in groupb docc"
a4 = "a word in groupc docd"
a5 ="a word in groupc doce"
data = [a1,a2,a3,a4,a5]
del a1,a2,a3,a4,a5
NO_DOCUMENTS = len(data)
print(NO_DOCUMENTS)
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer
NUM_TOPICS = 2
vectorizer = CountVectorizer(min_df=0.001, max_df=0.99998,
stop_words='english', lowercase=True,
token_pattern='[a-zA-Z\-][a-zA-Z\-]{2,}')
data_vectorized = vectorizer.fit_transform(data)
# Build a Latent Dirichlet Allocation Model
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10, learning_method='online')
lda_Z = lda_model.fit_transform(data_vectorized)
vocab = vectorizer.get_feature_names()
text = "The economy is working better than ever"
x = lda_model.transform(vectorizer.transform([text]))[0]
print(x, x.sum())
for iDocIndex,text in enumerate(data):
x = list(lda_model.transform(vectorizer.transform([text]))[0])
maxIndex = x.index(max(x))
if TOPICWISEDOCUMENTS[maxIndex]:
TOPICWISEDOCUMENTS[maxIndex].append(iDocIndex)
else:
TOPICWISEDOCUMENTS[maxIndex] = [iDocIndex]
print(TOPICWISEDOCUMENTS)
'''
每当我 运行 系统时,即使对于同一组输入数据,我也会得到不同的集群。
或者,LDA 不可重现。
如何使其可重现..?
为了在 scikit 中的可重复性,请在您在代码中看到的任何位置设置 random_state
参数。
你的情况是 LatentDirichletAllocation(...)
使用这个:
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10,
learning_method='online'
random_state = 42)
检查这个 link:
如果你想让你的整个脚本可重现并且不想搜索放在哪里random_state
,你可以设置一个全局 numpy 随机种子。
import numpy as np
np.random.seed(42)
看到这个:http://scikit-learn.org/stable/faq.html#how-do-i-set-a-random-state-for-an-entire-execution
lda_model = LatentDirichletAllocation(n_topics=NUM_TOPICS,
max_iter=10,
learning_method='online'
random_state = 42)
成功了...!!!
非常感谢
另外,这个我试过了
import numpy as np
np.random.seed(42)
但是没有效果。
感谢解决