- This repository contains code and tutorials for executing RBBR.
- The RBBR package supports parallel execution on multi-CPU platforms, enhancing accessibility for real-world Boolean rule inference applications.
Malekpour, S.A., Pezeshk, H., Explainable artificial intelligence with Boolean rule-aware predictions in ridge regression models, Neurocomputing, 132991 (2026).
install.packages("RBBR")To preprocess raw data, including steps such as rescaling to bring each input feature within the [0,1] range, you can use the rbbr_scaling() function from the RBBR package.
rbbr_scaling(data)
# Required input arguments# data Each row is a sample and each column a feature. The target variable is expected in the last column.############################# Example usage ############################## Load dataset
data(example_data)
# Inspect loaded data
head(MAGIC_data)
fLengthfWidthfSizefConcfConc1fAsymfM3LongfM3TransfAlphafDistclass128.796716.00212.64490.39180.198227.700422.0110-8.202740.092081.88281231.603611.72352.51850.53030.377326.272223.8238-9.95746.3609205.261013162.0520136.03104.06120.03740.0187116.7410-64.8580-45.216076.9600256.78801423.81729.57282.33850.61470.392227.2107-6.4633-7.151310.4490116.73701575.136230.92053.16110.31680.1832-5.527728.552521.83934.6480356.46201651.624021.15022.90850.24200.134050.876143.18879.81453.6130238.09801# Scale featuresdata_scaled<- rbbr_scaling(MAGIC_data)
head(data_scaled)
fLengthfWidthfSizefConcfConc1fAsymfM3LongfM3TransfAlphafDistclass10.14275320.20503900.34900450.498845750.429284160.89432050.82233080.79671310.468744590.2292175120.15909930.15021620.28630670.681286040.817787420.89169030.82654430.78964150.074369890.5800906130.91876880.99990000.99990000.032009380.039913230.99990000.62041760.64754680.899795060.7266274140.11375500.12265870.19702190.792462650.850108460.89341860.75614680.80095030.122166820.3283388150.41261250.39619220.60505350.400051370.396746200.83312700.83753550.91778450.054343130.9999000160.27568860.27100290.47975710.301520450.290021690.93700130.87155500.86932370.042242200.67347521For training the RBBR model on a dataset to extract Boolean rules, you can use the rbbr_train() function.
# For training the RBBR model
rbbr_train(
data,
max_feature=3,
mode="1L",
slope=10,
weight_threshold=0,
balancing=TRUE,
num_cores=NA,
verbose=FALSE
)
# Required input arguments# data The dataset with scaled features within the [0,1] interval.# Each row represents a sample and each column represents a feature. The target variable must be in the last column.## Optional input arguments # max_feature The maximum number of input features allowed in a Boolean rule.# The default value is 3.# mode Choose between "1L" for fitting 1-layered models or "2L" for fitting 2-layered models.# The default value is "1L".# slope The slope parameter used in the Sigmoid activation function.# The default value is 10.# weight_threshold Conjunctions with weights above this threshold in the fitted ridge regression models will be printed as active conjunctions in the output.# The default value is 0.# balancing Logical. This is for adjusting the distribution of target classes or categories within a dataset to ensure that each class is adequately represented.# The default value is TRUE. Set it to FALSE, if you don't need to perform the data balancing.# num_cores Number of parallel workers to use for computation.# Adjust according to your system. Default is NA (automatic selection).# verbose Logical. If TRUE, progress messages and a progress bar are shown.# Default is FALSE.############################# Example usage #############################
library(RBBR)
# Load dataset
data(example_data)
# Example for training a two-layer model
head(OR_data) # Y = OR([AND(C,D)],[NOR(A,B)]) datasetCDABY10.95039970.810281020.62496620.1931601120.14810770.500526850.15273220.8670829030.81517700.657701700.30313370.6836050140.80493810.078650070.60843610.5317733050.89601930.271241440.50902350.4332590060.15090370.797361720.17725680.51008280data_train<-OR_data[1:800, ]
data_test<-OR_data[801:1000, ]
# training modeltrained_model<- rbbr_train(data_train,
max_feature=2,
mode="2L",
balancing=FALSE,
num_cores=1, verbose=TRUE)
trainingprocessstartedwith1computingcores|====================|100%
head(trained_model$boolean_rules)
Boolean_RuleR2BICWeightsLayer1, Sub-Rule1WeightsLayer1, Sub-Rule2WeightsLayer21 [OR([AND(C,D)],[NOR(A,B)])] 0.71-2039.640.92:-0.37:-0.35:-0.2-0.2:-0.37:-0.36:0.910.48:0.41:0.37:-0.732 [AND([OR(C,¬B)],[OR(D,¬A)])] 0.44-1504.100.19:-0.68:0.34:0.180.09:-0.67:0.52:0.080.97:-0.15:-0.13:-0.433 [AND([OR(C,¬A)],[OR(D,¬B)])] 0.43-1480.530.08:-0.66:0.49:0.120.12:-0.62:0.46:0.050.9:-0.1:-0.08:-0.464 [OR([C],[NOR(A,B)])] 0.41-1468.380.29:-0.27-0.2:-0.37:-0.36:0.910.45:0.47:0.18:-0.635 [OR([D],[NOR(A,B)])] 0.41-1467.830.31:-0.29-0.2:-0.37:-0.36:0.910.5:0.43:0.15:-0.646 [OR([¬A],[AND(C,D)])] 0.40-1458.98-0.3:0.290.92:-0.37:-0.35:-0.20.54:0.37:0.15:-0.64# testing modeldata_test_x<-data_test[ ,1:(ncol(data_test)-1)]
labels<-data_test[ ,ncol(data_test)]
predicted_label_probabilities<- rbbr_predictor(trained_model,
data_test_x,
num_top_rules=10,
num_cores=1, verbose=TRUE)
head(predicted_label_probabilities)
[1] 0.0055873390.1109864790.8266925660.0363427870.7311979720.011742257For utilizing the trained model to predict target values or labels on a new dataset, you can use the rbbr_predictor() function. In datasets with binary (0/1) target features, the rbbr_predictor() function produces predicted probabilities for target labels. However, when dealing with a continuous target variable, the rbbr_predictor() output can be regarded as the predicted target value.
# For making predictions
rbbr_predictor(
trained_model,
data_test,
num_top_rules=1,
slope=10,
num_cores=1,
verbose=FALSE
)
# Required input arguments# trained_model Model returned by 'rbbr_train()'# data_test The new dataset for which we want to predict the target class or label probability. Each sample is represented as a row, and features are in columns.## Optional input arguments # num_top_rules Number of Boolean rules with the best Bayesian Information Criterion (BIC) scores to be used for prediction.# The default value is 1.# slope The slope parameter for the sigmoid activation function.# Default is 10.# num_cores Number of parallel workers to use for computation. Adjust according to your system.# Default is NA (automatic selection).# verbose Logical. If TRUE, progress messages are shown. Default is FALSE.############################# Example usage #############################
library(RBBR)
# Load dataset
data(example_data)
# Inspect loaded data
head(XOR_data) # Y = XOR(feat_0, feat_1) datasetfeat_0feat_1feat_2feat_3feat_4feat_5feat_6feat_7feat_8feat_9xor10.6258533520.483614740.105290460.576288660.8788116990.78950820.78240210.0190966360.91851680.34709053120.9033190410.026437910.371739890.059412280.2043675130.13482340.35024400.9289946180.31473150.95440266130.8801837940.647591000.099143870.262023470.0002177440.40280340.14071600.7674969570.53801300.22020956040.0004787790.096988730.790219140.219213780.9647837780.86058760.76251960.8695789550.98554410.03724109050.4427832730.224676660.612721490.118018440.3805106660.81450490.35359300.0076804830.39202200.62243004060.0889767730.680763540.921535000.046537720.1780605600.15997070.27440700.4143348180.76528310.261531531data_train<-XOR_data[1:800, ]
data_test<-XOR_data[801:1000, ]
# training modeltrained_model<- rbbr_train(data_train,
max_feature=2,
num_cores=1, verbose=TRUE)
trainingprocessstartedwith1computingcores|====================|100%
head(trained_model$boolean_rules)
Boolean_RuleR2BICWeightsLayer1, Sub-Rule11 [XOR(feat_0,feat_1)] 0.77-2363.32-1.1:0.99:1.08:-1.062 [¬feat_5] 0.00-1149.03-0.02:0.023 [feat_6] 0.00-1148.640.01:-0.014 [feat_0] 0.00-1147.630:05 [¬feat_7] 0.00-1147.370:06 [¬feat_1] 0.00-1147.050:0# testing modeldata_test_x<-data_test[ ,1:(ncol(data_test)-1)]
labels<-data_test[ ,ncol(data_test)]
predicted_label_probabilities<- rbbr_predictor(trained_model,
data_test_x,
num_top_rules=1,
num_cores=1, verbose=TRUE)
head(predicted_label_probabilities)
[1] 0.03273358440.98845601680.00035262810.32294424090.07898545450.0043184231
head(labels) # true labels
[1] 010000library(RBBR)
# Load dataset
data(example_data)
# Inspect loaded data
head(MAGIC_data)
fLengthfWidthfSizefConcfConc1fAsymfM3LongfM3TransfAlphafDistclass128.796716.00212.64490.39180.198227.700422.0110-8.202740.092081.88281231.603611.72352.51850.53030.377326.272223.8238-9.95746.3609205.261013162.0520136.03104.06120.03740.0187116.7410-64.8580-45.216076.9600256.78801423.81729.57282.33850.61470.392227.2107-6.4633-7.151310.4490116.73701575.136230.92053.16110.31680.1832-5.527728.552521.83934.6480356.46201651.624021.15022.90850.24200.134050.876143.18879.81453.6130238.09801# Scaling to bring each input feature within the (0,1) rangedata_scaled<- rbbr_scaling(MAGIC_data)
head(data_scaled)
fLengthfWidthfSizefConcfConc1fAsymfM3LongfM3TransfAlphafDistclass10.14275320.20503900.34900450.498845750.429284160.89432050.82233080.79671310.468744590.2292175120.15909930.15021620.28630670.681286040.817787420.89169030.82654430.78964150.074369890.5800906130.91876880.99990000.99990000.032009380.039913230.99990000.62041760.64754680.899795060.7266274140.11375500.12265870.19702190.792462650.850108460.89341860.75614680.80095030.122166820.3283388150.41261250.39619220.60505350.400051370.396746200.83312700.83753550.91778450.054343130.9999000160.27568860.27100290.47975710.301520450.290021690.93700130.87155500.86932370.042242200.67347521# Randomly select indices for the training datasettrain_indices<- sample(nrow(data_scaled), floor(0.8* nrow(data_scaled)))
# create train and test setsdata_train<-data_scaled[train_indices, ]
data_test<-data_scaled[-train_indices, ]
# training modeltrained_model<- rbbr_train(data_train,
max_feature=6,
num_cores=8, verbose=TRUE)
trainingprocessstartedwith8computingcores|====================|100%
# use input features from test data for making predictionsdata_test_x<-data_test[ ,1:(ncol(data_test)-1)]
labels<-data_test[ ,ncol(data_test)]
predicted_label_probabilities<- rbbr_predictor(trained_model,
data_test_x,
num_top_rules=1,
slope=10,
num_cores=8, verbose=TRUE)
head(predicted_label_probabilities) # the output from rbbr_predictor() as shown above is the predicted probabilities for target labels (0/1).
[1] 0.971063610.672503820.039865230.987822330.826807080.82235614
head(labels) # true labels
[1] 111111