KNORA-Union (KNORA-U)

We can evaluate a KNORA-Union model on the synthetic dataset.

In this case, we will use default model hyperparameters, including bagged decision trees as the pool of classifier models and a k=7 for the selection of the local neighborhood when making a prediction.

We will evaluate the model using repeated stratified k-fold cross-validation with three repeats and 10 folds. We will report the mean and standard deviation of the accuracy of the model across all repeats and folds.

The complete example is listed below.

evaluate dynamic KNORA-U dynamic ensemble selection for binary classification

from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from deslib.des.knora_u import KNORAU

define dataset

X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)

define the model

model = KNORAU()

define the evaluation procedure

cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)

evaluate the model

n_scores = cross_val_score(model, X, y, scoring=‘accuracy’, cv=cv, n_jobs=-1)

report performance

print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

evaluate dynamic KNORA-U dynamic ensemble selection for binary classification

from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from deslib.des.knora_u import KNORAU

define dataset

X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)

define the model

model = KNORAU()

define the evaluation procedure

cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)

evaluate the model

n_scores = cross_val_score(model, X, y, scoring=‘accuracy’, cv=cv, n_jobs=-1)

report performance

print(‘Mean Accuracy: %.3f (%.3f)’ % (mean(n_scores), std(n_scores)))
Running the example reports the mean and standard deviation accuracy of the model.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

In this case, we can see the KNORA-U dynamic ensemble selection model and default hyperparameters achieve a classification accuracy of about 93.3 percent.

Mean Accuracy: 0.933 (0.009)
1
Mean Accuracy: 0.933 (0.009)
We can also use the KNORA-U model as a final model and make predictions for classification.

First, the model is fit on all available data, then the predict() function can be called to make predictions on new data.

The example below demonstrates this on our binary classification dataset.

make a prediction with KNORA-U dynamic ensemble selection

from sklearn.datasets import make_classification
from deslib.des.knora_u import KNORAU

define dataset

X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)

define the model

model = KNORAU()

fit the model on the whole dataset

model.fit(X, y)

make a single prediction

row = [0.2929949,-4.21223056,-1.288332,-2.17849815,-0.64527665,2.58097719,0.28422388,-7.1827928,-1.91211104,2.73729512,0.81395695,3.96973717,-2.66939799,3.34692332,4.19791821,0.99990998,-0.30201875,-4.43170633,-2.82646737,0.44916808]
yhat = model.predict([row])
print(‘Predicted Class: %d’ % yhat[0])
1
2
3
4
5
6
7
8
9
10
11
12
13

make a prediction with KNORA-U dynamic ensemble selection

from sklearn.datasets import make_classification
from deslib.des.knora_u import KNORAU

define dataset

X, y = make_classification(n_samples=10000, n_features=20, n_informative=15, n_redundant=5, random_state=7)

define the model

model = KNORAU()

fit the model on the whole dataset

model.fit(X, y)

make a single prediction

row = [0.2929949,-4.21223056,-1.288332,-2.17849815,-0.64527665,2.58097719,0.28422388,-7.1827928,-1.91211104,2.73729512,0.81395695,3.96973717,-2.66939799,3.34692332,4.19791821,0.99990998,-0.30201875,-4.43170633,-2.82646737,0.44916808]
yhat = model.predict([row])
print(‘Predicted Class: %d’ % yhat[0])
Running the example fits the KNORA-U model on the entire dataset and is then used to make a prediction on a new row of data, as we might when using the model in an application.

Predicted Class: 0
1
Predicted Class: 0