Base classes and registration decorators for machine learning algorithms.
Classifier, Clusterer, Regressor, Associator) are defined here and integrate with the component registry (tuiml.registry).Classes
Abstract base class for all machine learning algorithms.
__init__( self, )
Overview
Classifier and Regressor. It defines standard methods for metadata retrieval, parameter handling, and REST serving.Attributes
_is_fitted
Notes
fit and predict.See Also
Methods
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
Return JSON Schema for algorithm parameters.
Returns
schema
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'Algorithm'
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'Algorithm'
Train the algorithm on data.
Parameters
X
y
Returns
self
fit_predict
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> np.ndarray
fit_predict
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> np.ndarray
Fit and predict in one step (useful for clustering).
Parameters
X
y
Returns
predictions
score
(self, X: np.ndarray, y: np.ndarray) -> float
score
(self, X: np.ndarray, y: np.ndarray) -> float
Return the default score of the model on the given data.
Parameters
X
y
Returns
score
evaluate
(self, X: np.ndarray, y: np.ndarray, metrics: Union[str, List[str]]='auto') -> Dict[str, float]
evaluate
(self, X: np.ndarray, y: np.ndarray, metrics: Union[str, List[str]]='auto') -> Dict[str, float]
Evaluate the model on test data with one or more metrics.
Parameters
X
y
metrics
tuiml.evaluation.metrics (e.g., ["accuracy_score", "f1_score"]). "auto" selects accuracy/F1 for classifiers and MSE/R² for regressors.
Returns
results
partial_fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, classes: Optional[np.ndarray]=None) -> 'Algorithm'
partial_fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, classes: Optional[np.ndarray]=None) -> 'Algorithm'
Incrementally fit the model on a batch of samples.
Parameters
X
y
classes
classes_ before all classes have been seen.
Returns
self
get_params
(self, deep: bool=True) -> Dict[str, Any]
get_params
(self, deep: bool=True) -> Dict[str, Any]
Return the algorithm's constructor parameters.
Parameters
deep
Returns
params
set_params
(self, **params) -> 'Algorithm'
set_params
(self, **params) -> 'Algorithm'
Set algorithm parameters.
Parameters
**params
Returns
self
Raises
ValueError
serve
(self, port: int=8000, host: str='127.0.0.1', model_id: Optional[str]=None, background: Optional[bool]=None)
serve
(self, port: int=8000, host: str='127.0.0.1', model_id: Optional[str]=None, background: Optional[bool]=None)
Serve this fitted model behind a REST API.
Parameters
port
host
model_id
background
True inside a notebook, where blocking would freeze the cell, and False in a plain script, where the process should stay up to serve requests.
Returns
dict or None
server_id, url, endpoints) when running in the background; None when blocking.
Raises
RuntimeError
Base class for supervised classification algorithms.
score is accuracy.Attributes
classes_
fit.
Base class for unsupervised clustering algorithms.
__init__( self, )
Overview
Attributes
n_clusters_
labels_
cluster_centers_
See Also
Methods
Base class for density-based clusterers.
Methods
Base class for clusterers that support incremental updates.
Methods
partial_fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, classes: Optional[np.ndarray]=None) -> 'UpdateableClusterer'
partial_fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, classes: Optional[np.ndarray]=None) -> 'UpdateableClusterer'
Incrementally fit the model on a batch of samples.
Parameters
X
y
classes
Returns
self
Base class for supervised regression algorithms.
score is R². Timeseries models also use this base class.Base class for survival analysis models.
Survival models estimate the time to an event of interest — failure, churn, relapse — where some subjects are right-censored: the event had not happened when observation stopped. Input is therefore (X, time, event) rather than (X, y), with event marking which time values are observed event times and which are censoring times.
A model predicts a risk score, where a larger value means an earlier expected event, and optionally a survival function S(t) = P(T > t).
Methods
Base class for uplift / heterogeneous-treatment-effect models.
Uplift models estimate the causal effect of a treatment on an individual: the difference between the outcome with treatment and without it. Input is therefore (X, treatment, y) rather than (X, y), with treatment a binary indicator of which group each sample belonged to.
The headline output is the individual treatment effect, or uplift, which is what decides who to treat.
See Also
Methods
Represents a set of items that appear together frequently.
Parameters
items
support
count
Represents a discovered relationship in the form $A \Rightarrow C$.
Overview
Parameters
antecedent
consequent
support
confidence
lift
leverage
conviction
jaccard
kulczynski
all_confidence
Base class for Association Rule Mining.
__init__( self, )
Theory
- Finding all Frequent Itemsets that satisfy a minimum support.
- Generating Association Rules that satisfy a minimum confidence.
Attributes
frequent_itemsets_
rules_
Methods
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'Associator'
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'Associator'
Find frequent itemsets and generate association rules.
Parameters
X
y
Returns
self
get_frequent_itemsets
(self, min_size: int=1, max_size: Optional[int]=None) -> List[FrequentItemset]
get_frequent_itemsets
(self, min_size: int=1, max_size: Optional[int]=None) -> List[FrequentItemset]
Return the discovered frequent itemsets.
Parameters
min_size
max_size
None for no limit).
Returns
itemsets
get_rules
(self, min_confidence: Optional[float]=None, min_lift: Optional[float]=None) -> List[AssociationRule]
get_rules
(self, min_confidence: Optional[float]=None, min_lift: Optional[float]=None) -> List[AssociationRule]
Return the discovered association rules.
Parameters
min_confidence
min_lift
Returns
rules
Central registry for all algorithms (legacy wrapper).
Notes
tuiml.registry) for backward compatibility. New code should use the registry directly.Methods
Functions
Call a metric function, choosing macro averaging for multiclass data.
f1_score/precision_score/recall_score default to binary averaging, which on multiclass labels silently reports only the score of class 1. Whenever the metric accepts an average argument and the data has more than two classes, macro averaging is used instead.Parameters
metric_func
tuiml.evaluation.metrics.
y_true
y_pred
Returns
float or np.ndarray
Decorator to register an algorithm with the component registry.
Parameters
type
'classifier', 'clusterer', 'regressor', 'associator').
Returns
decorator
>>> from tuiml.base.algorithms import algorithm, Classifier
>>> @algorithm(type="classifier")
... class RandomForest(Classifier):
... pass
Decorator to register a classifier with the component registry.
Parameters
name
tags
version
author
Returns
decorator
>>> from tuiml.base.algorithms import classifier, Classifier
>>> @classifier(tags=["ensemble", "tree"])
... class RandomForest(Classifier):
... pass
Decorator to register a clusterer with the component registry.
Parameters
name
tags
version
author
Returns
decorator
>>> from tuiml.base.algorithms import clusterer, Clusterer
>>> @clusterer(tags=["partitioning"])
... class KMeans(Clusterer):
... pass
Decorator to register a regressor with the component registry.
Parameters
name
tags
version
author
Returns
decorator
>>> from tuiml.base.algorithms import regressor, Regressor
>>> @regressor(tags=["linear"])
... class LinearRegression(Regressor):
... pass
Decorator to register an anomaly detector with the component registry.
Classifier and predict a discrete inlier/outlier label, so they score like a classifier. They register under their own component type instead, so list_algorithms can report them as anomaly detectors rather than burying them among the classifiers.Parameters
name
tags
version
author
Returns
decorator
>>> from tuiml.base.algorithms import anomaly_detector, Classifier
>>> @anomaly_detector(tags=["anomaly-detection", "tree-based"])
... class MyForestDetector(Classifier):
... pass
Decorator to register an associator with the component registry.
Parameters
name
tags
version
author
Returns
decorator
>>> from tuiml.base.algorithms import associator, Associator
>>> @associator(tags=["itemset", "frequent"])
... class Apriori(Associator):
... pass
Decorator to register a survival model with the component registry.
Survival and estimate time-to-event with right-censored data.Parameters
name
tags
version
author
Returns
decorator
Decorator to register an uplift model with the component registry.
UpliftModel and estimate heterogeneous treatment effects.Parameters
name
tags
version
author
Returns
decorator
Get an algorithm class by name from the component registry.
Parameters
name
Returns
algorithm_class
List available algorithms.
Parameters
type
'classifier', 'clusterer', 'regressor', 'associator').
Returns
algorithms