Splunk® Machine Learning Toolkit

ML-SPL API Guide

This documentation does not apply to the most recent version of Splunk® Machine Learning Toolkit. For documentation on the most recent version, go to the latest release.

Support Vector Regressor

This example uses the ML-SPL API available in the Splunk Machine Learning Toolkit version 2.2.0 and later. Verify your Splunk Machine Learning Toolkit version before using this example.

This example covers the following:

  • using BaseAlgo and a mixin
  • converting parameters
  • using register_codecs


In this example, we will add scikit-learn's Support Vector Regressor algorithm to the Splunk Machine Learning Toolkit. See http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html.

We will inherit from not only the BaseAlgo, but another class, RegressorMixin. The mixin has already filled out the fit and apply methods for us, so we only need to define the __init__ and register_codecs methods.

Steps

Do the following:

  1. Register the algorithm in __init__.py.
    Modify the __init__.py file located in $SPLUNK_HOME/etc/apps/Splunk_ML_Toolkit/bin/algos to "register" your algorithm by adding it to the list here:
    __all__ = [
        "SVR",
        "LinearRegression",
        "Lasso",
        ...
        ]
    
  2. Create the python file in the algos folder. For this example, we create $SPLUNK_HOME/etc/apps/Splunk_ML_Toolkit/bin/algos/SVR.py.
    from sklearn.svm import SVR as _SVR
     
    from base import BaseAlgo, RegressorMixin
    from util.param_util import convert_params
    
  3. Define the class.
    Here we inherit from both the RegressorMixin and the BaseAlgo.

    When inheriting from multiple classes here, we need to make sure the RegressorMixin comes first. BaseAlgo will raise errors if a method is not implemented. In this case, our methods are defined in RegressorMixin so we must list that class first.

    class SVR(RegressorMixin, BaseAlgo):
    	"""Predict numeric target variables via scikit-learn's SVR algorithm."""
    
  4. Define the __init__ method.
    • Note we use the RegressorMixin's handle_options method to check for feature & target variables.
    • The RegressorMixin also implicitly expects a variable 'estimator' to be attached to self.
        def __init__(self, options):
            self.handle_options(options)
    
            params = options.get('params', {})
            out_params = convert_params(
                params,
                floats=['C', 'gamma'],
                strs=['kernel'],
                ints=['degree'],
            )
    
            self.estimator = _SVR(**out_params)
    
  5. Define the register_codecs method.
    • We would like to save the model so that it can be applied on new data.
    • RegressorMixin has already defined the fit & apply methods for us, but to save, we must define the register_codecs method
    • Here we add two things to serialize:
      • one, the algorithm itself
      • two, the imported SVR module
        @staticmethod
        def register_codecs():
            from codec.codecs import SimpleObjectCodec
            from codec import codecs_manager
            codecs_manager.add_codec('algos.SVR', 'SVR', SimpleObjectCodec)
            codecs_manager.add_codec('sklearn.svm.classes', 'SVR', SimpleObjectCodec)
    

    Most often, you will not need to use anything outside of the SimpleObjectCodec but sometimes if there are circular references or unusual properties to the algorithm, you may need to write your own. Writing your own codec sounds harder than it really is. A codec defines how to serialize (save) and deserialize (load) python objects into and from strings. Here is an example of a custom codec needed for a subcomponent in the DecisionTreeClassifier algorithm.

    from codec.codecs import BaseCodec
    
    
    class TreeCodec(BaseCodec):
        @classmethod
        def encode(cls, obj):
            import sklearn.tree
            assert type(obj) == sklearn.tree._tree.Tree
    
            init_args = obj.__reduce__()[1]
            state = obj.__getstate__()
    
            return {
                '__mlspl_type': [type(obj).__module__, type(obj).__name__],
                'init_args': init_args,
                'state': state
            }
    
        @classmethod
        def decode(cls, obj):
            import sklearn.tree
    
            init_args = obj['init_args']
            state = obj['state']
    
            t = sklearn.tree._tree.Tree(*init_args)
            t.__setstate__(state)
    
            return t
    

    So then in DecisionTreeClassifier.py, the register_codecs method looks like this:

        @staticmethod
        def register_codecs():
            from codec.codecs import SimpleObjectCodec, TreeCodec
            codecs_manager.add_codec('algos.DecisionTreeClassifier', 'DecisionTreeClassifier', SimpleObjectCodec)
            codecs_manager.add_codec('sklearn.tree.tree', 'DecisionTreeClassifier', SimpleObjectCodec)
            codecs_manager.add_codec('sklearn.tree._tree', 'Tree', TreeCodec)
    

    Finished example

    from sklearn.svm import SVR as _SVR
    
    from base import BaseAlgo, RegressorMixin
    from util.param_util import convert_params
    
    
    class SVR(RegressorMixin, BaseAlgo):
    
        def __init__(self, options):
            self.handle_options(options)
    
            params = options.get('params', {})
            out_params = convert_params(
                params,
                floats=['C', 'gamma'],
                strs=['kernel'],
                ints=['degree'],
            )
    
            self.estimator = _SVR(**out_params)
    
        @staticmethod
        def register_codecs():
            from codec.codecs import SimpleObjectCodec
            from codec import codecs_manager
            codecs_manager.add_codec('algos.SVR', 'SVR', SimpleObjectCodec)
            codecs_manager.add_codec('sklearn.svm.classes', 'SVR', SimpleObjectCodec)
    
Last modified on 19 July, 2017
Agglomerative Clustering   Savitzky-Golay Filter

This documentation applies to the following versions of Splunk® Machine Learning Toolkit: 2.3.0


Was this topic useful?







You must be logged into splunk.com in order to post comments. Log in now.

Please try to keep this discussion focused on the content covered in this documentation topic. If you have a more general question about Splunk functionality or are experiencing a difficulty with Splunk, consider posting a question to Splunkbase Answers.

0 out of 1000 Characters