Skip to content Skip to sidebar Skip to footer

Subclassing Sklearn Linearsvc For Use As Estimator With Sklearn Gridsearchcv

I am trying to create a subclass from sklearn.svm.LinearSVC for use as an estimator for sklearn.model_selection.GridSearchCV. The child class has an extra function which in this ex

Solution 1:

You've got couple of problems:

  1. Methods defined do not have a return statement
  2. The dataset you've chosen doesn't converge with LinearSVC

As soon as you correct for those you're fine to go:

from sklearn.datasets import make_classification
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV

RANDOM_STATE = 123classLinearSVCSub(LinearSVC):
    def__init__(self, penalty='l2', loss='squared_hinge', additional_parameter1=1, additional_parameter2=100,
                 dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1,
                 class_weight=None, verbose=0, random_state=None, max_iter=100000):
        super(LinearSVCSub, self).__init__(penalty=penalty, loss=loss, dual=dual, tol=tol,
                                           C=C, multi_class=multi_class, fit_intercept=fit_intercept,
                                           intercept_scaling=intercept_scaling, class_weight=class_weight,
                                           verbose=verbose, random_state=random_state, max_iter=max_iter)

        self.additional_parameter1 = additional_parameter1
        self.additional_parameter2 = additional_parameter2

    deffit(self, X, y, sample_weight=None):
        X = self.transform_this(X)
        super(LinearSVCSub, self).fit(X, y, sample_weight)
        return self

    defpredict(self, X):
        X = self.transform_this(X)
        returnsuper(LinearSVCSub, self).predict(X)

    defscore(self, X, y, sample_weight=None):
        X = self.transform_this(X)
        returnsuper(LinearSVCSub, self).score(X, y, sample_weight)

    defdecision_function(self, X):
        X = self.transform_this(X)
        returnsuper(LinearSVCSub, self).decision_function(X)

    deftransform_this(self, X):
        return X


X, y = make_classification()

# Parameter tuning with custom LinearSVC
param_grid = {'C': [0.00001, 0.0001, 0.0005],
                  'dual': (True, False), 'random_state': [RANDOM_STATE],
                  'additional_parameter1': [0.90, 0.80, 0.60, 0.30],
                  'additional_parameter2': [20, 30]
             }

gs_model = GridSearchCV(estimator=LinearSVCSub(), verbose=1, param_grid=param_grid,
                        scoring='roc_auc', n_jobs=1)

gs_model.fit(X, y)
Fitting 5 folds for each of 48 candidates, totalling 240 fits
[Parallel(n_jobs=1)]: Using backend SequentialBackend with1 concurrent workers.
[Parallel(n_jobs=1)]: Done 240 out of 240 | elapsed:    0.9s finished
GridSearchCV(estimator=LinearSVCSub(), n_jobs=1,
             param_grid={'C': [1e-05, 0.0001, 0.0005],
                         'additional_parameter1': [0.9, 0.8, 0.6, 0.3],
                         'additional_parameter2': [20, 30],
                         'dual': (True, False), 'random_state': [123]},
             scoring='roc_auc', verbose=1)

gs_model.predict(X)
array([0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1,
       1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1,
       1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
       0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0,
       0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1])

Post a Comment for "Subclassing Sklearn Linearsvc For Use As Estimator With Sklearn Gridsearchcv"