Aluode/PerceptionLabPortable
0
1# Authors: The scikit-learn developers
2# SPDX-License-Identifier: BSD-3-Clause
3
4import pytest
5
6from sklearn import metrics
7from sklearn.ensemble import (
8 BaggingClassifier,
9 BaggingRegressor,
10 IsolationForest,
11 StackingClassifier,
12 StackingRegressor,
13)
14from sklearn.utils._testing import assert_docstring_consistency, skip_if_no_numpydoc
15
16CLASS_DOCSTRING_CONSISTENCY_CASES = [
17 {
18 "objects": [BaggingClassifier, BaggingRegressor, IsolationForest],
19 "include_params": ["max_samples"],
20 "exclude_params": None,
21 "include_attrs": False,
22 "exclude_attrs": None,
23 "include_returns": False,
24 "exclude_returns": None,
25 "descr_regex_pattern": r"The number of samples to draw from X to train each.*",
26 "ignore_types": ("max_samples"),
27 },
28 {
29 "objects": [StackingClassifier, StackingRegressor],
30 "include_params": ["cv", "n_jobs", "passthrough", "verbose"],
31 "exclude_params": None,
32 "include_attrs": True,
33 "exclude_attrs": ["final_estimator_"],
34 "include_returns": False,
35 "exclude_returns": None,
36 "descr_regex_pattern": None,
37 },
38]
39
40FUNCTION_DOCSTRING_CONSISTENCY_CASES = [
41 {
42 "objects": [
43 metrics.precision_recall_fscore_support,
44 metrics.f1_score,
45 metrics.fbeta_score,
46 metrics.precision_score,
47 metrics.recall_score,
48 ],
49 "include_params": True,
50 "exclude_params": ["average", "zero_division"],
51 "include_attrs": False,
52 "exclude_attrs": None,
53 "include_returns": False,
54 "exclude_returns": None,
55 "descr_regex_pattern": None,
56 },
57 {
58 "objects": [
59 metrics.precision_recall_fscore_support,
60 metrics.f1_score,
61 metrics.fbeta_score,
62 metrics.precision_score,
63 metrics.recall_score,
64 ],
65 "include_params": ["average"],
66 "exclude_params": None,
67 "include_attrs": False,
68 "exclude_attrs": None,
69 "include_returns": False,
70 "exclude_returns": None,
71 "descr_regex_pattern": " ".join(
72 (
73 r"""This parameter is required for multiclass/multilabel targets\.
74 If ``None``, the metrics for each class are returned\. Otherwise, this
75 determines the type of averaging performed on the data:
76 ``'binary'``:
77 Only report results for the class specified by ``pos_label``\.
78 This is applicable only if targets \(``y_\{true,pred\}``\) are binary\.
79 ``'micro'``:
80 Calculate metrics globally by counting the total true positives,
81 false negatives and false positives\.
82 ``'macro'``:
83 Calculate metrics for each label, and find their unweighted
84 mean\. This does not take label imbalance into account\.
85 ``'weighted'``:
86 Calculate metrics for each label, and find their average weighted
87 by support \(the number of true instances for each label\)\. This
88 alters 'macro' to account for label imbalance; it can result in an
89 F-score that is not between precision and recall\."""
90 r"[\s\w]*\.*" # optionally match additional sentence
91 r"""
92 ``'samples'``:
93 Calculate metrics for each instance, and find their average \(only
94 meaningful for multilabel classification where this differs from
95 :func:`accuracy_score`\)\."""
96 ).split()
97 ),
98 },
99]
100
101
102@pytest.mark.parametrize("case", CLASS_DOCSTRING_CONSISTENCY_CASES)
103@skip_if_no_numpydoc
104def test_class_docstring_consistency(case):
105 """Check docstrings parameters consistency between related classes."""
106 assert_docstring_consistency(**case)
107
108
109@pytest.mark.parametrize("case", FUNCTION_DOCSTRING_CONSISTENCY_CASES)
110@skip_if_no_numpydoc
111def test_function_docstring_consistency(case):
112 """Check docstrings parameters consistency between related functions."""
113 assert_docstring_consistency(**case)
114 