-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.m
88 lines (77 loc) · 2.12 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
%
% Author: Edgard Diaz
% Date: 10 - 04 - 2020
%
% Classification of Iris dataset using the k-nearest neighbors algorithm.
% In addition, the decision boundaries generated by the earlier mentioned
% algorithm are plotted in this code.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear;
%valor de K
k = 15;
dataset_iris = load('iris.mat');
dataset_iris = cell2mat(struct2cell(dataset_iris))';
%
% Mallado para pasar al algoritmo knn
%
[X, Y] = meshgrid(4:0.01:8,1:0.01:5);
X=X(:);
Y=Y(:);
xt = [X(:),Y(:)];
xi = zeros(150,2);
xi = dataset_iris(:,1:2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:length(xt)
S = knn(xi,xt(i,1:2),k);
indices = find(S);
clase = dataset_iris(indices,5);
moda = mode(clase);
xt(i,3) = moda;
end
xi(:,3) = S;
for i = 1:length(xt)
if(xt(i,3) == 1)
%plot(xt(i,1),xt(i,2),'m.');
aux1(i,1) = xt(i,1);
aux1(i,2) = xt(i,2);
elseif(xt(i,3) == 2)
%plot(xt(i,1),xt(i,2),'y.');
aux2(i,1) = xt(i,1);
aux2(i,2) = xt(i,2);
elseif(xt(i,3) == 3)
%plot(xt(i,1),xt(i,2),'g.');
aux3(i,1) = xt(i,1);
aux3(i,2) = xt(i,2);
end
end
xi(:,4) = dataset_iris(:,5);
for i = 1:length(xi)
if(xi(i,4) == 1)
%plot(xi(i,1),xi(i,2),'k o');
axi1(i,1) =xi(i,1);
axi1(i,2) =xi(i,2);
elseif(xi(i,4) == 2)
%plot(xi(i,1),xi(i,2),'k s');
axi2(i,1) =xi(i,1);
axi2(i,2) =xi(i,2);
elseif(xi(i,4) == 3)
%plot(xi(i,1),xi(i,2),'k *');
axi3(i,1) =xi(i,1);
axi3(i,2) =xi(i,2);
end
end
strk = num2str(k);
str = strcat('k= ',strk);
%figure('Algoritmo de clasificacion', 'NumberTitle','off');
title({'Regiones de decision algoritmo knn';str});
box on
axis([4 8 1 5]);
hold on
plot(aux1(:,1),aux1(:,2),'m.');
plot(aux2(:,1),aux2(:,2),'y.');
plot(aux3(:,1),aux3(:,2),'g.');
plot(axi1(:,1),axi1(:,2),'k o');
plot(axi2(:,1),axi2(:,2),'k p');
plot(axi3(:,1),axi3(:,2),'k s');