multimodel-analysis 0.3.0__tar.gz → 0.4.0__tar.gz

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 (26) hide show
  1. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/CHANGELOG.md +60 -48
  2. {multimodel_analysis-0.3.0/multimodel_analysis.egg-info → multimodel_analysis-0.4.0}/PKG-INFO +2 -1
  3. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/__init__.py +1 -1
  4. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/base.py +12 -3
  5. multimodel_analysis-0.4.0/multimodel_analysis/core/plotting.py +66 -0
  6. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/registry.py +28 -3
  7. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/supervised/classification.py +80 -26
  8. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/supervised/regression.py +90 -20
  9. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0/multimodel_analysis.egg-info}/PKG-INFO +2 -1
  10. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis.egg-info/SOURCES.txt +1 -0
  11. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis.egg-info/requires.txt +1 -0
  12. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/pyproject.toml +1 -1
  13. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/LICENSE +0 -0
  14. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/MANIFEST.in +0 -0
  15. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/__init__.py +0 -0
  16. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/persistence.py +0 -0
  17. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/preprocessing.py +0 -0
  18. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/core/utils.py +0 -0
  19. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/multimodel.py +0 -0
  20. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis/supervised/__init__.py +0 -0
  21. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis.egg-info/dependency_links.txt +0 -0
  22. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/multimodel_analysis.egg-info/top_level.txt +0 -0
  23. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/readme.md +0 -0
  24. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/requirements.txt +0 -0
  25. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/setup.cfg +0 -0
  26. {multimodel_analysis-0.3.0 → multimodel_analysis-0.4.0}/setup.py +0 -0
@@ -1,48 +1,60 @@
1
- # Changelog
2
-
3
- ## 0.3.0 - Phase 4: hyper-parameter tuning
4
-
5
- - `tune_models(include=, exclude=, search='random'|'grid'|'halving', n_iter=20, cv=5, scoring=, param_grids=)`: tunes each model on the training split with leak-free CV (preprocessing is refitted inside every fold) and returns the same report tuples as `run_all_models`, so `show_tabular_report`, `predict`, plots and `save_model` work on tuned models.
6
- - Every built-in classifier and regressor ships a default `search_space` (`ModelSpec.search_space`); pass `param_grids={'knn': {...}}` to override one.
7
- - New attributes `best_params_` (per model) and `tuning_results_` (best CV score, parameters, candidates tried, search time). Failures go to `failed_models_`; `n_iter` is capped at the grid size.
8
- - New notebook `examples/phase4_tuning.ipynb` with executed output.
9
- - Repo cleanup: removed stale example notebooks/CSV and an old PyPI screenshot; `.gitignore` and `MANIFEST.in` tidied.
10
-
11
- ## 0.2.0 - restructure, registry, preprocessing, prediction, cross-validation
12
-
13
- ### Phase 3: preprocessing, prediction, persistence, cross-validation
14
-
15
- - `preprocess=True` (classifier and regressor): impute missing values, one-hot encode text columns and scale, fitted on the training split only. Default stays strict.
16
- - `predict(X_new, model=None)`, `export_model()`, `save_model(path)` and `load_model(path)` (joblib); a saved `TrainedModel` carries its preprocessing and returns original class labels.
17
- - `cross_validate_models(cv=5, scoring=..., include=, exclude=, custom_models=)`: leak-free CV on the training split, results in `cv_results_`.
18
- - `class_weight='balanced'` for classification (Logistic Regression, SVM, Decision Tree, Random Forest).
19
- - `Fit Time (s)` column in both reports.
20
-
21
- ### Phase 2: package restructure
22
-
23
- - Code split into `multimodel_analysis/core` (base, registry, utils) and `multimodel_analysis/supervised` (classification, regression). `multimodel_analysis.multimodel` remains as a compatibility shim; all old imports and method names still work.
24
- - Model registry (`ModelSpec`, `ModelRegistry`): every built-in model is one registry entry (key, display name, factory, defaults).
25
- - New: `run_model(name, **params)`, `available_models()`, `get_model(name)`, `run_all_models(include=, exclude=)`, `estimators_` (fitted models) and `fit_times_`.
26
-
27
- ## 0.1.4 - Phase 0/1: bug fixes and packaging
28
-
29
- ### Behaviour changes (read these)
30
- - `scaled_data` now defaults to `True` (features are standardised, fit on the training split only).
31
- - `Support_vector_model` now defaults to `kernel='rbf'` (was `'linear'`).
32
- - The `ROC AUC` column is numeric (`NaN` when unavailable) instead of the string `"N/A"`.
33
- - `run_all_models()` no longer silences all warnings: they are stored in `warnings_` and the important ones are printed.
34
- - Models that fail are listed in `failed_models_` and shown under the report table.
35
- - `MultiModelRegressor.run_all_models()` now includes a KNN regressor (9 models); `SVR` standardises the target internally.
36
-
37
- ### Fixed
38
- - Confusion matrix / classification report crashed or had the wrong shape when the test split missed a class.
39
- - Multiclass ROC AUC was `NaN` whenever one class was absent from the test split; it is now computed on the classes present.
40
- - Continuous targets, NaN/inf or text columns in `X`, X/y length mismatch and invalid `test_size` now raise clear errors.
41
- - Seaborn palette `FutureWarning` in `plot_comparison`.
42
- - `LogisticRegression(n_jobs=...)` deprecation warning on scikit-learn >= 1.8.
43
- - `.xlsx` export without `openpyxl` gives an install hint; legacy `.xls` is rejected clearly.
44
- - Removed dead code (unused imports, broken `__main__` check).
45
-
46
- ### Added
47
- - `verbose`, `warnings_`, `failed_models_`, `sort_by=` in `show_tabular_report`.
48
- - `pyproject.toml`, GitHub Actions CI (tests + ruff), pytest suite (25 tests), `examples/phase1_results.ipynb`.
1
+ # Changelog
2
+
3
+ ## 0.4.0 - Phase 5: more models and optional boosters
4
+
5
+ - New opt-in models. `run_all_models()` still runs the same standard set; use `include=[...]` for specific ones or `include='all'` for every model whose package is installed.
6
+ - Classification: Extra Trees, Hist Gradient Boosting, Neural Network (MLP), Linear Discriminant Analysis, Ridge Classifier, Linear SVM.
7
+ - Regression: Elastic Net, Bayesian Ridge, Huber Regressor, Linear SVR, Extra Trees, Hist Gradient Boosting, Neural Network (MLP).
8
+ - Optional boosters (classification and regression): XGBoost, LightGBM, CatBoost - `pip install multimodel_analysis[boost]`. A missing package is skipped by `include='all'` and reported with an install hint when asked for by name.
9
+ - Every new model has a search space, so `tune_models`, `cross_validate_models`, `predict` and `save_model` work with them. `class_weight='balanced'` reaches Extra Trees, Ridge Classifier, Linear SVM and LightGBM.
10
+ - `ModelSpec` gained `default` (part of the standard set?) and `available` (is the optional package installed?). New `default_models()` and `available_models(installed_only=False)`.
11
+ - SVR, Linear SVR and the MLP regressor standardise the target internally.
12
+ - New notebook `examples/phase5_more_models.ipynb` with executed output.
13
+ - `plot_comparison` redesigned: one clean ranked bar chart (best model on top, value on every bar). It shows F1 Score / R2 Score by default; pick another with `metric=`. The old grouped bars were unreadable with 17+ models.
14
+
15
+ ## 0.3.0 - Phase 4: hyper-parameter tuning
16
+
17
+ - `tune_models(include=, exclude=, search='random'|'grid'|'halving', n_iter=20, cv=5, scoring=, param_grids=)`: tunes each model on the training split with leak-free CV (preprocessing is refitted inside every fold) and returns the same report tuples as `run_all_models`, so `show_tabular_report`, `predict`, plots and `save_model` work on tuned models.
18
+ - Every built-in classifier and regressor ships a default `search_space` (`ModelSpec.search_space`); pass `param_grids={'knn': {...}}` to override one.
19
+ - New attributes `best_params_` (per model) and `tuning_results_` (best CV score, parameters, candidates tried, search time). Failures go to `failed_models_`; `n_iter` is capped at the grid size.
20
+ - New notebook `examples/phase4_tuning.ipynb` with executed output.
21
+ - Repo cleanup: removed stale example notebooks/CSV and an old PyPI screenshot; `.gitignore` and `MANIFEST.in` tidied.
22
+
23
+ ## 0.2.0 - restructure, registry, preprocessing, prediction, cross-validation
24
+
25
+ ### Phase 3: preprocessing, prediction, persistence, cross-validation
26
+
27
+ - `preprocess=True` (classifier and regressor): impute missing values, one-hot encode text columns and scale, fitted on the training split only. Default stays strict.
28
+ - `predict(X_new, model=None)`, `export_model()`, `save_model(path)` and `load_model(path)` (joblib); a saved `TrainedModel` carries its preprocessing and returns original class labels.
29
+ - `cross_validate_models(cv=5, scoring=..., include=, exclude=, custom_models=)`: leak-free CV on the training split, results in `cv_results_`.
30
+ - `class_weight='balanced'` for classification (Logistic Regression, SVM, Decision Tree, Random Forest).
31
+ - `Fit Time (s)` column in both reports.
32
+
33
+ ### Phase 2: package restructure
34
+
35
+ - Code split into `multimodel_analysis/core` (base, registry, utils) and `multimodel_analysis/supervised` (classification, regression). `multimodel_analysis.multimodel` remains as a compatibility shim; all old imports and method names still work.
36
+ - Model registry (`ModelSpec`, `ModelRegistry`): every built-in model is one registry entry (key, display name, factory, defaults).
37
+ - New: `run_model(name, **params)`, `available_models()`, `get_model(name)`, `run_all_models(include=, exclude=)`, `estimators_` (fitted models) and `fit_times_`.
38
+
39
+ ## 0.1.4 - Phase 0/1: bug fixes and packaging
40
+
41
+ ### Behaviour changes (read these)
42
+ - `scaled_data` now defaults to `True` (features are standardised, fit on the training split only).
43
+ - `Support_vector_model` now defaults to `kernel='rbf'` (was `'linear'`).
44
+ - The `ROC AUC` column is numeric (`NaN` when unavailable) instead of the string `"N/A"`.
45
+ - `run_all_models()` no longer silences all warnings: they are stored in `warnings_` and the important ones are printed.
46
+ - Models that fail are listed in `failed_models_` and shown under the report table.
47
+ - `MultiModelRegressor.run_all_models()` now includes a KNN regressor (9 models); `SVR` standardises the target internally.
48
+
49
+ ### Fixed
50
+ - Confusion matrix / classification report crashed or had the wrong shape when the test split missed a class.
51
+ - Multiclass ROC AUC was `NaN` whenever one class was absent from the test split; it is now computed on the classes present.
52
+ - Continuous targets, NaN/inf or text columns in `X`, X/y length mismatch and invalid `test_size` now raise clear errors.
53
+ - Seaborn palette `FutureWarning` in `plot_comparison`.
54
+ - `LogisticRegression(n_jobs=...)` deprecation warning on scikit-learn >= 1.8.
55
+ - `.xlsx` export without `openpyxl` gives an install hint; legacy `.xls` is rejected clearly.
56
+ - Removed dead code (unused imports, broken `__main__` check).
57
+
58
+ ### Added
59
+ - `verbose`, `warnings_`, `failed_models_`, `sort_by=` in `show_tabular_report`.
60
+ - `pyproject.toml`, GitHub Actions CI (tests + ruff), pytest suite (25 tests), `examples/phase1_results.ipynb`.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multimodel_analysis
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: A Python package for automatic multi-model analysis (classification, regression and clustering)
5
5
  Author-email: Uditya Narayan Tiwari <tiwarimerit@gmail.com>
6
6
  License: Apache-2.0
@@ -24,6 +24,7 @@ Requires-Dist: openpyxl>=3.0; extra == "excel"
24
24
  Provides-Extra: boost
25
25
  Requires-Dist: xgboost>=1.7; extra == "boost"
26
26
  Requires-Dist: lightgbm>=3.3; extra == "boost"
27
+ Requires-Dist: catboost>=1.2; extra == "boost"
27
28
  Provides-Extra: dev
28
29
  Requires-Dist: pytest>=7; extra == "dev"
29
30
  Requires-Dist: ruff>=0.4; extra == "dev"
@@ -5,7 +5,7 @@ from .core.utils import save_report
5
5
  from .multimodel import MultiModelRegressior
6
6
  from .supervised import MultiModelClassifier, MultiModelRegressor
7
7
 
8
- __version__ = '0.3.0'
8
+ __version__ = '0.4.0'
9
9
  __author__ = 'Uditya Narayan Tiwari'
10
10
 
11
11
  __all__ = [
@@ -120,9 +120,18 @@ class _BaseMultiModel:
120
120
  return self._run_spec(name, random_state=random_state, **params)
121
121
 
122
122
  @classmethod
123
- def available_models(cls) -> List[str]:
124
- """Keys of the built-in models, usable in ``run_model`` / ``include`` / ``exclude``."""
125
- return cls.registry.keys()
123
+ def available_models(cls, installed_only: bool = False) -> List[str]:
124
+ """Keys of the built-in models, usable in ``run_model`` / ``include`` / ``exclude``.
125
+
126
+ Includes the extra models that ``run_all_models()`` skips by default; with ``installed_only=True``
127
+ models whose optional package (xgboost, lightgbm, catboost) is missing are left out.
128
+ """
129
+ return [s.key for s in cls.registry if s.available or not installed_only]
130
+
131
+ @classmethod
132
+ def default_models(cls) -> List[str]:
133
+ """Keys of the models ``run_all_models()`` runs when ``include`` is not given."""
134
+ return [s.key for s in cls.registry if s.default]
126
135
 
127
136
  def get_model(self, name: str) -> Any:
128
137
  """Returns the FITTED estimator for ``name`` (display name, as shown in the report)."""
@@ -0,0 +1,66 @@
1
+ """Shared plotting helper: a simple ranked bar chart for comparing many models at once."""
2
+ from typing import Optional, Sequence
3
+
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+
7
+ _BAR = "#8fb3c9" # every model
8
+ _BEST = "#1f5f8b" # the best model
9
+ _INK = "#2b2b2b"
10
+
11
+
12
+ def _fmt(value: float) -> str:
13
+ if not np.isfinite(value):
14
+ return "N/A"
15
+ return f"{value:.3f}" if abs(value) < 1000 else f"{value:,.0f}"
16
+
17
+
18
+ def comparison_figure(
19
+ names: Sequence[str],
20
+ values: Sequence[float],
21
+ metric: str,
22
+ title: str,
23
+ lower_is_better: bool = False,
24
+ footnote: Optional[str] = None,
25
+ ):
26
+ """Horizontal bar chart of one metric, best model on top, value printed at the end of each bar.
27
+
28
+ Returns the figure; the caller saves / shows it.
29
+ """
30
+ names = [str(n) for n in names]
31
+ vals = np.array([np.nan if v is None else float(v) for v in values])
32
+ # best first; missing values go last
33
+ sort_key = np.where(np.isfinite(vals), vals if lower_is_better else -vals, np.inf)
34
+ order = np.argsort(sort_key, kind="stable")
35
+ names = [names[i] for i in order]
36
+ vals = vals[order]
37
+ n = len(names)
38
+
39
+ fig, ax = plt.subplots(figsize=(9.5, max(3.6, 0.42 * n + 1.6)))
40
+ ys = np.arange(n)
41
+ colors = [_BEST if i == 0 else _BAR for i in range(n)]
42
+ ax.barh(ys, np.nan_to_num(vals, nan=0.0), color=colors, height=0.68)
43
+
44
+ finite = vals[np.isfinite(vals)]
45
+ lo = min(0.0, finite.min()) if finite.size else 0.0
46
+ hi = max(0.0, finite.max()) if finite.size else 1.0
47
+ pad = (hi - lo) * 0.012 or 0.01
48
+ for y, v in zip(ys, vals):
49
+ ax.text(max(v, 0.0) + pad if np.isfinite(v) else pad, y, _fmt(v), va="center", ha="left",
50
+ fontsize=10, color=_INK, fontweight="bold" if y == 0 else "normal")
51
+
52
+ ax.set_xlim(lo - (hi - lo) * 0.02 if lo < 0 else 0, hi + (hi - lo) * 0.12 or 1.0)
53
+ ax.set_yticks(ys)
54
+ ax.set_yticklabels(names, fontsize=10.5, color=_INK)
55
+ ax.invert_yaxis()
56
+ ax.set_xlabel(f"{metric} ({'lower' if lower_is_better else 'higher'} is better)", fontsize=10)
57
+ ax.set_title(title, loc="left", fontsize=14, fontweight="bold", color=_INK, pad=12)
58
+ ax.grid(axis="y", visible=False)
59
+ ax.grid(axis="x", color="#e5e5e5", lw=0.8)
60
+ ax.set_axisbelow(True)
61
+ for side in ("top", "right"):
62
+ ax.spines[side].set_visible(False)
63
+ if footnote:
64
+ fig.text(0.01, 0.005, footnote, ha="left", va="bottom", fontsize=8.5, color="#777777")
65
+ fig.tight_layout(rect=(0, 0.03 if footnote else 0, 1, 1))
66
+ return fig
@@ -1,4 +1,5 @@
1
1
  """Model registry: one ``ModelSpec`` per built-in model so adding a model is a one-line change."""
2
+ import importlib.util
2
3
  from dataclasses import dataclass, field
3
4
  from typing import Any, Callable, Dict, Iterable, List, Optional
4
5
 
@@ -25,6 +26,8 @@ class ModelSpec:
25
26
  (e.g. ``'regressor__'`` for a TransformedTargetRegressor); keys in ``search_space``
26
27
  and ``**params`` stay un-prefixed.
27
28
  requires optional third-party package needed, if any (e.g. ``'xgboost'``).
29
+ default ``True`` = part of the standard ``run_all_models()`` set; ``False`` = extra model that
30
+ runs only when asked for with ``include=[...]`` or ``include='all'``.
28
31
  """
29
32
  key: str
30
33
  name: str
@@ -33,8 +36,19 @@ class ModelSpec:
33
36
  search_space: Dict[str, Any] = field(default_factory=dict)
34
37
  requires: Optional[str] = None
35
38
  param_prefix: str = ""
39
+ default: bool = True
40
+
41
+ @property
42
+ def available(self) -> bool:
43
+ """``False`` when the optional package this model needs is not installed."""
44
+ return self.requires is None or importlib.util.find_spec(self.requires) is not None
36
45
 
37
46
  def build(self, ctx: BuildContext, **params):
47
+ if not self.available:
48
+ raise ImportError(
49
+ f"'{self.name}' needs the optional package '{self.requires}'. "
50
+ f"Install it with: pip install {self.requires}"
51
+ )
38
52
  return self.factory(ctx, **{**self.defaults, **params})
39
53
 
40
54
 
@@ -82,10 +96,21 @@ class ModelRegistry:
82
96
  return spec
83
97
  raise KeyError(f"Unknown model '{ref}'. Available models: {self.keys()}")
84
98
 
85
- def select(self, include: Optional[Iterable] = None, exclude: Optional[Iterable] = None) -> List[ModelSpec]:
86
- """Returns specs filtered by ``include`` (keep only these) and ``exclude`` (drop these), keeping order."""
99
+ def select(self, include=None, exclude: Optional[Iterable] = None) -> List[ModelSpec]:
100
+ """Returns specs filtered by ``include`` and ``exclude``, keeping registry order.
101
+
102
+ ``include=None`` the standard set (``default=True`` models).
103
+ ``include='all'`` every model whose optional package is installed.
104
+ ``include=[...]`` exactly these models (a missing optional package is reported when it runs).
105
+ """
87
106
  specs = list(self._specs.values())
88
- if include is not None:
107
+ if include is None:
108
+ specs = [s for s in specs if s.default]
109
+ elif isinstance(include, str) and _norm(include) == "all":
110
+ specs = [s for s in specs if s.available]
111
+ else:
112
+ if isinstance(include, (str, ModelSpec)):
113
+ include = [include]
89
114
  wanted = [self.resolve(r).key for r in include]
90
115
  specs = [s for s in specs if s.key in wanted]
91
116
  if exclude is not None:
@@ -6,8 +6,15 @@ import matplotlib.pyplot as plt
6
6
  import numpy as np
7
7
  import pandas as pd
8
8
  import seaborn as sns
9
- from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier
10
- from sklearn.linear_model import LogisticRegression
9
+ from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
10
+ from sklearn.ensemble import (
11
+ AdaBoostClassifier,
12
+ ExtraTreesClassifier,
13
+ GradientBoostingClassifier,
14
+ HistGradientBoostingClassifier,
15
+ RandomForestClassifier,
16
+ )
17
+ from sklearn.linear_model import LogisticRegression, RidgeClassifier
11
18
  from sklearn.metrics import (
12
19
  accuracy_score,
13
20
  classification_report,
@@ -20,12 +27,14 @@ from sklearn.metrics import (
20
27
  )
21
28
  from sklearn.naive_bayes import GaussianNB
22
29
  from sklearn.neighbors import KNeighborsClassifier
30
+ from sklearn.neural_network import MLPClassifier
23
31
  from sklearn.preprocessing import LabelEncoder
24
- from sklearn.svm import SVC
32
+ from sklearn.svm import SVC, LinearSVC
25
33
  from sklearn.tree import DecisionTreeClassifier
26
34
  from sklearn.utils.multiclass import type_of_target
27
35
 
28
36
  from ..core.base import _BaseMultiModel
37
+ from ..core.plotting import comparison_figure
29
38
  from ..core.registry import ModelRegistry, ModelSpec
30
39
  from ..core.utils import _apply_plot_style, _safe_print, save_report
31
40
 
@@ -42,6 +51,22 @@ def _knn_clf(ctx, n_neighbors=None, **kw):
42
51
  return KNeighborsClassifier(n_neighbors=n, n_jobs=ctx.n_jobs, **kw)
43
52
 
44
53
 
54
+ def _xgb_clf(ctx, **p):
55
+ from xgboost import XGBClassifier
56
+ return XGBClassifier(random_state=ctx.random_state, n_jobs=ctx.n_jobs, verbosity=0, **p)
57
+
58
+
59
+ def _lgbm_clf(ctx, **p):
60
+ from lightgbm import LGBMClassifier
61
+ return LGBMClassifier(random_state=ctx.random_state, n_jobs=ctx.n_jobs, verbose=-1, **_cw(ctx, p))
62
+
63
+
64
+ def _cat_clf(ctx, **p):
65
+ from catboost import CatBoostClassifier
66
+ return CatBoostClassifier(random_seed=ctx.random_state, thread_count=ctx.n_jobs, verbose=0,
67
+ allow_writing_files=False, **p)
68
+
69
+
45
70
  CLASSIFIER_REGISTRY = ModelRegistry([
46
71
  ModelSpec("logistic_regression", "Logistic Regression",
47
72
  lambda ctx, **p: LogisticRegression(random_state=ctx.random_state, **_cw(ctx, p)),
@@ -75,6 +100,47 @@ CLASSIFIER_REGISTRY = ModelRegistry([
75
100
  lambda ctx, **p: AdaBoostClassifier(random_state=ctx.random_state, **p),
76
101
  defaults={"n_estimators": 50},
77
102
  search_space={"n_estimators": [25, 50, 100, 200], "learning_rate": [0.01, 0.1, 0.5, 1.0]}),
103
+
104
+ # ---- extra models (Phase 5): run with include=[...] or include='all' ------------------------
105
+ ModelSpec("extra_trees", "Extra Trees",
106
+ lambda ctx, **p: ExtraTreesClassifier(random_state=ctx.random_state, n_jobs=ctx.n_jobs, **_cw(ctx, p)),
107
+ defaults={"n_estimators": 200}, default=False,
108
+ search_space={"n_estimators": [100, 200, 300], "max_depth": [None, 5, 10, 20],
109
+ "min_samples_split": [2, 5, 10], "min_samples_leaf": [1, 2, 4]}),
110
+ ModelSpec("hist_gradient_boosting", "Hist Gradient Boosting",
111
+ lambda ctx, **p: HistGradientBoostingClassifier(random_state=ctx.random_state, **p), default=False,
112
+ search_space={"learning_rate": [0.03, 0.1, 0.2], "max_iter": [100, 200, 300],
113
+ "max_depth": [None, 3, 6], "l2_regularization": [0.0, 0.1, 1.0]}),
114
+ ModelSpec("mlp", "Neural Network (MLP)",
115
+ lambda ctx, **p: MLPClassifier(random_state=ctx.random_state, **p),
116
+ defaults={"hidden_layer_sizes": (64,), "max_iter": 500}, default=False,
117
+ search_space={"hidden_layer_sizes": [(32,), (64,), (64, 32)], "alpha": [1e-5, 1e-4, 1e-3, 1e-2],
118
+ "learning_rate_init": [1e-3, 1e-2]}),
119
+ ModelSpec("lda", "Linear Discriminant Analysis", lambda ctx, **p: LinearDiscriminantAnalysis(**p), default=False,
120
+ search_space={"solver": ["svd", "lsqr", "eigen"]}),
121
+ ModelSpec("ridge_classifier", "Ridge Classifier",
122
+ lambda ctx, **p: RidgeClassifier(random_state=ctx.random_state, **_cw(ctx, p)), default=False,
123
+ search_space={"alpha": [0.01, 0.1, 1, 10, 100]}),
124
+ ModelSpec("linear_svm", "Linear SVM",
125
+ lambda ctx, **p: LinearSVC(random_state=ctx.random_state, **_cw(ctx, p)),
126
+ defaults={"max_iter": 5000, "dual": "auto"}, default=False,
127
+ search_space={"C": [0.01, 0.1, 1, 10, 100]}),
128
+
129
+ # ---- optional boosters: pip install xgboost / lightgbm / catboost -----------------------------
130
+ ModelSpec("xgboost", "XGBoost", _xgb_clf, requires="xgboost", default=False,
131
+ defaults={"n_estimators": 200, "learning_rate": 0.1},
132
+ search_space={"n_estimators": [100, 200, 400], "learning_rate": [0.03, 0.1, 0.2],
133
+ "max_depth": [3, 4, 6, 8], "subsample": [0.7, 0.85, 1.0],
134
+ "colsample_bytree": [0.7, 0.85, 1.0]}),
135
+ ModelSpec("lightgbm", "LightGBM", _lgbm_clf, requires="lightgbm", default=False,
136
+ defaults={"n_estimators": 200, "learning_rate": 0.1},
137
+ search_space={"n_estimators": [100, 200, 400], "learning_rate": [0.03, 0.1, 0.2],
138
+ "num_leaves": [15, 31, 63], "subsample": [0.7, 0.85, 1.0],
139
+ "colsample_bytree": [0.7, 0.85, 1.0]}),
140
+ ModelSpec("catboost", "CatBoost", _cat_clf, requires="catboost", default=False,
141
+ defaults={"iterations": 300, "learning_rate": 0.1},
142
+ search_space={"iterations": [200, 300, 500], "learning_rate": [0.03, 0.1, 0.2],
143
+ "depth": [4, 6, 8]}),
78
144
  ])
79
145
 
80
146
 
@@ -433,36 +499,24 @@ class MultiModelClassifier(_BaseMultiModel):
433
499
  self,
434
500
  models: Optional[List[Tuple]] = None,
435
501
  save_path: Optional[str] = None,
436
- show_plot: bool = True
502
+ show_plot: bool = True,
503
+ metric: str = 'F1 Score'
437
504
  ):
438
- """Plots comparison bar chart for Accuracy, Precision, Recall, and F1 Score."""
505
+ """Plots all models ranked on one metric (default 'F1 Score'; or 'Accuracy', 'Precision', 'Recall', 'ROC AUC')."""
439
506
  models = self._resolve_models(models)
440
507
  if not models:
441
508
  return
442
509
 
443
- model_names = [m[0] for m in models]
444
- accuracy = [m[3] for m in models]
445
- precision = [m[4] for m in models]
446
- recall = [m[5] for m in models]
447
- f1 = [m[6] for m in models]
448
-
449
- data = {
450
- 'Model': model_names * 4,
451
- 'Score': accuracy + precision + recall + f1,
452
- 'Metric': ['Accuracy']*len(models) + ['Precision']*len(models) + ['Recall']*len(models) + ['F1 Score']*len(models)
453
- }
454
- df_plot = pd.DataFrame(data)
510
+ columns = {'Accuracy': 3, 'Precision': 4, 'Recall': 5, 'F1 Score': 6, 'ROC AUC': 9}
511
+ if metric not in columns:
512
+ raise ValueError(f"metric must be one of {list(columns)}, got '{metric}'.")
455
513
 
456
514
  with _apply_plot_style():
457
- fig, ax = plt.subplots(figsize=(12, 7))
458
- sns.barplot(x='Model', y='Score', hue='Metric', data=df_plot, palette="viridis", ax=ax)
459
- ax.set_title("Comprehensive Model Comparison", fontsize=16, pad=20, fontweight='bold')
460
- ax.set_ylim(0, 1.1)
461
- ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
462
- ax.tick_params(axis='x', rotation=45)
463
- for container in ax.containers:
464
- ax.bar_label(container, fmt='%.2f', padding=3, fontsize=9)
465
- plt.tight_layout()
515
+ fig = comparison_figure(
516
+ [m[0] for m in models], [m[columns[metric]] for m in models], metric=metric,
517
+ title=f"Model comparison: {metric}",
518
+ footnote="Measured on the held-out test split.",
519
+ )
466
520
  self._save_and_show(fig, save_path=save_path, show_plot=show_plot)
467
521
 
468
522
  def get_summary(
@@ -4,17 +4,24 @@ from typing import Any, List, Optional, Tuple, Union
4
4
  import matplotlib.pyplot as plt
5
5
  import numpy as np
6
6
  import pandas as pd
7
- import seaborn as sns
8
7
  from sklearn.compose import TransformedTargetRegressor
9
- from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor, RandomForestRegressor
10
- from sklearn.linear_model import Lasso, LinearRegression, Ridge
8
+ from sklearn.ensemble import (
9
+ AdaBoostRegressor,
10
+ ExtraTreesRegressor,
11
+ GradientBoostingRegressor,
12
+ HistGradientBoostingRegressor,
13
+ RandomForestRegressor,
14
+ )
15
+ from sklearn.linear_model import BayesianRidge, ElasticNet, HuberRegressor, Lasso, LinearRegression, Ridge
11
16
  from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
12
17
  from sklearn.neighbors import KNeighborsRegressor
18
+ from sklearn.neural_network import MLPRegressor
13
19
  from sklearn.preprocessing import StandardScaler
14
- from sklearn.svm import SVR
20
+ from sklearn.svm import SVR, LinearSVR
15
21
  from sklearn.tree import DecisionTreeRegressor
16
22
 
17
23
  from ..core.base import _BaseMultiModel
24
+ from ..core.plotting import comparison_figure
18
25
  from ..core.registry import ModelRegistry, ModelSpec
19
26
  from ..core.utils import _LOWER_IS_BETTER, _apply_plot_style, _safe_print, save_report
20
27
 
@@ -24,9 +31,30 @@ def _knn_reg(ctx, n_neighbors=None, **kw):
24
31
  return KNeighborsRegressor(n_neighbors=n, n_jobs=ctx.n_jobs, **kw)
25
32
 
26
33
 
34
+ def _target_scaled(estimator):
35
+ """Wraps ``estimator`` so the target is standardised internally (needed by SVR, LinearSVR and MLP)."""
36
+ return TransformedTargetRegressor(regressor=estimator, transformer=StandardScaler())
37
+
38
+
27
39
  def _svr(ctx, **p):
28
40
  # SVR is sensitive to the target scale, so the target is standardised internally.
29
- return TransformedTargetRegressor(regressor=SVR(**p), transformer=StandardScaler())
41
+ return _target_scaled(SVR(**p))
42
+
43
+
44
+ def _xgb_reg(ctx, **p):
45
+ from xgboost import XGBRegressor
46
+ return XGBRegressor(random_state=ctx.random_state, n_jobs=ctx.n_jobs, verbosity=0, **p)
47
+
48
+
49
+ def _lgbm_reg(ctx, **p):
50
+ from lightgbm import LGBMRegressor
51
+ return LGBMRegressor(random_state=ctx.random_state, n_jobs=ctx.n_jobs, verbose=-1, **p)
52
+
53
+
54
+ def _cat_reg(ctx, **p):
55
+ from catboost import CatBoostRegressor
56
+ return CatBoostRegressor(random_seed=ctx.random_state, thread_count=ctx.n_jobs, verbose=0,
57
+ allow_writing_files=False, **p)
30
58
 
31
59
 
32
60
  REGRESSOR_REGISTRY = ModelRegistry([
@@ -61,6 +89,52 @@ REGRESSOR_REGISTRY = ModelRegistry([
61
89
  lambda ctx, **p: AdaBoostRegressor(random_state=ctx.random_state, **p),
62
90
  defaults={"n_estimators": 50},
63
91
  search_space={"n_estimators": [25, 50, 100, 200], "learning_rate": [0.01, 0.1, 0.5, 1.0]}),
92
+
93
+ # ---- extra models (Phase 5): run with include=[...] or include='all' ------------------------
94
+ ModelSpec("elastic_net", "Elastic Net",
95
+ lambda ctx, **p: ElasticNet(random_state=ctx.random_state, **p),
96
+ defaults={"alpha": 0.1, "l1_ratio": 0.5}, default=False,
97
+ search_space={"alpha": [0.001, 0.01, 0.1, 1, 10], "l1_ratio": [0.1, 0.3, 0.5, 0.7, 0.9]}),
98
+ ModelSpec("bayesian_ridge", "Bayesian Ridge", lambda ctx, **p: BayesianRidge(**p), default=False,
99
+ search_space={"alpha_1": [1e-7, 1e-6, 1e-5], "alpha_2": [1e-7, 1e-6, 1e-5],
100
+ "lambda_1": [1e-7, 1e-6, 1e-5], "lambda_2": [1e-7, 1e-6, 1e-5]}),
101
+ ModelSpec("huber", "Huber Regressor", lambda ctx, **p: HuberRegressor(**p),
102
+ defaults={"max_iter": 500}, default=False,
103
+ search_space={"epsilon": [1.1, 1.35, 1.5, 2.0], "alpha": [1e-5, 1e-4, 1e-3, 1e-2]}),
104
+ ModelSpec("linear_svr", "Linear SVR",
105
+ lambda ctx, **p: _target_scaled(LinearSVR(random_state=ctx.random_state, **p)),
106
+ defaults={"max_iter": 5000, "dual": "auto"}, param_prefix="regressor__", default=False,
107
+ search_space={"C": [0.1, 1, 10, 100], "epsilon": [0.0, 0.1, 0.5]}),
108
+ ModelSpec("extra_trees", "Extra Trees Regressor",
109
+ lambda ctx, **p: ExtraTreesRegressor(random_state=ctx.random_state, n_jobs=ctx.n_jobs, **p),
110
+ defaults={"n_estimators": 200}, default=False,
111
+ search_space={"n_estimators": [100, 200, 300], "max_depth": [None, 5, 10, 20],
112
+ "min_samples_split": [2, 5, 10], "min_samples_leaf": [1, 2, 4]}),
113
+ ModelSpec("hist_gradient_boosting", "Hist Gradient Boosting Regressor",
114
+ lambda ctx, **p: HistGradientBoostingRegressor(random_state=ctx.random_state, **p), default=False,
115
+ search_space={"learning_rate": [0.03, 0.1, 0.2], "max_iter": [100, 200, 300],
116
+ "max_depth": [None, 3, 6], "l2_regularization": [0.0, 0.1, 1.0]}),
117
+ ModelSpec("mlp", "Neural Network Regressor (MLP)",
118
+ lambda ctx, **p: _target_scaled(MLPRegressor(random_state=ctx.random_state, **p)),
119
+ defaults={"hidden_layer_sizes": (64,), "max_iter": 500}, param_prefix="regressor__", default=False,
120
+ search_space={"hidden_layer_sizes": [(32,), (64,), (64, 32)], "alpha": [1e-5, 1e-4, 1e-3, 1e-2],
121
+ "learning_rate_init": [1e-3, 1e-2]}),
122
+
123
+ # ---- optional boosters: pip install xgboost / lightgbm / catboost -----------------------------
124
+ ModelSpec("xgboost", "XGBoost Regressor", _xgb_reg, requires="xgboost", default=False,
125
+ defaults={"n_estimators": 200, "learning_rate": 0.1},
126
+ search_space={"n_estimators": [100, 200, 400], "learning_rate": [0.03, 0.1, 0.2],
127
+ "max_depth": [3, 4, 6, 8], "subsample": [0.7, 0.85, 1.0],
128
+ "colsample_bytree": [0.7, 0.85, 1.0]}),
129
+ ModelSpec("lightgbm", "LightGBM Regressor", _lgbm_reg, requires="lightgbm", default=False,
130
+ defaults={"n_estimators": 200, "learning_rate": 0.1},
131
+ search_space={"n_estimators": [100, 200, 400], "learning_rate": [0.03, 0.1, 0.2],
132
+ "num_leaves": [15, 31, 63], "subsample": [0.7, 0.85, 1.0],
133
+ "colsample_bytree": [0.7, 0.85, 1.0]}),
134
+ ModelSpec("catboost", "CatBoost Regressor", _cat_reg, requires="catboost", default=False,
135
+ defaults={"iterations": 300, "learning_rate": 0.1},
136
+ search_space={"iterations": [200, 300, 500], "learning_rate": [0.03, 0.1, 0.2],
137
+ "depth": [4, 6, 8]}),
64
138
  ])
65
139
 
66
140
 
@@ -248,28 +322,24 @@ class MultiModelRegressor(_BaseMultiModel):
248
322
  self,
249
323
  models: Optional[List[Tuple]] = None,
250
324
  save_path: Optional[str] = None,
251
- show_plot: bool = True
325
+ show_plot: bool = True,
326
+ metric: str = 'R2 Score'
252
327
  ):
253
- """Plots comparison bar charts for R2 score."""
328
+ """Plots all models ranked on one metric (default 'R2 Score'; or 'MAE', 'MSE', 'RMSE')."""
254
329
  models = self._resolve_models(models)
255
330
  if not models:
256
331
  return
257
332
 
258
- model_names = [m[0] for m in models]
259
- r2_scores = [m[4] for m in models]
333
+ columns = {'R2 Score': 4, 'MAE': 1, 'MSE': 2, 'RMSE': 3}
334
+ if metric not in columns:
335
+ raise ValueError(f"metric must be one of {list(columns)}, got '{metric}'.")
260
336
 
261
337
  with _apply_plot_style():
262
- fig, ax = plt.subplots(figsize=(10, 6))
263
- colors = sns.color_palette('viridis', len(model_names))
264
- ax.bar(model_names, r2_scores, color=colors)
265
- ax.set_title("Regressor R2 Score Comparison", pad=20, fontweight='bold')
266
- ax.set_ylabel("R2 Score")
267
- ax.tick_params(axis='x', rotation=45)
268
-
269
- for container in ax.containers:
270
- ax.bar_label(container, fmt='%.2f', padding=3, fontsize=10)
271
-
272
- plt.tight_layout()
338
+ fig = comparison_figure(
339
+ [m[0] for m in models], [m[columns[metric]] for m in models], metric=metric,
340
+ title=f"Model comparison: {metric}", lower_is_better=metric in _LOWER_IS_BETTER,
341
+ footnote="Measured on the held-out test split.",
342
+ )
273
343
  self._save_and_show(fig, save_path=save_path, show_plot=show_plot)
274
344
 
275
345
  def get_summary(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multimodel_analysis
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: A Python package for automatic multi-model analysis (classification, regression and clustering)
5
5
  Author-email: Uditya Narayan Tiwari <tiwarimerit@gmail.com>
6
6
  License: Apache-2.0
@@ -24,6 +24,7 @@ Requires-Dist: openpyxl>=3.0; extra == "excel"
24
24
  Provides-Extra: boost
25
25
  Requires-Dist: xgboost>=1.7; extra == "boost"
26
26
  Requires-Dist: lightgbm>=3.3; extra == "boost"
27
+ Requires-Dist: catboost>=1.2; extra == "boost"
27
28
  Provides-Extra: dev
28
29
  Requires-Dist: pytest>=7; extra == "dev"
29
30
  Requires-Dist: ruff>=0.4; extra == "dev"
@@ -15,6 +15,7 @@ multimodel_analysis.egg-info/top_level.txt
15
15
  multimodel_analysis/core/__init__.py
16
16
  multimodel_analysis/core/base.py
17
17
  multimodel_analysis/core/persistence.py
18
+ multimodel_analysis/core/plotting.py
18
19
  multimodel_analysis/core/preprocessing.py
19
20
  multimodel_analysis/core/registry.py
20
21
  multimodel_analysis/core/utils.py
@@ -7,6 +7,7 @@ scikit-learn>=1.3
7
7
  [boost]
8
8
  xgboost>=1.7
9
9
  lightgbm>=3.3
10
+ catboost>=1.2
10
11
 
11
12
  [dev]
12
13
  pytest>=7
@@ -27,7 +27,7 @@ dependencies = [
27
27
 
28
28
  [project.optional-dependencies]
29
29
  excel = ["openpyxl>=3.0"]
30
- boost = ["xgboost>=1.7", "lightgbm>=3.3"]
30
+ boost = ["xgboost>=1.7", "lightgbm>=3.3", "catboost>=1.2"]
31
31
  dev = ["pytest>=7", "ruff>=0.4", "openpyxl>=3.0"]
32
32
 
33
33
  [project.urls]