train_data=[13.8336 7.3271 6.2947 2.9990 1.2987 1.3748
14.0802 7.4438 6.1213 2.6686 1.1282 1.3482
13.8637 7.8701 6.2949 2.2442 1.3690 1.3524
14.0618 7.3072 6.4605 2.4999 1.0339 1.3386
13.3526 7.1324 6.6392 2.1221 1.2080 1.3472
13.5786 7.2090 6.2003 2.3374 0.7421 1.3169
13.8272 8.1654 6.8828 2.4842 1.0104 1.3452
23.4350 6.7689 3.7227 1.7995 0.7451 1.0577
23.1180 6.8822 4.0162 1.8768 0.8884 1.0891
27.8648 7.0439 4.1263 1.7442 1.0541 1.0243
24.1341 7.3097 4.0478 2.0285 1.3981 1.1193
25.3430 6.6661 4.9741 2.0206 1.3277 1.1100
23.2140 6.7087 3.7738 1.5655 0.9379 1.0635
24.3041 6.6158 4.5139 1.9130 1.1670 1.0995
29.4590 8.0514 2.2778 1.5340 0.7155 0.9138
30.6207 7.8663 2.8361 1.5217 0.8910 0.9317
27.8952 8.4306 3.1705 1.4139 0.7940 0.9782
29.5131 7.9239 3.3737 1.7485 1.1582 0.9974
29.6681 7.7839 3.8611 1.3718 0.9214 0.9746
30.0431 7.7115 2.8131 1.2094 0.7996 0.9108
30.1834 7.9658 2.6802 1.4850 0.9154 0.9323];
train_label=[1;1;1;1;1;1;1;2;2;2;2;2;2;2;3;3;3;3;3;3;3];
testdata=[13.6101 7.8069 7.1349 2.5897 1.0879 1.3583
13.9343 7.2479 6.1065 2.4124 0.8581 1.3219
13.7988 6.5283 6.1615 2.6378 1.0289 1.3419
25.1780 7.6983 3.9359 1.6390 0.7244 1.0413
18.8002 5.9563 3.8186 1.3544 0.7414 1.1054
25.4770 6.6755 4.4894 1.9252 1.0640 1.0748
29.4126 9.4013 2.8861 1.5931 0.9986 0.9839
30.2108 8.1666 2.3105 1.2425 1.1200 0.9181
28.5707 8.7876 2.4705 1.4557 0.7600 0.9452];
testdatalabel=[1;1;1;2;2;2;3;3;3];
%遗传算法求取支持向量机参数
ga_option.maxgen = 100; %最大进化代数,画出的图形为这个代数的一半
ga_option.sizepop = 50; %种群最大数量
ga_option.cbound = [0,100]; %参数c的变化范围
ga_option.gbound = [0,100]; %参数g的变化范围
ga_option.v = 10; %交叉
ga_option.ggap = 0.9; %变异
[bestacc,bestc,bestg]=gaSVMcgForClass(train_label,train_data,ga_option);
%上述命令为求得c和g的值,将两个值带入到支持向量机中
%本次计算得到c=0.5943,g=0.0084,代入到下式中
%支持向量机训练和识别
model=svmtrain(train_label,train_data,’-s 0 -t 2 -c 0.55847 -g 0.080681′);
%进行预测
[predictlabel,accuracy,dec_values]=svmpredict(testdatalabel,testdata,model);