开发者

MATLAB - usage of knnclassify

开发者 https://www.devze.com 2022-12-14 12:29 出处:网络
When doing: load training.mat training = G load testing.mat test = G and then: >> knnclassify(test.Inp, training.Inp, training.Ltr)

When doing:

load training.mat
training = G

load testing.mat
test = G

and then:

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

??? Error using ==> knnc开发者_如何学编程lassify at 91
The length of GROUP must equal the number of rows in TRAINING.

Since:

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

And:

>> length(training.Ltr)
ans =
        2016

How can I give the second parameter of knnclassify (TRAINING) the training.inp 3-D matrix so that the number of rows will be 2016 (the third dimension)?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消