-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGV.m
88 lines (71 loc) · 2.25 KB
/
GV.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
clear all
clear
clc
ds = datastore('house_prices_data_training_data.csv','TreatAsMissing','NA',.....
'MissingValue',0,'ReadSize',18000);
T = read(ds);
TrainingSet=T(1:1080,:);
size(T);
Alpha=.01;
m=length(T{:,1});
U0training=T{1:1080,2};
U0training=(U0training-mean(U0training))./std(U0training);
U0CV=T{1081:1500,2};
U0CV=(U0CV-mean(U0CV))./std(U0CV); %Normalizing output
UTraining=T{1:1080,6:10};
UCrossValidation=T{1081:1800,6:10};
LengthTraining = length(UTraining);
LengthCrossValidation = length(UCrossValidation);
z=length(UTraining);
XTraining=[ones(z,1) UTraining UTraining.^2];
XCrossValidation=[ones(length(UCrossValidation),1) UCrossValidation UCrossValidation.^2];
nTraining=length(XTraining(1,:));
for w=2:nTraining
if max(abs(XTraining(:,w)))~=0
XTraining(:,w)=(XTraining(:,w)-mean((XTraining(:,w))))./std(XTraining(:,w));
end
end
nCV=length(XCrossValidation(1,:));
for w=2:nCV
if max(abs(XCrossValidation(:,w)))~=0
XCrossValidation(:,w)=(XCrossValidation(:,w)-mean((XCrossValidation(:,w))))./std(XCrossValidation(:,w));
end
end
YTraining=TrainingSet{:,3}/mean(TrainingSet{:,3});
YCrossValidation=TrainingSet{11,3}/mean(TrainingSet{11,3});
ThetaTraining=zeros(nTraining,1);
ThetaCrossValidation=zeros(nCV,1);
% for k=1:500
%
% ETrain(k)=(1/(2*m))*sum((XTrain*ThetaTrain-Y).^2);
% ECV(k)=(1/(2*m))*sum((XCV*ThetaCV-Y).^2);
% end
k=1;
R=1;
ETraining=[];
ECrossValidation=[];
while R==1
Alpha=Alpha*1;
ThetaTraining=ThetaTraining-(Alpha/m)*XTraining'*(XTraining*ThetaTraining-YTraining);
ThetaCrossValidation=ThetaCrossValidation-(Alpha/m)*XCrossValidation'*(XCrossValidation*ThetaCrossValidation-YCrossValidation);
ETraining=(1/(2*m))*sum((XTraining*ThetaTraining-YTraining).^2);
ETraining=[ETraining;ETraining];
ECrossValidation(k)=(1/(2*m))*sum((XCrossValidation*ThetaCrossValidation-YCrossValidation).^2);
ECrossValidation=[ECrossValidation;ECrossValidation];
k=k+1
if ETraining(k-1)-ETraining(k)<0
break
end
q=(ETraining(k-1)-ETraining(k))./ETraining(k-1);
if q <.000001
R=0;
end
end
figure (1)
plot(k,ECrossValidation,'black')
hold on
plot(k,ETraining,'red')
legend('CV','Train')
title('House Price')
ylabel('Cost Fun')
xlabel('Iter')