mirror of
https://github.com/Doctorado-ML/STree.git
synced 2025-08-16 16:06:01 +00:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
cb80e8606b
|
|||
c93d3fbcc7
|
|||
f4ca4bbd5b
|
|||
e676ddbfcc
|
|||
|
dc637018e8 | ||
517013be09
|
|||
941c2ff5e0
|
|||
2ebf48145d
|
|||
7fbfd3622e
|
|||
bc839a80d6
|
|||
ba15ea2cc0
|
|||
85b56785c8
|
|||
b627bb7531
|
|||
5f8ca8f3bb
|
|||
|
fb8b9b344f | ||
036d1ba2a7
|
|||
4de74973b8
|
|||
|
28dd04b95a | ||
|
542bbce7db
|
58
.github/workflows/codeql-analysis.yml
vendored
58
.github/workflows/codeql-analysis.yml
vendored
@@ -2,12 +2,12 @@ name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
branches: [master]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master ]
|
||||
branches: [master]
|
||||
schedule:
|
||||
- cron: '16 17 * * 3'
|
||||
- cron: "16 17 * * 3"
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
@@ -17,40 +17,40 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'python' ]
|
||||
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
|
||||
- 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
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
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
|
||||
# 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@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
# ℹ️ 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
|
||||
# ✏️ 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
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
|
10
.github/workflows/main.yml
vendored
10
.github/workflows/main.yml
vendored
@@ -13,12 +13,12 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
python: [3.8, "3.10"]
|
||||
python: [3.11, 3.12]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python ${{ matrix.python }}
|
||||
uses: actions/setup-python@v2
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python }}
|
||||
- name: Install dependencies
|
||||
@@ -28,14 +28,14 @@ jobs:
|
||||
pip install -q --upgrade codecov coverage black flake8 codacy-coverage
|
||||
- name: Lint
|
||||
run: |
|
||||
black --check --diff stree
|
||||
# black --check --diff stree
|
||||
flake8 --count stree
|
||||
- name: Tests
|
||||
run: |
|
||||
coverage run -m unittest -v stree.tests
|
||||
coverage xml
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v1
|
||||
uses: codecov/codecov-action@v4
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
files: ./coverage.xml
|
||||
|
@@ -3,8 +3,12 @@ version: 2
|
||||
sphinx:
|
||||
configuration: docs/source/conf.py
|
||||
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
python: "3.12"
|
||||
|
||||
python:
|
||||
version: 3.8
|
||||
install:
|
||||
- requirements: requirements.txt
|
||||
- requirements: docs/requirements.txt
|
1
MANIFEST.in
Normal file
1
MANIFEST.in
Normal file
@@ -0,0 +1 @@
|
||||
include README.md LICENSE
|
44
Makefile
44
Makefile
@@ -1,46 +1,36 @@
|
||||
SHELL := /bin/bash
|
||||
.DEFAULT_GOAL := help
|
||||
.PHONY: coverage deps help lint push test doc build
|
||||
.PHONY: audit coverage help lint test doc doc-clean build
|
||||
|
||||
coverage: ## Run tests with coverage
|
||||
coverage erase
|
||||
coverage run -m unittest -v stree.tests
|
||||
coverage report -m
|
||||
@coverage erase
|
||||
@coverage run -m unittest -v stree.tests
|
||||
@coverage report -m
|
||||
|
||||
deps: ## Install dependencies
|
||||
pip install -r requirements.txt
|
||||
|
||||
devdeps: ## Install development dependencies
|
||||
pip install black pip-audit flake8 mypy coverage
|
||||
|
||||
lint: ## Lint and static-check
|
||||
black stree
|
||||
flake8 stree
|
||||
mypy stree
|
||||
|
||||
push: ## Push code with tags
|
||||
git push && git push --tags
|
||||
lint: ## Lint source files
|
||||
@black stree
|
||||
@flake8 stree
|
||||
|
||||
test: ## Run tests
|
||||
python -m unittest -v stree.tests
|
||||
@python -m unittest -v stree.tests
|
||||
|
||||
doc: ## Update documentation
|
||||
make -C docs --makefile=Makefile html
|
||||
@make -C docs --makefile=Makefile html
|
||||
|
||||
build: ## Build package
|
||||
rm -fr dist/*
|
||||
rm -fr build/*
|
||||
python setup.py sdist bdist_wheel
|
||||
@rm -fr dist/*
|
||||
@rm -fr build/*
|
||||
@hatch build
|
||||
|
||||
doc-clean: ## Update documentation
|
||||
make -C docs --makefile=Makefile clean
|
||||
doc-clean: ## Clean documentation folders
|
||||
@make -C docs --makefile=Makefile clean
|
||||
|
||||
audit: ## Audit pip
|
||||
pip-audit
|
||||
@pip-audit
|
||||
|
||||
help: ## Show help message
|
||||
help: ## Show this help message
|
||||
@IFS=$$'\n' ; \
|
||||
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
|
||||
help_lines=(`grep -Fh "##" $(MAKEFILE_LIST) | grep -Fv fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
|
||||
printf "%s\n\n" "Usage: make [task]"; \
|
||||
printf "%-20s %s\n" "task" "help" ; \
|
||||
printf "%-20s %s\n" "------" "----" ; \
|
||||
|
12
README.md
12
README.md
@@ -1,21 +1,23 @@
|
||||
# STree
|
||||
|
||||

|
||||
[](https://github.com/Doctorado-ML/STree/actions/workflows/codeql-analysis.yml)
|
||||
[](https://codecov.io/gh/doctorado-ml/stree)
|
||||
[](https://www.codacy.com/gh/Doctorado-ML/STree?utm_source=github.com&utm_medium=referral&utm_content=Doctorado-ML/STree&utm_campaign=Badge_Grade)
|
||||
[](https://lgtm.com/projects/g/Doctorado-ML/STree/context:python)
|
||||
[](https://badge.fury.io/py/STree)
|
||||

|
||||

|
||||
[](https://deepwiki.com/Doctorado-ML/STree)
|
||||
[](https://zenodo.org/badge/latestdoi/262658230)
|
||||
|
||||
# STree
|
||||

|
||||
|
||||
Oblique Tree classifier based on SVM nodes. The nodes are built and splitted with sklearn SVC models. Stree is a sklearn estimator and can be integrated in pipelines, grid searches, etc.
|
||||
|
||||

|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install git+https://github.com/doctorado-ml/stree
|
||||
pip install Stree
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
@@ -1,9 +1,10 @@
|
||||
Siterator
|
||||
=========
|
||||
|
||||
.. automodule:: Splitter
|
||||
.. automodule:: stree
|
||||
.. autoclass:: Siterator
|
||||
:members:
|
||||
:undoc-members:
|
||||
:private-members:
|
||||
:show-inheritance:
|
||||
:show-inheritance:
|
||||
:noindex:
|
@@ -1,9 +1,9 @@
|
||||
Snode
|
||||
=====
|
||||
|
||||
.. automodule:: Splitter
|
||||
.. autoclass:: Snode
|
||||
.. autoclass:: stree.Splitter.Snode
|
||||
:members:
|
||||
:undoc-members:
|
||||
:private-members:
|
||||
:show-inheritance:
|
||||
:show-inheritance:
|
||||
:noindex:
|
@@ -1,9 +1,10 @@
|
||||
Splitter
|
||||
========
|
||||
|
||||
.. automodule:: Splitter
|
||||
.. automodule:: stree.Splitter
|
||||
.. autoclass:: Splitter
|
||||
:members:
|
||||
:undoc-members:
|
||||
:private-members:
|
||||
:show-inheritance:
|
||||
:show-inheritance:
|
||||
:noindex:
|
@@ -6,4 +6,5 @@ Stree
|
||||
:members:
|
||||
:undoc-members:
|
||||
:private-members:
|
||||
:show-inheritance:
|
||||
:show-inheritance:
|
||||
:noindex:
|
@@ -6,26 +6,21 @@
|
||||
|
||||
# -- Path setup --------------------------------------------------------------
|
||||
|
||||
# If extensions (or modules to document with autodoc) are in another directory,
|
||||
# add these directories to sys.path here. If the directory is relative to the
|
||||
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||
#
|
||||
import os
|
||||
import sys
|
||||
from stree._version import __version__
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, os.path.abspath("../../stree/"))
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||
|
||||
import stree
|
||||
|
||||
# -- Project information -----------------------------------------------------
|
||||
|
||||
project = "STree"
|
||||
copyright = "2020 - 2022, Ricardo Montañana Gómez"
|
||||
copyright = "2020 - 2024, Ricardo Montañana Gómez"
|
||||
author = "Ricardo Montañana Gómez"
|
||||
|
||||
# The full version, including alpha/beta/rc tags
|
||||
version = __version__
|
||||
release = version
|
||||
|
||||
version = release = stree.__version__
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
|
@@ -5,7 +5,6 @@ Welcome to STree's documentation!
|
||||
:caption: Contents:
|
||||
:titlesonly:
|
||||
|
||||
|
||||
stree
|
||||
install
|
||||
hyperparameters
|
||||
|
@@ -178,7 +178,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Stree\n",
|
||||
"stree = Stree(random_state=random_state, C=.01, max_iter=1e3, kernel=\"liblinear\", multiclass_strategy=\"ovr\")"
|
||||
"stree = Stree(random_state=random_state, C=.01, max_iter=1000, kernel=\"liblinear\", multiclass_strategy=\"ovr\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -198,7 +198,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# SVC (linear)\n",
|
||||
"svc = LinearSVC(random_state=random_state, C=.01, max_iter=1e3)"
|
||||
"svc = LinearSVC(random_state=random_state, C=.01, max_iter=1000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@@ -1,5 +1,65 @@
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "STree"
|
||||
dependencies = ["scikit-learn>1.0", "mufs"]
|
||||
license = { file = "LICENSE" }
|
||||
description = "Oblique decision tree with svm nodes."
|
||||
readme = "README.md"
|
||||
authors = [
|
||||
{ name = "Ricardo Montañana", email = "ricardo.montanana@alu.uclm.es" },
|
||||
]
|
||||
dynamic = ['version']
|
||||
requires-python = ">=3.11"
|
||||
keywords = [
|
||||
"scikit-learn",
|
||||
"oblique-classifier",
|
||||
"oblique-decision-tree",
|
||||
"decision-tree",
|
||||
"svm",
|
||||
"svc",
|
||||
]
|
||||
classifiers = [
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Intended Audience :: Science/Research",
|
||||
"Intended Audience :: Developers",
|
||||
"Topic :: Software Development",
|
||||
"Topic :: Scientific/Engineering",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Natural Language :: English",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = ["black", "flake8", "coverage", "hatch", "pip-audit"]
|
||||
doc = ["sphinx", "myst-parser", "sphinx_rtd_theme", "sphinx-autodoc-typehints"]
|
||||
|
||||
[project.urls]
|
||||
Code = "https://github.com/Doctorado-ML/STree"
|
||||
Documentation = "https://stree.readthedocs.io/en/latest/index.html"
|
||||
|
||||
[tool.hatch.version]
|
||||
path = "stree/_version.py"
|
||||
|
||||
[tool.hatch.build.targets.sdist]
|
||||
include = ["/stree"]
|
||||
|
||||
[tool.coverage.run]
|
||||
branch = true
|
||||
source = ["stree"]
|
||||
command_line = "-m unittest discover -s stree.tests"
|
||||
|
||||
[tool.coverage.report]
|
||||
show_missing = true
|
||||
fail_under = 100
|
||||
|
||||
[tool.black]
|
||||
line-length = 79
|
||||
target-version = ["py311"]
|
||||
include = '\.pyi?$'
|
||||
exclude = '''
|
||||
/(
|
||||
@@ -13,4 +73,4 @@ exclude = '''
|
||||
| build
|
||||
| dist
|
||||
)/
|
||||
'''
|
||||
'''
|
||||
|
@@ -1,2 +1,3 @@
|
||||
scikit-learn>0.24
|
||||
mufs
|
||||
scikit-learn==1.5.2
|
||||
coverage
|
||||
mufs
|
||||
|
@@ -1 +0,0 @@
|
||||
python-3.8
|
56
setup.py
56
setup.py
@@ -1,56 +0,0 @@
|
||||
import setuptools
|
||||
import os
|
||||
|
||||
|
||||
def readme():
|
||||
with open("README.md") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def get_data(field, file_name="__init__.py"):
|
||||
item = ""
|
||||
with open(os.path.join("stree", file_name)) as f:
|
||||
for line in f.readlines():
|
||||
if line.startswith(f"__{field}__"):
|
||||
delim = '"' if '"' in line else "'"
|
||||
item = line.split(delim)[1]
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(f"Unable to find {field} string.")
|
||||
return item
|
||||
|
||||
|
||||
def get_requirements():
|
||||
with open("requirements.txt") as f:
|
||||
return f.read().splitlines()
|
||||
|
||||
|
||||
setuptools.setup(
|
||||
name="STree",
|
||||
version=get_data("version", "_version.py"),
|
||||
license=get_data("license"),
|
||||
description="Oblique decision tree with svm nodes",
|
||||
long_description=readme(),
|
||||
long_description_content_type="text/markdown",
|
||||
packages=setuptools.find_packages(),
|
||||
url="https://github.com/Doctorado-ML/STree#stree",
|
||||
project_urls={
|
||||
"Code": "https://github.com/Doctorado-ML/STree",
|
||||
"Documentation": "https://stree.readthedocs.io/en/latest/index.html",
|
||||
},
|
||||
author=get_data("author"),
|
||||
author_email=get_data("author_email"),
|
||||
keywords="scikit-learn oblique-classifier oblique-decision-tree decision-\
|
||||
tree svm svc",
|
||||
classifiers=[
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"License :: OSI Approved :: " + get_data("license"),
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Natural Language :: English",
|
||||
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
||||
"Intended Audience :: Science/Research",
|
||||
],
|
||||
install_requires=get_requirements(),
|
||||
test_suite="stree.tests",
|
||||
zip_safe=False,
|
||||
)
|
@@ -267,7 +267,6 @@ class Splitter:
|
||||
random_state=None,
|
||||
normalize=False,
|
||||
):
|
||||
|
||||
self._clf = clf
|
||||
self._random_state = random_state
|
||||
if random_state is not None:
|
||||
@@ -415,7 +414,8 @@ class Splitter:
|
||||
)
|
||||
return tuple(
|
||||
sorted(
|
||||
range(len(feature_list)), key=lambda sub: feature_list[sub]
|
||||
range(len(feature_list)),
|
||||
key=lambda sub: feature_list[sub],
|
||||
)[-max_features:]
|
||||
)
|
||||
|
||||
@@ -530,7 +530,10 @@ class Splitter:
|
||||
return entropy
|
||||
|
||||
def information_gain(
|
||||
self, labels: np.array, labels_up: np.array, labels_dn: np.array
|
||||
self,
|
||||
labels: np.array,
|
||||
labels_up: np.array,
|
||||
labels_dn: np.array,
|
||||
) -> float:
|
||||
"""Compute information gain of a split candidate
|
||||
|
||||
@@ -743,7 +746,7 @@ class Splitter:
|
||||
Train time - True / Test time - False
|
||||
"""
|
||||
# data contains the distances of every sample to every class hyperplane
|
||||
# array of (m, nc) nc = # classes
|
||||
# array of (m, nc) nc = k if ovr, nc = k*(k-1)/2 if ovo
|
||||
data = self._distances(node, samples)
|
||||
if data.shape[0] < self._min_samples_split:
|
||||
# there aren't enough samples to split
|
||||
|
@@ -153,7 +153,6 @@ class Stree(BaseEstimator, ClassifierMixin):
|
||||
multiclass_strategy: str = "ovo",
|
||||
normalize: bool = False,
|
||||
):
|
||||
|
||||
self.max_iter = max_iter
|
||||
self.C = C
|
||||
self.kernel = kernel
|
||||
@@ -169,12 +168,18 @@ class Stree(BaseEstimator, ClassifierMixin):
|
||||
self.splitter = splitter
|
||||
self.normalize = normalize
|
||||
self.multiclass_strategy = multiclass_strategy
|
||||
self.depth_ = 0
|
||||
|
||||
@staticmethod
|
||||
def version() -> str:
|
||||
"""Return the version of the package."""
|
||||
return __version__
|
||||
|
||||
def __call__(self) -> None:
|
||||
"""Only added to comply with scikit-learn base sestimator for
|
||||
ensembles"""
|
||||
pass
|
||||
|
||||
def _more_tags(self) -> dict:
|
||||
"""Required by sklearn to supply features of the classifier
|
||||
make mandatory the labels array
|
||||
@@ -185,7 +190,10 @@ class Stree(BaseEstimator, ClassifierMixin):
|
||||
return {"requires_y": True}
|
||||
|
||||
def fit(
|
||||
self, X: np.ndarray, y: np.ndarray, sample_weight: np.array = None
|
||||
self,
|
||||
X: np.ndarray,
|
||||
y: np.ndarray,
|
||||
sample_weight: np.array = None,
|
||||
) -> "Stree":
|
||||
"""Build the tree based on the dataset of samples and its labels
|
||||
|
||||
@@ -340,7 +348,11 @@ class Stree(BaseEstimator, ClassifierMixin):
|
||||
)
|
||||
node.set_down(
|
||||
self._train(
|
||||
X_D, y_d, sw_d, depth + 1, title + f" - Down({depth+1})"
|
||||
X_D,
|
||||
y_d,
|
||||
sw_d,
|
||||
depth + 1,
|
||||
title + f" - Down({depth+1})",
|
||||
)
|
||||
)
|
||||
return node
|
||||
@@ -485,6 +497,43 @@ class Stree(BaseEstimator, ClassifierMixin):
|
||||
X = self.check_predict(X)
|
||||
return self.classes_[np.argmax(self.__predict_class(X), axis=1)]
|
||||
|
||||
def get_nodes(self) -> int:
|
||||
"""Return the number of nodes in the tree
|
||||
|
||||
Returns
|
||||
-------
|
||||
int
|
||||
number of nodes
|
||||
"""
|
||||
nodes = 0
|
||||
for _ in self:
|
||||
nodes += 1
|
||||
return nodes
|
||||
|
||||
def get_leaves(self) -> int:
|
||||
"""Return the number of leaves in the tree
|
||||
|
||||
Returns
|
||||
-------
|
||||
int
|
||||
number of leaves
|
||||
"""
|
||||
leaves = 0
|
||||
for node in self:
|
||||
if node.is_leaf():
|
||||
leaves += 1
|
||||
return leaves
|
||||
|
||||
def get_depth(self) -> int:
|
||||
"""Return the depth of the tree
|
||||
|
||||
Returns
|
||||
-------
|
||||
int
|
||||
depth of the tree
|
||||
"""
|
||||
return self.depth_
|
||||
|
||||
def nodes_leaves(self) -> tuple:
|
||||
"""Compute the number of nodes and leaves in the built tree
|
||||
|
||||
|
@@ -1,8 +1,9 @@
|
||||
from .Strees import Stree, Siterator
|
||||
from ._version import __version__
|
||||
|
||||
__author__ = "Ricardo Montañana Gómez"
|
||||
__copyright__ = "Copyright 2020-2021, Ricardo Montañana Gómez"
|
||||
__license__ = "MIT License"
|
||||
__author_email__ = "ricardo.montanana@alu.uclm.es"
|
||||
|
||||
__all__ = ["Stree", "Siterator"]
|
||||
__all__ = ["__version__", "Stree", "Siterator"]
|
||||
|
@@ -1 +1 @@
|
||||
__version__ = "1.3.1"
|
||||
__version__ = "1.4.0"
|
||||
|
@@ -239,6 +239,7 @@ class Stree_test(unittest.TestCase):
|
||||
)
|
||||
tcl.fit(*load_dataset(self._random_state))
|
||||
self.assertEqual(depth, tcl.depth_)
|
||||
self.assertEqual(depth, tcl.get_depth())
|
||||
|
||||
def test_unfitted_tree_is_iterable(self):
|
||||
tcl = Stree()
|
||||
@@ -288,12 +289,12 @@ class Stree_test(unittest.TestCase):
|
||||
"impurity sigmoid": 0.824,
|
||||
},
|
||||
"Iris": {
|
||||
"max_samples liblinear": 0.9550561797752809,
|
||||
"max_samples liblinear": 0.9887640449438202,
|
||||
"max_samples linear": 1.0,
|
||||
"max_samples rbf": 0.6685393258426966,
|
||||
"max_samples poly": 0.6853932584269663,
|
||||
"max_samples sigmoid": 0.6404494382022472,
|
||||
"impurity liblinear": 0.9550561797752809,
|
||||
"impurity liblinear": 0.9887640449438202,
|
||||
"impurity linear": 1.0,
|
||||
"impurity rbf": 0.6685393258426966,
|
||||
"impurity poly": 0.6853932584269663,
|
||||
@@ -307,9 +308,9 @@ class Stree_test(unittest.TestCase):
|
||||
for kernel in self._kernels:
|
||||
clf = Stree(
|
||||
max_iter=int(1e4),
|
||||
multiclass_strategy="ovr"
|
||||
if kernel == "liblinear"
|
||||
else "ovo",
|
||||
multiclass_strategy=(
|
||||
"ovr" if kernel == "liblinear" else "ovo"
|
||||
),
|
||||
kernel=kernel,
|
||||
random_state=self._random_state,
|
||||
)
|
||||
@@ -439,10 +440,10 @@ class Stree_test(unittest.TestCase):
|
||||
clf.fit(X, y)
|
||||
score = clf.score(X, y)
|
||||
# Check accuracy of the whole model
|
||||
self.assertAlmostEquals(0.98, score, 5)
|
||||
self.assertAlmostEqual(0.98, score, 5)
|
||||
svm = LinearSVC(random_state=0)
|
||||
svm.fit(X, y)
|
||||
self.assertAlmostEquals(0.9666666666666667, svm.score(X, y), 5)
|
||||
self.assertAlmostEqual(0.9666666666666667, svm.score(X, y), 5)
|
||||
data = svm.decision_function(X)
|
||||
expected = [
|
||||
0.4444444444444444,
|
||||
@@ -454,7 +455,7 @@ class Stree_test(unittest.TestCase):
|
||||
ty[data > 0] = 1
|
||||
ty = ty.astype(int)
|
||||
for i in range(3):
|
||||
self.assertAlmostEquals(
|
||||
self.assertAlmostEqual(
|
||||
expected[i],
|
||||
clf.splitter_._gini(ty[:, i]),
|
||||
)
|
||||
@@ -592,7 +593,7 @@ class Stree_test(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(0.9526666666666667, clf2.fit(X, y).score(X, y))
|
||||
X, y = load_wine(return_X_y=True)
|
||||
self.assertEqual(0.9831460674157303, clf.fit(X, y).score(X, y))
|
||||
self.assertEqual(0.9887640449438202, clf.fit(X, y).score(X, y))
|
||||
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
||||
|
||||
def test_zero_all_sample_weights(self):
|
||||
@@ -640,10 +641,12 @@ class Stree_test(unittest.TestCase):
|
||||
clf = Stree(random_state=self._random_state)
|
||||
clf.fit(X, y)
|
||||
self.assertEqual(6, clf.depth_)
|
||||
self.assertEqual(6, clf.get_depth())
|
||||
X, y = load_wine(return_X_y=True)
|
||||
clf = Stree(random_state=self._random_state)
|
||||
clf.fit(X, y)
|
||||
self.assertEqual(4, clf.depth_)
|
||||
self.assertEqual(4, clf.get_depth())
|
||||
|
||||
def test_nodes_leaves(self):
|
||||
"""Check number of nodes and leaves."""
|
||||
@@ -657,13 +660,17 @@ class Stree_test(unittest.TestCase):
|
||||
clf.fit(X, y)
|
||||
nodes, leaves = clf.nodes_leaves()
|
||||
self.assertEqual(31, nodes)
|
||||
self.assertEqual(31, clf.get_nodes())
|
||||
self.assertEqual(16, leaves)
|
||||
self.assertEqual(16, clf.get_leaves())
|
||||
X, y = load_wine(return_X_y=True)
|
||||
clf = Stree(random_state=self._random_state)
|
||||
clf.fit(X, y)
|
||||
nodes, leaves = clf.nodes_leaves()
|
||||
self.assertEqual(11, nodes)
|
||||
self.assertEqual(11, clf.get_nodes())
|
||||
self.assertEqual(6, leaves)
|
||||
self.assertEqual(6, clf.get_leaves())
|
||||
|
||||
def test_nodes_leaves_artificial(self):
|
||||
"""Check leaves of artificial dataset."""
|
||||
@@ -682,7 +689,9 @@ class Stree_test(unittest.TestCase):
|
||||
clf.tree_ = n1
|
||||
nodes, leaves = clf.nodes_leaves()
|
||||
self.assertEqual(6, nodes)
|
||||
self.assertEqual(6, clf.get_nodes())
|
||||
self.assertEqual(2, leaves)
|
||||
self.assertEqual(2, clf.get_leaves())
|
||||
|
||||
def test_bogus_multiclass_strategy(self):
|
||||
"""Check invalid multiclass strategy."""
|
||||
@@ -716,6 +725,11 @@ class Stree_test(unittest.TestCase):
|
||||
clf = Stree()
|
||||
self.assertEqual(__version__, clf.version())
|
||||
|
||||
def test_call(self) -> None:
|
||||
"""Check call method."""
|
||||
clf = Stree()
|
||||
self.assertIsNone(clf())
|
||||
|
||||
def test_graph(self):
|
||||
"""Check graphviz representation of the tree."""
|
||||
X, y = load_wine(return_X_y=True)
|
||||
|
Reference in New Issue
Block a user