-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.m
71 lines (62 loc) · 1.04 KB
/
test.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
function test()
%台大机器学习基石第一次作业Q.15-17 matlab实现
clc;
workmode=1;
switch workmode
case 0
naive_cycle();
case 1
%tic;
predefined_random(2000,1);
%toc;
end
function [X,Y]=load_data()
dataset=load('train.txt');
X=dataset(:,1:4);
Y=dataset(:,5);
[n,m]=size(X);
X=[ones(n,1),X];%这一步很重要
function naive_cycle()
[X,Y]=load_data();
randflag=false;
t=train(X,Y,randflag,1);
disp(t);
function predefined_random(n,alpha)
[X,Y]=load_data();
randflag=true;
count=0;
for i=1:n
t=train(X,Y,randflag,alpha);
count=count+t;
end
t=count/n;
disp(t);
function t=train(X,Y,randflag,alpha)
w=zeros(1,5);
n=length(Y);
idx=[1:1:n];
if randflag
idx=randperm(length(Y));
end
flag=true;
t=0;
k=1;
while true
if k==n
if flag
break
else
k=1;
flag=true;
end
end
i=idx(k);
x=X(i,1:5)';
y= Y(i);
if (sign(dot(w,x)))~= y
flag=false;
t=t+1;
w=w+alpha*y*X(i,1:5);
end
k=k+1;
end