pydaltoolbox 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. pydaltoolbox/__init__.py +141 -0
  2. pydaltoolbox/adjust.py +75 -0
  3. pydaltoolbox/autoenc.py +58 -0
  4. pydaltoolbox/base.py +103 -0
  5. pydaltoolbox/classification/__init__.py +28 -0
  6. pydaltoolbox/classification/base.py +140 -0
  7. pydaltoolbox/classification/baseline.py +24 -0
  8. pydaltoolbox/classification/dtree.py +65 -0
  9. pydaltoolbox/classification/ensemble.py +78 -0
  10. pydaltoolbox/classification/knn.py +21 -0
  11. pydaltoolbox/classification/linear.py +81 -0
  12. pydaltoolbox/classification/naive_bayes.py +20 -0
  13. pydaltoolbox/classification/neural.py +50 -0
  14. pydaltoolbox/classification/svm.py +49 -0
  15. pydaltoolbox/clustering/__init__.py +8 -0
  16. pydaltoolbox/clustering/base.py +62 -0
  17. pydaltoolbox/clustering/dbscan.py +55 -0
  18. pydaltoolbox/clustering/gmm.py +53 -0
  19. pydaltoolbox/clustering/hclust.py +67 -0
  20. pydaltoolbox/clustering/kmeans.py +44 -0
  21. pydaltoolbox/clustering/metrics.py +139 -0
  22. pydaltoolbox/datasets.py +50 -0
  23. pydaltoolbox/regression/__init__.py +19 -0
  24. pydaltoolbox/regression/base.py +77 -0
  25. pydaltoolbox/regression/dtree.py +28 -0
  26. pydaltoolbox/regression/ensemble.py +31 -0
  27. pydaltoolbox/regression/knn.py +19 -0
  28. pydaltoolbox/regression/lm.py +18 -0
  29. pydaltoolbox/regression/neural.py +50 -0
  30. pydaltoolbox/regression/svm.py +42 -0
  31. pydaltoolbox/sampling.py +113 -0
  32. pydaltoolbox/transform/__init__.py +34 -0
  33. pydaltoolbox/transform/balance.py +158 -0
  34. pydaltoolbox/transform/clean.py +67 -0
  35. pydaltoolbox/transform/curvature.py +58 -0
  36. pydaltoolbox/transform/encode.py +48 -0
  37. pydaltoolbox/transform/feature_selection.py +234 -0
  38. pydaltoolbox/transform/normalize.py +89 -0
  39. pydaltoolbox/transform/outliers.py +77 -0
  40. pydaltoolbox/transform/pca.py +74 -0
  41. pydaltoolbox/tune/__init__.py +6 -0
  42. pydaltoolbox/tune/base.py +60 -0
  43. pydaltoolbox/tune/clustering.py +29 -0
  44. pydaltoolbox/tune/supervised.py +72 -0
  45. pydaltoolbox-0.1.0.dist-info/METADATA +154 -0
  46. pydaltoolbox-0.1.0.dist-info/RECORD +48 -0
  47. pydaltoolbox-0.1.0.dist-info/WHEEL +4 -0
  48. pydaltoolbox-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,141 @@
1
+ """pydaltoolbox -- Leveraging Experiment Lines for Data Analytics.
2
+
3
+ Python port of the R ``daltoolbox`` framework. The public API exposes the
4
+ Experiment Line base classes plus concrete components, all following the uniform
5
+ ``fit`` / ``action`` / ``evaluate`` contract.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from . import datasets
10
+ from .adjust import (
11
+ adjust_class_label,
12
+ adjust_dataframe,
13
+ adjust_factor,
14
+ adjust_matrix,
15
+ )
16
+ from .autoenc import AutoencBaseE, AutoencBaseEd
17
+ from .base import DalBase, DalLearner, DalTransform, Predictor
18
+ from .classification import (
19
+ ClaBagging,
20
+ ClaBoosting,
21
+ ClaDtree,
22
+ ClaGlm,
23
+ ClaGlmnet,
24
+ ClaKnn,
25
+ ClaMajority,
26
+ ClaMlp,
27
+ ClaMultinom,
28
+ ClaNb,
29
+ ClaRf,
30
+ ClaRpart,
31
+ Classification,
32
+ ClaSvm,
33
+ SklearnClassification,
34
+ )
35
+ from .clustering import (
36
+ ClusterDbscan,
37
+ Clusterer,
38
+ ClusterGmm,
39
+ ClusterHclust,
40
+ ClusterKmeans,
41
+ )
42
+ from .regression import (
43
+ RegDtree,
44
+ RegKnn,
45
+ RegLm,
46
+ RegMlp,
47
+ Regression,
48
+ RegRf,
49
+ RegSvm,
50
+ SklearnRegression,
51
+ )
52
+ from .sampling import DataSample, SampleRandom, SampleStratified, train_test_from_folds
53
+ from .transform import (
54
+ BalOversampling,
55
+ BalSubsampling,
56
+ CategMapping,
57
+ DtPca,
58
+ FeatureSelectionCorr,
59
+ FeatureSelectionInfoGain,
60
+ FeatureSelectionLasso,
61
+ FeatureSelectionStepwise,
62
+ FitCurvatureMax,
63
+ FitCurvatureMin,
64
+ ImputationSimple,
65
+ MinMax,
66
+ NaRemoval,
67
+ OutliersBoxplot,
68
+ OutliersGaussian,
69
+ SampleBalance,
70
+ ZScore,
71
+ )
72
+ from .tune import ClaTune, CluTune, DalTune, RegTune
73
+
74
+ __version__ = "0.1.0"
75
+
76
+ __all__ = [
77
+ "AutoencBaseE",
78
+ "AutoencBaseEd",
79
+ "BalOversampling",
80
+ "BalSubsampling",
81
+ "CategMapping",
82
+ "ClaBagging",
83
+ "ClaBoosting",
84
+ "ClaDtree",
85
+ "ClaGlm",
86
+ "ClaGlmnet",
87
+ "ClaKnn",
88
+ "ClaMajority",
89
+ "ClaMlp",
90
+ "ClaMultinom",
91
+ "ClaNb",
92
+ "ClaRf",
93
+ "ClaRpart",
94
+ "ClaSvm",
95
+ "ClaTune",
96
+ "Classification",
97
+ "CluTune",
98
+ "ClusterDbscan",
99
+ "ClusterGmm",
100
+ "ClusterHclust",
101
+ "ClusterKmeans",
102
+ "Clusterer",
103
+ "DalBase",
104
+ "DalLearner",
105
+ "DalTransform",
106
+ "DalTune",
107
+ "DataSample",
108
+ "DtPca",
109
+ "FeatureSelectionCorr",
110
+ "FeatureSelectionInfoGain",
111
+ "FeatureSelectionLasso",
112
+ "FeatureSelectionStepwise",
113
+ "FitCurvatureMax",
114
+ "FitCurvatureMin",
115
+ "ImputationSimple",
116
+ "MinMax",
117
+ "NaRemoval",
118
+ "OutliersBoxplot",
119
+ "OutliersGaussian",
120
+ "Predictor",
121
+ "RegDtree",
122
+ "RegKnn",
123
+ "RegLm",
124
+ "RegMlp",
125
+ "RegRf",
126
+ "RegSvm",
127
+ "RegTune",
128
+ "Regression",
129
+ "SampleBalance",
130
+ "SampleRandom",
131
+ "SampleStratified",
132
+ "SklearnClassification",
133
+ "SklearnRegression",
134
+ "ZScore",
135
+ "adjust_class_label",
136
+ "adjust_dataframe",
137
+ "adjust_factor",
138
+ "adjust_matrix",
139
+ "datasets",
140
+ "train_test_from_folds",
141
+ ]
pydaltoolbox/adjust.py ADDED
@@ -0,0 +1,75 @@
1
+ """Data adjustment helpers.
2
+
3
+ Python counterparts of the R ``dal_adjust.R`` helpers. They coerce inputs into
4
+ the data structures the Experiment Line expects (``pandas``/``numpy``) so that
5
+ every component can assume a consistent input shape.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from collections.abc import Sequence
10
+
11
+ import numpy as np
12
+ import pandas as pd
13
+
14
+ __all__ = [
15
+ "adjust_class_label",
16
+ "adjust_dataframe",
17
+ "adjust_factor",
18
+ "adjust_matrix",
19
+ ]
20
+
21
+
22
+ def adjust_dataframe(data) -> pd.DataFrame:
23
+ """Coerce ``data`` to a :class:`pandas.DataFrame` if needed.
24
+
25
+ Mirrors R's ``adjust_data.frame``.
26
+ """
27
+ if isinstance(data, pd.DataFrame):
28
+ return data
29
+ if isinstance(data, pd.Series):
30
+ return data.to_frame()
31
+ return pd.DataFrame(data)
32
+
33
+
34
+ def adjust_matrix(data) -> np.ndarray:
35
+ """Coerce ``data`` to a 2-D :class:`numpy.ndarray`.
36
+
37
+ Mirrors R's ``adjust_matrix``.
38
+ """
39
+ if isinstance(data, np.ndarray):
40
+ return data
41
+ if isinstance(data, (pd.DataFrame, pd.Series)):
42
+ return data.to_numpy()
43
+ return np.asarray(data)
44
+
45
+
46
+ def adjust_factor(value, slevels: Sequence) -> pd.Categorical:
47
+ """Convert ``value`` to an ordered categorical using ``slevels`` as levels.
48
+
49
+ Mirrors R's ``adjust_factor``. Numeric codes (1-based, as in R) are mapped to
50
+ the corresponding label in ``slevels``; existing labels are kept.
51
+ """
52
+ slevels = list(slevels)
53
+ arr = np.asarray(value)
54
+
55
+ if np.issubdtype(arr.dtype, np.number):
56
+ # R uses 1-based factor codes; map i -> slevels[i - 1].
57
+ labels = [slevels[int(i) - 1] for i in arr]
58
+ return pd.Categorical(labels, categories=slevels)
59
+
60
+ return pd.Categorical(arr, categories=slevels)
61
+
62
+
63
+ def adjust_class_label(x, val_true: float = 1, val_false: float = 0) -> pd.DataFrame:
64
+ """One-hot encode a label vector into an indicator DataFrame.
65
+
66
+ Mirrors R's ``adjust_class_label``: columns are the sorted distinct levels of
67
+ ``x`` and each row has ``val_true`` in the column of its label.
68
+ """
69
+ cat = x if isinstance(x, pd.Categorical) else pd.Categorical(np.asarray(x))
70
+ levels = list(cat.categories)
71
+ codes = cat.codes
72
+ res = np.full((len(codes), len(levels)), val_false, dtype=float)
73
+ valid = codes >= 0
74
+ res[np.arange(len(codes))[valid], codes[valid]] = val_true
75
+ return pd.DataFrame(res, columns=levels)
@@ -0,0 +1,58 @@
1
+ """Autoencoder base classes.
2
+
3
+ Python counterparts of ``autoenc_base_e.R`` (encoder) and ``autoenc_base_ed.R``
4
+ (encoder + decoder). As in the R package, these are **identity** base classes: they do
5
+ not train or transform by themselves. Concrete implementations (e.g. a deep-learning
6
+ backed autoencoder in a companion package, or the detectors in pyharbinger) subclass
7
+ them and override ``fit``/``transform``. They exist here so that downstream code has a
8
+ stable base to extend, without pulling a deep-learning dependency into pydaltoolbox.
9
+ """
10
+ from __future__ import annotations
11
+
12
+ from .base import DalTransform
13
+
14
+ __all__ = ["AutoencBaseE", "AutoencBaseEd"]
15
+
16
+
17
+ class AutoencBaseE(DalTransform):
18
+ """Base class for encoder-only autoencoders (identity by default).
19
+
20
+ Parameters
21
+ ----------
22
+ input_size:
23
+ Dimensionality of the input vector.
24
+ encoding_size:
25
+ Dimensionality of the latent (encoded) representation.
26
+ """
27
+
28
+ def __init__(self, input_size: int, encoding_size: int) -> None:
29
+ self.input_size = input_size
30
+ self.encoding_size = encoding_size
31
+
32
+ def fit(self, data, **kwargs) -> AutoencBaseE:
33
+ return self
34
+
35
+ def transform(self, data, **kwargs):
36
+ return data
37
+
38
+
39
+ class AutoencBaseEd(DalTransform):
40
+ """Base class for encoder + decoder autoencoders (identity by default).
41
+
42
+ Parameters
43
+ ----------
44
+ input_size:
45
+ Dimensionality of the input vector.
46
+ encoding_size:
47
+ Dimensionality of the latent (encoded) representation.
48
+ """
49
+
50
+ def __init__(self, input_size: int, encoding_size: int) -> None:
51
+ self.input_size = input_size
52
+ self.encoding_size = encoding_size
53
+
54
+ def fit(self, data, **kwargs) -> AutoencBaseEd:
55
+ return self
56
+
57
+ def transform(self, data, **kwargs):
58
+ return data
pydaltoolbox/base.py ADDED
@@ -0,0 +1,103 @@
1
+ """Core Experiment Line base classes.
2
+
3
+ Python counterpart of the R ``dal_base.R``, ``dal_transform.R``,
4
+ ``dal_learner.R`` and ``dal_predictor.R`` files.
5
+
6
+ The Experiment Line is a *rigid but extensible* contract. Every component is an
7
+ object that understands the same small protocol:
8
+
9
+ * :meth:`DalBase.fit` -- learn / adjust internal state from data (returns ``self``
10
+ so calls can be chained).
11
+ * :meth:`DalBase.action` -- apply the component to data. It is **polymorphic**:
12
+ transforms dispatch to :meth:`transform`, predictors to :meth:`predict`,
13
+ clusterers to ``cluster`` and pattern miners to ``discover``. This uniform entry
14
+ point is what lets heterogeneous steps be composed in a pipeline.
15
+ * :meth:`DalBase.set_params` -- inject (hyper)parameters, used by tuning.
16
+ * :meth:`DalLearner.evaluate` -- score the component's output.
17
+
18
+ Creating a new method means subclassing the right base and implementing two or
19
+ three methods; everything else (sampling, evaluation, tuning) is inherited.
20
+ """
21
+ from __future__ import annotations
22
+
23
+ from collections.abc import Mapping
24
+
25
+ from .adjust import adjust_dataframe
26
+
27
+ __all__ = ["DalBase", "DalLearner", "DalTransform", "Predictor"]
28
+
29
+
30
+ class DalBase:
31
+ """Minimal abstract base class for all DAL objects."""
32
+
33
+ def fit(self, data=None, **kwargs) -> DalBase:
34
+ """Train/adjust the object. Default is a no-op returning ``self``."""
35
+ return self
36
+
37
+ def action(self, data=None, **kwargs):
38
+ """Apply the object to data. Default forwards ``data`` unchanged."""
39
+ return data
40
+
41
+ def set_params(self, params: Mapping | None) -> DalBase:
42
+ """Assign matching parameters to existing attributes (best effort).
43
+
44
+ Only keys that correspond to an existing attribute are assigned, mirroring
45
+ the R ``set_params.dal_base`` behaviour used by the tuning machinery.
46
+ """
47
+ if params:
48
+ for key, value in dict(params).items():
49
+ if hasattr(self, key):
50
+ setattr(self, key, value)
51
+ return self
52
+
53
+
54
+ class DalTransform(DalBase):
55
+ """Base class for data transformations with optional inverse support."""
56
+
57
+ def transform(self, data, **kwargs):
58
+ """Apply the transformation. Default is identity."""
59
+ return data
60
+
61
+ def inverse_transform(self, data, **kwargs):
62
+ """Revert the transformation. Default is identity."""
63
+ return data
64
+
65
+ def action(self, data, **kwargs):
66
+ """For transforms, ``action`` dispatches to :meth:`transform`."""
67
+ return self.transform(data, **kwargs)
68
+
69
+
70
+ class DalLearner(DalBase):
71
+ """Base ancestor for learning tasks (classification, regression, ...)."""
72
+
73
+ def action(self, data, **kwargs):
74
+ """For learners, ``action`` dispatches to :meth:`predict`."""
75
+ return self.predict(data, **kwargs)
76
+
77
+ def predict(self, data, **kwargs): # pragma: no cover - overridden by subclasses
78
+ raise NotImplementedError
79
+
80
+ def evaluate(self, *args, **kwargs):
81
+ """Evaluate learner performance. Default returns ``None``."""
82
+ return
83
+
84
+
85
+ class Predictor(DalLearner):
86
+ """Ancestor for supervised predictors (classification and regression).
87
+
88
+ Provides a default :meth:`fit` that records the feature columns (everything
89
+ except the target ``attribute``).
90
+ """
91
+
92
+ def __init__(self) -> None:
93
+ self.attribute: str | None = None
94
+ self.x: list | None = None
95
+
96
+ def _prepare_fit(self, data):
97
+ data = adjust_dataframe(data)
98
+ self.x = [c for c in data.columns if c != self.attribute]
99
+ return data
100
+
101
+ def fit(self, data, **kwargs) -> Predictor:
102
+ self._prepare_fit(data)
103
+ return self
@@ -0,0 +1,28 @@
1
+ """Classification family."""
2
+ from .base import Classification, SklearnClassification
3
+ from .baseline import ClaMajority
4
+ from .dtree import ClaDtree, ClaRpart
5
+ from .ensemble import ClaBagging, ClaBoosting, ClaRf
6
+ from .knn import ClaKnn
7
+ from .linear import ClaGlm, ClaGlmnet, ClaMultinom
8
+ from .naive_bayes import ClaNb
9
+ from .neural import ClaMlp
10
+ from .svm import ClaSvm
11
+
12
+ __all__ = [
13
+ "ClaBagging",
14
+ "ClaBoosting",
15
+ "ClaDtree",
16
+ "ClaGlm",
17
+ "ClaGlmnet",
18
+ "ClaKnn",
19
+ "ClaMajority",
20
+ "ClaMlp",
21
+ "ClaMultinom",
22
+ "ClaNb",
23
+ "ClaRf",
24
+ "ClaRpart",
25
+ "ClaSvm",
26
+ "Classification",
27
+ "SklearnClassification",
28
+ ]
@@ -0,0 +1,140 @@
1
+ """Classification base class.
2
+
3
+ Python counterpart of ``cla_classification.R``. Holds the target attribute and
4
+ class levels, and provides the standard classification ``evaluate`` producing
5
+ accuracy plus a binary (one-vs-rest) confusion summary for a reference class.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from collections.abc import Sequence
10
+
11
+ import numpy as np
12
+ import pandas as pd
13
+
14
+ from ..adjust import adjust_dataframe, adjust_factor
15
+ from ..base import Predictor
16
+
17
+ __all__ = ["Classification", "SklearnClassification"]
18
+
19
+
20
+ def _as_labels(prediction, slevels: Sequence) -> np.ndarray:
21
+ """Normalize a prediction (scores matrix or labels) into a label array."""
22
+ slevels = list(slevels)
23
+ if isinstance(prediction, pd.DataFrame):
24
+ cols = [c for c in slevels if c in prediction.columns]
25
+ base = cols if cols else list(prediction.columns)
26
+ mat = prediction[base].to_numpy()
27
+ return np.array([base[i] for i in mat.argmax(axis=1)], dtype=object)
28
+
29
+ arr = prediction.to_numpy() if isinstance(prediction, pd.Series) else np.asarray(prediction)
30
+ if arr.ndim == 2:
31
+ return np.array([slevels[i] for i in arr.argmax(axis=1)], dtype=object)
32
+ return arr.astype(object)
33
+
34
+
35
+ class Classification(Predictor):
36
+ """Ancestor class for classification models.
37
+
38
+ Parameters
39
+ ----------
40
+ attribute:
41
+ Name of the target column.
42
+ slevels:
43
+ Possible class labels. If ``None`` they are inferred at fit time.
44
+ """
45
+
46
+ def __init__(self, attribute: str, slevels: Sequence | None = None) -> None:
47
+ super().__init__()
48
+ self.attribute = attribute
49
+ self.slevels = list(slevels) if slevels is not None else None
50
+
51
+ def _prepare_classification_data(self, data):
52
+ data = adjust_dataframe(data)
53
+ if self.attribute not in data.columns:
54
+ raise ValueError(f"{type(self).__name__}: attribute '{self.attribute}' not found in data.")
55
+ if self.slevels is None:
56
+ y = data[self.attribute]
57
+ if isinstance(y.dtype, pd.CategoricalDtype):
58
+ self.slevels = list(y.cat.categories)
59
+ else:
60
+ self.slevels = list(pd.unique(y.astype(object)))
61
+ data = data.copy()
62
+ data[self.attribute] = adjust_factor(data[self.attribute], self.slevels)
63
+ self._prepare_fit(data)
64
+ return data
65
+
66
+ def evaluate(self, data, prediction, ref: int = 0, **kwargs) -> dict:
67
+ """Compute classification metrics.
68
+
69
+ ``data`` holds the true labels (or a score matrix), ``prediction`` holds
70
+ predicted scores (or labels). ``ref`` is the 0-based index of the positive
71
+ class for the one-vs-rest confusion metrics.
72
+ """
73
+ slevels = self.slevels
74
+ true_f = _as_labels(data, slevels)
75
+ pred_f = _as_labels(prediction, slevels)
76
+
77
+ metrics = {}
78
+ metrics["accuracy"] = float(np.mean(true_f == pred_f))
79
+
80
+ pos = slevels[ref]
81
+ t = true_f == pos
82
+ p = pred_f == pos
83
+ tp = int(np.sum(t & p))
84
+ tn = int(np.sum(~t & ~p))
85
+ fp = int(np.sum(~t & p))
86
+ fn = int(np.sum(t & ~p))
87
+ metrics.update(TP=tp, TN=tn, FP=fp, FN=fn)
88
+
89
+ precision = tp / (tp + fp) if (tp + fp) else float("nan")
90
+ recall = tp / (tp + fn) if (tp + fn) else float("nan")
91
+ specificity = tn / (tn + fp) if (tn + fp) else float("nan")
92
+ f1 = (
93
+ 2 * precision * recall / (precision + recall)
94
+ if precision + recall
95
+ else float("nan")
96
+ )
97
+ metrics.update(
98
+ precision=precision,
99
+ recall=recall,
100
+ sensitivity=recall,
101
+ specificity=specificity,
102
+ f1=f1,
103
+ )
104
+
105
+ return {
106
+ "data": true_f,
107
+ "prediction": pred_f,
108
+ "metrics": pd.DataFrame([metrics]),
109
+ }
110
+
111
+
112
+ class SklearnClassification(Classification):
113
+ """Base for classifiers that wrap a scikit-learn estimator.
114
+
115
+ This captures the whole integration contract once: ``fit`` prepares the data,
116
+ builds the estimator and trains it; ``predict`` returns a class-score table.
117
+ Concrete learners only implement :meth:`_make_model` (and store their
118
+ hyperparameters as attributes, so ``set_params`` can tune them). This is the
119
+ Experiment Line extensibility promise in practice.
120
+ """
121
+
122
+ def _make_model(self): # pragma: no cover - overridden by subclasses
123
+ raise NotImplementedError
124
+
125
+ def fit(self, data, **kwargs) -> SklearnClassification:
126
+ data = self._prepare_classification_data(data)
127
+ self.model = self._make_model()
128
+ self.model.fit(data[self.x], data[self.attribute].astype(object))
129
+ return self
130
+
131
+ def predict(self, x, **kwargs) -> pd.DataFrame:
132
+ """Return a class-score table (one column per class level).
133
+
134
+ Only the feature columns (``self.x``) are used, so ``x`` may be the feature
135
+ frame or the full dataset including the target; the target is ignored.
136
+ """
137
+ x = adjust_dataframe(x)
138
+ proba = self.model.predict_proba(x[self.x])
139
+ result = pd.DataFrame(proba, columns=[str(c) for c in self.model.classes_])
140
+ return result.reindex(columns=[str(s) for s in self.slevels], fill_value=0.0)
@@ -0,0 +1,24 @@
1
+ """Baseline classifier (``cla_majority.R`` analog)."""
2
+ from __future__ import annotations
3
+
4
+ from collections.abc import Sequence
5
+
6
+ from sklearn.dummy import DummyClassifier
7
+
8
+ from .base import SklearnClassification
9
+
10
+ __all__ = ["ClaMajority"]
11
+
12
+
13
+ class ClaMajority(SklearnClassification):
14
+ """Majority-class baseline (wraps scikit-learn's DummyClassifier).
15
+
16
+ Always predicts the most frequent class. Useful as a reference floor against
17
+ which real learners are compared.
18
+ """
19
+
20
+ def __init__(self, attribute: str, slevels: Sequence | None = None) -> None:
21
+ super().__init__(attribute, slevels)
22
+
23
+ def _make_model(self) -> DummyClassifier:
24
+ return DummyClassifier(strategy="most_frequent")
@@ -0,0 +1,65 @@
1
+ """Decision tree classifiers.
2
+
3
+ Thin adapters over scikit-learn trees. ``ClaDtree`` mirrors ``cla_dtree.R`` and
4
+ ``ClaRpart`` mirrors ``cla_rpart.R``. scikit-learn trees grow to full depth by
5
+ default (which overfits), whereas R's ``tree`` and ``rpart`` pre-prune; we set
6
+ pre-pruning defaults that approximate those packages so quality matches.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Sequence
11
+
12
+ from sklearn.tree import DecisionTreeClassifier
13
+
14
+ from .base import SklearnClassification
15
+
16
+ __all__ = ["ClaDtree", "ClaRpart"]
17
+
18
+
19
+ class ClaDtree(SklearnClassification):
20
+ """Decision tree for classification (wraps scikit-learn).
21
+
22
+ Pre-pruning defaults (``min_samples_split=10``, ``min_samples_leaf=5``)
23
+ approximate R ``tree::tree`` (``minsize=10``, ``mincut=5``).
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ attribute: str,
29
+ slevels: Sequence | None = None,
30
+ random_state: int | None = None,
31
+ ) -> None:
32
+ super().__init__(attribute, slevels)
33
+ self.random_state = random_state
34
+
35
+ def _make_model(self) -> DecisionTreeClassifier:
36
+ return DecisionTreeClassifier(
37
+ min_samples_split=10,
38
+ min_samples_leaf=5,
39
+ random_state=self.random_state,
40
+ )
41
+
42
+
43
+ class ClaRpart(SklearnClassification):
44
+ """CART decision tree (the ``rpart`` analog, wraps scikit-learn).
45
+
46
+ Pre-pruning defaults (``min_samples_split=20``, ``min_samples_leaf=7``)
47
+ approximate R ``rpart`` (``minsplit=20``, ``minbucket=7``).
48
+ """
49
+
50
+ def __init__(
51
+ self,
52
+ attribute: str,
53
+ slevels: Sequence | None = None,
54
+ random_state: int | None = None,
55
+ ) -> None:
56
+ super().__init__(attribute, slevels)
57
+ self.random_state = random_state
58
+
59
+ def _make_model(self) -> DecisionTreeClassifier:
60
+ return DecisionTreeClassifier(
61
+ criterion="gini",
62
+ min_samples_split=20,
63
+ min_samples_leaf=7,
64
+ random_state=self.random_state,
65
+ )