To evaluate the accuracy and the confusion matrix of a random forest model, we can use the below code.
load census1994
Mdl1 = fitcensemble(adultdata,'salary')
rng(1) % For reproducibility
[pX,pIdx] = datasample(adultdata,5);
label = predict(Mdl1,pX);
% To calculate accuracy
accuracy = mean(label == adultdata.salary(pIdx))
% confusion chart
cm = confusionchart(label, adultdata.salary(pIdx));
The data and code used for illustration can be obtained by running the following command from the Command Window.
openExample('stats/EstimateResubstitutionLossOfBoostingEnsembleExample')
Hope this helps!
