📄 knnclass.m
字号:
% load data files
train=load('spambase_train.txt','-ascii');
train(:,end)=train(:,end)+1;
trainLabels=train(:,end);
numTraining=length(trainLabels);
test=load('spambase_test.txt','-ascii');
test(:,end)=test(:,end)+1;
testLabels=test(:,end);
numTesting=length(testLabels);
test(:,end)=[];
uniqueClassLabels=unique(trainLabels); % unique class labels
numClasses = length(uniqueClassLabels); % number of classes
% show the training data and testing data
figure;
subplot(3,2,1);
scatter(train(:,1),train(:,2),3,trainLabels,'.');
title('training data');
set(gca,'xlim',[-4 4], 'ylim', [-6,4]);
subplot(3,2,2);
scatter(test(:,1),test(:,2),3,testLabels,'+');
title('testing data');
set(gca,'xlim',[-4 4], 'ylim', [-6,4]);
k=[ 1 3 5 7];
for i=1:length(k)
[predictLabels]=knn(train,test,k(i)); % knn classifier
subplot(3,2,i+2);
scatter(test(:,1),test(:,2),5,predictLabels,'+');
hold on;
errorIdx=predictLabels~=testLabels;
scatter(test(errorIdx,1),test(errorIdx,2),3,'*r');
set(gca,'xlim',[-4 4], 'ylim', [-6,4]);
hold off;
pError=sum(predictLabels~=testLabels)/numTesting; % if predicted label does not match the true label, we get a error
title(['k = ' num2str(k(i)) ', errorRate = ' num2str(pError)]);
% do we need to display the density matrix?
% disp(['Density estimated by parzen window with window width of ', num2str(windowWidth(i)), ' is:'] );
% disp(estDensity);
disp(['Error rate for k = ', num2str(k(i)), ' is: ' num2str(pError)] ); % display the error rate.
end
suptitle('knn classifier on spambase');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -