Source code for simpleml.models.classifiers.sklearn.svm

'''
Wrapper module around `sklearn.svm`
'''

[docs]__author__ = 'Elisha Yadgaran'
from .base_sklearn_classifier import SklearnClassifier from simpleml.models.classifiers.external_models import ClassificationExternalModelMixin from sklearn.svm import LinearSVC, NuSVC, SVC import logging
[docs]LOGGER = logging.getLogger(__name__)
''' Support Vectors '''
[docs]class WrappedSklearnLinearSVC(LinearSVC, ClassificationExternalModelMixin):
[docs] def get_feature_metadata(self, features, **kwargs): # coefficients generated for each class >2, only report for class 0 coefficients = self.coef_[0].squeeze() if features is None or len(features) < len(coefficients): LOGGER.warning('Fewer feature names than features passed, defaulting to numbered list') features = range(len(coefficients)) return dict(zip(features, coefficients))
[docs]class SklearnLinearSVC(SklearnClassifier):
[docs] def _create_external_model(self, **kwargs): return WrappedSklearnLinearSVC(**kwargs)
[docs]class WrappedSklearnNuSVC(NuSVC, ClassificationExternalModelMixin):
[docs] def get_feature_metadata(self, features, **kwargs): # coefficients generated only for linear kernels. # Report support vectors instead support_vectors = self.support_vectors_[0].squeeze() if features is None or len(features) < len(support_vectors): LOGGER.warning('Fewer feature names than features passed, defaulting to numbered list') features = range(len(support_vectors)) return dict(zip(features, support_vectors))
[docs]class SklearnNuSVC(SklearnClassifier):
[docs] def _create_external_model(self, **kwargs): return WrappedSklearnNuSVC(**kwargs)
[docs]class WrappedSklearnSVC(SVC, ClassificationExternalModelMixin):
[docs] def get_feature_metadata(self, features, **kwargs): # coefficients generated only for linear kernels. # Report support vectors instead support_vectors = self.support_vectors_[0].squeeze() if features is None or len(features) < len(support_vectors): LOGGER.warning('Fewer feature names than features passed, defaulting to numbered list') features = range(len(support_vectors)) return dict(zip(features, support_vectors))
[docs]class SklearnSVC(SklearnClassifier):
[docs] def _create_external_model(self, **kwargs): return WrappedSklearnSVC(**kwargs)