📄 pocket_vccore.m
字号:
% Learns classifier and classifies test set% using the perceptron learning algorithm% Works with 2 class labels, any number of features% when the class labels are 0 and 1.% Invoke using Perceptron_VC% % Inputs:% Usage% [trainError, testError, estTrainLabels, estTestLabels] = ...% Perceptron_VCcore(trainFeatures, trainLabels,algParam ,testFeatures, testLabels)% where%% Inputs:% trainFeatures - the training set vectors, one vector per column% trainLabels - the labels of the above% algParam - algorithm parameters% Number of iterations, or weights vector, or [weights, number of iterations]% testFeatures - test set, one column per vector% testLabels - labels for test set%% Outputs% trainError - the error rate on the training set (one entry per% class + total error)% testError - the error rate on the test set (one entry per class% + total error)% estTrainLabels - the labels produced by the algorithm for the% training samples% estTestLabels - the labels produced by the algorithm for the% test samplesfunction [trainError, testError, estTrainLabels, estTestLabels] = ... Pocket_VCcore(trainFeatures, trainLabels,algParam,testFeatures, testLabels)[Nclasses, classes] = find_classes([trainLabels(:);testLabels(:)]);if ( max(classes) > 1 | min(classes < 0)), fprintf('When invoking this classifier, use only labels 0 and 1\n'); estTrainLabels = zeros(size(trainLabels)); estTestLabels = zeros(size(testLabels)); trainError = computeError(classes, trainLabels, estTrainLabels); testError = computeError(classes, testLabels , estTestLabels); return;end[Dim, r] = size(trainFeatures);%Weighted Pocket or not?switch length(algParam),case r + 1, %Ada boost form p = algParam(1:end-1); max_iter = algParam(end);case {r, 0}, %No parameter given p = ones(1,r); max_iter = 500;otherwise %Number of iterations given max_iter = algParam; p = ones(1,r);endtrainFeatures = [trainFeatures ; ones(1,r)];train_one = find(trainLabels == 1);train_zero = find(trainLabels == 0);%PreprocessingprocessedFeatures = trainFeatures;processedFeatures(:,train_zero) = -processedFeatures(:,train_zero);%Initial weightsw_percept = rand(Dim+1,1);w_pocket = rand(Dim+1,1);correct_classified = 0;n = length(trainLabels);iter = 0;while ((longest_run(w_percept, processedFeatures) < 0.9*n) ... & (iter < max_iter)) iter = iter + 1; %Every 10 points, do the pocket switchover for i = 1:10, indice = 1 + floor(rand(1)*n); if (w_percept' * processedFeatures(:,indice) <= 0) w_percept = w_percept + p(indice) * processedFeatures(:,indice); end end % Find if it is neccessary to change weights: if (longest_run(w_percept, processedFeatures) > longest_run(w_pocket, processedFeatures)), w_pocket = w_percept; endendif (iter == max_iter)&(length(algParam)~= r + 1), disp(['Maximum iteration (' num2str(max_iter) ') reached']);end% Now computesestTrainLabels = w_percept' * trainFeatures;estTrainLabels = estTrainLabels > 0;[Dim, Ntest] = size(testFeatures);testFeatures = [testFeatures ; ones(1,Ntest)];estTestLabels = w_percept' * testFeatures;estTestLabels = (estTestLabels > 0);trainError = computeError(classes, trainLabels, estTrainLabels);testError = computeError(classes, testLabels , estTestLabels);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -