2

I have a sklearn pipeline that does text classification using two types of features: standard tfidf features generated by CountVectorizer() and TfidfTransformer() (TfidfVectorizer()) and some linguistic features. I try to pass different ngrams ranges to CountVectorizer() and then find the best n using GridSearh.

Here is my code:

text_clf = Pipeline([('union', FeatureUnion([
                              ('tfidf', Pipeline([
                                       ('sents', GetItem(key='sent')), 
                                       ('vect', CountVectorizer()),
                                       ('transform', TfidfTransformer())
                               ])),
                              ('LF', Pipeline([
                                     ('features', GetItem(key='features')), 
                                     ('dict_vect', DictVectorizer())
                               ]))],
                               transformer_weights={'LF': 0.6, 'tfidf': 0.8}
                               )),
                              ('clf', SGDClassifier())
                     ])

parameters = [{'union__tfidf__vect__model__ngram_range': ((1, 1), (1, 2), (1, 3), (1, 4)), 
            'clf__alpha': (1e-2, 1e-3, 1e-4, 1e-5), 
            'clf__loss': ('hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron'), 
            'clf__penalty': ('none', 'l2', 'l1', 'elasticnet'), 
            'clf__n_iter': (3, 4, 5, 6, 7, 8, 9, 10)}]

gs_clf = GridSearchCV(text_clf, parameters, cv=5, n_jobs=-1)
gs_clf = gs_clf.fit(all_data, labels)

(I'm omitting some lines that seem not to be related to the problem.)

But it throws an error:

ValueError: Invalid parameter model for estimator CountVectorizer(analyzer=u'word', binary=False, charset=None,
    charset_error=None, decode_error=u'strict',
    dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content',
    lowercase=True, max_df=1.0, max_features=None, min_df=1,
    ngram_range=(1, 1), preprocessor=None, stop_words=None,
    strip_accents=None, token_pattern=u'(?u)\\b\\w\\w+\\b',
    tokenizer=None, vocabulary=None)

The same happens with TfidfVectorizer().

And everything works fine if I pass ngram_range to the vectorizer in the pipeline directly: ('vect', CountVectorizer(ngram_range=(1,2)))

Thanks!

2 Answers2

2

The error is because you have union__tfidf__vect__model__ngram_range when it should be union__tfidf__vect__ngram_range. Notice how it calls out "model" as the invalid param:

ValueError: Invalid parameter model

Also, as a note, I think using TfidfVectorizer would simplify things.

David
  • 9,284
  • 3
  • 41
  • 40
  • Thank you David for your help! I just got confused by this post: http://stackoverflow.com/questions/27810855/python-sklearn-how-to-pass-parameters-to-the-customize-modeltransformer-clas – Katya Stolpovskaya Oct 07 '15 at 08:59
1

You do have a strict named relation in between vectr__ tfidf__ clfsvm__

pipe_clf_svm = Pipeline([('vectr', CountVectorizer(analyzer=textproc_max, 
                    preprocessor=no_numb_preprocessor, min_df=4)),
                            ('tfidf', tfidfT), 
                                ('clfsvm', clf_svm),])

parameters2 = {'vectr__ngram_range': [(1,1),(1,2),(1,3)],
                  'tfidf__use_idf': (True, False),  #}
                    'clfsvm__alpha': (1e-2, 1e-3),}

this 3 vectr__ tfidf__ clfsvm__ are named alias as in pipe_clf_svm.named_steps['vectr']

Max Kleiner
  • 1,442
  • 1
  • 13
  • 14