- Train Your Random Forest Model: You already have this step covered with your existing code.
- Calculate Feature Importance: Use the OOBPermutedPredictorDeltaError property to get the importance of each feature.
How to compute Gini impurity in random forest (treebagger)?
7 次查看(过去 30 天)
显示 更早的评论
Hello. I am doing classification using treebagger random forest. I should compute the gini index or gini impurity to understand each feature importance in classification. Can any one help me with that? thanks. Here is my random forest classifier code:
nTrees=50;
B2 = TreeBagger(nTrees,train_data,train_label, 'Method', 'classification');
predChar2 = B2.predict(test_data);
predictedClass2 = str2double(predChar2);
0 个评论
回答(1 个)
Shubham
2024-9-6
Hi Alex,
To compute feature importance using the Gini index (or Gini impurity) in a random forest model with MATLAB's TreeBagger, you can use the OOBPermutedPredictorDeltaError property. This property provides a measure of feature importance based on the increase in prediction error when the values of a given feature are permuted. Here's how you can do it:
Step-by-Step Guide to Compute Feature Importance
Here's how you can modify your code to include feature importance calculation:
% Number of trees
nTrees = 50;
% Train the TreeBagger model
B2 = TreeBagger(nTrees, train_data, train_label, 'Method', 'classification', 'OOBPredictorImportance', 'on');
% Predict the test data
predChar2 = B2.predict(test_data);
predictedClass2 = str2double(predChar2);
% Compute feature importance
featureImportance = B2.OOBPermutedPredictorDeltaError;
% Display feature importance
fprintf('Feature Importance (using Gini index):\n');
for i = 1:length(featureImportance)
fprintf('Feature %d: %.4f\n', i, featureImportance(i));
end
% Plot feature importance
figure;
bar(featureImportance);
title('Feature Importance (Gini Index)');
xlabel('Feature Number');
ylabel('Importance');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!