Skip to content

Commit 18392e5

Browse files
jmloyolaglemaitre
andcommitted
DOC Ensures that TransformedTargetRegressor passes numpydoc validation (#21261)
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent 73cd69a commit 18392e5

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

maint_tools/test_docstrings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"SpectralCoclustering",
2828
"SpectralEmbedding",
2929
"StackingRegressor",
30-
"TransformedTargetRegressor",
3130
]
3231

3332

sklearn/compose/_target.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
2020
"""Meta-estimator to regress on a transformed target.
2121
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
2323
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`.
2626
27-
The computation during ``fit`` is::
27+
The computation during :meth:`fit` is::
2828
2929
regressor.fit(X, func(y))
3030
3131
or::
3232
3333
regressor.fit(X, transformer.transform(y))
3434
35-
The computation during ``predict`` is::
35+
The computation during :meth:`predict` is::
3636
3737
inverse_func(regressor.predict(X))
3838
@@ -47,41 +47,42 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
4747
Parameters
4848
----------
4949
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.
5354
5455
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.
6162
6263
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.
6768
6869
inverse_func : function, default=None
6970
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 return
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
7273
predictions to the same space of the original training labels.
7374
7475
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.
7778
7879
Attributes
7980
----------
8081
regressor_ : object
8182
Fitted regressor.
8283
8384
transformer_ : object
84-
Transformer used in ``fit`` and ``predict``.
85+
Transformer used in :meth:`fit` and :meth:`predict`.
8586
8687
n_features_in_ : int
8788
Number of features seen during :term:`fit`. Only defined if the
@@ -95,6 +96,20 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
9596
9697
.. versionadded:: 1.0
9798
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+
98113
Examples
99114
--------
100115
>>> import numpy as np
@@ -110,16 +125,6 @@ class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
110125
1.0
111126
>>> tt.regressor_.coef_
112127
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-
123128
"""
124129

125130
def __init__(
@@ -194,13 +199,13 @@ def fit(self, X, y, **fit_params):
194199
Target values.
195200
196201
**fit_params : dict
197-
Parameters passed to the ``fit`` method of the underlying
202+
Parameters passed to the `fit` method of the underlying
198203
regressor.
199204
200-
201205
Returns
202206
-------
203207
self : object
208+
Fitted estimator.
204209
"""
205210
y = check_array(
206211
y,
@@ -248,8 +253,8 @@ def fit(self, X, y, **fit_params):
248253
def predict(self, X, **predict_params):
249254
"""Predict using the base regressor, applying inverse.
250255
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.
253258
254259
Parameters
255260
----------
@@ -264,7 +269,6 @@ def predict(self, X, **predict_params):
264269
-------
265270
y_hat : ndarray of shape (n_samples,)
266271
Predicted values.
267-
268272
"""
269273
check_is_fitted(self)
270274
pred = self.regressor_.predict(X, **predict_params)
@@ -295,6 +299,7 @@ def _more_tags(self):
295299

296300
@property
297301
def n_features_in_(self):
302+
"""Number of features seen during :term:`fit`."""
298303
# For consistency with other estimators we raise a AttributeError so
299304
# that hasattr() returns False the estimator isn't fitted.
300305
try:

0 commit comments

Comments
 (0)