Trefle stands for Trefle is a Revised and Evolutionary-based Fuzzy Logic Engine. It is an implementation of the FuzzyCoCo algorithm i.e. a scikit-learn compatible estimator that use a cooperative coevolution algorithm to find and build interpretable fuzzy systems. Designed for both students and researchers
Project description
Trefle — A scikit-learn compatible classifier using interpretable fuzzy systems
Trefle is a scikit-learn compatible estimator implementing the FuzzyCoCo algorithm that uses a cooperative coevolution algorithm to find and build interpretable fuzzy systems.
Here is a basic example using Wisconsin Breast Cancer Dataset, a binary classification problem, from scikit-learn:
import random
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from trefle.fitness_functions.output_thresholder import round_to_cls
from trefle.trefle_classifier import TrefleClassifier
np.random.seed(0)
random.seed(0)
# Load dataset
data = load_breast_cancer()
# Organize our data
X = data["data"]
y = data["target"]
y = np.reshape(y, (-1, 1)) # output needs to be at least 1 column wide
# Split our data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# Declare the fitness function we want to use. Fitness value: higher is better.
def fit(y_true, y_pred):
# y_pred are floats in [0, n_classes-1]. To use accuracy metric we need
# to binarize the output using round_to_cls()
y_pred_bin = round_to_cls(y_pred, n_classes=2)
return accuracy_score(y_true, y_pred_bin)
# Initialize our classifier
clf = TrefleClassifier(
n_rules=4,
n_classes_per_cons=[2], # a list where each element indicates the number of classes a consequent has. Specify 0 if one consequent is a continuous (regression) value.
n_labels_per_mf=3,
default_cons=[0], # the default rule will use the class 0
n_max_vars_per_rule=3,
n_generations=20,
fitness_function=fit,
verbose=False,
)
# Train our classifier
clf.fit(X_train, y_train)
# Make predictions
y_pred = clf.predict_classes(X_test)
clf.print_best_fuzzy_system()
# Evaluate accuracy
score = accuracy_score(y_test, y_pred)
print("Score on test set: {:.3f}".format(score))
This will output the fuzzy system:
IF v0 is low AND v5 is medium AND v16 is low THEN [0]
IF v25 is high AND v9 is high AND v14 is medium THEN [0]
IF v6 is high THEN [0]
IF v21 is low AND v23 is low THEN [1]
ELSE [0]
Variables definition
v0: -0.366, -0.347, -0.343,
v5: 0.155, 2.03, 2.03,
v6: 0.0756, 0.151, 1.36,
v9: 5.06, 11.2, 16.6,
v14: 5.89, 34.2, 37.2,
v16: 0.0815, 0.652, 1.06,
v21: -0.299, -0.294, -0.294,
v23: -0.0555, -0.0553, -0.0553,
v25: 0.193, 0.568, 0.631,
Score on test set: 0.910
If you have never heard of fuzzy systems before you can basically think of them as a set of rules giving a prediction after they have been evaluated. For example "IF temperature is HIGH and sunshine is MEDIUM THEN tourists is MEDIUM".
Installation
Start using Trefle today with pip :-)
pip install trefle
Examples of use
See other examples in the examples folder.
Cool features
- Support classification (binary and multiclass), regression and mixed (i.e. both classification and regression) problems
- Fully compatible scikit-learn estimator
- Use it like a regular estimator
- Support GridSearch
- Fuzzy systems parameters are customizable e.g. the number of rules, the number of linguistic labels per rule,...
- Evolutionary parameters are customizable e.g. number of generations, population size, hall of fame size,...
- Custom fitness function
- Import and Export the best fuzzy system for future use in an interoperable format
- Fine tune your best fuzzy system using the companion library LFA Toolbox. Add or remove a fuzzy rule to increase either the performance or interpretability of the fuzzy system. Or tweak the membership functions.
- The fuzzy engine is implemented in C++14 allowing Trefle to be quite fast and use all the CPU cores
- Last but not least, Trefle is a recursive acronym like GNU which is cool. It stands for Trefle is a Revised and Evolutionary-based Fuzzy Logic Engine. And trefle also means clover in French.
What are fuzzy logic and FuzzyCoco algorithm?
FuzzyCoCo algorithm
The following sentences are drawn from the PhD thesis "Coevolutionary Fuzzy Modeling" by Carlos Andrés PEÑA REYES that you can find here.
Fuzzy CoCo is a novel approach that combines two methodologies - fuzzy systems and coevolutionary algorithms - so as to automatically produce accurate and interpretable systems. The approach is based on two elements: (1) a system model capable of providing both accuracy and human understandability, and (2) an algorithm that allows to build such a model from available information.
In short, as a user this algorithm will give you a model that is interpretable and accurate (i.e. you can see how the model works) given a classification or a regression problem. From this model you can read the features that it extracted.
How it works?
- Load dataset
- Configure experiment i.e. the number of rules, the number of generations and other fuzzy systems or evolutionary parameters
- Create two initial populations (also called "species"; one for the fuzzy rules and the other for the variables definition). Both represent individuals as a list of bits.
- Run evolutionary algorithm. It will perform the following steps.
- Select
- Crossover
- Mutate
- Evaluate by combining individuals from a population with representatives of the other population to form a fuzzy system
- Save the best couple (i.e. the combination of an individual from pop1 and one from pop2)
- Repeat these steps until max generations is reached
- Retrieve best individual couple i.e. the best fuzzy system
- Optionally use the LFA Toolbox to visualize or fine tune it
Deployment and Tests
Both are available in the docs
folder.
Build from sources
See docs/DEPLOYMENT.md.
Where is the doc?!
There is no documentation like a Sphinx one. Start by looking in the docs folder or directly in the source code of TrefleClassifier
.
Credits
- Gary Marigliano
- Carlos Andrés PEÑA REYES
- CI4CB Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file trefle-0.2.tar.gz
.
File metadata
- Download URL: trefle-0.2.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76678aa9c50b14db263073766411cce0189e5ad4ff0667e06f1d148daf770154 |
|
MD5 | c0cc953dbff1671d5388a9468de19ac7 |
|
BLAKE2b-256 | de2d6b1a169e20af1f727bef335edd4bf2ec0f5d6ed2e3b683c462d4a1a37f3a |
File details
Details for the file trefle-0.2-cp37-cp37m-win32.whl
.
File metadata
- Download URL: trefle-0.2-cp37-cp37m-win32.whl
- Upload date:
- Size: 189.1 kB
- Tags: CPython 3.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c89d373d09ff3e4396498f15ac1b7ac9faf651f8a2917563e1520f0992c39b2 |
|
MD5 | 6051b44365b362307f54570ebf4cec40 |
|
BLAKE2b-256 | 9b8dd0edfa7a3beb775ed2e095075e7a2b42af383a3bd5237c42fc4ad9a6e9e4 |
File details
Details for the file trefle-0.2-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 718.4 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a4787951a2060bce0af3f5741defac1354937ebd88a5034b20201d1ded539fb |
|
MD5 | 802afa0754e45ca90fbf5e2bdb0c43b9 |
|
BLAKE2b-256 | 497f8ccc3c492bca33a0b977265a4084781b5c2ad73cd81f6b6fa1b6030c7b12 |
File details
Details for the file trefle-0.2-cp37-cp37m-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp37-cp37m-macosx_10_12_x86_64.whl
- Upload date:
- Size: 268.9 kB
- Tags: CPython 3.7m, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b9b69370df2f370552846a1839f9d2c837dbec9d9b7a1a797b47731c8daec2a |
|
MD5 | 95653422d9c7487babbfe2da13062078 |
|
BLAKE2b-256 | a653b5e8f1ade966327cea46af75d17a6823a37da44c4e43bac46bf5bce1d88a |
File details
Details for the file trefle-0.2-cp36-cp36m-win32.whl
.
File metadata
- Download URL: trefle-0.2-cp36-cp36m-win32.whl
- Upload date:
- Size: 189.1 kB
- Tags: CPython 3.6m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 598eaeae72f9d1a26ff0edddc717f425f1308a7bd26d5201b06ae5eb6cf16363 |
|
MD5 | 5b144199fbccdbe4cd96cf128a37336f |
|
BLAKE2b-256 | 8152c133be8fd407cbb58675cd0698e90d69ef107240f06f1a27f07eaf3b702d |
File details
Details for the file trefle-0.2-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 718.4 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec35f45e0d9960f3d60c85fee60a17ce5a638bf919ff0bc882cdc286f828f343 |
|
MD5 | 4ba3be73c680761c52c71df29cbc6f72 |
|
BLAKE2b-256 | d14a3043c0f0c99f3dc6fda6c2e52d0bc522b26c06cd0191f91695b3cd5c678f |
File details
Details for the file trefle-0.2-cp36-cp36m-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp36-cp36m-macosx_10_12_x86_64.whl
- Upload date:
- Size: 268.9 kB
- Tags: CPython 3.6m, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2febdc588a27c097bc4257401c7e222040779dd5dc2650e867a2299eea5dfbbb |
|
MD5 | dd53885c712563740307271b9ba14e24 |
|
BLAKE2b-256 | 1dcdb787745f06f3b0f29cd7264ff70792a4c5c27d7c43e7f04c9899018bb218 |
File details
Details for the file trefle-0.2-cp35-cp35m-win32.whl
.
File metadata
- Download URL: trefle-0.2-cp35-cp35m-win32.whl
- Upload date:
- Size: 189.1 kB
- Tags: CPython 3.5m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3efa6d759276afdc00b8f24a80eb88bf368c514476573b0818ccfc8b8d5b90e9 |
|
MD5 | 928ea27f7d2d27ed9fb0429b4a932209 |
|
BLAKE2b-256 | 364dc78416fe89d37c71c92f99137591fd6bf11d0a292f2a957d196666e06184 |
File details
Details for the file trefle-0.2-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 718.4 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c52042e1ff8113ebaecfedbb8ee5313f4e414987afe386386517396df280ca3 |
|
MD5 | 4eac1f2210550afb739f4a92cee7b6c3 |
|
BLAKE2b-256 | 3b9467b30c11a1a50b5d6ecda015aa99aac2158b7d46402831d1fdef9a76cb0a |
File details
Details for the file trefle-0.2-cp35-cp35m-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: trefle-0.2-cp35-cp35m-macosx_10_12_x86_64.whl
- Upload date:
- Size: 268.9 kB
- Tags: CPython 3.5m, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.5.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f01b039d562cfa5092a2d767afc6e6c9eef13630152f8fedb1208e6f66bcd21f |
|
MD5 | d4275bc7a5b0fb77352711109791afcc |
|
BLAKE2b-256 | 6790b23a3857a17d1fe75e0635985e880a5863bb11c623a4de87000d607979d9 |