Using probability and membership values of classes for prediction

This example shows how to use probability and membership values of class labels for prediction when applying single hyperbox-based models, ensemble models of hyperbox-based classifiers, and multigranular hyperbox-based models. We employ the original online learning algorithm for general fuzzy min-max neural network, accelerated agglomerative learning algorithm for general fuzzy min-max neural network, Simpson’s online learning agorithm for fuzzy min-max neural network, bagging of hyperbox-based models, and multigranular hyperbox-based models for demostration in this tutorial.

[1]:
%matplotlib notebook
[2]:
import os
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np

Load training and testing datasets

[3]:
# Get the path to the this jupyter notebook file
this_notebook_dir = os.path.dirname(os.path.abspath("__file__"))
this_notebook_dir
[3]:
'C:\\hyperbox-brain\\examples\\other_learning_ability_gfmm'
[4]:
# Get the home folder of the hyperbox-brain toolbox
from pathlib import Path
project_dir = Path(this_notebook_dir).parent.parent
project_dir
[4]:
WindowsPath('C:/hyperbox-brain')
[5]:
# Create the path to the training and testing files
training_file = os.path.join(project_dir, Path("dataset/syn_num_train.csv"))
testing_file = os.path.join(project_dir, Path("dataset/syn_num_test.csv"))
[6]:
# Create training and testing data sets
df_train = pd.read_csv(training_file, header=None)
df_test = pd.read_csv(testing_file, header=None)

Xy_train = df_train.to_numpy()
Xy_test = df_test.to_numpy()

Xtr = Xy_train[:, :-1]
ytr = Xy_train[:, -1]

Xtest = Xy_test[:, :-1]
ytest = Xy_test[:, -1]

1. Original online learning algorithm for General fuzzy min-max neural network (Onln-GFMM)

[7]:
from hbbrain.numerical_data.incremental_learner.onln_gfmm import OnlineGFMM
[8]:
# Initializing parameters
theta = 0.1
theta_min = 0.1
gamma = 4
is_draw = False

Train a model

[9]:
onln_gfmm_clf = OnlineGFMM(theta=theta, theta_min=theta_min, gamma=gamma, is_draw=is_draw)
onln_gfmm_clf.fit(Xtr, ytr)
[9]:
OnlineGFMM(C=array([1, 2, 1, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1,
       1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1, 1,
       2, 2, 1, 2, 2, 2, 2, 2, 1]),
           V=array([[0.42413   , 0.53516   ],
       [0.70577   , 0.397105  ],
       [0.82785   , 0.78025   ],
       [0.66038   , 0.51128   ],
       [0.48794   , 0.672     ],
       [0.26651   , 0.18424   ],
       [0.32289   , 0.60194   ],
       [0.19944   , 0.03      ],
       [0.29343   , 0.28975   ],
       [0.63683   , 0.6936    ],
       [0.32906   , 0.55512   ],
       [0.03      , 0.47757   ],
       [0.54...
       [0.815     , 0.397095  ],
       [0.67906   , 0.83605   ],
       [0.37033   , 0.26124   ],
       [0.52197   , 0.91371   ],
       [0.66037   , 0.57837   ],
       [0.52621   , 0.66846   ],
       [0.80583   , 0.43242   ],
       [0.79935   , 0.7757    ],
       [0.35813   , 0.58772   ],
       [0.79516   , 0.32629   ],
       [0.70743   , 0.50325   ],
       [0.36057   , 0.71561   ],
       [0.72496   , 0.38674   ],
       [0.28822   , 0.62174   ],
       [0.14737   , 0.28498   ],
       [0.56487   , 0.17003   ],
       [0.68469   , 0.2221    ],
       [0.55763   , 0.43813   ]]),
           gamma=4, theta=0.1, theta_min=0.1)

Make prediction

Use probability values and membership values to make prediction for the first ten testing samples. The orders of columns are also the orders of class labels in an ascending order. In this example, the first column contains the predicted values for class 1, and the second column contains the predicted values for class 2.

The predicted class probability is the fraction of the membership value of the representative hyperbox of that class and the sum of all membership values of all representative hyperboxes of all classes. The predicted class membership value is the membership value of the representative hyperbox of that class.

[10]:
print("Input classes: ", np.unique(ytr))
Input classes:  [1. 2.]
[11]:
onln_gfmm_clf.predict_proba(Xtest[:10])
[11]:
array([[0.50044451, 0.49955549],
       [0.48259685, 0.51740315],
       [0.42006751, 0.57993249],
       [0.52674382, 0.47325618],
       [0.46011316, 0.53988684],
       [0.50352398, 0.49647602],
       [0.49915114, 0.50084886],
       [0.3190052 , 0.6809948 ],
       [0.50079564, 0.49920436],
       [0.44152243, 0.55847757]])
[12]:
onln_gfmm_clf.predict_with_membership(Xtest[:10])
[12]:
array([[0.95696  , 0.95526  ],
       [0.76536  , 0.82056  ],
       [0.72176  , 0.99644  ],
       [0.94304  , 0.84728  ],
       [0.85224  , 1.       ],
       [0.96876  , 0.9552   ],
       [0.92908  , 0.93224  ],
       [0.46844  , 1.       ],
       [1.       , 0.9968225],
       [0.78976  , 0.99896  ]])

2. Accelerated agglomerative learning algorithm for General fuzzy min-max neural network (AGGLO-2)

[13]:
from hbbrain.numerical_data.batch_learner.accel_agglo_gfmm import AccelAgglomerativeLearningGFMM
[14]:
# Initializing parameters
theta=0.1
gamma=4
min_simil=0
simil_measure='long'
is_draw=False

Train a model

[15]:
agglo2_gfmm_clf = AccelAgglomerativeLearningGFMM(theta=theta, gamma=gamma, min_simil=min_simil, simil_measure=simil_measure, is_draw=is_draw)
agglo2_gfmm_clf.fit(Xtr, ytr)
[15]:
AccelAgglomerativeLearningGFMM(gamma=4, min_simil=0, simil_measure='long',
                               theta=0.1)

Make prediction

[16]:
agglo2_gfmm_clf.predict_proba(Xtest[:10])
[16]:
array([[0.50882641, 0.49117359],
       [0.47279466, 0.52720534],
       [0.42148046, 0.57851954],
       [0.56160076, 0.43839924],
       [0.46758668, 0.53241332],
       [0.50352398, 0.49647602],
       [0.49915114, 0.50084886],
       [0.20099716, 0.79900284],
       [0.49770692, 0.50229308],
       [0.44615176, 0.55384824]])
[17]:
agglo2_gfmm_clf.predict_with_membership(Xtest[:10])
[17]:
array([[0.95696, 0.92376],
       [0.76536, 0.85344],
       [0.72176, 0.99068],
       [0.94304, 0.73616],
       [0.87824, 1.     ],
       [0.96876, 0.9552 ],
       [0.92908, 0.93224],
       [0.25156, 1.     ],
       [0.9116 , 0.92   ],
       [0.78976, 0.9804 ]])

3. Original online learning algorithm for Simpson’s Fuzzy min-max neural network (FMNN)

[18]:
from hbbrain.numerical_data.incremental_learner.fmnn import FMNNClassifier
[19]:
# Initializing parameters
theta = 0.1
gamma = 4
is_draw = False

Train a model

[20]:
fmnn_clf = FMNNClassifier(theta=theta, gamma=gamma, is_draw=is_draw)
fmnn_clf.fit(Xtr, ytr)
[20]:
FMNNClassifier(C=array([1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2,
       2, 2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1,
       2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2,
       2, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 2, 2, 2, 1, 1, 1, 1, 2,
       1, 1, 1]),
               V=array([[0.36239   , 0.55942   ],
       [0.64082   , 0.43016875],
       [0.91059   , 0.82085   ],
       [0.65328   , 0.50326   ],
       [0.46107   , 0.68306   ],
       [0.29812   , 0.18424   ],
       [0.33593   , 0.68775   ],
       [0.1...
       [0.25621   , 0.62174   ],
       [0.42403   , 0.7592    ],
       [0.79157   , 0.59996   ],
       [0.72496   , 0.34978   ],
       [0.36842   , 0.76576   ],
       [0.73681   , 0.71261   ],
       [0.66773   , 0.31155   ],
       [0.32289   , 0.6747    ],
       [0.28077   , 0.27116   ],
       [0.61106   , 0.28476   ],
       [0.75421   , 0.40498   ],
       [0.38038   , 0.67232   ],
       [0.36745   , 0.52006   ],
       [0.91185   , 0.48697   ],
       [0.35813   , 0.58584   ],
       [0.25924   , 0.42696   ],
       [0.70685   , 0.64383   ],
       [0.75047   , 0.6092    ],
       [0.72842   , 0.61048   ]]),
               gamma=4, theta=0.1)

Make prediction

[21]:
fmnn_clf.predict_proba(Xtest[:10])
[21]:
array([[0.5001282 , 0.4998718 ],
       [0.49113606, 0.50886394],
       [0.47048553, 0.52951447],
       [0.51190571, 0.48809429],
       [0.49033913, 0.50966087],
       [0.5028714 , 0.4971286 ],
       [0.49750079, 0.50249921],
       [0.41923606, 0.58076394],
       [0.49781361, 0.50218639],
       [0.47269621, 0.52730379]])
[22]:
fmnn_clf.predict_with_membership(Xtest[:10])
[22]:
array([[0.9801625 , 0.97966   ],
       [0.92338   , 0.95671   ],
       [0.8885225 , 1.        ],
       [0.96807   , 0.92304   ],
       [0.96158875, 0.99948   ],
       [1.        , 0.98858   ],
       [0.97989   , 0.989735  ],
       [0.72187   , 1.        ],
       [0.9912925 , 1.        ],
       [0.89644   , 1.        ]])

4. Bagging of base general fuzzy min-max neural networks trained by the original online learning algorithm

[23]:
from hbbrain.numerical_data.ensemble_learner.decision_comb_bagging import DecisionCombinationBagging
[24]:
# Initialise parameters
n_estimators = 20 # number of base learners
max_samples = 0.5 # sampling rate for samples
bootstrap = False # random subsampling without replacement
class_balanced = False # do not use the class-balanced sampling mode
n_jobs = 4 # number of processes is used to build base learners

Train a bagging model

[25]:
# Init a hyperbox-based model used to train base learners
# Using the GFMM classifier with the original online learning algorithm with the maximum hyperbox size 0.1
base_estimator = OnlineGFMM(theta=theta, gamma=gamma)
[26]:
dc_bagging_subsampling = DecisionCombinationBagging(base_estimator=base_estimator, n_estimators=n_estimators, max_samples=max_samples, bootstrap=bootstrap, class_balanced=class_balanced, n_jobs=n_jobs, random_state=0)
[27]:
dc_bagging_subsampling.fit(Xtr, ytr)
[27]:
DecisionCombinationBagging(base_estimator=OnlineGFMM(C=array([], dtype=float64),
                                                     V=array([], dtype=float64),
                                                     W=array([], dtype=float64),
                                                     gamma=4, theta=0.1),
                           n_estimators=20, n_jobs=4, random_state=0)

Make prediction

This example shows how to use predict_proba and predict_with_membership functions to make prediction. The predicted class probabilities of an input sample with respect to an ensemble model are computed as the mean predicted class probabilities of the hyperbox-based learners in the ensemble model. The class probability of a single hyperbox-based learner is the fraction of the membership value of the representative hyperbox of that class and the sum of all membership values of all representative hyperboxes of all classes.

The predicted class memberships of an input sample are computed as the mean predicted class memberships of the hyperbox-based learners in the ensemble model. The class membership of a single hyperbox-based learner is the membership from the input X to the representative hyperbox of that class to join the prediction procedure.

[28]:
dc_bagging_subsampling.predict_proba(Xtest[:10])
[28]:
array([[0.47815396, 0.52184604],
       [0.5012498 , 0.4987502 ],
       [0.3955224 , 0.6044776 ],
       [0.54362581, 0.45637419],
       [0.47159695, 0.52840305],
       [0.52292342, 0.47707658],
       [0.49756119, 0.50243881],
       [0.1964586 , 0.8035414 ],
       [0.47362699, 0.52637301],
       [0.39199205, 0.60800795]])
[29]:
dc_bagging_subsampling.predict_with_membership(Xtest[:10])
[29]:
array([[0.843956  , 0.91573   ],
       [0.818432  , 0.810635  ],
       [0.639162  , 0.963144  ],
       [0.87822   , 0.7370675 ],
       [0.858645  , 0.958337  ],
       [0.98751575, 0.90252725],
       [0.916385  , 0.927697  ],
       [0.232785  , 0.932006  ],
       [0.86215975, 0.953138  ],
       [0.6373855 , 0.977412  ]])

5. Multi-resolution Hierarchical Granular Representation based Classifier using GFMM

[30]:
from hbbrain.numerical_data.multigranular_learner.multi_resolution_gfmm import MultiGranularGFMM
[31]:
# Initializing parameters
# number of disjoint partitions to build base learners
n_partitions = 4
# a list of maximum hyperbox sizes for granularity levels
granular_theta = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
# minimum membership values between two hyperboxes aggregated at higher abstraction levels
min_membership_aggregation = 0.1
# the speed of decreasing of membership values
gamma = 4

Training a multigranular model

[32]:
from hbbrain.constants import HETEROGENEOUS_CLASS_LEARNING
multi_granular_gfmm_clf = MultiGranularGFMM(n_partitions=n_partitions, granular_theta=granular_theta, gamma=gamma, min_membership_aggregation=min_membership_aggregation)
# Training using the heterogeneous model for class labels.
multi_granular_gfmm_clf.fit(Xtr, ytr, learning_type=HETEROGENEOUS_CLASS_LEARNING)
[Parallel(n_jobs=4)]: Using backend LokyBackend with 4 concurrent workers.
[Parallel(n_jobs=4)]: Done   2 out of   4 | elapsed:    2.7s remaining:    2.7s
[Parallel(n_jobs=4)]: Done   4 out of   4 | elapsed:    2.7s finished
[32]:
MultiGranularGFMM(gamma=4, granular_theta=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
                  min_membership_aggregation=0.1)

Make prediction

The predicted class probability at a given granularity level is the fraction of the membership value of the representative hyperbox of that class at the given granularity level and the sum of all membership values of all representative hyperboxes of all classes joining the prediction procedure. If the given granularity level gets the values of -1, then the predicted class probability value for each sample is the average of probability values at all available granularity levels. Similar meaning is applied for the predicted class membership values.

[33]:
# Predicted class probability values for the first ten samples based on the average values of all available granularity levels
multi_granular_gfmm_clf.predict_proba(Xtest[:10], level=-1)
[33]:
array([[0.51102808, 0.48897192],
       [0.54380893, 0.45619107],
       [0.39490266, 0.60509734],
       [0.54865927, 0.45134073],
       [0.46152604, 0.53847396],
       [0.52639798, 0.47360202],
       [0.48346396, 0.51653604],
       [0.2453813 , 0.7546187 ],
       [0.45120077, 0.54879923],
       [0.44126587, 0.55873413]])
[34]:
# Predicted class probability values for the first ten samples at the second granularity level
multi_granular_gfmm_clf.predict_proba(Xtest[:10], level=1)
[34]:
array([[0.51670508, 0.48329492],
       [0.54928154, 0.45071846],
       [0.38979741, 0.61020259],
       [0.54133645, 0.45866355],
       [0.45073053, 0.54926947],
       [0.51150372, 0.48849628],
       [0.47904728, 0.52095272],
       [0.20099716, 0.79900284],
       [0.43810123, 0.56189877],
       [0.44126587, 0.55873413]])
[35]:
# Predicted class membership values for the first ten samples based on the average values of all available granularity levels
multi_granular_gfmm_clf.predict_with_membership(Xtest[:10], level=-1)
[35]:
array([[0.98879333, 0.94611667],
       [0.97816   , 0.82056   ],
       [0.65262667, 1.        ],
       [0.9936    , 0.81736   ],
       [0.8571    , 1.        ],
       [1.        , 0.89970333],
       [0.93597333, 1.        ],
       [0.32505333, 0.99963333],
       [0.82216   , 1.        ],
       [0.78976   , 1.        ]])
[36]:
# Predicted class membership values for the first ten samples at the second granularity level
multi_granular_gfmm_clf.predict_with_membership(Xtest[:10], level=1)
[36]:
array([[1.     , 0.93534],
       [1.     , 0.82056],
       [0.6388 , 1.     ],
       [1.     , 0.84728],
       [0.8206 , 1.     ],
       [1.     , 0.95502],
       [0.91956, 1.     ],
       [0.25156, 1.     ],
       [0.77968, 1.     ],
       [0.78976, 1.     ]])