Top

noxer.gm.base module

Interfaces and general functionality for generative models, such as a score function.

"""
Interfaces and general functionality for generative
models, such as a score function.
"""

from sklearn.base import BaseEstimator, TransformerMixin
from noxer.sequences import FlattenShape
from .metrics import distribution_similarity
from sklearn.preprocessing import StandardScaler


class GeneratorBase(BaseEstimator):
    def fit(self, X, Y, **kwargs):
        """Fit generative model to the data.

        Parameters
        ----------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that should be generated by particular model.

        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.
        """
        raise NotImplementedError("Please implement a fit method for your model.")

    def predict(self, X, **kwargs):
        """Make estimations with generative model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.

        Returns
        -------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that is generated by a generative model.

        """
        raise NotImplementedError("Please implement a fit method for your model.")

    def score(self, X, Y, **kwargs):
        """Score the generative model on the real data.

        Parameters
        ----------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that should be generated by particular model.

        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.
        """
        Yp = self.predict(X, **kwargs)
        score = distribution_similarity(Y, Yp)
        return score

Classes

class GeneratorBase

Base class for all estimators in scikit-learn

Notes

All estimators should specify all the parameters that can be set at the class level in their __init__ as explicit keyword arguments (no *args or **kwargs).

class GeneratorBase(BaseEstimator):
    def fit(self, X, Y, **kwargs):
        """Fit generative model to the data.

        Parameters
        ----------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that should be generated by particular model.

        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.
        """
        raise NotImplementedError("Please implement a fit method for your model.")

    def predict(self, X, **kwargs):
        """Make estimations with generative model.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.

        Returns
        -------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that is generated by a generative model.

        """
        raise NotImplementedError("Please implement a fit method for your model.")

    def score(self, X, Y, **kwargs):
        """Score the generative model on the real data.

        Parameters
        ----------
        Y : {array-like, sparse matrix}, shape [n_samples, ...]
            The data that should be generated by particular model.

        X : {array-like, sparse matrix}, shape [n_samples, ...]
            The data used to condition the generative model's outputs.
        """
        Yp = self.predict(X, **kwargs)
        score = distribution_similarity(Y, Yp)
        return score

Ancestors (in MRO)

Static methods

def fit(

self, X, Y, **kwargs)

Fit generative model to the data.

Parameters

Y : {array-like, sparse matrix}, shape [n_samples, ...] The data that should be generated by particular model.

X : {array-like, sparse matrix}, shape [n_samples, ...] The data used to condition the generative model's outputs.

def fit(self, X, Y, **kwargs):
    """Fit generative model to the data.
    Parameters
    ----------
    Y : {array-like, sparse matrix}, shape [n_samples, ...]
        The data that should be generated by particular model.
    X : {array-like, sparse matrix}, shape [n_samples, ...]
        The data used to condition the generative model's outputs.
    """
    raise NotImplementedError("Please implement a fit method for your model.")

def get_params(

self, deep=True)

Get parameters for this estimator.

Parameters

deep : boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params : mapping of string to any Parameter names mapped to their values.

def get_params(self, deep=True):
    """Get parameters for this estimator.
    Parameters
    ----------
    deep : boolean, optional
        If True, will return the parameters for this estimator and
        contained subobjects that are estimators.
    Returns
    -------
    params : mapping of string to any
        Parameter names mapped to their values.
    """
    out = dict()
    for key in self._get_param_names():
        # We need deprecation warnings to always be on in order to
        # catch deprecated param values.
        # This is set in utils/__init__.py but it gets overwritten
        # when running under python3 somehow.
        warnings.simplefilter("always", DeprecationWarning)
        try:
            with warnings.catch_warnings(record=True) as w:
                value = getattr(self, key, None)
            if len(w) and w[0].category == DeprecationWarning:
                # if the parameter is deprecated, don't show it
                continue
        finally:
            warnings.filters.pop(0)
        # XXX: should we rather test if instance of estimator?
        if deep and hasattr(value, 'get_params'):
            deep_items = value.get_params().items()
            out.update((key + '__' + k, val) for k, val in deep_items)
        out[key] = value
    return out

def predict(

self, X, **kwargs)

Make estimations with generative model.

Parameters

X : {array-like, sparse matrix}, shape [n_samples, ...] The data used to condition the generative model's outputs.

Returns

Y : {array-like, sparse matrix}, shape [n_samples, ...] The data that is generated by a generative model.

def predict(self, X, **kwargs):
    """Make estimations with generative model.
    Parameters
    ----------
    X : {array-like, sparse matrix}, shape [n_samples, ...]
        The data used to condition the generative model's outputs.
    Returns
    -------
    Y : {array-like, sparse matrix}, shape [n_samples, ...]
        The data that is generated by a generative model.
    """
    raise NotImplementedError("Please implement a fit method for your model.")

def score(

self, X, Y, **kwargs)

Score the generative model on the real data.

Parameters

Y : {array-like, sparse matrix}, shape [n_samples, ...] The data that should be generated by particular model.

X : {array-like, sparse matrix}, shape [n_samples, ...] The data used to condition the generative model's outputs.

def score(self, X, Y, **kwargs):
    """Score the generative model on the real data.
    Parameters
    ----------
    Y : {array-like, sparse matrix}, shape [n_samples, ...]
        The data that should be generated by particular model.
    X : {array-like, sparse matrix}, shape [n_samples, ...]
        The data used to condition the generative model's outputs.
    """
    Yp = self.predict(X, **kwargs)
    score = distribution_similarity(Y, Yp)
    return score

def set_params(

self, **params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it's possible to update each component of a nested object.

Returns

self

def set_params(self, **params):
    """Set the parameters of this estimator.
    The method works on simple estimators as well as on nested objects
    (such as pipelines). The latter have parameters of the form
    ``<component>__<parameter>`` so that it's possible to update each
    component of a nested object.
    Returns
    -------
    self
    """
    if not params:
        # Simple optimization to gain speed (inspect is slow)
        return self
    valid_params = self.get_params(deep=True)
    nested_params = defaultdict(dict)  # grouped by prefix
    for key, value in params.items():
        key, delim, sub_key = key.partition('__')
        if key not in valid_params:
            raise ValueError('Invalid parameter %s for estimator %s. '
                             'Check the list of available parameters '
                             'with `estimator.get_params().keys()`.' %
                             (key, self))
        if delim:
            nested_params[key][sub_key] = value
        else:
            setattr(self, key, value)
    for key, sub_params in nested_params.items():
        valid_params[key].set_params(**sub_params)
    return self