random forest block in simulink

3 views (last 30 days)
insherah
insherah on 4 Mar 2025
Commented: insherah on 6 Mar 2025

I need guidance how to create random forest block in simulink My coding use tree bagger for classification

Accepted Answer

aditi bagora
aditi bagora on 6 Mar 2025
I understand that you want to use a trained Random Forest model for prediction in Simulink, utilize the ClassificationEnsemble Predict block.
  1. If you already have a trained model in your workspace, simply load it via the 'Select trained machine learning model' option in the block parameters.
  2. If you don't have a trained model yet, you can refer to the follwowing example: https://www.mathworks.com/help/stats/predict-class-labels-using-classification-ensemble-predict-block.html
For further details, explore these MathWorks documentation links on the ClassificationEnsemble Predict block and ClassificationBaggedEnsemble:
  1 Comment
insherah
insherah on 6 Mar 2025

% %Random Forest % Load feature and target tables Input = xlsread('Feature Table (LL_LLG_LG_LLL) x2.xlsx'); Target = xlsread('Target Table (LL_LLG_LG_LLL) x2.xlsx');

% Split ratio ratio = 0.8; Data_size = size(Input, 1); % Number of rows Shuffle = randperm(Data_size); % Randomize indices

% Training data Train_Size = round(ratio * Data_size); X_train = Input(Shuffle(1:Train_Size), :); Y_train = Target(Shuffle(1:Train_Size), :);

% Testing data X_test = Input(Shuffle(Train_Size+1:end), :); Y_test = Target(Shuffle(Train_Size+1:end), :);

% Train the model using TreeBagger TreeModel = TreeBagger(200, X_train, Y_train, 'Method', 'classification'); % Model = fitcensemble(X_train, Y_train, 'Method', 'Bag', 'NumLearningCycles', 10);

% Predict using the trained model predictions = predict(TreeModel, X_test);

% Convert predictions to numeric (TreeBagger outputs strings for classification) predictions = str2double(predictions);

% Calculate accuracy accuracy = sum(predictions == Y_test) / numel(Y_test) * 100; fprintf('Model Accuracy: %.2f%%\n', accuracy);

This is our trained model of data set 972 x 24 and when it is trained it gives 95% accuracy not 100% . At first it was 100% when in target data we had only fault type but as we added bus location also in single model of target accuracy drop

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!