gpt4 book ai didi

python - sklearn.model_selection.GridSearchCV 对 LatentDirichletAllocation 的评分策略

转载 作者:行者123 更新时间:2023-12-02 00:28:03 25 4
gpt4 key购买 nike

我正在尝试使用 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 提供这些数据可能有点复杂。

您可以在此处对 LDA 进行包装,您可以在其中重新实现 score() 函数以返回 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,
)
...
...

关于python - sklearn.model_selection.GridSearchCV 对 LatentDirichletAllocation 的评分策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52986253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com