19
19
class TransformedTargetRegressor (RegressorMixin , BaseEstimator ):
20
20
"""Meta-estimator to regress on a transformed target.
21
21
22
- Useful for applying a non-linear transformation to the target ``y` ` in
22
+ Useful for applying a non-linear transformation to the target `y ` in
23
23
regression problems. This transformation can be given as a Transformer
24
- such as the QuantileTransformer or as a function and its inverse such as
25
- `` log`` and `` exp` `.
24
+ such as the :class:`~sklearn.preprocessing. QuantileTransformer` or as a
25
+ function and its inverse such as `np. log` and `np. exp`.
26
26
27
- The computation during `` fit` ` is::
27
+ The computation during :meth:` fit` is::
28
28
29
29
regressor.fit(X, func(y))
30
30
31
31
or::
32
32
33
33
regressor.fit(X, transformer.transform(y))
34
34
35
- The computation during `` predict` ` is::
35
+ The computation during :meth:` predict` is::
36
36
37
37
inverse_func(regressor.predict(X))
38
38
@@ -47,41 +47,42 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
47
47
Parameters
48
48
----------
49
49
regressor : object, default=None
50
- Regressor object such as derived from ``RegressorMixin``. This
51
- regressor will automatically be cloned each time prior to fitting.
52
- If regressor is ``None``, ``LinearRegression()`` is created and used.
50
+ Regressor object such as derived from
51
+ :class:`~sklearn.base.RegressorMixin`. This regressor will
52
+ automatically be cloned each time prior to fitting. If `regressor is
53
+ None`, :class:`~sklearn.linear_model.LinearRegression` is created and used.
53
54
54
55
transformer : object, default=None
55
- Estimator object such as derived from ``TransformerMixin``. Cannot be
56
- set at the same time as ``func`` and ``inverse_func``. If
57
- ``transformer`` is `` None`` as well as ``func`` and ``inverse_func``,
58
- the transformer will be an identity transformer. Note that the
59
- transformer will be cloned during fitting. Also, the transformer is
60
- restricting ``y` ` to be a numpy array.
56
+ Estimator object such as derived from
57
+ :class:`~sklearn.base.TransformerMixin`. Cannot be set at the same time
58
+ as `func` and `inverse_func`. If `transformer is None` as well as
59
+ `func` and `inverse_func`, the transformer will be an identity
60
+ transformer. Note that the transformer will be cloned during fitting.
61
+ Also, the transformer is restricting `y ` to be a numpy array.
61
62
62
63
func : function, default=None
63
- Function to apply to ``y`` before passing to `` fit`` . Cannot be set at
64
- the same time as `` transformer` `. The function needs to return a
65
- 2-dimensional array. If `` func`` is `` None`` , the function used will be
66
- the identity function.
64
+ Function to apply to `y` before passing to :meth:` fit`. Cannot be set
65
+ at the same time as `transformer`. The function needs to return a
66
+ 2-dimensional array. If `func is None`, the function used will be the
67
+ identity function.
67
68
68
69
inverse_func : function, default=None
69
70
Function to apply to the prediction of the regressor. Cannot be set at
70
- the same time as `` transformer`` as well . The function needs to retur
8000
n
71
- a 2-dimensional array. The inverse function is used to return
71
+ the same time as `transformer`. The function needs to return a
72
+ 2-dimensional array. The inverse function is used to return
72
73
predictions to the same space of the original training labels.
73
74
74
75
check_inverse : bool, default=True
75
- Whether to check that `` transform`` followed by `` inverse_transform` `
76
- or `` func`` followed by `` inverse_func` ` leads to the original targets.
76
+ Whether to check that `transform` followed by `inverse_transform`
77
+ or `func` followed by `inverse_func` leads to the original targets.
77
78
78
79
Attributes
79
80
----------
80
81
regressor_ : object
81
82
Fitted regressor.
82
83
83
84
transformer_ : object
84
- Transformer used in `` fit`` and `` predict` `.
85
+ Transformer used in :meth:` fit` and :meth:` predict`.
85
86
86
87
n_features_in_ : int
87
88
Number of features seen during :term:`fit`. Only defined if the
@@ -95,6 +96,20 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
95
96
96
97
.. versionadded:: 1.0
97
98
99
+ See Also
100
+ --------
101
+ sklearn.preprocessing.FunctionTransformer : Construct a transformer from an
102
+ arbitrary callable.
103
+
104
+ Notes
105
+ -----
106
+ Internally, the target `y` is always converted into a 2-dimensional array
107
+ to be used by scikit-learn transformers. At the time of prediction, the
108
+ output will be reshaped to a have the same number of dimensions as `y`.
109
+
110
+ See :ref:`examples/compose/plot_transformed_target.py
111
+ <sphx_glr_auto_examples_compose_plot_transformed_target.py>`.
112
+
98
113
Examples
99
114
--------
100
115
>>> import numpy as np
@@ -110,16 +125,6 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
110
125
1.0
111
126
>>> tt.regressor_.coef_
112
127
array([2.])
113
-
114
- Notes
115
- -----
116
- Internally, the target ``y`` is always converted into a 2-dimensional array
117
- to be used by scikit-learn transformers. At the time of prediction, the
118
- output will be reshaped to a have the same number of dimensions as ``y``.
119
-
120
- See :ref:`examples/compose/plot_transformed_target.py
121
- <sphx_glr_auto_examples_compose_plot_transformed_target.py>`.
122
-
123
128
"""
124
129
125
130
def __init__ (
@@ -194,13 +199,13 @@ def fit(self, X, y, **fit_params):
194
199
Target values.
195
200
196
201
**fit_params : dict
197
- Parameters passed to the `` fit` ` method of the underlying
202
+ Parameters passed to the `fit` method of the underlying
198
203
regressor.
199
204
200
-
201
205
Returns
202
206
-------
203
207
self : object
208
+ Fitted estimator.
204
209
"""
205
210
y = check_array (
206
211
y ,
@@ -248,8 +253,8 @@ def fit(self, X, y, **fit_params):
248
253
def predict (self , X , ** predict_params ):
249
254
"""Predict using the base regressor, applying inverse.
250
255
251
- The regressor is used to predict and the `` inverse_func` ` or
252
- `` inverse_transform` ` is applied before returning the prediction.
256
+ The regressor is used to predict and the `inverse_func` or
257
+ `inverse_transform` is applied before returning the prediction.
253
258
254
259
Parameters
255
260
----------
@@ -264,7 +269,6 @@ def predict(self, X, **predict_params):
264
269
-------
265
270
y_hat : ndarray of shape (n_samples,)
266
271
Predicted values.
267
-
268
272
"""
269
273
check_is_fitted (self )
270
274
pred = self .regressor_ .predict (X , ** predict_params )
@@ -295,6 +299,7 @@ def _more_tags(self):
295
299
296
300
@property
297
301
def n_features_in_ (self ):
302
+ """Number of features seen during :term:`fit`."""
298
303
# For consistency with other estimators we raise a AttributeError so
299
304
# that hasattr() returns False the estimator isn't fitted.
300
305
try :
0 commit comments