Compare commits

..

3 Commits

Author SHA1 Message Date
Ricardo Montañana Gómez
601b5e04d3 Create codeql-analysis.yml 2021-02-12 00:45:28 +01:00
Ricardo Montañana Gómez
147dad684c 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
2021-01-19 11:40:46 +01:00
Ricardo Montañana Gómez
3bdac9bd60 Complete source comments (#22)
* 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

* Update Jupyter notebooks
2021-01-19 10:44:59 +01:00
5 changed files with 87 additions and 43 deletions

56
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@@ -0,0 +1,56 @@
name: "CodeQL"
on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '16 17 * * 3'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
steps:
- name: Checkout repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

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