Using prediction model inside a matlab function block
Show older comments
This is the content in the prediction file.
data1new=readtable('processed_data.csv');
X1 = data1new{:,1:end-1};
Y1 = data1new{:,end};
% Train a Random Forest ensemble using all the data
ensnew = fitrensemble(X1,Y1,'Method','bag','NumLearningCycles',150,'Learners','tree');
NH5pred=predict(ensnew,X1);
%saving the model for future use.
save('NH5predModel.mat','ensnew');
The image shows the data being collected and the MUX block delivers the input to the function block.
Inside the matlab function block:
function y = Regpred(inputt)
% Load the regression model
loadedData = load('NH5predModel.mat', 'ensnew');
ensnewLoaded = loadedData.ensnew;
% Ensure input has 8 columns
if size(inputt, 2) ~= 8
error('Input must have 8 columns.');
end
% Preallocate the output variable y based on the size of input
numRows = size(inputt, 1);
y = zeros(numRows, 1); % Assuming a single output value per row of input
% Make predictions using the loaded model
for i = 1:numRows
y(i) = predict(ensnewLoaded, inputt(i, :));
end
The outport the matlab function block is a To workspace block.
The error encountered is also attached. Please help!
Accepted Answer
More Answers (0)
Categories
Find more on Regression Tree Ensembles 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!