## （1）数据集的划分

```% 训练集与测试集的划分
[m,n] = size(Sample);         % 数据的大小相等
ratio = 2/3;                  % 训练数据比例
m_train = round(m*ratio);      % 训练数据行数
train_sample = Sample(1:m_train,1:5);    % 训练集和测试集的样本
test_sample = Sample(m_train+1:m,1:5);
train_labels = Sample(1:m_train,6);      % 训练集和测试集的样本标签
test_labels = Sample(m_train+1:m,6);```

## （2）数据归一化

B= mapminmax(A,0,1);

A为原始数据，B为归一化后的数据。

```% 将训练集和测试集的样本分别归一化到[0,1]区间
[train_scale,ps] = mapminmax(train_sample',0,1);
train_x = train_scale';
test_scale = mapminmax('apply',test_sample',ps);
test_x = test_scale';```

ps是一个结构体，里面存放的是数据归一化的规则。为了使测试样本的归一化与训练样本保持一致，就可以用到ps。

```% 将训练集和测试集的样本标签分别归一化到[0,1]区间
[train_lab,ps] = mapminmax(train_labels',0,1);
train_y = train_lab';
test_lab = mapminmax('apply',test_labels',ps);
test_y = test_lab';```

## （3）SVM训练与预测

```cmd = ('-s 3 -t 2 -b 1');
model = svmtrain(train_y,train_x,cmd); %#ok<SVMTRAIN>
[predict_y,accuracy,prob_estimates] = svmpredict(test_y,test_x,model);```

## （4）预测数据的反归一化

```% 反归一化
predict_labels = mapminmax('reverse',predict_y,ps);```

## 三、MATLAB代码

```clc;
clear;
close all;
% 训练集与测试集的划分
[m,n] = size(Sample);         % 数据的大小相等
ratio = 2/3;                  % 训练数据比例
m_train = round(m*ratio);      % 训练数据行数
train_sample = Sample(1:m_train,1:5);    % 训练集和测试集的样本
test_sample = Sample(m_train+1:m,1:5);
train_labels = Sample(1:m_train,6);      % 训练集和测试集的样本标签
test_labels = Sample(m_train+1:m,6);
% 将训练集和测试集的样本分别归一化到[0,1]区间
[train_scale,ps] = mapminmax(train_sample',0,1);
train_x = train_scale';
test_scale = mapminmax('apply',test_sample',ps);
test_x = test_scale';
% 将训练集和测试集的样本标签分别归一化到[0,1]区间
[train_lab,ps] = mapminmax(train_labels',0,1);
train_y = train_lab';
test_lab = mapminmax('apply',test_labels',ps);
test_y = test_lab';
%% SVM网络训练、验证与测试
cmd = ('-s 3 -t 2 -b 1');
model = svmtrain(train_y,train_x,cmd); %#ok<SVMTRAIN>
[predict_y,accuracy,prob_estimates] = svmpredict(test_y,test_x,model);
% 反归一化
predict_labels = mapminmax('reverse',predict_y,ps);
%% 结果分析
% 验证集、测试集的实际分类和预测分类图
figure;
hold on;
plot(test_labels,'b-');
plot(predict_labels,'r-.');
axis tight;
xlabel('测试集样本','FontSize',12);
ylabel('标签','FontSize',12);
legend('测试集实际值','测试集预测值');
title('测试集的实际值和预测值','FontSize',12);
grid on;```