module mlmodel.target_predictors

Inheritance diagram of mlinsights.mlmodel.target_predictors

Short summary

module mlinsights.mlmodel.target_predictors

Implements a slightly different version of the sklearn.compose.TransformedTargetRegressor.

source on GitHub

Classes

class

truncated documentation

TransformedTargetClassifier2

Meta-estimator to classify on a transformed target. Useful for applying permutation transformation in classification …

TransformedTargetRegressor2

Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation in regression …

Functions

function

truncated documentation

_common_get_transform

Properties

property

truncated documentation

classes_

Returns the classes.

Methods

method

truncated documentation

__init__

__init__

_apply

Calls predict, predict_proba or decision_function using the base classifier, applying inverse. Parameters …

_check_is_fitted

_more_tags

_more_tags

decision_function

Predicts using the base classifier, applying inverse. Parameters ———- X : {array-like, sparse …

fit

Fits the model according to the given training data. Parameters ———- X : {array-like, sparse …

fit

Fits the model according to the given training data. Parameters ———- X : {array-like, sparse …

predict

Predicts using the base classifier, applying inverse. Parameters ———- X : {array-like, sparse …

predict

Predicts using the base regressor, applying inverse. Parameters ———- X : {array-like, sparse …

predict_proba

Predicts using the base classifier, applying inverse. Parameters ———- X : {array-like, sparse …

score

Scores the model with sklearn.metrics.accuracy_score.

score

Scores the model with sklearn.metrics.r2_score.

Documentation

Implements a slightly different version of the sklearn.compose.TransformedTargetRegressor.

source on GitHub

class mlinsights.mlmodel.target_predictors.TransformedTargetClassifier2(classifier=None, transformer=None)[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.ClassifierMixin

Meta-estimator to classify on a transformed target. Useful for applying permutation transformation in classification problems.

Parameters
  • classifier (object, default=LogisticRegression()) – Classifier object such as derived from ClassifierMixin. This classifier will automatically be cloned each time prior to fitting.

  • transformer (str or object of type BaseReciprocalTransformer) –

classifier_

Fitted classifier.

Type

object

transformer_

Transformer used in fit, predict, decision_function, predict_proba.

Type

object

Examples

<<<

import numpy
from sklearn.linear_model import LogisticRegression
from mlinsights.mlmodel import TransformedTargetClassifier2

tt = TransformedTargetClassifier2(classifier=LogisticRegression(),
                                  transformer='permute')
X = numpy.arange(4).reshape(-1, 1)
y = numpy.array([0, 1, 0, 1])
print(tt.fit(X, y))
print(tt.score(X, y))
print(tt.classifier_.coef_)

>>>

    TransformedTargetClassifier2(classifier=LogisticRegression(C=1.0,
                                                               class_weight=None,
                                                               dual=False,
                                                               fit_intercept=True,
                                                               intercept_scaling=1,
                                                               l1_ratio=None,
                                                               max_iter=100,
                                                               multi_class='auto',
                                                               n_jobs=None,
                                                               penalty='l2',
                                                               random_state=None,
                                                               solver='lbfgs',
                                                               tol=0.0001,
                                                               verbose=0,
                                                               warm_start=False),
                                 transformer='permute')
    0.5
    [[-0.453]]

See notebook Transformed Target for a more complete example.

source on GitHub

__init__(classifier=None, transformer=None)[source]

Initialize self. See help(type(self)) for accurate signature.

_apply(X, method)[source]

Calls predict, predict_proba or decision_function using the base classifier, applying inverse.

Parameters

X ({array-like, sparse matrix}, shape = (n_samples, n_features)) – Samples.

Returns

y_hat – Predicted values.

Return type

array, shape = (n_samples,)

source on GitHub

_check_is_fitted()[source]
_more_tags()[source]
property classes_

Returns the classes.

source on GitHub

decision_function(X)[source]

Predicts using the base classifier, applying inverse.

Parameters

X ({array-like, sparse matrix}, shape = (n_samples, n_features)) – Samples.

Returns

raw score

Return type

array, shape = (n_samples, ?)

source on GitHub

fit(X, y, sample_weight=None)[source]

Fits the model according to the given training data.

Parameters
  • X ({array-like, sparse matrix}, shape (n_samples, n_features)) – Training vector, where n_samples is the number of samples and n_features is the number of features.

  • y (array-like, shape (n_samples,)) – Target values.

  • sample_weight (array-like, shape (n_samples,) optional) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.

Returns

self

Return type

object

source on GitHub

predict(X)[source]

Predicts using the base classifier, applying inverse.

Parameters

X ({array-like, sparse matrix}, shape = (n_samples, n_features)) – Samples.

Returns

y_hat – Predicted values.

Return type

array, shape = (n_samples,)

source on GitHub

predict_proba(X)[source]

Predicts using the base classifier, applying inverse.

Parameters

X ({array-like, sparse matrix}, shape = (n_samples, n_features)) – Samples.

Returns

predict probabilities – Predicted values.

Return type

array, shape = (n_samples, n_classes)

source on GitHub

score(X, y, sample_weight=None)[source]

Scores the model with sklearn.metrics.accuracy_score.

source on GitHub

class mlinsights.mlmodel.target_predictors.TransformedTargetRegressor2(regressor=None, transformer=None)[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.RegressorMixin

Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation in regression problems.

Parameters
  • regressor (object, default=LinearRegression()) – Regressor object such as derived from RegressorMixin. This regressor will automatically be cloned each time prior to fitting.

  • transformer (str or object of type BaseReciprocalTransformer) –

regressor_

Fitted regressor.

Type

object

transformer_

Transformer used in fit and predict.

Type

object

Examples

<<<

import numpy
from sklearn.linear_model import LinearRegression
from mlinsights.mlmodel import TransformedTargetRegressor2

tt = TransformedTargetRegressor2(regressor=LinearRegression(),
                                 transformer='log')
X = numpy.arange(4).reshape(-1, 1)
y = numpy.exp(2 * X).ravel()
print(tt.fit(X, y))
print(tt.score(X, y))
print(tt.regressor_.coef_)

>>>

    TransformedTargetRegressor2(regressor=LinearRegression(copy_X=True,
                                                           fit_intercept=True,
                                                           n_jobs=None,
                                                           normalize=False),
                                transformer='log')
    1.0
    [2.]

See notebook Transformed Target for a more complete example.

source on GitHub

__init__(regressor=None, transformer=None)[source]

Initialize self. See help(type(self)) for accurate signature.

_more_tags()[source]
fit(X, y, sample_weight=None)[source]

Fits the model according to the given training data.

Parameters
  • X ({array-like, sparse matrix}, shape (n_samples, n_features)) – Training vector, where n_samples is the number of samples and n_features is the number of features.

  • y (array-like, shape (n_samples,)) – Target values.

  • sample_weight (array-like, shape (n_samples,) optional) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.

Returns

self

Return type

object

source on GitHub

predict(X)[source]

Predicts using the base regressor, applying inverse.

Parameters

X ({array-like, sparse matrix}, shape = (n_samples, n_features)) – Samples.

Returns

y_hat – Predicted values.

Return type

array, shape = (n_samples,)

source on GitHub

score(X, y, sample_weight=None)[source]

Scores the model with sklearn.metrics.r2_score.

source on GitHub

mlinsights.mlmodel.target_predictors._common_get_transform(transformer, is_regression)[source]