Source code for simpleml.models.classifiers.sklearn.svm
"""
Wrapper module around `sklearn.svm`
"""
import logging
from sklearn.svm import SVC, LinearSVC, NuSVC
from simpleml.models.classifiers.external_models import ClassificationExternalModelMixin
from .base_sklearn_classifier import SklearnClassifier
"""
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 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 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))