how can I classify the loads based on Bagging decision trees and compare it with actual data?

1 回表示 (過去 30 日間)
Hello all I need some help, please. I can not obtain the result correctly for the classification and i want to compare it with actual data. I am going to attach the code and I can not attach the data due to the huge size so I will share it via google drive Data
clear all
Traindata = readtable('training_a.csv');
X = [Traindata.I Traindata.THD Traindata.H3 Traindata.H5 Traindata.H7 Traindata.H9];
Y = ordinal(Traindata.Loads);
%% Build bagged decision tree classifer
leaf = 1;
nTrees = 50;
savedRng = rng; % Save the current RNG settings
color = 'bgr';
for ii = 1:length(leaf)
% Reinitialize the random number generator, so that the
% random samples are the same for each leaf size
% Create a bagged decision tree for each leaf size and plot out-of-bag
% error 'oobError'
b = TreeBagger(nTrees,X,Y,'OOBPrediction','on',...
hold on
xlabel('Number of grown trees')
ylabel('Out-of-bag classification error')
title('Classification Error for Different Leaf Sizes')
hold off
%% -Features importance results-
nTrees = 50;
leaf = 1;
b = TreeBagger(nTrees,X,Y,'OOBPredictorImportance','on', ...
'CategoricalPredictors',6, ...
xlabel('Feature number')
ylabel('Out-of-bag feature importance')
title('Feature importance results')
b = compact(b);
%% Load Classification
Testdata = readtable('testing_a.csv');
[predClass,classifScore] = predict(b,[Testdata.I Testdata.THD Testdata.H3 Testdata.H5 Testdata.H7 Testdata.H9]);
for i = 1:6
fprintf(' Current = %5.2f\n',Testdata.I(i));
fprintf(' Total harmonic distortion = %5.2f\n',Testdata.THD(i));
fprintf(' Third harmonic = %2d\n',Testdata.H3(i));
fprintf(' Fifth harmonic = %5.2f\n',Testdata.H5(i));
fprintf(' Seventh harmonic = %5.2f\n',Testdata.H7(i));
fprintf(' Ninth harmonic = %2d\n',Testdata.H9(i));
fprintf(' Predicted Rating : %s\n',predClass{i});
fprintf(' Classification score : \n');
for j = 1:length(b.ClassNames)
if (classifScore(i,j)>0)
fprintf(' %s : %5.4f \n',b.ClassNames{j},classifScore(i,j));
classnames = b.ClassNames;
Preddata = [table(Testdata.Loads,predClass),array2table(classifScore)];
Preddata.Properties.VariableNames = [{'I'},{'Loads'},classnames'];
Actualdata = readtable('testing_aa.csv');
C = confusionchart(Actualdata.Loads,Preddata.Loads);
sortClasses(C,{'1' '2' '3' '4'})
  1 件のコメント
Image Analyst
Image Analyst 2022 年 8 月 25 日
Can you at least post a screenshot of your confusion matrix? What accuracy do you get and what is the min acceptable to you?



the cyclist
the cyclist 2022 年 8 月 26 日
編集済み: the cyclist 2022 年 8 月 26 日
I have not gone through your whole code, but I loaded your MAT file and then ran
C = confusionchart(Actualdata.Loads,Preddata.Loads);
One problem is that Preddata.Loads is a cell array, not a double. So, I converted it :
C = confusionchart(Actualdata.Loads,[Preddata.Loads{:}]-'0'); % This is a TERRIBLE obfuscated way to convert
which gives the confusion matrix
The way I did that conversion is terrible, but it works. I would not do that in real code, but I was lazy. You should instead figure out why some or your labels are doubles, and some are characters, and fix that upstream.
  2 件のコメント
OMAR MOUSA 2022 年 8 月 26 日
Thank you sir for helping me
OMAR MOUSA 2022 年 8 月 26 日
Also, this actually for total power consumption to do the classification. Is there any way to get the actual signal of current (I) and compare it with predicted signal (I) for each class or load, please if you can help.


その他の回答 (0 件)


Help Center および File ExchangeAI for Signals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by