From 11b473d5605d6e8c9a2f9682d23b54ac59f9eced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Tue, 19 Jan 2021 11:13:23 +0100 Subject: [PATCH] Fix problem with zero weighted samples Solve WARNING: class label x specified in weight is not found with a different approach --- requirements.txt | 2 +- setup.py | 4 +-- stree/Strees.py | 14 +++++----- stree/tests/Stree_test.py | 54 ++++++++++++++++----------------------- 4 files changed, 31 insertions(+), 43 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2359d77..dbfe578 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ numpy -scikit-learn==0.23.2 +scikit-learn pandas ipympl \ No newline at end of file diff --git a/setup.py b/setup.py index adf861d..588f7a3 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ import setuptools -__version__ = "0.9rc6" +__version__ = "1.0rc1" __author__ = "Ricardo Montañana Gómez" @@ -30,7 +30,7 @@ setuptools.setup( "Topic :: Scientific/Engineering :: Artificial Intelligence", "Intended Audience :: Science/Research", ], - install_requires=["scikit-learn==0.23.2", "numpy", "ipympl"], + install_requires=["scikit-learn", "numpy", "ipympl"], test_suite="stree.tests", zip_safe=False, ) diff --git a/stree/Strees.py b/stree/Strees.py index 9bdd511..ab35b4b 100644 --- a/stree/Strees.py +++ b/stree/Strees.py @@ -629,6 +629,12 @@ class Stree(BaseEstimator, ClassifierMixin): """ if depth > self.__max_depth: return None + # Mask samples with 0 weight + if any(sample_weight == 0): + indices_zero = sample_weight == 0 + X = X[~indices_zero, :] + y = y[~indices_zero] + sample_weight = sample_weight[~indices_zero] if np.unique(y).shape[0] == 1: # only 1 class => pure dataset return Snode( @@ -643,14 +649,6 @@ class Stree(BaseEstimator, ClassifierMixin): # Train the model clf = self._build_clf() Xs, features = self.splitter_.get_subspace(X, y, self.max_features_) - # solve WARNING: class label 0 specified in weight is not found - # in bagging - if any(sample_weight == 0): - indices = sample_weight == 0 - y_next = y[~indices] - # touch weights if removing any class - if np.unique(y_next).shape[0] != self.n_classes_: - sample_weight += 1e-5 clf.fit(Xs, y, sample_weight=sample_weight) impurity = self.splitter_.partition_impurity(y) node = Snode(clf, X, y, features, impurity, title, sample_weight) diff --git a/stree/tests/Stree_test.py b/stree/tests/Stree_test.py index 4cd6b59..65afeef 100644 --- a/stree/tests/Stree_test.py +++ b/stree/tests/Stree_test.py @@ -413,39 +413,29 @@ class Stree_test(unittest.TestCase): with self.assertRaises(ValueError): Stree().fit(X, y, np.zeros(len(y))) - def test_weights_removing_class(self): - # This patch solves an stderr message from sklearn svm lib - # "WARNING: class label x specified in weight is not found" + def test_mask_samples_weighted_zero(self): X = np.array( [ - [0.1, 0.1], - [0.1, 0.2], - [0.2, 0.1], - [5, 6], - [8, 9], - [6, 7], - [0.2, 0.2], + [1, 1], + [1, 1], + [1, 1], + [2, 2], + [2, 2], + [2, 2], + [3, 3], + [3, 3], + [3, 3], ] ) - y = np.array([0, 0, 0, 1, 1, 1, 0]) - epsilon = 1e-5 - weights = [1, 1, 1, 0, 0, 0, 1] - weights = np.array(weights, dtype="float64") - weights_epsilon = [x + epsilon for x in weights] - weights_no_zero = np.array([1, 1, 1, 0, 0, 2, 1]) - original = weights_no_zero.copy() - clf = Stree() - clf.fit(X, y) - node = clf.train( - X, - y, - weights, - 1, - "test", - ) - # if a class is lost with zero weights the patch adds epsilon - self.assertListEqual(weights.tolist(), weights_epsilon) - self.assertListEqual(node._sample_weight.tolist(), weights_epsilon) - # zero weights are ok when they don't erase a class - _ = clf.train(X, y, weights_no_zero, 1, "test") - self.assertListEqual(weights_no_zero.tolist(), original.tolist()) + y = np.array([1, 1, 1, 2, 2, 2, 5, 5, 5]) + yw = np.array([1, 1, 1, 5, 5, 5, 5, 5, 5]) + w = [1, 1, 1, 0, 0, 0, 1, 1, 1] + model1 = Stree().fit(X, y) + model2 = Stree().fit(X, y, w) + predict1 = model1.predict(X) + predict2 = model2.predict(X) + self.assertListEqual(y.tolist(), predict1.tolist()) + self.assertListEqual(yw.tolist(), predict2.tolist()) + self.assertEqual(model1.score(X, y), 1) + self.assertAlmostEqual(model2.score(X, y), 0.66666667) + self.assertEqual(model2.score(X, y, w), 1)