10000 DOC add examples in docstring of enet_path, lars_path*, orthogonal_mp… · jeremiedbb/scikit-learn@7398d06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7398d06

Browse files
DOC add examples in docstring of enet_path, lars_path*, orthogonal_mp* (scikit-learn#28288)
Co-authored-by: Guillaume Lemaitre <guillaume@probabl.ai>
1 parent baa8003 commit 7398d06

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

sklearn/linear_model/_coordinate_descent.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,25 @@ def enet_path(
528528
For an example, see
529529
:ref:`examples/linear_model/plot_lasso_coordinate_descent_path.py
530530
<sphx_glr_auto_examples_linear_model_plot_lasso_coordinate_descent_path.py>`.
531+
532+
Examples
533+
--------
534+
>>> from sklearn.linear_model import enet_path
535+
>>> from sklearn.datasets import make_regression
536+
>>> X, y, true_coef = make_regression(
537+
... n_samples=100, n_features=5, n_informative=2, coef=True, random_state=0
538+
... )
539+
>>> true_coef
540+
array([ 0. , 0. , 0. , 97.9..., 45.7...])
541+
>>> alphas, estimated_coef, _ = enet_path(X, y, n_alphas=3)
542+
>>> alphas.shape
543+
(3,)
544+
>>> estimated_coef
545+
array([[ 0. , 0.78..., 0.56...],
546+
[ 0. , 1.12..., 0.61...],
547+
[-0. , -2.12..., -1.12...],
548+
[ 0. , 23.04..., 88.93...],
549+
[ 0. , 10.63..., 41.56...]])
531550
"""
532551
X_offset_param = params.pop("X_offset", None)
533552
X_scale_param = params.pop("X_scale", None)

sklearn/linear_model/_least_angle.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ def lars_path(
189189
190190
.. [3] `Wikipedia entry on the Lasso
191191
<https://en.wikipedia.org/wiki/Lasso_(statistics)>`_
192+
193+
Examples
194+
--------
195+
>>> from sklearn.linear_model import lars_path
196+
>>> from sklearn.datasets import make_regression
197+
>>> X, y, true_coef = make_regression(
198+
... n_samples=100, n_features=5, n_informative=2, coef=True, random_state=0
199+
... )
200+
>>> true_coef
201+
array([ 0. , 0. , 0. , 97.9..., 45.7...])
202+
>>> alphas, _, estimated_coef = lars_path(X, y)
203+
>>> alphas.shape
204+
(3,)
205+
>>> estimated_coef
206+
array([[ 0. , 0. , 0. ],
207+
[ 0. , 0. , 0. ],
208+
[ 0. , 0. , 0. ],
209+
[ 0. , 46.96..., 97.99...],
210+
[ 0. , 0. , 45.70...]])
192211
"""
193212
if X is None and Gram is not None:
194213
raise ValueError(
@@ -351,6 +370,25 @@ def lars_path_gram(
351370
352371
.. [3] `Wikipedia entry on the Lasso
353372
<https://en.wikipedia.org/wiki/Lasso_(statistics)>`_
373+
374+
Examples
375+
--------
376+
>>> from sklearn.linear_model import lars_path_gram
377+
>>> from sklearn.datasets import make_regression
378+
>>> X, y, true_coef = make_regression(
379+
... n_samples=100, n_features=5, n_informative=2, coef=True, random_state=0
380+
... )
381+
>>> true_coef
382+
array([ 0. , 0. , 0. , 97.9..., 45.7...])
383+
>>> alphas, _, estimated_coef = lars_path_gram(X.T @ y, X.T @ X, n_samples=100)
384+
>>> alphas.shape
385+
(3,)
386+
>>> estimated_coef
387+
array([[ 0. , 0. , 0. ],
388+
[ 0. , 0. , 0. ],
389+
[ 0. , 0. , 0. ],
390+
[ 0. , 46.96..., 97.99...],
391+
[ 0. , 0. , 45.70...]])
354392
"""
355393
return _lars_path_solver(
356394
X=None,

sklearn/linear_model/_omp.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ def orthogonal_mp(
388388
M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
389389
Matching Pursuit Technical Report - CS Technion, April 2008.
390390
https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf
391+
392+
Examples
393+
--------
394+
>>> from sklearn.datasets import make_regression
395+
>>> from sklearn.linear_model import orthogonal_mp
396+
>>> X, y = make_regression(noise=4, random_state=0)
397+
>>> coef = orthogonal_mp(X, y)
398+
>>> coef.shape
399+
(100,)
400+
>>> X[:1,] @ coef
401+
array([-78.68...])
391402
"""
392403
X = check_array(X, order="F", copy=copy_X)
393404
copy_X = False
@@ -555,6 +566,17 @@ def orthogonal_mp_gram(
555566
M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
556567
Matching Pursuit Technical Report - CS Technion, April 2008.
557568
https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf
569+
570+
Examples
571+
--------
572+
>>> from sklearn.datasets import make_regression
573+
>>> from sklearn.linear_model import orthogonal_mp_gram
574+
>>> X, y = make_regression(noise=4, random_state=0)
575+
>>> coef = orthogonal_mp_gram(X.T @ X, X.T @ y)
576+
>>> coef.shape
577+
(100,)
578+
>>> X[:1,] @ coef
579+
array([-78.68...])
558580
"""
559581
Gram = check_array(Gram, order="F", copy=copy_Gram)
560582
Xy = np.asarray(Xy)

0 commit comments

Comments
 (0)
0