SpectralClustering
Spectral clustering model for the Models module.
Spectral clustering applies eigenvalue decomposition to an affinity matrix
derived from the input data. It can detect clusters of arbitrary shape by
leveraging graph-theoretic connectivity. Because it does not expose a
predict method, cluster labels for new samples cannot be inferred
without re-fitting.
Key hyperparameters include n_clusters (partitions of the spectral
embedding), affinity (graph construction method), gamma (RBF kernel
width), and random_state (seed). The implementation wraps scikit-learn's
SpectralClustering estimator.
References
- [1] Ng, A. Y., Jordan, M. I., & Weiss, Y. (2002). "On spectral clustering: Analysis and an algorithm."
- [2] https://scikit-learn.org/stable/modules/generated/ sklearn.cluster.SpectralClustering.html
Parameters
- n_clusters : integer, default=
8 - Number of clusters to form.
- affinity : string, default=
rbf - Similarity measure used to construct the affinity graph. 'rbf' uses a Gaussian kernel controlled by 'gamma'.
- gamma : number, default=
1.0 - Kernel coefficient for the 'rbf' affinity. Ignored for other affinities.
- random_state : integer, default=
0 - Random seed for eigenvector decomposition and k-means initialisation.
Methods
calculate_metrics(self, split: DashAI.back.core.enums.metrics.SplitEnum = <SplitEnum.VALIDATION: 'validation'>, level: DashAI.back.core.enums.metrics.LevelEnum = <LevelEnum.LAST: 'last'>, log_index: int = None, x_data: 'DashAIDataset' = None, y_data: 'DashAIDataset' = None, fold_index: int = None, inner_fold_index: int = None)
BaseModelCalculate metrics for a data split and save them to the database.
Parameters
- split : SplitEnum
- The data split to evaluate (TRAIN, VALIDATION, or TEST). Defaults to SplitEnum.VALIDATION.
- level : LevelEnum
- The metric granularity level (LAST, TRIAL, STEP, or BATCH). Defaults to LevelEnum.LAST.
- log_index : int, optional
- Explicit step index for the metric entry. If None, the next step index is computed automatically. Defaults to None.
- x_data : DashAIDataset, optional
- Input features. If None, the dataset stored in the model for the given split is used. Defaults to None.
- y_data : DashAIDataset, optional
- Target labels. If None, the labels stored in the model for the given split are used. Defaults to None.
Returns
- Dict[str, float] or None
- What was written, so a caller that also wants the numbers does not have to score the same split twice.
Nonewhen nothing was written: no run to write against, or nothing to score.
compute_metrics(self, split: DashAI.back.core.enums.metrics.SplitEnum = <SplitEnum.VALIDATION: 'validation'>, x_data: 'DashAIDataset' = None, y_data: 'DashAIDataset' = None) -> Optional[Dict[str, float]]
BaseModelScore a data split with this model's metrics, without persisting.
Parameters
- split : SplitEnum
- The data split to evaluate. Defaults to
SplitEnum.VALIDATION. - x_data : DashAIDataset, optional
- Input features. Defaults to the split stored on the model.
- y_data : DashAIDataset, optional
- Target labels. Defaults to the split stored on the model.
Returns
- Dict[str, float] or None
- Metric name to score. A metric that scored a non-finite value is absent from the mapping rather than present with a NaN, so the mapping is empty when every metric returned one.
Nonewhen there is nothing to score at all: no metrics configured, or no data for this split. Callers that need a particular metric must therefore check that it is there.
get_cluster_labels(self, x: 'DashAIDataset | None' = None) -> 'ndarray'
SklearnLikeClustererReturn fitted labels or assign labels to new samples when supported.
Parameters
- x : DashAIDataset, optional
- Samples to assign to clusters. If omitted, or if the wrapped estimator does not support
predict, the labels discovered during fitting are returned.
Returns
- numpy.ndarray
- Cluster label assigned to each sample.
get_credential(self, name: str)
ConfigObjectResolve a registered credential component by name.
Parameters
- name : str
- Credential component class name (e.g. "HuggingFaceCredential").
Returns
- BaseCredential
- An instance of the requested credential component.
get_fit_attributes(self) -> Dict[str, Any]
ClusteringModelReturn algorithm-specific post-fit attributes for the converter report.
Returns
- Dict[str, Any]
- JSON-serializable dict of algorithm-specific attributes.
get_metadata(cls) -> Dict[str, Any]
BaseModelGet metadata values for the current model.
Returns
- Dict[str, Any]
- Dictionary containing UI metadata such as the model icon used in the DashAI frontend.
get_registry(cls) -> Dict[str, Type[ForwardRef('ClusteringModel')]]
ClusteringModelReturn concrete clustering algorithm classes keyed by class name.
get_schema(cls) -> dict
ConfigObjectGenerates the component related Json Schema.
Returns
- dict
- Dictionary representing the Json Schema of the component.
load(filename: str) -> Any
SklearnLikeClustererDeserialise a model from disk using joblib.
Parameters
- filename : str
- Path to the file previously written by :meth:
save.
Returns
- Any
- The loaded model instance.
predict_prepared(self, features: Any) -> Any
BaseModelPredict from data that is already in this model's feature space.
Parameters
- features : pandas.DataFrame or numpy.ndarray
- Feature matrix as returned by
prepare_dataset(..., is_fit=False). No further preparation is applied to it.
Returns
- Any
- The same kind of output as
predict: predicted values for regressors, class probabilities for DashAI classifiers.
predict_proba_prepared(self, features: Any) -> Any
BaseModelReturn class probabilities for data already in the feature space.
Parameters
- features : pandas.DataFrame or numpy.ndarray
- Feature matrix as returned by
prepare_dataset(..., is_fit=False).
Returns
- numpy.ndarray
- Array of shape
(n_samples, n_classes)with class probabilities.
prepare_dataset(self, dataset: 'DashAIDataset', is_fit: bool = False) -> 'DashAIDataset'
BaseModelHook for model specific preprocessing of input features.
Parameters
- dataset : DashAIDataset
- The input dataset to preprocess.
- is_fit : bool
- Whether the call is part of a fitting phase. Defaults to False.
Returns
- DashAIDataset
- The preprocessed dataset ready to be fed into the model.
prepare_output(self, dataset: 'DashAIDataset', is_fit: bool = False) -> 'DashAIDataset'
BaseModelHook for model-specific preprocessing of output targets.
Parameters
- dataset : DashAIDataset
- The output dataset (target labels) to preprocess.
- is_fit : bool
- Whether the call is part of a fitting phase. Defaults to False.
Returns
- DashAIDataset
- The preprocessed output dataset.
save(self, filename: str) -> None
SklearnLikeClustererSerialise the model to disk using joblib.
Parameters
- filename : str
- Destination file path where the model will be written.
train(self, x_train: 'DashAIDataset') -> 'SklearnLikeClusterer'
SklearnLikeClustererFit the clustering model using input features only.
Parameters
- x_train : DashAIDataset
- Dataset containing the input columns selected for clustering.
Returns
- SklearnLikeClusterer
- The fitted clustering adapter.
validate_and_transform(self, raw_data: dict) -> dict
ConfigObjectIt takes the data given by the user to initialize the model and returns it with all the objects that the model needs to work.
Parameters
- raw_data : dict
- A dictionary with the data provided by the user to initialize the model.
Returns
- dict
- A validated dictionary with the necessary objects.