- 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)?
22 ビュー (過去 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!