ML2
ML2
load('fg.mat')
data = fg(:,2:17);
label= fg(:,1)
k = fg(:,1);
ens = fitensemble(data,k,'AdaBoostM1',50,'Tree');
bag = fitensemble(data,k,'Bag',200,'Tree',...
'Type','Classification')
YfitS = (predict(bag,data));
YfitS1 = (predict(ens,data));
sum=0;
for i = 1:1576
if((YfitS(:,1)==label(:,1)))
sum = sum+1;
end
end
accuracy1 = sum\size(fg(:,1));
sum1=0;
for i = 1:1576
if((YfitS1(:,1)==label(:,1)))
end
end
accuracy2 = sum1\size(fg(:,1));
end
X_tr = fg(1:1000,:);
y_tr = fg(1:1000,1);
X_te = fg(1001:1576,:);
Y_te = fg(1001:1576,1);
n_trees = 100;
N = size(X_tr, 1);
% training collection
training = ones(n_trees, N);
testing = ones(n_trees, size(X_te, 1));
for t = 1:n_trees
stump = fitctree(X_tr, y_tr, "SplitCriterion", "deviance", "MaxNumSplits",
1, "Weights", D);
% learn g_t from the stump for both train and test sets
g_t = predict(stump, X_tr);
g_t_test = predict(stump, X_te);
% calculate epsilon_t
errors = g_t ~= y_tr;
products = D .* errors;
epsilon_t = sum(products);
% calculate alpha_t
alpha(t) = 1.0/2.0 * log((1-epsilon_t) / epsilon_t);
% reweight rule
D = D .* exp( (-1) * alpha(t) .* g_t .* y_tr);
% adjusting for normalization constant
Z = sum(D);
D = D ./ Z;
% aggregation rule
Gx = sign(sum(alpha .* training));
Gx_test = sign(sum(alpha .* testing));
figure;
hold on;
plot(1:n_trees, train_err, 'Color', 'red');
plot(1:n_trees, test_err, 'Color', [0 0.39 0]);
ylabel("Training & Testing Error");
xlabel("# of weak hypotheses");
legend("Training Error", "Testing Error");
if option == 1
title("One (1) vs. Three (3) Problem - AdaBoost");
elseif option == 3
title("Three (3) vs. Five (5) Problem - AdaBoost ");
end
end
X=data;
Y = label;
testX = fg(1:1000,:);
testY = fg(1001:1576,1);
numBags = 100;
option=1;
if option == 1
Y = Y - 2;
testY = testY - 2;
elseif option == 3
Y = Y - 4;
testY = testY - 4;
end
baggingSize = size(X,1);
OOB = zeros(numBags, 1);
for t=1:numBags
if testX == 0
input = X(data, :);
output = Y(index);
ct = fitctree(input, output);
% learning hypotheses
G(unique, t) = predict(ct, uniqueX);
else
input = testX(data, :);
output = testY(index);
uniqueX = testX;
if option == 1
title("One (1) vs. Three (3) Problem");
elseif option == 3
title("Three (3) vs. Five (5) Problem");
end
end
end
q2)
Q2)
load('fg.mat')
data = fg(:,2:17);
label = fg(:,1);
xt = data';
yt = label';
load('fg.mat')
data = fg(:,2:17);
label = fg(:,1);
xt = data';
yt = label';
[remse_val, rmse_train] = mlp(xt,yt)
for i = 1:60
hiddenlayersize=i;
net = fitnet(hiddenlayersize);
net.divideParam.trainRatio=70/100;
net.divideParam.valRatio = 30/100;
net.divideParam.testRatio = 0/100;
[net, tr] = train(net,xt,yt);
ytrain = exp(net(xt(:,tr.trainInd)))-1;
ytrainTrue = exp(yt(tr.trainInd))-1;
yval = exp(net(xt(:,tr.valInd)))-1;
yValTrue = exp(yt(tr.valInd))-1;
rmse_val(i)=sqrt(mean((yval-yValTrue).^2));
rmse_train(i) = sqrt(mean((ytrain-ytrainTrue).^2));
end
Q
3)
load('fg.mat')
data = fg(:,2:17);
label= fg(:,1)
ens = fitensemble(data,k,'AdaBoostM1',50,'Tree');
bag = fitensemble(data,k,'Bag',200,'Tree',...
'Type','Classification')
0.0019
cvtrainAccuracy =
0.9981
6.3452e-04
cvtrainAccuracy =
0.9994
MLP error == .001
Acuuracy : 0.9990