I understand that you are trying to create an ensemble of Naive Baye's classifiers. MATLAB's "fitensemble" function doesn't directly support creating ensembles with Naive Bayes classifiers, but we can manually implement an ensemble.
The steps for creating an ensemble of Naive Bayes classifiers are as follows:
- Create multiple bootstrap samples from your training data.
- Train a Naive Bayes model on each bootstrap sample.
- Use majority voting or averaging to combine predictions from all models.
Here for illustration, I will use "fisheriris" dataset. And I have used the number of ensembles to be 10. Please refer to the below code for the implementation.
% Load your data (using the fisheriris dataset as an example)
load fisheriris
X = meas; % Predictor variables
Y = species; % Response variable
% Parameters
numModels = 10; % Number of models in the ensemble
n = size(X, 1);
predictions = cell(n, numModels); % Use a cell array to store predictions
% Train multiple Naive Bayes models
for i = 1:numModels
% Create a bootstrap sample
idx = randsample(n, n, true);
X_bootstrap = X(idx, :);
Y_bootstrap = Y(idx);
% Train Naive Bayes model
nbModel = fitcnb(X_bootstrap, Y_bootstrap);
% Predict on the entire dataset
predictions(:, i) = predict(nbModel, X);
end
% Convert cell array of predictions to a categorical array
predictionsCategorical = categorical(predictions);
% Combine predictions using majority voting
finalPredictions = mode(predictionsCategorical, 2);
% Calculate accuracy
accuracy = sum(finalPredictions == categorical(Y)) / n;
fprintf('Ensemble Accuracy: %.2f%%\n', accuracy * 100);
For more information on "fitncb" function, please refer to the following link.
Hope you find this information helpful!
