Reputation: 11718
Using scikit-learn, I fit a classifier using Grid Search like this:
from sklearn.svm import SVC
param_grid = {
'C': [1e-2, 0.1, 1.0],
'gamma': [1e-4, 1e-3, 1e-2],
'class_weight': ['auto']
}
clf = SVC()
gs = grid_search.GridSearchCV(clf, param_grid, cv=3, n_jobs=12)
gs.fit(x_train, y_train)
I now want to re-train the classifier using the best parameters found and the extra argument probability=True
. How can I re-fit the classifier using the best parameters, plus the extra parameter probability
?
Upvotes: 4
Views: 3369
Reputation: 14377
Try
best_estimator = grid_search.best_estimator_.set_params(probability=True)
You can also clone it to be sure no other part of your code reuses this estimator.
Upvotes: 0
Reputation: 77484
You can also use the set_params
method for an instance of SVC
and modify the probability
attribute before calling fit
.
from sklearn import svm, grid_search
x_train = np.random.randn(10,5)
y_train = np.random.randint(0, 2, size=(10,1))
param_grid = {
'C': [1e-2, 0.1, 1.0],
'gamma': [1e-4, 1e-3, 1e-2],
'class_weight': ['auto']
}
svc1 = svm.SVC()
gs = grid_search.GridSearchCV(svc1, param_grid, cv=3, n_jobs=12)
gs_fitted = gs.fit(x_train, y_train)
svc2 = svm.SVC(probability=True)
# or manually set svc2.probability = True before ever calling svc2.fit
svc2.set_params(**gs_fitted.best_params_)
svc2.fit(x_train, y_train)
Upvotes: 3
Reputation: 911
You can use gs.best_params_
to get the parameters, then create a new classifier like this
clf = SVC(probability=True, **gs.best_params_)
Upvotes: 5