%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 读取数据
res = xlsread('数据集.xlsx');
%% 划分训练集和测试集%
P_train = res(1: 260, 1: 12)';
T_train = res(1: 260, 13)';
M = size(P_train, 2);
P_test = res(261: end, 1: 12)';
T_test = res(261: end, 13)';
N = size(P_test, 2);
f_ = size(P_train, 1); % 特征维度
num_class = length(unique(res(:, end))); % 类别数(Excel最后一列放类别)
%% 数据转置
% P_train = P_train'; P_test = P_test';
% T_train = T_train'; T_test = T_test';
%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input );
t_train = T_train;
t_test = T_test ;
%% 转置以适应模型
p_train = p_train'; p_test = p_test';
t_train = t_train'; t_test = t_test';
%% 参数初始化
pop=6; %种群数量
Max_iter=20; % 设定最大迭代次数
dim = 2;% 维度为2,即优化两个超参数
lb = [1,1];%下边界
ub = [10,f_];%上边界
fobj = @(x) fun(x, p_train, t_train);
[Best_pos,curve,~]=BES(pop,Max_iter,lb,ub,dim,fobj); %开始优化
%% 提取最优参数
n_trees = round(Best_pos.pos(1));
n_layer = round(Best_pos.pos(2));
%% 创建模型
model = classRF_train(p_train, t_train, n_trees, n_layer);
importance = model.importance; % 特征的重要性
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.