Hierarchical classification with two models

3 views (last 30 days)
I haved 2 classification models. In 1st model I can classify between class 1 and class 2. Now from class 2 I want to classify class 3 and class 4 with model 2. How can I do that in matlab. pls help

Answers (1)

Shantanu Dixit
Shantanu Dixit on 9 Sep 2024
Hi Supriya, hierarchical classification with two models can be done given we have ground truth data for both classes 3 and 4. Without the ground truth for classes 3 and 4 it is not possible to train or validate the second classifier.
  • Train two models: Train two separate models to classify between classes 1,2 and classes 3,4 respectively.
  • Classification step: Using model 1 make predictions and classify input data into either class 1 or class 2. For the data points classified as class 2, use model 2 to further classify them into class 3 and class 4 using model 2.
Here's a high level implementation of the above steps (assuming pre-trained models 1 and 2)
% Assume you have trained classifiers 'model1' and 'model2'
% Input data - X (e.g., feature matrix X)
pred_model1 = predict(model1, X); % (class 1 vs class 2) using model 1
% Find instances classified as class 2
class2_indices = find(pred_model1 == 2);
% Classify these instances using model 2
if ~isempty(class2_indices)
X_class2 = X(class2_indices, :); % Extract features for class 2
pred_model2 = predict(model2, X_class2); % Classify into class 3 or 4
pred_model1(class2_indices) = pred_model2 + 2; % Add 2 to shift to class 3 or 4
end
% pred_model1 now contains class 1, 3, and 4
Refer to the 'predict' function in MATLAB for more information:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!