• 大小: 9KB
    文件类型: .m
    金币: 2
    下载: 1 次
    发布日期: 2021-05-28
  • 语言: Matlab
  • 标签: 分类  

资源简介

这是pso优化的支持向量机程序,主要用于各种数据的分类

资源截图

代码片段和文件信息

%% 清空环境变量
function chapter_PSO
close all;
clear;
clc;
format compact;
%% 数据提取
%wine=xlsread(‘C:\Users\Administrator\Desktop\聚类1.xlsx‘‘A1:D649‘)
%wine_labels=xlsread(‘C:\Users\Administrator\Desktop\聚类1.xlsx‘‘E1:E649‘)

%train_wine = [wine(1:200:);wine(249:290:);wine(305:380:);wine(392:590:);wine(635:645:)];
%train_wine_labels = [wine_labels(1:200);wine_labels(249:290);wine_labels(305:380);wine_labels(392:590:);wine_labels(635:645:)];
%test_wine = [wine(201:248:);wine(291:304:);wine(381:391:);wine(591:634:);wine(646:649:)];
%test_wine_labels = [wine_labels(201:248);wine_labels(291:304);wine_labels(381:391);wine_labels(591:634);wine_labels(646:649)];
%wine=xlsread(‘C:\Users\Administrator\Desktop\新处理3聚类升序.xlsx‘‘E1:G1650‘)
%wine_labels=xlsread(‘C:\Users\Administrator\Desktop\新处理3聚类升序.xlsx‘‘H1:H1650‘)
wine=xlsread(‘C:\Users\Administrator\Desktop\归一化\pso.xlsx‘‘A1:D150200‘)
wine_labels=xlsread(‘C:\Users\Administrator\Desktop\归一化\pso.xlsx‘‘E1:E150200‘)

train_wine = [wine(1:1000:);wine(100001:101000:);wine(144500:145000:);wine(149650:150150:)];
train_wine_labels = [wine_labels(1:1000);wine_labels(100001:101000);wine_labels(144500:145000:);wine_labels(149650:150150:)];
test_wine = [wine(1001:1050:);wine(101001:101050:);wine(145001:145050:);wine(150150:150200:)];
test_wine_labels = [wine_labels(1001:1050);wine_labels(101001:101050);wine_labels(145001:145050);wine_labels(150150:150200)];

%train_wine = [wine(1:20000:);wine(10000:110000:);wine(141000:143000:);wine(148000:150000:)];
%train_wine_labels = [wine_labels(1:20000);wine_labels(10000:110000);wine_labels(141000:143000:);wine_labels(148000:150000:)];
%test_wine = [wine(20001:22000:);wine(110001:111000:);wine(143001:143100:);wine(150001:150200:)];
%test_wine_labels = [wine_labels(20001:22000);wine_labels(110001:111000);wine_labels(143001:143100);wine_labels(150001:150200)];
%train_wine = [wine(1:500:);wine(551:1050:);wine(1111:1600:);wine(1651:2150:)];
%train_wine_labels = [wine_labels(1:500);wine_labels(551:1050);wine_labels(1111:1600:);wine_labels(1651:2150:)];
%test_wine = [wine(501:550:);wine(1051:1100:);wine(1601:1650:);wine(2151:2200:)];
%test_wine_labels = [wine_labels(501:550);wine_labels(1051:1100);wine_labels(2151:2200)];

%% 数据预处理
% 数据预处理将训练集和测试集归一化到[01]区间

[mtrainntrain] = size(train_wine);
[mtestntest] = size(test_wine);

dataset = [train_wine;test_wine];
% mapminmax为MATLAB自带的归一化函数
[dataset_scaleps] = mapminmax(dataset‘01);
dataset_scale = dataset_scale‘;

train_wine = dataset_scale(1:mtrain:);
test_wine = dataset_scale( (mtrain+1):(mtrain+mtest): );
%% 选择最佳的SVM参数c&g

[bestaccbestcbestg] = psoSVMcgForClass(train_wine_labelstrain_wine);

% 打印选择结果
disp(‘打印选择结果‘);
str = sprintf( ‘Best Cross Validation Accuracy = %g%% Best c = %g Best g = %g‘bestaccbestcbestg);
%str = sprintf( ‘Best c = %g Best g = %g‘bestcbestg);
disp(str);

%% 利用最佳的参数进行SVM网络训练
cmd = [‘-c ‘num2str(bestc)‘ 

评论

共有 条评论