使用matlab随机森林计算240个特征的重要性排序代码
以下是使用MATLAB随机森林计算240个特征的重要性排序的示例代码:
%% 读入数据和标签 data = readtable('data.csv'); labels = data.Labels; data = table2array(data(:,1:end-1));
%% 随机森林参数设置 numTrees = 500; % 设置决策树的数量 numFeatures = 'auto'; % 设置每个决策树使用的特征数量
%% 训练随机森林模型 model = TreeBagger(numTrees, data, labels, 'Method', 'classification', ... 'NumPredictorsToSample', numFeatures);
%% 计算特征重要性 importance = model.OOBPermutedPredictorDeltaError;
%% 按重要性排序 [~,idx] = sort(importance,'descend');
%% 输出前10个重要特征 top10 = idx(1:10); data_header = readtable('data.csv', 'ReadVariableNames', false); data_header = data_header(1,1:end-1); top10_features = data_header(top10,:)
%% 可视化特征重要性 figure bar(importance(idx)) title('Feature Importance') xlabel('Feature Index') ylabel('Importance') xticklabels(data_header(idx)) xtickangle(90
原文地址: https://www.cveoy.top/t/topic/diV2 著作权归作者所有。请勿转载和采集!