%% 清空环境变量
clc;
clear;
warning off
close all
%% 读取数据
res = xlsread('特征选择数据集.xlsx');
%% 分析数据
num_class = length(unique(res(:, end))); % 类别数(Excel最后一列放类别)
num_res = size(res, 1); % 样本数(每一行,是一个样本)
num_size = 0.7; % 训练集占数据集的比例
res = res(randperm(num_res), :); % 打乱数据集(不打乱数据时,注释该行)
flag_conusion = 1; % 标志位为1,打开混淆矩阵(要求2018版本及以上)
%% 设置变量存储数据
P_train = []; P_test = [];
T_train = []; T_test = [];
%% 划分数据集
for i = 1 : num_class
mid_res = res((res(:, end) == i), :); % 循环取出不同类别的样本
mid_size = size(mid_res, 1); % 得到不同类别样本个数
mid_tiran = round(num_size * mid_size); % 得到该类别的训练样本个数
P_train = [P_train; mid_res(1: mid_tiran, 1: end - 1)]; % 训练集输入
T_train = [T_train; mid_res(1: mid_tiran, end)]; % 训练集输出
P_test = [P_test; mid_res(mid_tiran + 1: end, 1: end - 1)]; % 测试集输入
T_test = [T_test; mid_res(mid_tiran + 1: end, end)]; % 测试集输出
end
%% 数据转置
P_train = P_train'; P_test = P_test';
T_train = T_train'; T_test = T_test';
%% 得到训练集和测试样本个数
M = size(P_train, 2);
N = size(P_test , 2);
%% 数据归一化
[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';
%% 特征选择
k = 10; % 保留特征个数
disp(['搜索:'])
disp(['https://mbd.pub/o/DDR1'])
%% 混淆矩阵
%%if flag_conusion == 1
%% figure
%% cm = confusionchart(T_train, T_sim1);
%% cm.Title = 'Confusion Matrix for Train Data';
%% cm.ColumnSummary = 'column-normalized';
%% cm.RowSummary = 'row-normalized';
%% figure
%% cm = confusionchart(T_test, T_sim2);
%% cm.Title = 'Confusion Matrix for Test Data';
%% cm.ColumnSummary = 'column-normalized';
%% cm.RowSummary = 'row-normalized';
%%end