You are on page 1of 4

Jan Alfred - 1701533272

1. Regresi (Simple Linear Regression)


Dataset
Abalone (http://archive.ics.uci.edu/ml/datasets/Abalone)
Jumlah Data 4177
X
Length
Y
Diameter
Metode
Simple Linear Regression
Figure

Error Rate
Code

2.651938637532004
clc;
clear;
abalone = importdata('Abalone/abalone.data',',');
X = abalone(:,2);
Y = abalone(:,3);
format long
X1 = [X ones(length(X),1)];
w = inv(X1' * X1) * (X1' * Y);
value = X * w(1,1);
error = Y - value;
E = error' * error;
disp(E);

Jan Alfred - 1701533272


%figure
scatter(X,Y)
hold on
plot(X,value)
xlabel('Length')
ylabel('Diameter')
title('Linear Regression Relation Between Abalone
Length & Diameter')
grid on

2. Klasifikasi (Nearest Neighbor)


Dataset
Abalone (http://archive.ics.uci.edu/ml/datasets/Abalone)
Jumlah Data 4177
x1
Length
x2
Diameter
y
Sex
Metode
Nearest Neighbor
Figure

Accuracy
Error Rate
Code

35.928143712574851
64.071856287425149
clc;
clear;
%initialization
k = 3; %3 class
max_epoch = 20;
train_data = 0.6; %60% data for training
abalone = importdata('Abalone/abalone.data',',');

Jan Alfred - 1701533272


X1 = abalone(:,2) %diameter
X2 = abalone(:,3) %length
Y = abalone(:,1) %sex (1 = M, 2 = F, 3 = I)
X = [X1, X2, Y];
plot(X);
n_train = ceil(length(X) * 0.6);
n_test = length(X) - n_train;
for i = 1:max_epoch
d_random = randperm(length(X));
d_train =
[X1(d_random(1:n_train)),X2(d_random(1:n_train)),Y(d_
random(1:n_train))];
d_test =
[X1(d_random(n_train+1:length(X))),X2(d_random(n_trai
n+1:length(X))),Y(d_random(n_train+1:length(X)))];
Error = 0;
for j = 1:n_test
d = zeros(2,n_test);
for q = 1:n_train
sigma_X1 = power((d_test(j,1) d_train(q,1)),2);
sigma_X2 = power((d_test(j,2) d_train(q,2)),2);

end

d(1,q) = sqrt(sigma_X1 + sigma_X2);


d(2,q) = d_train(q,3);

d = d';
d_sort = sortrows(d,1);
vote = zeros(k:2);
pool_vote = zeros(k:1);
for q = 1:k
vote(q,1) = d_sort(q,1);
vote(q,2) = d_sort(q,2);
if vote(q,2) == 0
pool_vote(q) = 0;
else
pool_vote(q) = 1;
end
end
target = mode(pool_vote(:,:));
if d_test(j,3) == target

Jan Alfred - 1701533272


Error = Error + 0;
else
Error = Error + 1;
end
end
p_error = Error *100/length(d_test);
accuracy = (length(d_test)-Error) * 100 /
length(d_test);
disp(p_error);
disp(accuracy);
end