www.pudn.com > snippets(1).rar > learn_classification_npairs.m, change:2009-10-12,size:795b


function classifier = learn_classification_npairs(data, classes, Globals)
%
% train an NPAIRS-SVM to classify the data

data = data';
classes = classes';
numclasses = length(unique(classes));
[m,n] = size(data);

classifier = cell(numclasses,numclasses);

for u = 1:numclasses

  if Globals.DISPLAY, fprintf('|'); end

  for v = u+1:numclasses

    if Globals.DISPLAY, fprintf('.'); end

    % extract data for current pair
    curr_idx = find(classes==u | classes==v);
    curr_classes = double(classes(curr_idx)==u);
    curr_classes(curr_classes<1) = -1;
    curr_data = data(curr_idx,:);

    % now, train classifier
    curr_classifier = svm(size(data,2), Globals.svm_kernel, Globals.svm_kernelpar, 1);
    classifier{u}{v} = svmtrain(curr_classifier, curr_data, curr_classes);

  end

end