sklearn.model_selection.GridSearchCV 的 LatentDirichletAllocation 评分策略
Scoring strategy of sklearn.model_selection.GridSearchCV for LatentDirichletAllocation
我正在尝试使用 sklearn 库在 LatentDirichletAllocation 上应用 GridSearchCV。
当前管道如下所示:
vectorizer = CountVectorizer(analyzer='word',
min_df=10,
stop_words='english',
lowercase=True,
token_pattern='[a-zA-Z0-9]{3,}'
)
data_vectorized = vectorizer.fit_transform(doc_clean) #where doc_clean is processed text.
lda_model = LatentDirichletAllocation(n_components =number_of_topics,
max_iter=10,
learning_method='online',
random_state=100,
batch_size=128,
evaluate_every = -1,
n_jobs = -1,
)
search_params = {'n_components': [10, 15, 20, 25, 30], 'learning_decay': [.5, .7, .9]}
model = GridSearchCV(lda_model, param_grid=search_params)
model.fit(data_vectorized)
目前 GridSearchCV 使用近似对数似然作为分数来确定哪个是最佳模型。我想做的是将我的评分方法更改为基于模型的 approximate perplexity。
根据 sklearn 的 documentation of GridSearchCV,有一个我可以使用的评分参数。但是,我不知道如何应用困惑度作为评分方法,而且我在网上找不到任何人应用它的例子。这可能吗?
GridSearchCV
默认情况下将使用管道中最终估算器的 score()
函数。
make_scorer
可以在这里使用,但是为了计算困惑度,您还需要来自拟合模型的其他数据,通过 make_scorer
.[=19= 提供这些数据可能有点复杂]
您可以在此处对您的 LDA 进行包装,您可以在其中将 score()
函数重新实现为 return perplexity
。沿线的东西:
class MyLDAWithPerplexityScorer(LatentDirichletAllocation):
def score(self, X, y=None):
# You can change the options passed to perplexity here
score = super(MyLDAWithPerplexityScorer, self).perplexity(X, sub_sampling=False)
# Since perplexity is lower for better, so we do negative
return -1*score
然后可以在您的代码中使用它代替 LatentDirichletAllocation
,例如:
...
...
...
lda_model = MyLDAWithPerplexityScorer(n_components =number_of_topics,
....
....
n_jobs = -1,
)
...
...
分数和困惑度参数似乎有问题并且取决于主题的数量。因此,网格中的结果将为您提供最少数量的主题
我正在尝试使用 sklearn 库在 LatentDirichletAllocation 上应用 GridSearchCV。
当前管道如下所示:
vectorizer = CountVectorizer(analyzer='word',
min_df=10,
stop_words='english',
lowercase=True,
token_pattern='[a-zA-Z0-9]{3,}'
)
data_vectorized = vectorizer.fit_transform(doc_clean) #where doc_clean is processed text.
lda_model = LatentDirichletAllocation(n_components =number_of_topics,
max_iter=10,
learning_method='online',
random_state=100,
batch_size=128,
evaluate_every = -1,
n_jobs = -1,
)
search_params = {'n_components': [10, 15, 20, 25, 30], 'learning_decay': [.5, .7, .9]}
model = GridSearchCV(lda_model, param_grid=search_params)
model.fit(data_vectorized)
目前 GridSearchCV 使用近似对数似然作为分数来确定哪个是最佳模型。我想做的是将我的评分方法更改为基于模型的 approximate perplexity。
根据 sklearn 的 documentation of GridSearchCV,有一个我可以使用的评分参数。但是,我不知道如何应用困惑度作为评分方法,而且我在网上找不到任何人应用它的例子。这可能吗?
GridSearchCV
默认情况下将使用管道中最终估算器的 score()
函数。
make_scorer
可以在这里使用,但是为了计算困惑度,您还需要来自拟合模型的其他数据,通过 make_scorer
.[=19= 提供这些数据可能有点复杂]
您可以在此处对您的 LDA 进行包装,您可以在其中将 score()
函数重新实现为 return perplexity
。沿线的东西:
class MyLDAWithPerplexityScorer(LatentDirichletAllocation):
def score(self, X, y=None):
# You can change the options passed to perplexity here
score = super(MyLDAWithPerplexityScorer, self).perplexity(X, sub_sampling=False)
# Since perplexity is lower for better, so we do negative
return -1*score
然后可以在您的代码中使用它代替 LatentDirichletAllocation
,例如:
...
...
...
lda_model = MyLDAWithPerplexityScorer(n_components =number_of_topics,
....
....
n_jobs = -1,
)
...
...
分数和困惑度参数似乎有问题并且取决于主题的数量。因此,网格中的结果将为您提供最少数量的主题