Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid parameter loss for estimator Pipeline

I'm trying to implement a SGDClassifier with sklearn, but I'm getting this error:

ValueError: Invalid parameter loss for estimator Pipeline. Check the list of available parameters with `estimator.get_params().keys()`.

This is my code:

pipeline = Pipeline([
 ('clf', SGDClassifier())
])

parameters = {
    'seed': [0],
    'loss': ('log', 'hinge'),
    'penalty': ['l1', 'l2', 'elasticnet'],
    'alpha': [0.001, 0.0001, 0.00001, 0.000001]
}

score_func = make_scorer(metrics.f1_score)

grid_search = GridSearchCV(pipeline, parameters, n_jobs=3,
verbose=1, scoring=score_func)

grid_search.fit(X, Y)

How can I fix this?

like image 706
Filipe Ferminiano Avatar asked Jan 21 '26 02:01

Filipe Ferminiano


2 Answers

From the User's Guide:

Parameters of the estimators in the pipeline can be accessed using the <estimator>__<parameter> syntax

So try this:

parameters = {
    'clf__seed': [0],
    'clf__loss': ('log', 'hinge'),
    'clf__penalty': ['l1', 'l2', 'elasticnet'],
    'clf__alpha': [0.001, 0.0001, 0.00001, 0.000001]
}
like image 136
PaSTE Avatar answered Jan 23 '26 14:01

PaSTE


This displays exact keys to be used to reference parameters within the pipeline

pipeline.get_params().keys()
like image 35
David Miller Avatar answered Jan 23 '26 14:01

David Miller