k最近邻:误差使用-矩阵维数必须符合[MATLAB]?
在执行下面的代码后,我尝试实现用于图像分类的k-最近邻分类器,我得到了以下错误: 使用- 矩阵尺寸必须一致 Sevenn_分类器错误(第19行) tmp=测试点-训练点(1:end-1,:) 认为:k最近邻:误差使用-矩阵维数必须符合[MATLAB]?,matlab,matrix,knn,Matlab,Matrix,Knn,在执行下面的代码后,我尝试实现用于图像分类的k-最近邻分类器,我得到了以下错误: 使用- 矩阵尺寸必须一致 Sevenn_分类器错误(第19行) tmp=测试点-训练点(1:end-1,:) 认为: size(test_point) = 1 10000 size(train-point)= 1 60000 这是我的密码: % load training set and testing set clear all; train_set = loadMNISTImages
size(test_point) = 1 10000
size(train-point)= 1 60000
这是我的密码:
% load training set and testing set
clear all;
train_set = loadMNISTImages('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/train-images.idx3-ubyte');
train_label = loadMNISTLabels('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/train-labels.idx1-ubyte');
test_set = loadMNISTImages('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/t10k-images.idx3-ubyte');
test_label = loadMNISTLabels('/home/anelmad/Desktop/TER/mnist_ml2/MNIST_digit_recognition-master/load_data/t10k-labels.idx1-ubyte');
% cliassify the testing set
train_scale = size(train_set);
test_scale = size(test_set);
test_classify_label = zeros(test_scale(1),1);
tic;
for i=1:test_scale(1)
test_point = test_set(i,:);
dist = zeros(train_scale(1),1);
for j=1:train_scale(1)
% calculate the distance between test point i and train point j
train_point = train_set(j, :);
tmp = test_point-train_point;
dist(j) = sqrt(sum(tmp.*tmp));
end
% find the 5-nearest neighbor
dist_tmp = sort(dist);
num = zeros(10, 1);
for k=1:7
idx = find(dist==dist_tmp(k));
num(train_label(idx)+1) = num(train_label(idx)+1)+1;
end
% classification
maxIdx = 0;
maxNum = -1;
for k=1:10
if(num(k)>maxNum)
maxIdx = k;
maxNum = num(k);
end
end
test_classify_label(i) = maxIdx-1;
end
t1 = toc;
% calculate accuracy
num_correct = sum(test_label==test_classify_label);
accuracy = num_correct / test_scale(1);
save -mat 7nn_time.mat t1
save -mat 7nn_accuracy.mat accuracy
因此,19号线为
tmp=测试点-列车点代码>,对吗?测试点
和训练点
的尺寸是多少。显然,减法的尺寸必须一致。到目前为止,您尝试了什么?如果您有1x10000和1x60000,您认为应该如何解决?那么我必须将trainpoint设置为trainpoint=(1:size(test_point))?您是否忘记替换复制代码中的行?尺寸必须匹配。