自定义&#34中给出的代码; MATLAB中的高效多类加权多数表决实现"

时间:2015-10-18 20:18:26

标签: matlab

我尝试自定义" Efficient multiclass weighted majority voting implementation in MATLAB"中提供的代码用于4个分类器,而选项保持3个原样。 bsxfun返回错误:两个输入数组的非单例维度必须相互匹配。在这种情况下,如何匹配输入的维度。如何将此代码与4个分类器和3个选项一起使用?有任何想法,请帮忙。 以下显示了我尝试自定义它的方式。

knn_weight = Accuracy_knn_datasets_overall; % average classification accuracy on the 5 fixed folds

nearest_centroid_weight = Accuracy_centroid_datasets_overall; % average classification accuracy on the 5 fixed folds

bayes_weight = Accuracy_bayes_datasets_overall; % average classification accuracy on the 5 fixed folds

nn_weight = mean([89.5,89.5,94.4,92.3,89.5]); % average of the classification accuracy of the testing sets for 5 iterations

% [knn,nearest_centroid,bayes,nn]; order of the base classifiers

w = [knn_weight nearest_centroid_weight bayes_weight nn_weight]; % weights of the classifiers based on their individual performance.

%% Merging the outputs of the base classifiers to yield a single output


knn_output_3 = knnPredicted_vehicleclass_dataset_3;

nearest_centroid_output_3 = centroidPredicted_vehicleclass_dataset_3;

nn_output_2 = nnPredicted_vehicleclass_dataset_2;

votes = [knn_output_3 nearest_centroid_output_3 bayes_output_3 nn_output_2]; % base classifiers predictions

output = votes;
expression = 'smallvehicle';
replace = 'S';
A = regexprep(output,expression,replace);
output = A;
expression = 'mediumvehicle';
replace = 'M';

A = regexprep(output,expression,replace);

output = A;
expression = 'largevehicle';
replace = 'L';

A = regexprep(output,expression,replace);

B = A(:,1); % outputs of knn
C = A(:,2); % outputs of nearest centroid
D = A(:,3); % outputs of bayes
E = A(:,4) % outputs of nn

A = char(strcat(B,C,D,E)); 

votes = A;

Classlabels = ['S', 'M', 'L']';

%'//Make a cube of the Classlabels that is number of Classlabels by m by n

OPTIONS = repmat(Classlabels, [1, size(w, 2), size(votes, 1)]);

%//Compare the votes (streched to make surface) against a uniform surface of each option

B = bsxfun(@eq, permute(votes, [3 2 1]) ,OPTIONS);

%//Find a weighted sum
W = squeeze(sum(bsxfun(@times, repmat(w, size(Classlabels, 1), 1), B), 2))';

%'//Find the Classlabels with the highest weighted sum
[xx, i] = max(W, [], 2);


output = cellstr(Classlabels(i));

output = output;
expression = 'S';
replace = 'smallvehicle';

A = regexprep(output,expression,replace);

output = A;
expression = 'L';
replace = 'largevehicle';

A = regexprep(output,expression,replace);

output = A;
expression = 'M';
replace = 'mediumvehicle';

Ensemble_output_3 = regexprep(output,expression,replace); % predicted class labels by the Ensemble classifier

Confusionmatrix_dataset_3 = confusionmat(YSML_3,Ensemble_output_3)

Ensmble_Accuracy_dataset_3 = (sum(diag(Confusionmatrix_dataset_3))/sum(sum(Confusionmatrix_dataset_3)))*100

返回的错误是:使用bsxfun时出错 两个输入数组的非单例维度必须相互匹配。

我已多次更改,希望我已正确复制它。请协助定制。对于引用的其他权重,如果需要,可以为代码开发提供任意数值。非常感谢你。

1 个答案:

答案 0 :(得分:0)

我认为您链接的代码已经足够通用,可以处理不同数量的选民和不同数量的课程。如果我按如下方式更改 输入:

w=[7 2 6 5];

votes = ['A' 'B' 'B' 'A'
         'C' 'A' 'A' 'B'
         'B' 'B' 'A' 'C'
         'A' 'A' 'C' 'A'];

options = ['A', 'B', 'C']';

然后它仍然有效,现在在概念上聚合4个分类器,只有3个类。即这仍然有效:

%//Make a cube of the options that is number of options by m by n
OPTIONS = repmat(options, [1, size(w, 2), size(votes, 1)]);

%//Compare the votes (streched to make surface) against a uniforma surface of each option
B = bsxfun(@eq, permute(votes, [3 2 1]) ,OPTIONS);

%//Find a weighted sum
W = squeeze(sum(bsxfun(@times, repmat(w, size(options, 1), 1), B), 2))'

%'//Find the options with the highest weighted sum
[xx, i] = max(W, [], 2);
options(i)

因此,您的错误可能源于您的输入格式。在运行其余代码之前,请提供您的woptionsvotes矩阵的内容摘要?