add error plot for this code

1 view (last 30 days)
Nandhu mol M. V
Nandhu mol M. V on 16 Jun 2023
Commented: Ronit on 21 Jun 2023

i want to add error plot for this code.Can you help?

% Load the engine_dataset
load engine_dataset
% Prepare the data
X = engineInputs.';
Y= engineTargets.';
% Split the data into training and testing sets
cv = cvpartition(size(X,1),'HoldOut',0.2);
Xtrain = X(cv.training,:);
Ytrain = Y(cv.training,:);
Xtest = X(cv.test,:);
Ytest = Y(cv.test,:);
% Train the Gradient Boosting model
Model1 = fitrensemble(Xtrain,Ytrain(:,1),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
Model2 = fitrensemble(Xtrain,Ytrain(:,2),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
sampleTime = 1;
numSteps = 1199;
time = sampleTime*(0:numSteps-1);
time = time';
simin = timeseries(X,time);
simout = timeseries(Y,time);
model = 'SimMdlName_V1';
system_under_design = [model '/Gradient Boost Predict Model'];
baseline_output = [model '/yarr'];
open_system(model);
hdlsetup(model);
loggingInfo = get_param(model, 'DataLoggingOverride');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression before conversion');
opts = fxpOptimizationOptions();
opts.addTolerance(system_under_design, 1, 'RelTol', 0.05);
opts.addTolerance(system_under_design, 1, 'AbsTol', 50)
opts.AllowableWordLengths = 8:32;
solution = fxpopt(model, system_under_design, opts);
best_solution = solution.explore;
set_param(model, 'DataLoggingOverride', loggingInfo);
Simulink.sdi.markSignalForStreaming([model '/yarr'], 1, 'on');
Simulink.sdi.markSignalForStreaming([model '/diff'], 1, 'on');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression after conversion');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression after function replacement');
systemname = 'SimMdlName_V1/Gradient Boost Predict Model';
workingdir = 'C:/Temp/hdlsrc';
checkhdl(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');
makehdl(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');
makehdltb(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');

Answers (1)

Ronit
Ronit on 16 Jun 2023
Hi,
To add an error plot, you first need to calculate the model's predictions on the test data.
% Predict on test data and calculate errors
Ypred1 = predict(Model1, Xtest);
Ypred2 = predict(Model2, Xtest);
error1 = Ytest(:,1) - Ypred1;
error2 = Ytest(:,2) - Ypred2;
% Plot the errors
figure;
subplot(2,1,1);
plot(error1);
title('Error in y1');
ylabel('Error');
subplot(2,1,2);
plot(error2);
title('Error in y2');
ylabel('Error');
xlabel('Sample');
This code shows two subplots, one for each output variable ('y1' and 'y2'), showing the errors between the true output ('Ytest') and the model's predictions for the test data. You can adjust the subplot parameters and plot settings as desired.
Hope this helps.
  2 Comments
Nandhu mol M. V
Nandhu mol M. V on 16 Jun 2023
Thank you for your support. This code have some errors which is given below. Can you resolve this?
% Load the engine_dataset
load engine_dataset
% Prepare the data
X = engineInputs.';
Y= engineTargets.';
% Split the data into training and testing sets
cv = cvpartition(size(X,1),'HoldOut',0.2);
Xtrain = X(cv.training,:);
Ytrain = Y(cv.training,:);
Xtest = X(cv.test,:);
Ytest = Y(cv.test,:);
% Train the Gradient Boosting model
Model1 = fitrensemble(Xtrain,Ytrain(:,1),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
Model2 = fitrensemble(Xtrain,Ytrain(:,2),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
sampleTime = 1;
numSteps = 1199;
time = sampleTime*(0:numSteps-1);
time = time';
simin = timeseries(X,time);
simout = timeseries(Y,time);
model = 'SimMdlName_V1';
system_under_design = [model '/Gradient Boost Predict Model'];
baseline_output = [model '/yarr'];
open_system(model);
% Predict on test data and calculate errors
Ypred1 = predict(Model1, Xtest);
Ypred2 = predict(Model2, Xtest);
error1 = Ytest(:,1) - Ypred1;
error2 = Ytest(:,2) - Ypred2;
% Plot the errors
figure;
subplot(2,1,1);
plot(error1);
title('Error in y1');
ylabel('Error');
subplot(2,1,2);
plot(error2);
title('Error in y2');
ylabel('Error');
xlabel('Sample');
hdlsetup(model);
loggingInfo = get_param(model, 'DataLoggingOverride');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression before conversion');
opts = fxpOptimizationOptions();
opts.addTolerance(system_under_design, 1, 'RelTol', 0.05);
opts.addTolerance(system_under_design, 1, 'AbsTol', 50)
opts.AllowableWordLengths = 8:32;
solution = fxpopt(model, system_under_design, opts);
best_solution = solution.explore;
set_param(model, 'DataLoggingOverride', loggingInfo);
Simulink.sdi.markSignalForStreaming([model '/yarr'], 1, 'on');
Simulink.sdi.markSignalForStreaming([model '/diff'], 1, 'on');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression after conversion');
sim_out = sim(model, 'SaveFormat', 'Dataset');
plotRegression(sim_out, baseline_output, system_under_design, 'Regression after function replacement');
systemname = 'SimMdlName_V1/Gradient Boost Predict Model';
workingdir = 'C:/Temp/hdlsrc';
checkhdl(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');
makehdl(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');
makehdltb(systemname,'TargetDirectory',workingdir,'TargetLanguage','Verilog');
>> gr_try
### The configuration parameter values use the recommended settings for HDL code generation and does not need any modification as a part of hdlsetup. Please refer to hdlsetup document for best practices on model settings.
Error using gr_try
Error due to multiple causes.
Caused by:
Error using gr_try
Data type mismatch. 'Input Port 1' of 'SimMdlName_V1/Gradient Boost Predict Model/RegressionEnsemble
Predict/aggregatePredict/Data Store Write' expects a signal of data type 'sfix30_En21'. However, it is driven by a
signal of data type 'double'.
Error using gr_try
Data type mismatch. 'Output Port 1' of 'SimMdlName_V1/Gradient Boost Predict Model/RegressionEnsemble
Predict/aggregatePredict/Constant' is a signal of data type 'double'. However, it is driving a signal of data type
'sfix30_En21'.
Ronit
Ronit on 21 Jun 2023
It seems like there are some errors related to the mismatch in data type. Specifically, the 'Input Port 1' and 'Output Port 1' of the 'RegressionEnsemble Predict' block in the 'Gradient Boost Predict Model' block is expecting a signal of data type 'sfix30_En21' while it is driven by a signal of data type 'double'.
This may happen when there is a mismatch in the data types used in the Simulink model and the data types required by the HDL code generation process.
To resolve the error, you could try changing the data types of the inputs and outputs in the Simulink model to 'sfix30_En21' or updating the HDL code generation settings to match the data types used in the Simulink model.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!