8000 DOC Adds missing imports into examples and test them (#21186) · scikit-learn/scikit-learn@48d4ee9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48d4ee9

Browse files
thomasjpfanglemaitre
authored andcommitted
DOC Adds missing imports into examples and test them (#21186)
1 parent 21218bf commit 48d4ee9

15 files changed

+30
-1
lines changed

sklearn/compose/_column_transformer.py

+1
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ class make_column_selector:
10021002
>>> from sklearn.preprocessing import StandardScaler, OneHotEncoder
10031003
>>> from sklearn.compose import make_column_transformer
10041004
>>> from sklearn.compose import make_column_selector
1005+
>>> import numpy as np
10051006
>>> import pandas as pd # doctest: +SKIP
10061007
>>> X = pd.DataFrame({'city': ['London', 'London', 'Paris', 'Sallisaw'],
10071008
... 'rating': [5, 3, 4, 5]}) # doctest: +SKIP

sklearn/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def pytest_collection_modifyitems(config, items):
154154
except ImportError:
155155
pass
156156

157+
# Normally doctest has the entire module's scope. Here we set globs to an empty dict
158+
# to remove the module's scope:
159+
# https://docs.python.org/3/library/doctest.html#what-s-the-execution-context
160+
for item in items:
161+
if isinstance(item, DoctestItem):
162+
item.dtest.globs = {}
163+
157164
if skip_doctests:
158165
skip_marker = pytest.mark.skip(reason=reason)
159166

sklearn/feature_selection/_variance_threshold.py

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class VarianceThreshold(SelectorMixin, BaseEstimator):
5858
The following dataset has integer features, two of which are the same
5959
in every sample. These are removed with the default setting for threshold::
6060
61< ED4F span class="diff-text-marker">+
>>> from sklearn.feature_selection import VarianceThreshold
6162
>>> X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]]
6263
>>> selector = VarianceThreshold()
6364
>>> selector.fit_transform(X)

sklearn/linear_model/_coordinate_descent.py

+2
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ def lasso_path(
325325
326326
Comparing lasso_path and lars_path with interpolation:
327327
328+
>>> import numpy as np
329+
>>> from sklearn.linear_model import lasso_path
328330
>>> X = np.array([[1, 2, 3.1], [2.3, 5.4, 4.3]]).T
329331
>>> y = np.array([1, 2, 3.1])
330332
>>> # Use lasso_path to compute a coefficient path

sklearn/metrics/_ranking.py

+3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ class scores must correspond to the order of ``labels``,
523523
524524
Multilabel case:
525525
526+
>>> import numpy as np
526527
>>> from sklearn.datasets import make_multilabel_classification
527528
>>> from sklearn.multioutput import MultiOutputClassifier
528529
>>> X, y = make_multilabel_classification(random_state=0)
@@ -1429,6 +1430,7 @@ def dcg_score(
14291430
14301431
Examples
14311432
--------
1433+
>>> import numpy as np
14321434
>>> from sklearn.metrics import dcg_score
14331435
>>> # we have groud-truth relevance of some answers to a query:
14341436
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])
@@ -1578,6 +1580,7 @@ def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False
15781580
15791581
Examples
15801582
--------
1583+
>>> import numpy as np
15811584
>>> from sklearn.metrics import ndcg_score
15821585
>>> # we have groud-truth relevance of some answers to a query:
15831586
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])

sklearn/model_selection/_search_successive_halving.py

+1
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ class HalvingRandomSearchCV(BaseSuccessiveHalving):
10021002
>>> from sklearn.experimental import enable_halving_search_cv # noqa
10031003
>>> from sklearn.model_selection import HalvingRandomSearchCV
10041004
>>> from scipy.stats import randint
1005+
>>> import numpy as np
10051006
...
10061007
>>> X, y = load_iris(return_X_y=True)
10071008
>>> clf = RandomForestClassifier(random_state=0)

sklearn/neighbors/_kde.py

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class KernelDensity(BaseEstimator):
102102
--------
103103
Compute a gaussian kernel density estimate with a fixed bandwidth.
104104
105+
>>> from sklearn.neighbors import KernelDensity
105106
>>> import numpy as np
106107
>>> rng = np.random.RandomState(42)
107108
>>> X = rng.random_sample((100, 3))

sklearn/pipeline.py

+1
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ def make_pipeline(*steps, memory=None, verbose=False):
863863
--------
864864
>>> from sklearn.naive_bayes import GaussianNB
865865
>>> from sklearn.preprocessing import StandardScaler
866+
>>> from sklearn.pipeline import make_pipeline
866867
>>> make_pipeline(StandardScaler(), GaussianNB(priors=None))
867868
Pipeline(steps=[('standardscaler', StandardScaler()),
868869
('gaussiannb', GaussianNB())])

sklearn/preprocessing/_discretization.py

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class KBinsDiscretizer(TransformerMixin, BaseEstimator):
107107
108108
Examples
109109
--------
110+
>>> from sklearn.preprocessing import KBinsDiscretizer
110111
>>> X = [[-2, 1, -4, -1],
111112
... [-1, 2, -3, -0.5],
112113
... [ 0, 3, -2, 0.5],

sklearn/random_projection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def johnson_lindenstrauss_min_dim(n_samples, *, eps=0.1):
9696
9797
Examples
9898
--------
99-
99+
>>> from sklearn.random_projection import johnson_lindenstrauss_min_dim
100100
>>> johnson_lindenstrauss_min_dim(1e6, eps=0.5)
101101
663
102102

sklearn/utils/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Bunch(dict):
8989
9090
Examples
9191
--------
92+
>>> from sklearn.utils import Bunch
92< 10000 /td>93
>>> b = Bunch(a=1, b=2)
9394
>>> b['b']
9495
2
@@ -493,6 +494,7 @@ def resample(*arrays, replace=True, n_samples=None, random_state=None, stratify=
493494
--------
494495
It is possible to mix sparse and dense arrays in the same run::
495496
497+
>>> import numpy as np
496498
>>> X = np.array([[1., 0.], [2., 1.], [0., 0.]])
497499
>>> y = np.array([0, 1, 2])
498500
@@ -630,6 +632,7 @@ def shuffle(*arrays, random_state=None, n_samples=None):
630632
--------
631633
It is possible to mix sparse and dense arrays in the same run::
632634
635+
>>> import numpy as np
633636
>>> X = np.array([[1., 0.], [2., 1.], [0., 0.]])
634637
>>> y = np.array([0, 1, 2])
635638
@@ -999,6 +1002,8 @@ def is_scalar_nan(x):
9991002
10001003
Examples
10011004
--------
1005+
>>> import numpy as np
1006+
>>> from sklearn.utils import is_scalar_nan
10021007
>>> is_scalar_nan(np.nan)
10031008
True
10041009
>>> is_scalar_nan(float("nan"))

sklearn/utils/_testing.py

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def ignore_warnings(obj=None, category=Warning):
255255
256256
Examples
257257
--------
258+
>>> import warnings
259+
>>> from sklearn.utils._testing import ignore_warnings
258260
>>> with ignore_warnings():
259261
... warnings.warn('buhuhuhu')
260262

sklearn/utils/extmath.py

+1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ def cartesian(arrays, out=None):
670670
671671
Examples
672672
--------
673+
>>> from sklearn.utils.extmath import cartesian
673674
>>> cartesian(([1, 2, 3], [4, 5], [6, 7]))
674675
array([[1, 4, 6],
675676
[1, 4, 7],

sklearn/utils/multiclass.py

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def type_of_target(y):
238238
239239
Examples
240240
--------
241+
>>> from sklearn.utils.multiclass import type_of_target
241242
>>> import numpy as np
242243
>>> type_of_target([0.1, 0.6])
243244
'continuous'

sklearn/utils/validation.py

+2
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ def has_fit_parameter(estimator, parameter):
10681068
Examples
10691069
--------
10701070
>>> from sklearn.svm import SVC
1071+
>>> from sklearn.utils.validation import has_fit_parameter
10711072
>>> has_fit_parameter(SVC(), "sample_weight")
10721073
True
10731074
@@ -1372,6 +1373,7 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False):
13721373
13731374
Examples
13741375
--------
1376+
>>> from sklearn.utils.validation import _check_psd_eigenvalues
13751377
>>> _check_psd_eigenvalues([1, 2]) # nominal case
13761378
array([1, 2])
13771379
>>> _check_psd_eigenvalues([5, 5j]) # significant imag part

0 commit comments

Comments
 (0)
0