diff --git a/code/bolsonaro/models/model_factory.py b/code/bolsonaro/models/model_factory.py index 2dc578cfaacc99f9fea17b9ae8e64cc08e3038dc..a93e6090e253dc9bdb3aacfc53e1c99a1f9ef120 100644 --- a/code/bolsonaro/models/model_factory.py +++ b/code/bolsonaro/models/model_factory.py @@ -27,6 +27,6 @@ class ModelFactory(object): model_parameters = ModelParameters.load(directory_path, experiment_id) model = ModelFactory.build(task, model_parameters) # todo faire ce qu'il faut ici pour rétablir correctement le modèle - # model.set_forest(model_raw_results.forest) - # model.set_weights(model_raw_results.weights) + model.set_forest(model_raw_results.model_object.forest) + model.set_weights(model_raw_results.model_object.weights) return model diff --git a/code/bolsonaro/models/omp_forest.py b/code/bolsonaro/models/omp_forest.py index 0c33f09dd07142cfc9f94cee500be3ed8c795fba..2da0beab64ef5361efbde6d6197f957fe627886c 100644 --- a/code/bolsonaro/models/omp_forest.py +++ b/code/bolsonaro/models/omp_forest.py @@ -1,10 +1,9 @@ -from abc import abstractmethod, ABCMeta +from bolsonaro import LOG_PATH +from bolsonaro.error_handling.logger_factory import LoggerFactory +from abc import abstractmethod, ABCMeta import numpy as np from sklearn.linear_model import OrthogonalMatchingPursuit - -from bolsonaro import LOG_PATH -from bolsonaro.error_handling.logger_factory import LoggerFactory from sklearn.base import BaseEstimator @@ -21,7 +20,6 @@ class OmpForest(BaseEstimator, metaclass=ABCMeta): def score_base_estimator(self, X, y): return self._base_forest_estimator.score(X, y) - def _base_estimator_predictions(self, X): return np.array([tree.predict(X) for tree in self._base_forest_estimator.estimators_]).T @@ -120,4 +118,4 @@ class SingleOmpForest(OmpForest): if self._models_parameters.normalize_D: forest_predictions /= self._forest_norms - return self._make_omp_weighted_prediction(forest_predictions, self._omp, self._models_parameters.normalize_weights) \ No newline at end of file + return self._make_omp_weighted_prediction(forest_predictions, self._omp, self._models_parameters.normalize_weights) diff --git a/code/bolsonaro/models/omp_forest_classifier.py b/code/bolsonaro/models/omp_forest_classifier.py index c0526fbad4da9255b99c88a7c2e1239047c08587..128347aa61caf79dc908397ef0588d646d8b0dee 100644 --- a/code/bolsonaro/models/omp_forest_classifier.py +++ b/code/bolsonaro/models/omp_forest_classifier.py @@ -1,29 +1,22 @@ -from collections import namedtuple -from copy import deepcopy +from bolsonaro.models.omp_forest import OmpForest, SingleOmpForest +from bolsonaro.utils import binarize_class_data -from sklearn.base import BaseEstimator +import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import OrthogonalMatchingPursuit -from bolsonaro import LOG_PATH -from bolsonaro.error_handling.logger_factory import LoggerFactory -from bolsonaro.models.omp_forest import OmpForest, SingleOmpForest -import numpy as np - -from bolsonaro.utils import binarize_class_data - class OmpForestBinaryClassifier(SingleOmpForest): DEFAULT_SCORE_METRIC = 'indicator' def __init__(self, models_parameters): - estimator = RandomForestClassifier(n_estimators=models_parameters.forest_size, + estimator = RandomForestClassifier(**models_parameters.hyperparameters, random_state=models_parameters.seed, n_jobs=-1) super().__init__(models_parameters, estimator) def _check_classes(self, y): - assert len(set(y).difference({-1, 1})) == 0, "Classes for binary classifier should be {-1, +1}" + assert len(set(y).difference({-1, 1})) == 0, "Classes for binary classifier must be {-1, +1}" def fit(self, X_forest, y_forest, X_omp, y_omp): self._check_classes(y_forest) @@ -31,7 +24,6 @@ class OmpForestBinaryClassifier(SingleOmpForest): return super().fit(X_forest, y_forest, X_omp, y_omp) - def score(self, X, y, metric=DEFAULT_SCORE_METRIC): """ Evaluate OMPForestClassifer on (`X`, `y`) using `metric` @@ -56,7 +48,7 @@ class OmpForestMulticlassClassifier(OmpForest): DEFAULT_SCORE_METRIC = 'indicator' def __init__(self, models_parameters): - estimator = RandomForestClassifier(n_estimators=models_parameters.forest_size, + estimator = RandomForestClassifier(**models_parameters.hyperparameters, random_state=models_parameters.seed, n_jobs=-1) super().__init__(models_parameters, estimator) # question: peut-être initialiser les omps dans le __init__? comme pour le SingleOmpForest @@ -95,7 +87,6 @@ class OmpForestMulticlassClassifier(OmpForest): max_preds = np.argmax(preds, axis=1) return np.array(label_names)[max_preds] - def score(self, X, y, metric=DEFAULT_SCORE_METRIC): predictions = self.predict(X) @@ -107,8 +98,6 @@ class OmpForestMulticlassClassifier(OmpForest): return evaluation - - if __name__ == "__main__": forest = RandomForestClassifier(n_estimators=10) X = np.random.rand(10, 5) diff --git a/code/bolsonaro/models/omp_forest_regressor.py b/code/bolsonaro/models/omp_forest_regressor.py index 9e95453df26e9dc5a688b2dd5217276361b5e96d..a0c8b4708d52336bf39544ffd0b66c527466620a 100644 --- a/code/bolsonaro/models/omp_forest_regressor.py +++ b/code/bolsonaro/models/omp_forest_regressor.py @@ -1,10 +1,8 @@ - +from bolsonaro.models.omp_forest import SingleOmpForest from sklearn.ensemble import RandomForestRegressor import numpy as np -from bolsonaro.models.omp_forest import SingleOmpForest - class OmpForestRegressor(SingleOmpForest):