From 21944ddb20aa4bed006bd11eaaa87f6179a4df0a Mon Sep 17 00:00:00 2001 From: rbroc Date: Wed, 20 Mar 2024 16:12:08 +0100 Subject: [PATCH] use hasattr to check if model has been fitted --- turftopic/models/cluster.py | 3 +-- turftopic/models/gmm.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/turftopic/models/cluster.py b/turftopic/models/cluster.py index 4e1ab22..092c359 100644 --- a/turftopic/models/cluster.py +++ b/turftopic/models/cluster.py @@ -181,7 +181,6 @@ def __init__( self.feature_importance = feature_importance self.n_reduce_to = n_reduce_to self.reduction_method = reduction_method - self.components_ = None def _merge_agglomerative(self, n_reduce_to: int) -> np.ndarray: n_topics = self.components_.shape[0] @@ -328,7 +327,7 @@ def fit_transform_dynamic( if embeddings is None: embeddings = self.encoder_.encode(raw_documents) for i_timebin in np.arange(len(self.time_bin_edges) - 1): - if self.components_ is not None: + if hasattr(self, 'components_'): doc_topic_matrix = label_binarize( self.labels_, classes=self.classes_ ) diff --git a/turftopic/models/gmm.py b/turftopic/models/gmm.py index ef34403..854fa34 100644 --- a/turftopic/models/gmm.py +++ b/turftopic/models/gmm.py @@ -101,7 +101,6 @@ def __init__( self.gmm_ = make_pipeline(dimensionality_reduction, mixture) else: self.gmm_ = mixture - self.components_ = None def fit_transform( self, raw_documents, y=None, embeddings: Optional[np.ndarray] = None @@ -163,7 +162,7 @@ def fit_transform_dynamic( bins: Union[int, list[datetime]] = 10, ): time_labels, self.time_bin_edges = bin_timestamps(timestamps, bins) - if self.components_ is not None: + if hasattr(self, 'components_'): doc_topic_matrix = self.transform( raw_documents, embeddings=embeddings )