% 将第一类的1-30,第二类的60-95,第三类的131-153做为训练集
train_wine=[wine(1:30,:);wine(60:95,:);wine((131:153),:)];
% 相应的训练集的标签也要分离出来
train_wine_labels = [wine_label(1:30);wine_label(60:95);wine_label(131:153)];
%分离测试集
test_wine=[wine(31:59,:);wine(96:130,:);wine(154:178,:)];
test_wine_labels=[wine_label(31:59);wine_label(96:130);wine_label(154:178)];
%%数据预处理
%数据预处理 将训练集和测试集合归一化到[0,1]区间
dataset = [train_wine;test_wine];
% mapminmax为MATLAB自带的归一化函数
dataset = [train_wine;test_wine];
% mapminmax为MATLAB自带的归一化函数
[train_wine,PS] = mapminmax(train_wine');
train_wine = train_wine';
test_wine = mapminmax('apply',test_wine',PS);
test_wine = test_wine';
%%训练SVM
model=svmtrain(train_wine_labels, train_wine, '-c 2 -g 1');
%%SVM网络预测
[predict_label,accuracy,decision_values] = svmpredict(test_wine_labels,test_wine,model
figure;
hold on;
plot(test_wine_labels,'o');
plot(predict_label,'r*');
xlabel('测试集样本','FontSize',12);
ylabel('类别标签','FontSize',12);
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',12);
grid on
%%
figure;
boxplot(wine,'orientation','horizontal','labels',categories);
title('wine数据的box可视化图','FontSize',12);
xlabel('属性值','FontSize',12);
grid on;
% 画出测试数据的分维可视化图
figure
subplot(3,5,1);
hold on
for run = 1:178
plot(run,wine_labels(run),'*');
end
xlabel('样本','FontSize',10);
ylabel('类别标签','FontSize',10);
title('class','FontSize',10);
for run = 2:14
subplot(3,5,run);
hold on;
str = ['attrib ',num2str(run-1)];
for i = 1:178
plot(i,wine(i,run-1),'*');
end
xlabel('样本','FontSize',10);
ylabel('属性值','FontSize',10);
title(str,'FontSize',10);
end