class sklearn.compose.TransformedTargetRegressor(regressor=None, transformer=None, func=None, inverse_func=None, check_inverse=True)
[source]
Meta-estimator to regress on a transformed target.
Useful for applying a non-linear transformation in regression problems. This transformation can be given as a Transformer such as the QuantileTransformer or as a function and its inverse such as log
and exp
.
The computation during fit
is:
regressor.fit(X, func(y))
or:
regressor.fit(X, transformer.transform(y))
The computation during predict
is:
inverse_func(regressor.predict(X))
or:
transformer.inverse_transform(regressor.predict(X))
Read more in the User Guide.
Parameters: |
|
---|---|
Attributes: |
|
Internally, the target y
is always converted into a 2-dimensional array to be used by scikit-learn transformers. At the time of prediction, the output will be reshaped to a have the same number of dimensions as y
.
See examples/compose/plot_transformed_target.py.
>>> import numpy as np >>> from sklearn.linear_model import LinearRegression >>> from sklearn.compose import TransformedTargetRegressor >>> tt = TransformedTargetRegressor(regressor=LinearRegression(), ... func=np.log, inverse_func=np.exp) >>> X = np.arange(4).reshape(-1, 1) >>> y = np.exp(2 * X).ravel() >>> tt.fit(X, y) TransformedTargetRegressor(...) >>> tt.score(X, y) 1.0 >>> tt.regressor_.coef_ array([2.])
fit (X, y[, sample_weight]) | Fit the model according to the given training data. |
get_params ([deep]) | Get parameters for this estimator. |
predict (X) | Predict using the base regressor, applying inverse. |
score (X, y[, sample_weight]) | Returns the coefficient of determination R^2 of the prediction. |
set_params (**params) | Set the parameters of this estimator. |
__init__(regressor=None, transformer=None, func=None, inverse_func=None, check_inverse=True)
[source]
fit(X, y, sample_weight=None)
[source]
Fit the model according to the given training data.
Parameters: |
|
---|---|
Returns: |
|
get_params(deep=True)
[source]
Get parameters for this estimator.
Parameters: |
|
---|---|
Returns: |
|
predict(X)
[source]
Predict using the base regressor, applying inverse.
The regressor is used to predict and the inverse_func
or inverse_transform
is applied before returning the prediction.
Parameters: |
|
---|---|
Returns: |
|
score(X, y, sample_weight=None)
[source]
Returns the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - u/v), where u is the residual sum of squares ((y_true - y_pred) ** 2).sum() and v is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.
Parameters: |
|
---|---|
Returns: |
|
set_params(**params)
[source]
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: |
|
---|
sklearn.compose.TransformedTargetRegressor
© 2007–2018 The scikit-learn developers
Licensed under the 3-clause BSD License.
http://scikit-learn.org/stable/modules/generated/sklearn.compose.TransformedTargetRegressor.html