-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.m
144 lines (118 loc) · 3.75 KB
/
exercise1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%% import the data
irisdata = importfile('irisdata.csv', 2, 151);
X = irisdata{:,3};
Y = irisdata{:,4};
Xsetosa = X(1:50);
Xversicolor = X(51:100);
Xvirginica = X(101:150);
Ysetosa = Y(1:50);
Yversicolor = Y(51:100);
Yvirginica = Y(101:150);
%% 1(a) - plot the data
scatter(Xsetosa, Ysetosa, [], 'red', 'o')
hold on, scatter(Xversicolor, Yversicolor, [], 'green','x')
hold on, scatter(Xvirginica, Yvirginica, [], 'blue','+')
legend('setosa', 'versicolor', 'virginica')
xlabel('Length (mm)'), ylabel('Width (mm)')
title('Iris Data Set')
xlim([min(X) max(X)]), ylim([min(Y) max(Y)])
%% 1(b) - plot a linear decision boundary by hand
% note that the decision boundary I am using is between the versicolor and
% virginica classes
boundary = -1/2*X + 4;
hold on, plot(X, boundary)
%% 1(c) - define a simple threshold classifier
% this counts the number of data points that would be miss classified for
% each classification.
missclassifiedVersicolor = 0;
for i = 1:50
if Yversicolor(i) >= -1/2*Xversicolor(i) + 4
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
for i = 1:50
if Yvirginica(i) <= -1/2*Xvirginica(i) + 4
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
missclassifiedVirginca = 0;
for i = 1:50
if Yvirginica(i) <= -1/2*Xvirginica(i) + 4
missclassifiedVirginca = missclassifiedVirginca + 1;
end
end
for i = 1:50
if Yversicolor(i) >= -1/2*Xversicolor(i) + 4
missclassifiedVirginca = missclassifiedVirginca + 1;
end
end
missclassifiedVersicolor
missclassifiedVirginca
%% 1(d) - define a circle decision boundary
scatter(Xsetosa, Ysetosa, [], 'red', 'o')
hold on, scatter(Xversicolor, Yversicolor, [], 'green','x')
hold on, scatter(Xvirginica, Yvirginica, [], 'blue','+')
legend('setosa', 'versicolor', 'virginica')
xlabel('Length (mm)'), ylabel('Width (mm)')
title('Iris Data Set')
xlim([min(X) max(X)]), ylim([min(Y) max(Y)])
plotcircle(4.1, 1.2, 1.1, 'b')
plotcircle(5.7, 2, 1.1, 'b')
missclassifiedVersicolor = 0;
for i = 1:50
if (4.1-Xversicolor(i))^2 + (1.2-Yversicolor(i))^2 > 1.1^2
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
for i = 1:50
if (4.1-Xvirginica(i))^2 + (1.2-Yvirginica(i))^2 <= 1.1^2
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
missclassifiedVersicolor
missclassifiedVirginica = 0;
for i = 1:50
if (5.7-Xvirginica(i))^2 + (2.0-Yvirginica(i))^2 > 1.1^2
missclassifiedVirginica = missclassifiedVirginica + 1;
end
end
for i = 1:50
if (5.7-Xversicolor(i))^2 + (2.0-Yversicolor(i))^2 <= 1.1^2
missclassifiedVirginica = missclassifiedVirginica + 1;
end
end
missclassifiedVirginica
%% 1(d) - third circle - define a circle decision boundary
scatter(Xsetosa, Ysetosa, [], 'red', 'o')
hold on, scatter(Xversicolor, Yversicolor, [], 'green','x')
hold on, scatter(Xvirginica, Yvirginica, [], 'blue','+')
legend('setosa', 'versicolor', 'virginica')
xlabel('Length (mm)'), ylabel('Width (mm)')
title('Iris Data Set')
xlim([min(X) max(X)]), ylim([min(Y) max(Y)])
plotcircle(4.1, 1.2, 1.1, 'b')
plotcircle(5.9, 2, 1.2, 'b')
missclassifiedVersicolor = 0;
for i = 1:50
if (4.1-Xversicolor(i))^2 + (1.2-Yversicolor(i))^2 > 1.1^2
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
for i = 1:50
if (4.1-Xvirginica(i))^2 + (1.2-Yvirginica(i))^2 <= 1.1^2
missclassifiedVersicolor = missclassifiedVersicolor + 1;
end
end
missclassifiedVersicolor
missclassifiedVirginica = 0;
for i = 1:50
if (5.9-Xvirginica(i))^2 + (2.0-Yvirginica(i))^2 > 1.2^2
missclassifiedVirginica = missclassifiedVirginica + 1;
end
end
for i = 1:50
if (5.9-Xversicolor(i))^2 + (2.0-Yversicolor(i))^2 <= 1.2^2
missclassifiedVirginica = missclassifiedVirginica + 1;
end
end
missclassifiedVirginica