查看: 2065|回复: 5|关注: 0

[已答复] 安装libsvmmat后在调用svmtrain时出现错误the number of elements in B...

[复制链接]

新手

15 麦片

财富积分


050


1

主题

7

帖子

0

最佳答案
发表于 2016-6-3 17:06:55 | 显示全部楼层 |阅读模式
MATLAB版本为2013a,研究目的是做支持向量机预测模型,已安装了libsvm3.21和libsvmmat version 1.0,在调用函数gaSVMcgForClass时,报错
Length of label vector does not match # of instances.
In an assignment  A(I) = B, the number of elements in B and I must be the
same.
Error in gaSVMcgForClass (line 43)
individuals.fitness(i) = svmtrain(train,train_label,cmd);
麻烦各位大神帮忙解决一下问题,谢谢~

下面是代码

主代码
%清空环境变量
clc
clear
%读取数据
a=xlsread('F:\4.数据挖掘讲义—马景义\数据和程序\matlab\199-input.csv');
b=xlsread('F:\4.数据挖掘讲义—马景义\数据和程序\matlab\199-output.csv');
save data1.mat b
save data2.mat a
load data1
load data2
%训练数据和预测数据
train = a(:,1:132);
train_label = b(:,1:132);
test = a(:,133:199);
test_label = b(:,133:199);
%数据的归一化
[train,pstrain] = mapminmax(train');
pstrain.ymin = 0;
pstrain.ymax = 1;
[train,pstrain] = mapminmax(train,pstrain);
[test,pstest] = mapminmax(test');
pstest.ymin = 0;
pstest.ymax = 1;
[test,pstest] = mapminmax(test,pstest);
train = train';
test = test';
%%
% maxgen:最大的进化代数,默认为100,一般取值范围为[100,500]
% sizepop:种群最大数量,默认为20,一般取值范围为[20,100]
% pCrossover:交叉概率,默认为0.4,一般取值范围为[0.4,0.99]
% pMutation:变异概率,默认为0.01,一般取值范围为[0.001,0.1]
% cbound = [cmin,cmax],参数c的变化范围,默认为[0.1,100]
% gbound = [gmin,gmax],参数g的变化范围,默认为[0.01,1000]
% v:SVM Cross Validation参数,默认为10
tic;
ga_option.maxgen = 100;
ga_option.sizepop = 20;
ga_option.pCrossover = 0.4;
ga_option.pMutation = 0.01;
ga_option.cbound = [0.1,100];
ga_option.gbound = [0.01,100];
ga_option.v = 3;
[bestCVaccuarcy,bestc,bestg,ga_option] = gaSVMcgForClass(train_label,train,ga_option);
bestc
bestg
bestCVaccuarcy
cmd = ['-c ',num2str(bestc),' -g ',num2str(bestg)];
model = svmtrain(train_label,train,cmd);
[pretrain,trainacc] = svmpredict(train_label,train,model);
[pretest,testacc] = svmpredict(test_label,test,model);
toc;


子函数 gaSVMcgForClass代码
function [bestCVaccuarcy,bestc,bestg,ga_option] = gaSVMcgForClass(train_label,train,ga_option)
% gaSVMcgForClass
% by faruto
% Email:farutoliyang@gmail.com
% 2009.10.07

% 参数初始化
if nargin == 2
    ga_option = struct('maxgen',100,'sizepop',132,'pCrossover',0.4,'pMutation',0.01, ...
                'cbound',[0.1,100],'gbound',[0.01,1000],'v',10);
end
% maxgen:最大的进化代数,默认为100,一般取值范围为[100,500]
% sizepop:种群最大数量,默认为20,一般取值范围为[20,100]
% pCrossover:交叉概率,默认为0.4,一般取值范围为[0.4,0.99]
% pMutation:变异概率,默认为0.01,一般取值范围为[0.001,0.1]
% cbound = [cmin,cmax],参数c的变化范围,默认为[0.1,100]
% gbound = [gmin,gmax],参数g的变化范围,默认为[0.01,1000]
% v:SVM Cross Validation参数,默认为10
c_len_chromosome = ceil(log2((ga_option.cbound(2)-ga_option.cbound(1))*100));
g_len_chromosome = ceil(log2((ga_option.gbound(2)-ga_option.gbound(1))*100));
len_chromosome = c_len_chromosome+g_len_chromosome;

% 将种群信息定义为一个结构体
individuals=struct('fitness',zeros(1,ga_option.sizepop), ...
                   'chromosome',zeros(ga_option.sizepop,len_chromosome));  
% 每一代种群的平均适应度
avgfitness_gen = zeros(1,ga_option.maxgen);         
% 每一代种群的最佳适应度
bestfitness_gen = zeros(1,ga_option.maxgen);
% 最佳适应度
bestfitness = 0;   
% 适应度最好的染色体
bestchromosome = zeros(1,len_chromosome);                       

% 初始化种群
for i = 1:ga_option.sizepop
    % 编码
    individuals.chromosome(i,:) = unidrnd(2,1,len_chromosome)-1;
    % 解码
    [c,g] = ga_decode(individuals.chromosome(i,:),ga_option.cbound,ga_option.gbound);
    % 计算初始适应度(CV准确率)
    cmd = ['-v ',num2str(ga_option.v),' -c ',num2str( c ),' -g ',num2str( g )];
individuals.fitness(i) = svmtrain(train_label,train,cmd);
end

% 找最佳的适应度和最好的染色体的位置
[bestfitness,bestindex]=max(individuals.fitness);
% 最好的染色体
bestchromosome = individuals.chromosome(bestindex,:);

%迭代寻优
for i=1:ga_option.maxgen
    % Selection Operator
    individuals = Selection(individuals,ga_option);
    % Crossover Operator
    individuals = Crossover(individuals,ga_option);
    % Mutation Operator
    individuals = Mutation(individuals,ga_option);
     
    % 计算适应度
    for j = 1:ga_option.sizepop
        % 解码
        [c,g] = ga_decode(individuals.chromosome(j,:),ga_option.cbound,ga_option.gbound);
        % 计算初始适应度(CV准确率)
        cmd = ['-v ',num2str(ga_option.v),' -c ',num2str( c ),' -g ',num2str( g )];
        individuals.fitness(j) = svmtrain(train_label, train, cmd);
    end
     
% 找最佳的适应度和最好的染色体的位置
    [new_bestfitness,bestindex]=max(individuals.fitness);
    % 最好的染色体
    new_bestchromosome = individuals.chromosome(bestindex,:);
     
    [new_c,g] = ga_decode(new_bestchromosome,ga_option.cbound,ga_option.gbound);
    [c,g] = ga_decode(bestchromosome,ga_option.cbound,ga_option.gbound);
    if new_bestfitness == bestfitness && new_c < c
        bestfitness = new_bestfitness;
        bestchromosome = new_bestchromosome;
    end
    if new_bestfitness > bestfitness
        bestfitness = new_bestfitness;
        bestchromosome = new_bestchromosome;
    end
     
    % 这一代染色体的最佳适应度
    bestfitness_gen(i) = bestfitness;
    % 这一代染色体的平均适应度
    avgfitness_gen(i) = sum(individuals.fitness)/ga_option.sizepop;
     
end
% 结果分析
figure;
hold on;
plot(bestfitness_gen,'r');
plot(avgfitness_gen);
legend('最佳适应度','平均适应度');
title(['适应度曲线','(终止代数=',num2str(ga_option.maxgen),',种群数量pop=',num2str(ga_option.sizepop),')']);
xlabel('进化代数');ylabel('适应度');
axis([0 ga_option.maxgen 0 100]);
grid on;

[bestc,bestg] = ga_decode(bestchromosome,ga_option.cbound,ga_option.gbound);
bestCVaccuarcy = bestfitness_gen(ga_option.maxgen);

% sub function ga_decode
function [c,g] = ga_decode(chromosome,cbound,gbound)
% ga_decode by faruto
% Email:farutoliyang@gmail.com
% 2009.10.08
c_len_chromosome = ceil(log2((cbound(2)-cbound(1))*100));
g_len_chromosome = ceil(log2((gbound(2)-gbound(1))*100));
len_chromosome = c_len_chromosome+g_len_chromosome;

cdec = bin2dec( num2str(chromosome(1:c_len_chromosome)) );
gdec = bin2dec( num2str(chromosome(c_len_chromosome+1:len_chromosome)) );

c = cbound(1) + cdec*(cbound(2)-cbound(1))/(2^(c_len_chromosome)-1);
g = gbound(1) + gdec*(gbound(2)-gbound(1))/(2^(g_len_chromosome)-1);
% sub function Selection
function individuals_afterSelect = Selection(individuals,ga_option)
% Selection by faruto
% Email:farutoliyang@gmail.com
% 2009.10.08
individuals_afterSelect = individuals;
sum_fitness = sum(individuals.fitness);
P = individuals.fitness / sum_fitness;
Q = zeros(1,ga_option.sizepop);
for k = 1:ga_option.sizepop
    Q(k) = sum(P(1:k));
end

for i = 1:ga_option.sizepop
    r = rand;
    while r == 0
        r = rand;
    end
    k = 1;
    while k <= ga_option.sizepop-1 && r > Q(k)
        k = k+1;
    end
%     individuals_afterSelect.fitness(i) = individuals.fitness(k);
    individuals_afterSelect.chromosome(i,:) = individuals.chromosome(k,:);
end
% sub function Crossover
function individuals_afterCross = Crossover(individuals,ga_option)
% Crossover by faruto
% Email:farutoliyang@gmail.com
% 2009.10.08
individuals_afterCross = individuals;
c_len_chromosome = ceil(log2((ga_option.cbound(2)-ga_option.cbound(1))*100));
g_len_chromosome = ceil(log2((ga_option.gbound(2)-ga_option.gbound(1))*100));
len_chromosome = c_len_chromosome+g_len_chromosome;

for i = 1:ga_option.sizepop
    % 交叉概率决定是否进行交叉
    r = rand;
    if r > ga_option.pCrossover
        continue;
    end
    % 随机选择两个染色体进行交叉
    pick=rand(1,2);
    while prod(pick)==0
        pick=rand(1,2);
    end
    index=ceil(pick.*ga_option.sizepop);
     
    % 随机选择交叉位置
    pos_cross = unidrnd(len_chromosome-1);
    % 进行交叉
    individuals_afterCross.chromosome(index(1),pos_cross+1:len_chromosome) ...
        = individuals.chromosome(index(2),pos_cross+1:len_chromosome);
    individuals_afterCross.chromosome(index(2),pos_cross+1:len_chromosome) ...
= individuals.chromosome(index(1),pos_cross+1:len_chromosome);
end

% sub function Mutation
function individuals_afterMutate = Mutation(individuals,ga_option)
% Mutation by faruto
% Email:farutoliyang@gmail.com
% 2009.10.08
individuals_afterMutate = individuals;
c_len_chromosome = ceil(log2((ga_option.cbound(2)-ga_option.cbound(1))*100));
g_len_chromosome = ceil(log2((ga_option.gbound(2)-ga_option.gbound(1))*100));
len_chromosome = c_len_chromosome+g_len_chromosome;

for i = 1:ga_option.sizepop
    % 变异概率决定是否进行交叉
    r = rand;
    if r > ga_option.pMutation
        continue;
    end
    % 随机选择一个染色体进行变异
    pick = unidrnd(ga_option.sizepop);
    % 随机选择变异位置
    pos_mutate = unidrnd(len_chromosome);
    % 进行变异
    if individuals_afterMutate.chromosome(pick,pos_mutate) == 0
       individuals_afterMutate.chromosome(pick,pos_mutate) = 1;
    else
       individuals_afterMutate.chromosome(pick,pos_mutate) = 0;
    end
end

新手

15 麦片

财富积分


050


1

主题

7

帖子

0

最佳答案
 楼主| 发表于 2016-6-7 10:05:10 | 显示全部楼层
补充一下,主要的问题应该是虽然安装了libsvmmat,但是在调用svmtain函数市仍然调用的MATLAB自带的函数,所以,individuals.fitness(i) = svmtrain(train_label,train,cmd);返回的是一个模型,而不是一个交叉验证的值,这个关键问题怎么解决呀~各位亲们,帮帮我呀~

新手

15 麦片

财富积分


050


1

主题

7

帖子

0

最佳答案
 楼主| 发表于 2016-6-7 10:08:16 | 显示全部楼层
还有就是,导入后,svmtrain的格式不是fun的格式,不知道这是不是没能调用它的原因,如图

H$L9VMC8XJ``OQ$KWBN75[Y.png

新手

16 麦片

财富积分


050


4

主题

29

帖子

0

最佳答案
发表于 2016-8-18 16:17:36 | 显示全部楼层
你好,你的问题解决了吗?我安装完成libsvm3.21以后重新打开matlab,输入which svmtrain显示的也仍然是matlab自带的svmtrain函数……

新手

6 麦片

财富积分


050


3

主题

64

帖子

0

最佳答案
发表于 2016-12-4 21:51:25 | 显示全部楼层
楼主您好,我也遇到同样的问题了,请问楼主解决了吗

新手

5 麦片

财富积分


050


1

主题

11

帖子

0

最佳答案
发表于 2019-2-2 00:22:08 | 显示全部楼层
请问楼主问题解决了吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /2 下一条

快速回复 返回顶部 返回列表