Compare commits

...

4 Commits

Author SHA1 Message Date
11b473d560 Fix problem with zero weighted samples
Solve WARNING: class label x specified in weight is not found
with a different approach
2021-01-19 11:13:23 +01:00
adb0b9f398 Merge branch 'complete-source-comments' of github.com:Doctorado-ML/STree into Weight0samplesError 2021-01-19 10:48:13 +01:00
0340584c52 Complete source comments
Change docstring type to numpy
update hyperameters table and explanation
2021-01-18 14:07:43 +01:00
9b3c7ccdfa Add Hyperparameters description to README
Comment get_subspace method
Add environment info for binder (runtime.txt)
2021-01-13 11:39:47 +01:00
4 changed files with 31 additions and 43 deletions

View File

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

View File

@@ -1,6 +1,6 @@
import setuptools import setuptools
__version__ = "0.9rc6" __version__ = "1.0rc1"
__author__ = "Ricardo Montañana Gómez" __author__ = "Ricardo Montañana Gómez"
@@ -30,7 +30,7 @@ setuptools.setup(
"Topic :: Scientific/Engineering :: Artificial Intelligence", "Topic :: Scientific/Engineering :: Artificial Intelligence",
"Intended Audience :: Science/Research", "Intended Audience :: Science/Research",
], ],
install_requires=["scikit-learn==0.23.2", "numpy", "ipympl"], install_requires=["scikit-learn", "numpy", "ipympl"],
test_suite="stree.tests", test_suite="stree.tests",
zip_safe=False, zip_safe=False,
) )

View File

@@ -629,6 +629,12 @@ class Stree(BaseEstimator, ClassifierMixin):
""" """
if depth > self.__max_depth: if depth > self.__max_depth:
return None 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: if np.unique(y).shape[0] == 1:
# only 1 class => pure dataset # only 1 class => pure dataset
return Snode( return Snode(
@@ -643,14 +649,6 @@ class Stree(BaseEstimator, ClassifierMixin):
# Train the model # Train the model
clf = self._build_clf() clf = self._build_clf()
Xs, features = self.splitter_.get_subspace(X, y, self.max_features_) 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) clf.fit(Xs, y, sample_weight=sample_weight)
impurity = self.splitter_.partition_impurity(y) impurity = self.splitter_.partition_impurity(y)
node = Snode(clf, X, y, features, impurity, title, sample_weight) 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): with self.assertRaises(ValueError):
Stree().fit(X, y, np.zeros(len(y))) Stree().fit(X, y, np.zeros(len(y)))
def test_weights_removing_class(self): def test_mask_samples_weighted_zero(self):
# This patch solves an stderr message from sklearn svm lib
# "WARNING: class label x specified in weight is not found"
X = np.array( X = np.array(
[ [
[0.1, 0.1], [1, 1],
[0.1, 0.2], [1, 1],
[0.2, 0.1], [1, 1],
[5, 6], [2, 2],
[8, 9], [2, 2],
[6, 7], [2, 2],
[0.2, 0.2], [3, 3],
[3, 3],
[3, 3],
] ]
) )
y = np.array([0, 0, 0, 1, 1, 1, 0]) y = np.array([1, 1, 1, 2, 2, 2, 5, 5, 5])
epsilon = 1e-5 yw = np.array([1, 1, 1, 5, 5, 5, 5, 5, 5])
weights = [1, 1, 1, 0, 0, 0, 1] w = [1, 1, 1, 0, 0, 0, 1, 1, 1]
weights = np.array(weights, dtype="float64") model1 = Stree().fit(X, y)
weights_epsilon = [x + epsilon for x in weights] model2 = Stree().fit(X, y, w)
weights_no_zero = np.array([1, 1, 1, 0, 0, 2, 1]) predict1 = model1.predict(X)
original = weights_no_zero.copy() predict2 = model2.predict(X)
clf = Stree() self.assertListEqual(y.tolist(), predict1.tolist())
clf.fit(X, y) self.assertListEqual(yw.tolist(), predict2.tolist())
node = clf.train( self.assertEqual(model1.score(X, y), 1)
X, self.assertAlmostEqual(model2.score(X, y), 0.66666667)
y, self.assertEqual(model2.score(X, y, w), 1)
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())