一、简单介绍
二、实验准备
实验数据:IRIS也称鸢尾花卉数据集,是一类多重变量分析的数据集。数据集包含150个数据集,分为3类(本次实验中,分别记为1,2,3类),每类50个数据,每个数据包含4个属性。
实验语言:MATLAB
目录结构:
三、具体代码实现
1) knn 实现
function y = knn(trainData, sample_label, testData, k)
%KNN k-Nearest Neighbors Algorithm.
%
% INPUT: trainData: training sample Data, M-by-N matrix.
% sample_label: training sample labels, 1-by-N row vector.
% testData: testing sample Data, M-by-N_test matrix.
% K: the k in k-Nearest Neighbors
%
% OUTPUT: y : predicted labels, 1-by-N_test row vector.
%
% Author: Sophia_Dz
[M_train, N] = size(trainData);
[M_test, ~] = size(testData);
%calculate the distance between testData and trainData
Dis = zeros(M_train,1);
class_test = zeros(M_test,1);
for n = 1:M_test
for i = 1:M_train
distance1 = 0;
for j = 1:N
distance1 = (testData(n,j) - trainData(i,j)).^2 + distance1;
end
Dis(i,1) = distance1.^0.5;
end
%find the k nearest neighbor
[~, index] = sort(Dis);
for i = 1:k
temp(i) = sample_label(index(i));
end
table = tabulate(temp);
MaxCount=max(table(:,2,:));
[row,col]=find(table==MaxCount);
MaxValue=table(row,1);
class_test(n) = MaxValue;
end
y = class_test;
2) 测试demo
clear all,clc
M = dlmread('IRIS_original_data.txt');
sample_label = M(:,5);
trainData = M(:,1:4);
testData = dlmread('IRIS_test_data.txt');
k = 5;
y = knn(trainData, sample_label, testData, k)
四、运行结果
测试数据集(从鸢尾花卉数据集中任选的四个数据):
4.3 3.0 1.1 0.1
6.9 3.1 5.4 2.1
5.6 2.5 3.9 1.1
5.6 2.8 4.9 2.0
测试结果: