一、简介
支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本、非线性及高维模式识别中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中。
1 数学部分
1.1 二维空间
2 算法部分
二、源代码
clc;
clear;
load A_fear fearVec;
load F_happiness hapVec;
load N_neutral neutralVec;
load T_sadness sadnessVec;
load W_anger angerVec;
sampleang=angerVec';
samplehap=hapVec';
sampleneu=neutralVec';
samplesad=sadnessVec';
samplefear=fearVec';
train(1:30,:)=sampleang(1:30,:); %每类三十个样本作为训练样本
test(1:20,:)=sampleang(31:50,:);%每类二十个样本作为测试样本
train(31:60,:)=samplehap(1:30,:);
test(21:40,:)=samplehap(31:50,:);%
train(61:90,:)=sampleneu(1:30,:);
test(41:60,:)=sampleneu(31:50,:);%
train(91:120,:)=samplesad(1:30,:);
test(61:80,:)=samplesad(31:50,:);%
train(121:150,:)=samplefear(1:30,:);
function rate=svmclassfiction(samples,test) %构造五种情感分类器
train1=samples(1:60,:);%用来构造生气-高兴分类模型训练样本
train2=[samples(1:30,:);samples(61:90,:)];%用来构造生气-中性分类模型训练样本
train3=[samples(1:30,:);samples(91:120,:)];%用来构造生气-悲伤分类模型训练样本
train4=[samples(1:30,:);samples(121:150,:)];%用来构造生气-害怕分类模型训练样本
train5=[samples(31:60,:);samples(61:90,:)];%用来构造高兴-中性分类模型训练样本
train6=[samples(31:60,:);samples(91:120,:)];%用来构造高兴-悲伤分类模型训练样本
train7=[samples(31:60,:);samples(121:150,:)];%用来构造高兴-害怕分类模型训练样本
train8=[samples(61:90,:);samples(91:120,:)];%用来构造中性-悲伤分类模型训练样本
train9=[samples(61:90,:);samples(121:150,:)];%用来构造中性-害怕分类模型训练样本
train10=[samples(91:120,:);samples(121:150,:)];%用来构造悲伤-害怕分类模型训练样本
for i=1:30 %正类样本标记
trainlabel(i)=1;
end
for i=30:60 %负类样本标记
trainlabel(i)=-1;
end
trainlabel=trainlabel';
svmStruct(1)= svmtrain(train1,trainlabel); %构造两类SVM分类模型
svmStruct(2)= svmtrain(train2,trainlabel);
svmStruct(3)= svmtrain(train3,trainlabel);
svmStruct(4)= svmtrain(train4,trainlabel);
svmStruct(5)= svmtrain(train5,trainlabel);
svmStruct(6)= svmtrain(train6,trainlabel);
svmStruct(7)= svmtrain(train7,trainlabel);
svmStruct(8)= svmtrain(train8,trainlabel);
svmStruct(9)= svmtrain(train9,trainlabel);
svmStruct(10)= svmtrain(train10,trainlabel);
sumang=0; %生气情感正确识别个数
sumhap=0; %高兴情感正确识别个数
sumneu=0; %中性情感正确识别个数
sumsad=0; %悲伤情感正确识别个数
sumfea=0; %害怕情感正确识别个数
for i=1:100
for k=1:5
votes(k)=0; %多个SVM分类器将待测样本规定为某一类别个数
end
for j=1:10
C(j) = svmclassify(svmStruct(j),test(i,:));
end
if(C(1)==1) %第一个判决器结果
votes(1)=votes(1)+1; %生气情感获得票数
elseif(C(1)==-1)
votes(2)=votes(2)+1; %高兴情感获得票数
end
if(C(2)==1) %第二个判决器结果
votes(1)=votes(1)+1; %生气情感获得票数
elseif(C(2)==-1)
votes(3)=votes(3)+1; %中性情感获得票数
end
if(C(3)==1) %第三个判决器结果
votes(1)=votes(1)+1; %生气情感获得票数
elseif(C(3)==-1)
votes(4)=votes(4)+1; %悲伤情感获得票数
end
if(C(4)==1) %第四个判决器结果
votes(1)=votes(1)+1; %生气情感获得票数
elseif(C(4)==-1)
votes(5)=votes(5)+1; %害怕情感获得票数
end
三、运行结果
四、备注
版本:2014a
完整代码或代写加1564658423