Package for training rule set classifiers for tabular data.
Project description
rsclassifier
Overview
This package consist of the module rsclassifier
which contains the class RuleSetClassifier
.
RuleSetClassifier
is a non-parametric supervised learning method that can be used for classification and data mining. As the name suggests, RuleSetClassifier
produces classifiers which consist of a set of rules which are learned from the given data. As a concrete example, the following classifier was produced from the well-known Iris data set.
IF
(petal_length_in_cm > 2.45 AND petal_width_in_cm > 1.75) {support: 33, confidence: 0.97}
THEN virginica
ELSE IF
(petal_length_in_cm <= 2.45 AND petal_width_in_cm <= 1.75) {support: 31, confidence: 1.00}
THEN setosa
ELSE versicolor
Notice that each rule is accompanied by:
- Support: The number of data points that satisfy the rule.
- Confidence: The probability that a data point satisfying the rule is correctly classified.
As an another concrete example, the following classifier was produced from the Breast Cancer Wisconsin data set.
IF
(bare_nuclei > 2.50 AND clump_thickness > 4.50) {support: 134, confidence: 0.94}
OR (uniformity_of_cell_size > 3.50) {support: 150, confidence: 0.94}
OR (bare_nuclei > 5.50) {support: 119, confidence: 0.97}
THEN 4
ELSE 2
This classifier classifiers all tumors which satisfy one of the four rules listed above as malign (4) and all other tumors as benign (2).
Advantages
RuleSetClassifier
produces extremely interpretable and transparent classifiers.- It is very easy to use, as it has only two hyperparameters.
- It can handle both categorical and numerical data.
- The learning process is very fast.
How to use RuleSetClassifier
Let rsc
be an instance of RuleSetClassifier
and let X
be a pandas dataframe (input features) and y
a pandas series (target labels).
- Load the data: Use
rsc.load_data(X, y, boolean, categorical, numerical)
whereboolean
,categorical
andnumerical
are (possibly empty) lists specifying which features inX
are boolean, categorical or numerical, respectively. This function converts the data into a Boolean form for rule learning and store is torsc
. - Fit the classifier: After loading the data, call
rsc.fit(num_prop, growth_size)
. Note that unlike in scikit-learn, this function doesn't takeX
andy
directly as arguments; they are loaded beforehand as part ofload_data
. The two hyperparametersnum_prop
andgrowth_size
work as follows.num_prop
is an upper bound on the number of proposition symbols allowed in the rules. The smallernum_prop
is, the more interpretable the models are. The downside of having smallnum_prop
is of course that the resulting model has low accuracy (i.e., it underfits), so an optimal value fornum_prop
is the one which strikes a balance between interpretability and accuracy.growth_size
is a float in the range (0, 1], determining the proportion of X used for learning rules. The remaining portion is used for pruning. Ifgrowth_size
is set to 1, which is the default value, no pruning is performed. Also 2/3 seems to work well in practice.
- Make predictions: Use
rsc.predict(X)
to generate predictions. This function returns a pandas Series. - Visualize the classifier: Simply print the classifier to visualize the learned rules (together with their support and confidence).
Note: At present, RuleSetClassifier
does not support datasets with missing values. You will need to preprocess your data (e.g., removing missing values) before using the classifier.
Installation
To install the package, you can use pip
:
pip install rsclassifier
Background
The rule learning method implemented by RuleSetClassifier
was inspired by and extends the approach taken in the paper, which we refer here as the ideal DNF-method. The ideal DNF-method goes as follows. First, the input data is Booleanized. Then, a small number of promising features is selected. Finally, a DNF-formula is computed for those promising features for which the number of misclassified points is as small as possible.
The way RuleSetClassifier
extends and modifies the ideal DNF-method is mainly as follows.
- We use an entropy-based Booleanization for numerical features with minimum description length principle working as a stopping rule.
RuleSetClassifier
is not restricted to binary classification tasks.- We implement rule pruning as a postprocessing step. This is important, as it makes the rules shorter and hence more interpretable.
Example
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from rsclassifier import RuleSetClassifier
# Load the data set.
df = pd.read_csv('iris.csv')
# Split it into train and test.
X = df.drop(columns = ['class'], axis = 1)
y = df['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.8)
# Initialize RuleSetClassifier.
rsc = RuleSetClassifier()
# All the features of iris.csv are numerical.
rsc.load_data(X = X_train, y = y_train, numerical = X.columns)
# Fit the classifier with a maximum of 2 proposition symbols.
rsc.fit(num_prop = 2)
# Measure the accuracy of the resulting classifier.
train_accuracy = accuracy_score(rsc.predict(X_train), y_train)
test_accuracy = accuracy_score(rsc.predict(X_test), y_test)
# Display the classifier and its accuracies.
print()
print(rsc)
print(f'Rule set classifier training accuracy: {train_accuracy}')
print(f'Rule set classifier test accuracy: {test_accuracy}')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rsclassifier-1.1.1.tar.gz
.
File metadata
- Download URL: rsclassifier-1.1.1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66d9290deaee7d17554f9844cb69ffd880f7a2741fdab6aedb8f42eed8c9b1b0 |
|
MD5 | 1b18370a29fc2f97826f69a7604ac8d4 |
|
BLAKE2b-256 | 484447549ad383e690fdb5ac00225d399af785b063c49ab0a669e23c7d467d81 |
File details
Details for the file rsclassifier-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: rsclassifier-1.1.1-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66181a3400bb11b8ab7b1733910e952f1e799c3e2e3394df2ae262e4f9fb3991 |
|
MD5 | 8d9f6ded584d5f954f8d3dbea0d6dc09 |
|
BLAKE2b-256 | b04be4bbe3df580b30f83b19ab140bdec2ddf69491286418d8d5ed69d46fbb56 |