如何使用FeatureUnion构建参数网格?

我正在尝试运行一个结合了文本和数值特征的模型,但遇到了错误 ValueError: Invalid parameter tfidf for estimator。问题出在parameters的语法上吗?可能有帮助的链接:FeatureUnion的使用FeatureUnion文档

tknzr = tokenize.word_tokenizevect = CountVectorizer(tokenizer=tknzr, stop_words={'english'}, max_df=0.9, min_df=2)scl = StandardScaler(with_mean=False)tfidf = TfidfTransformer(norm=None)parameters = {    'vect__ngram_range': [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)],    'tfidf__use_idf': (True, False),    'clf__alpha': tuple(10 ** (np.arange(-4, 4, dtype='float'))),    'clf__loss': ('hinge', 'squared_hinge', 'log', 'modified_huber', 'perceptron'),    'clf__penalty': ('l1', 'l2'),    'clf__tol': (1e07, 1e-6, 1e-5, 1e-4, 1e-3)}combined_clf = Pipeline([    ('features', FeatureUnion([        ('numeric_features', Pipeline([            ('selector', transfomer_numeric)        ])),        ('text_features', Pipeline([            ('selector', transformer_text),            ('vect', vect),            ('tfidf', tfidf),            ('scaler', scl),        ]))    ])),    ('clf', SGDClassifier(random_state=42,                          max_iter=int(10 ** 6 / len(X_train)), shuffle=True))])

回答:

这里所述,嵌套参数必须通过__(双下划线)语法访问。根据您要访问的参数的深度,这适用于递归情况。参数use_idf位于以下路径:

features > text_features > tfidf > use_idf

因此,您的网格中的参数应为:

'features__text_features__tfidf__use_idf': [True, False]

同样,ngram_range的语法应为:

'features__text_features__vect__ngram_range': [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注