2

做的时候:

load training.mat
training = G

load testing.mat
test = G

接着:

>> knnclassify(test.Inp, training.Inp, training.Ltr)

??? Error using ==> knnclassify at 91
The length of GROUP must equal the number of rows in TRAINING.

自从:

>> size(training.Inp)
ans =
          40          40        2016

和:

>> length(training.Ltr)
ans =
        2016

如何给 knnclassify (TRAINING) 的第二个参数 training.inp 3-D 矩阵,以便行数为 2016(第三维)?

4

1 回答 1

3

Assuming that your 3D data is interpreted as 40-by-40 matrix of features for each of the 2016 instances (third dimension), we will have to re-arrange it as a matrix of size 2016-by-1600 (rows are samples, columns are dimensions):

%# random data instead of the `load data.mat`
testing = rand(40,40,200);
training = rand(40,40,2016);
labels = randi(3, [2016 1]);     %# a class label for each training instance
                                 %# (out of 3 possible classes)

%# arrange data as a matrix whose rows are the instances,
%# and columns are the features
training = reshape(training, [40*40 2016])';
testing = reshape(testing, [40*40 200])';

%# k-nearest neighbor classification
prediction = knnclassify(testing, training, labels);
于 2009-12-14T22:09:14.720 回答