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

_repr_html_

HTML representation of estimator. This is redundant with the logic of _repr_mimebundle_. The latter should …

_repr_html_

HTML representation of estimator. This is redundant with the logic of _repr_mimebundle_. The latter should …

classes_

Returns the classes.

Methods#

method

truncated documentation

__init__

__init__

_apply

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

_check_is_fitted

_more_tags

_more_tags

decision_function

Predicts using the base classifier, applying inverse.

fit

Fits the model according to the given training data.

fit

Fits the model according to the given training data.

predict

Predicts using the base classifier, applying inverse.

predict

Predicts using the base regressor, applying inverse.

predict_proba

Predicts using the base classifier, applying inverse.

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)#

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

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

classifierobject, 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_object

Fitted classifier.

transformer_object

Transformer used in fit, predict, decision_function, predict_proba.

<<<

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(),
                                 transformer='permute')
    0.5
    [[-0.453]]

See notebook Transformed Target for a more complete example.

source on GitHub

__init__(classifier=None, transformer=None)#
_apply(X, method)#

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, array, shape = (n_samples,) Predicted values.

source on GitHub

_check_is_fitted()#
_more_tags()#
property classes_#

Returns the classes.

source on GitHub

decision_function(X)#

Predicts using the base classifier, applying inverse.

Parameters

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

Returns

raw score : array, shape = (n_samples, ?)

source on GitHub

fit(X, y, sample_weight=None)#

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, object

source on GitHub

predict(X)#

Predicts using the base classifier, applying inverse.

Parameters

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

Returns

y_hat, array, shape = (n_samples,) Predicted values.

source on GitHub

predict_proba(X)#

Predicts using the base classifier, applying inverse.

Parameters

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

Returns

predict probabilities, array, shape = (n_samples, n_classes) Predicted values.

source on GitHub

score(X, y, sample_weight=None)#

Scores the model with sklearn.metrics.accuracy_score.

source on GitHub

class mlinsights.mlmodel.target_predictors.TransformedTargetRegressor2(regressor=None, transformer=None)#

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.

regressorobject, 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_object

Fitted regressor.

transformer_object

Transformer used in fit and predict.

<<<

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(), transformer='log')
    1.0
    [2.]

See notebook Transformed Target for a more complete example.

source on GitHub

__init__(regressor=None, transformer=None)#
_more_tags()#
fit(X, y, sample_weight=None)#

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, object

source on GitHub

predict(X)#

Predicts using the base regressor, applying inverse.

Parameters

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

Returns

y_hat : array, shape = (n_samples,) Predicted values.

source on GitHub

score(X, y, sample_weight=None)#

Scores the model with sklearn.metrics.r2_score.

source on GitHub

mlinsights.mlmodel.target_predictors._common_get_transform(transformer, is_regression)#