- RegressionEnsemble Predict Predict responses using ensemble of decision trees for regression (Since R2021a)
Using prediction model inside a matlab function block
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Drew
2023-11-3
MathWorks provides Simulink blocks for machine learning model prediction. So, rather than using a generic MATLAB function block for the model prediction, you can use the native Simulink block designed for model prediction for your random forest model. Check the "Blocks" section of the doc page https://www.mathworks.com/help/stats/code-generation.html.
Given that your model is a random forest trained with fitrensemble, the matching Simulink block is the RegressionEnsemble Predict block:
The sidebar indicates that you are using R2020a. Note that this "RegressionEnsemble Predict" block was added in R2021a.
If this answer helps you, please remember to accept the answer.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Regression Tree Ensembles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!