做的时候:
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.inp 3-D 矩阵提供第二个参数,以便行数为 2016(第三维)?
最佳答案
假设您的 3D 数据被解释为 2016 年每个实例(第三维)的 40×40 特征矩阵,我们将不得不将其重新排列为大小为 2016×1600 的矩阵(行是样本,列是尺寸):
%# 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);
关于MATLAB - knnclassify 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1903165/