diff --git a/code/bolsonaro/models/ensemble_selection_forest_regressor.py b/code/bolsonaro/models/ensemble_selection_forest_regressor.py index b82e131d296392963e31d85f5c1444fc5cb7fd09..956acdc93723bbb21962449d9c43361229500a27 100644 --- a/code/bolsonaro/models/ensemble_selection_forest_regressor.py +++ b/code/bolsonaro/models/ensemble_selection_forest_regressor.py @@ -52,7 +52,7 @@ class EnsembleSelectionForestRegressor(BaseEstimator, metaclass=ABCMeta): del class_list[candidate_index] def score(self, X, y): - predictions = self._predict_base_estimator(X) + predictions = self.predict_base_estimator(X) return self._score_metric(predictions, y) def predict_base_estimator(self, X): diff --git a/code/bolsonaro/models/omp_forest.py b/code/bolsonaro/models/omp_forest.py index 86359fa38e325726d49b1f48c84e3fded723282e..cd13312fdf81942496c6cdee10f25754fa3cc71d 100644 --- a/code/bolsonaro/models/omp_forest.py +++ b/code/bolsonaro/models/omp_forest.py @@ -5,6 +5,7 @@ from abc import abstractmethod, ABCMeta import numpy as np from sklearn.linear_model import OrthogonalMatchingPursuit from sklearn.base import BaseEstimator +import warnings class OmpForest(BaseEstimator, metaclass=ABCMeta): @@ -109,7 +110,17 @@ class SingleOmpForest(OmpForest): super().__init__(models_parameters, base_forest_estimator) def fit_omp(self, atoms, objective): - self._omp.fit(atoms, objective) + with warnings.catch_warnings(record=True) as caught_warnings: + # Cause all warnings to always be triggered. + warnings.simplefilter("always") + + self._omp.fit(atoms, objective) + + # ignore any non-custom warnings that may be in the list + caught_warnings = list(filter(lambda i: i.message != RuntimeWarning(omp_premature_warning), caught_warnings)) + + if len(caught_warnings) > 0: + logger.error(f'number of linear dependences in the dictionary: {len(caught_warnings)}. model parameters: {str(self._models_parameters.__dict__)}') def predict(self, X): """ diff --git a/code/bolsonaro/models/omp_forest_classifier.py b/code/bolsonaro/models/omp_forest_classifier.py index 30012c05f066cc9452e92f7976c143cb2dcf967f..fe6096db0579eb41da42302bed31278e31ea6e76 100644 --- a/code/bolsonaro/models/omp_forest_classifier.py +++ b/code/bolsonaro/models/omp_forest_classifier.py @@ -1,9 +1,10 @@ from bolsonaro.models.omp_forest import OmpForest, SingleOmpForest -from bolsonaro.utils import binarize_class_data +from bolsonaro.utils import binarize_class_data, omp_premature_warning import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import OrthogonalMatchingPursuit +import warnings class OmpForestBinaryClassifier(SingleOmpForest): @@ -92,7 +93,19 @@ class OmpForestMulticlassClassifier(OmpForest): omp_class = OrthogonalMatchingPursuit( n_nonzero_coefs=self.models_parameters.extracted_forest_size, fit_intercept=True, normalize=False) - omp_class.fit(atoms_binary, objective_binary) + + with warnings.catch_warnings(record=True) as caught_warnings: + # Cause all warnings to always be triggered. + warnings.simplefilter("always") + + omp_class.fit(atoms_binary, objective_binary) + + # ignore any non-custom warnings that may be in the list + caught_warnings = list(filter(lambda i: i.message != RuntimeWarning(omp_premature_warning), caught_warnings)) + + if len(caught_warnings) > 0: + logger.error(f'number of linear dependences in the dictionary: {len(caught_warnings)}. model parameters: {str(self._models_parameters.__dict__)}') + self._dct_class_omp[class_label] = omp_class return self._dct_class_omp diff --git a/code/bolsonaro/utils.py b/code/bolsonaro/utils.py index ee316bcf0a9049288e93841c362fa0cc55b542c5..c7d3f52e0635cec81108aa54da1f6c976055be65 100644 --- a/code/bolsonaro/utils.py +++ b/code/bolsonaro/utils.py @@ -124,3 +124,7 @@ def is_float(value): return True except ValueError: return False + +omp_premature_warning = """ Orthogonal matching pursuit ended prematurely due to linear + dependence in the dictionary. The requested precision might not have been met. + """ diff --git a/results/boston/stage5/losses_base-random-omp-omp_without_weights-kmeans.png b/results/boston/stage5/losses_base-random-omp-omp_without_weights-kmeans.png new file mode 100644 index 0000000000000000000000000000000000000000..a8ccaabdc605e1879f6332729f68e492b6713c46 Binary files /dev/null and b/results/boston/stage5/losses_base-random-omp-omp_without_weights-kmeans.png differ diff --git a/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-kmeans.png b/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-kmeans.png new file mode 100644 index 0000000000000000000000000000000000000000..3dff5daba5be1b56f679e7a107e0fd9e6184a7fb Binary files /dev/null and b/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-kmeans.png differ diff --git a/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-similarity.png b/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-similarity.png new file mode 100644 index 0000000000000000000000000000000000000000..033c01a5504b7e49effbcfab21cd72c0a3c48f7a Binary files /dev/null and b/results/diabetes/stage5/losses_base-random-omp-omp_without_weights-similarity.png differ diff --git a/results/diamonds/stage5/losses_base-random-omp-omp_without_weights-kmeans.png b/results/diamonds/stage5/losses_base-random-omp-omp_without_weights-kmeans.png new file mode 100644 index 0000000000000000000000000000000000000000..9ed302f32dd379f36c8b692bcaa9c22e6b29f0ce Binary files /dev/null and b/results/diamonds/stage5/losses_base-random-omp-omp_without_weights-kmeans.png differ