From 5fcfa369f5a0dfb7d0f8c48e1729cf9056f85a0d Mon Sep 17 00:00:00 2001 From: Alinson S Xavier Date: Wed, 15 Apr 2020 14:37:17 -0500 Subject: [PATCH] Make PrimalSolutionComponent accept n_jobs argument --- src/python/miplearn/components/primal.py | 94 ++++++++++--------- .../miplearn/components/tests/test_primal.py | 8 ++ 2 files changed, 60 insertions(+), 42 deletions(-) diff --git a/src/python/miplearn/components/primal.py b/src/python/miplearn/components/primal.py index d9f2b6f..1ce3a7f 100644 --- a/src/python/miplearn/components/primal.py +++ b/src/python/miplearn/components/primal.py @@ -7,6 +7,7 @@ from copy import deepcopy from miplearn.classifiers.adaptive import AdaptiveClassifier from miplearn.components import classifier_evaluation_dict from sklearn.metrics import roc_curve +from p_tqdm import p_map from .component import Component from ..extractors import * @@ -45,54 +46,63 @@ class PrimalSolutionComponent(Component): def after_solve(self, solver, instance, model, results): pass - def fit(self, training_instances): + def fit(self, training_instances, n_jobs=1): logger.debug("Extracting features...") features = VariableFeaturesExtractor().extract(training_instances) solutions = SolutionExtractor().extract(training_instances) - for category in tqdm(features.keys(), desc="Fit (primal)"): + def _fit(args): + category, label = args[0], args[1] x_train = features[category] y_train = solutions[category] - for label in [0, 1]: - y = y_train[:, label].astype(int) - - logger.debug("Fitting predictors[%s, %s]:" % (category, label)) - if isinstance(self.classifier_prototype, list): - pred = deepcopy(self.classifier_prototype[label]) - else: - pred = deepcopy(self.classifier_prototype) - pred.fit(x_train, y) - self.classifiers[category, label] = pred - - # If y is either always one or always zero, set fixed threshold - y_avg = np.average(y) - if (not self.dynamic_thresholds) or y_avg <= 0.001 or y_avg >= 0.999: - self.thresholds[category, label] = self.min_threshold[label] - logger.debug(" Setting threshold to %.4f" % self.min_threshold[label]) - continue - - proba = pred.predict_proba(x_train) - assert isinstance(proba, np.ndarray), \ - "classifier should return numpy array" - assert proba.shape == (x_train.shape[0], 2), \ - "classifier should return (%d,%d)-shaped array, not %s" % ( - x_train.shape[0], 2, str(proba.shape)) - - # Calculate threshold dynamically using ROC curve - y_scores = proba[:, 1] - fpr, tpr, thresholds = roc_curve(y, y_scores) - k = 0 - while True: - if (k + 1) > len(fpr): - break - if fpr[k + 1] > self.max_fpr[label]: - break - if thresholds[k + 1] < self.min_threshold[label]: - break - k = k + 1 - logger.debug(" Setting threshold to %.4f (fpr=%.4f, tpr=%.4f)" % - (thresholds[k], fpr[k], tpr[k])) - self.thresholds[category, label] = thresholds[k] + y = y_train[:, label].astype(int) + + if isinstance(self.classifier_prototype, list): + clf = deepcopy(self.classifier_prototype[label]) + else: + clf = deepcopy(self.classifier_prototype) + clf.fit(x_train, y) + + y_avg = np.average(y) + if (not self.dynamic_thresholds) or y_avg <= 0.001 or y_avg >= 0.999: + return {"classifier": clf, + "threshold": self.min_threshold[label]} + + proba = clf.predict_proba(x_train) + assert isinstance(proba, np.ndarray), \ + "classifier should return numpy array" + assert proba.shape == (x_train.shape[0], 2), \ + "classifier should return (%d,%d)-shaped array, not %s" % ( + x_train.shape[0], 2, str(proba.shape)) + + y_scores = proba[:, 1] + fpr, tpr, thresholds = roc_curve(y, y_scores) + k = 0 + while True: + if (k + 1) > len(fpr): + break + if fpr[k + 1] > self.max_fpr[label]: + break + if thresholds[k + 1] < self.min_threshold[label]: + break + k = k + 1 + self.thresholds[category, label] = thresholds[k] + + return {"classifier": clf, + "threshold": thresholds[k]} + + items = [(category, label) + for category in features.keys() + for label in [0, 1]] + + if n_jobs == 1: + results = list(map(_fit, tqdm(items, desc="Fit (primal)"))) + else: + results = p_map(_fit, items, num_cpus=n_jobs) + + for (idx, (category, label)) in enumerate(items): + self.thresholds[category, label] = results[idx]["threshold"] + self.classifiers[category, label] = results[idx]["classifier"] def predict(self, instance): x_test = VariableFeaturesExtractor().extract([instance]) diff --git a/src/python/miplearn/components/tests/test_primal.py b/src/python/miplearn/components/tests/test_primal.py index c926494..58a1eaf 100644 --- a/src/python/miplearn/components/tests/test_primal.py +++ b/src/python/miplearn/components/tests/test_primal.py @@ -90,3 +90,11 @@ def test_evaluate(): 'True negative (%)': 50.0, 'True positive': 1, 'True positive (%)': 25.0}}} + + +def test_primal_parallel_fit(): + instances, models = get_training_instances_and_models() + comp = PrimalSolutionComponent() + comp.fit(instances, n_jobs=2) + assert len(comp.classifiers) == 2 + assert len(comp.thresholds) == 2