gpt4 book ai didi

python - scikit-learn FeatureUnion 网格搜索特征子集

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:35 24 4
gpt4 key购买 nike

如何在 scikit learn 中使用 FeatureUnion,以便 Gridsearch 可以选择性地处理其部分?

下面的代码有效并设置了一个 FeatureUnion,其中一个 TfidfVectorizer 用于单词,一个 TfidfVectorizer 用于字符。

在进行 Gridsearch 时,除了测试定义的参数空间外,我还想只测试“vect__wordvect”及其 ngram_range 参数(没有用于字符的 TfidfVectorizer),并且也只测试“vect__lettervect”小写参数 True 和 False,另一个 TfidfVectorizer 被禁用。

编辑:基于 maxymoo 建议的完整代码示例。

如何做到这一点?

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.grid_search import GridSearchCV
from sklearn.datasets import fetch_20newsgroups

# setup the featureunion
wordvect = TfidfVectorizer(analyzer='word')
lettervect = CountVectorizer(analyzer='char')
featureunionvect = FeatureUnion([("lettervect", lettervect), ("wordvect", wordvect)])

# setup the pipeline
classifier = LogisticRegression(class_weight='balanced')
pipeline = Pipeline([('vect', featureunionvect), ('classifier', classifier)])

# gridsearch parameters
parameters = {
'vect__wordvect__ngram_range': [(1, 1), (1, 2)], # commenting out these two lines
'vect__lettervect__lowercase': [True, False], # runs, but there is no parameterization anymore
'vect__transformer_list': [[('wordvect', wordvect)],
[('lettervect', lettervect)],
[('wordvect', wordvect), ('lettervect', lettervect)]]}
gs_clf = GridSearchCV(pipeline, parameters)

# data
newsgroups_train = fetch_20newsgroups(subset='train', categories=['alt.atheism', 'sci.space'])

# gridsearch CV
gs_clf = GridSearchCV(pipeline, parameters)
gs_clf = gs_clf.fit(newsgroups_train.data, newsgroups_train.target)
for score in gs_clf.grid_scores_:
print "gridsearch scores: ", score

最佳答案

FeatureUnion 有一个名为 transformer_list 的参数,您可以使用它进行网格搜索;所以在你的情况下你的网格搜索参数将变成

parameters = {'vect__wordvect__ngram_range': [(1, 1), (1, 2)],
'vect__lettervect__lowercase': [True, False],
'vect__transformer_weights': [{"lettervect":1,"wordvect":0},
{"lettervect":0,"wordvect":1},
{"lettervect":1,"wordvect":1}]}

关于python - scikit-learn FeatureUnion 网格搜索特征子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038394/

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