module mlmodel.target_predictors
#
Short summary#
module mlinsights.mlmodel.target_predictors
Implements a slightly different version of the sklearn.compose.TransformedTargetRegressor.
Classes#
class |
truncated documentation |
---|---|
Meta-estimator to classify on a transformed target. Useful for applying permutation transformation in classification … |
|
Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation in regression … |
Functions#
function |
truncated documentation |
---|---|
Properties#
property |
truncated documentation |
---|---|
|
HTML representation of estimator. This is redundant with the logic of _repr_mimebundle_. The latter should … |
|
HTML representation of estimator. This is redundant with the logic of _repr_mimebundle_. The latter should … |
Returns the classes. |
Methods#
method |
truncated documentation |
---|---|
Calls predict, predict_proba or decision_function using the base classifier, applying inverse. |
|
Predicts using the base classifier, applying inverse. |
|
Fits the model according to the given training data. |
|
Fits the model according to the given training data. |
|
Predicts using the base classifier, applying inverse. |
|
Predicts using the base regressor, applying inverse. |
|
Predicts using the base classifier, applying inverse. |
|
Scores the model with sklearn.metrics.accuracy_score. |
|
Scores the model with sklearn.metrics.r2_score. |
Documentation#
Implements a slightly different version of the sklearn.compose.TransformedTargetRegressor.
- class mlinsights.mlmodel.target_predictors.TransformedTargetClassifier2(classifier=None, transformer=None)#
Bases:
BaseEstimator
,ClassifierMixin
Meta-estimator to classify on a transformed target. Useful for applying permutation transformation in classification problems.
Parameters#
- 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
Attributes#
- classifier_object
Fitted classifier.
- transformer_object
Transformer used in
fit
,predict
,decision_function
,predict_proba
.
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(), transformer='permute') 0.5 [[-0.453]]
See notebook Transformed Target for a more complete example.
- __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.
- _check_is_fitted()#
- _more_tags()#
- property classes_#
Returns the classes.
- 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, ?)
- 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
- 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.
- 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.
- score(X, y, sample_weight=None)#
Scores the model with sklearn.metrics.accuracy_score.
- class mlinsights.mlmodel.target_predictors.TransformedTargetRegressor2(regressor=None, transformer=None)#
Bases:
BaseEstimator
,RegressorMixin
Meta-estimator to regress on a transformed target. Useful for applying a non-linear transformation in regression problems.
Parameters#
- 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
Attributes#
- regressor_object
Fitted regressor.
- transformer_object
Transformer used in
fit
andpredict
.
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(), transformer='log') 1.0 [2.]
See notebook Transformed Target for a more complete example.
- __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
- 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.
- score(X, y, sample_weight=None)#
Scores the model with sklearn.metrics.r2_score.
- mlinsights.mlmodel.target_predictors._common_get_transform(transformer, is_regression)#