⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ml_vc.m

📁 最新的模式识别分类工具箱,希望对朋友们有用!
💻 M
字号:
% Learns classifier and classifies test set%  by representing the distributions as Gaussians,%  learning the parameters using Maximum Likelihood%  % Inputs:% Usage%      [trainError, testError, estTrainLabels, estTestLabels] = ...%           ML_VC(trainFeatures, trainLabels,unused ,testFeatures, testLabels)% where%% Inputs:% 	trainFeatures  - the training set vectors, one vector per column%	trainLabels    - the labels of the above%       unuse          - unused parameter%       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] =  ...    ML_VC(trainFeatures, trainLabels, AlgorithmParameters, testFeatures, testLabels);[Nclasses, classes]  = find_classes([trainLabels(:);testLabels(:)]); % Number of classes in labels[Dim, Ntrain]           = size(trainFeatures);[Dim, Ntest]            = size(testFeatures);for cl =1:Nclasses,  trainIndexes   = find(trainLabels == classes(cl));  priors(cl)     = length(trainIndexes);  means(:,cl)    = (mean(trainFeatures(:,trainIndexes)'))';  covars(:,:,cl) = cov(trainFeatures(:,trainIndexes)');endpriors = priors/length(trainLabels);% now classifies% first training setcounts = zeros(Nclasses, Ntrain);oneVect = ones(1,Ntrain);for (cl = 1:Nclasses)  covariance = covars(:,:,cl);  meanval    = means(:,cl) * oneVect;  diff       = trainFeatures - meanval;    diff       = exp(-sum((diff' * inv(covariance))'.*diff)/2);  counts(cl,:) = diff/sqrt(det(covariance)) * priors(cl);end[counts, indexes] = max(counts);estTrainLabels = classes(indexes);% then test setcounts = zeros(Nclasses, Ntest);oneVect = ones(1,Ntest);for (cl = 1:Nclasses)  covariance = covars(:,:,cl);  meanval    = means(:,cl) * oneVect;  diff       = testFeatures - meanval;    diff       = exp(-sum((diff' * inv(covariance))'.*diff)/2);  counts(cl,:) = diff/sqrt(det(covariance)) * priors(cl);end[counts, indexes] = max(counts);estTestLabels = classes(indexes);trainError = computeError(classes, trainLabels, estTrainLabels);testError  = computeError(classes, testLabels , estTestLabels);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -