Weight0samples error (#23)

* Add Hyperparameters description to README
Comment get_subspace method
Add environment info for binder (runtime.txt)

* Complete source comments
Change docstring type to numpy
update hyperameters table and explanation

* Fix problem with zero weighted samples
Solve WARNING: class label x specified in weight is not found
with a different approach

* Allow update of scikitlearn to latest version
This commit is contained in:
Ricardo Montañana Gómez
2021-01-19 11:40:46 +01:00
committed by GitHub
parent 3bdac9bd60
commit 147dad684c
4 changed files with 31 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
numpy
scikit-learn==0.23.2
scikit-learn
pandas
ipympl

View File

@@ -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,
)

View File

@@ -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)

View File

@@ -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)