-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNNmodel_classification.m
More file actions
56 lines (41 loc) · 1.49 KB
/
KNNmodel_classification.m
File metadata and controls
56 lines (41 loc) · 1.49 KB
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
%% KNN Model
% knn.m 이후 돌리기
nTestDataPerUser = 5;
nTrainingDataPerUser = nDataPerUser-nTestDataPerUser;
for i = 1:userNum
trainingAnswer((i - 1) * nTrainingDataPerUser + (1:nTrainingDataPerUser)) = i;
end
for i = 1:userNum
testAnswer((i - 1) * nTestDataPerUser + (1:nTestDataPerUser)) = i;
end
testIndexes = combnk(1:nDataPerUser,nTestDataPerUser);
total_accuracy = 0;
for cnt = 1:length(testIndexes)
thisTestIndexes = [];
for cnt2 = 1:userNum
thisTestIndexes((cnt2-1)*nTestDataPerUser+(1:nTestDataPerUser)) = testIndexes(cnt,:)+(cnt2-1)*nDataPerUser;
end
trainingData = [];
testData = [];
for cnt3 = 1:userNum*nDataPerUser
isContinue = false;
for idx = thisTestIndexes
if cnt3 == idx
testData = [testData; data(cnt3,:)];
isContinue = true;
break;
end
end
if isContinue
continue;
end
trainingData = [trainingData; data(cnt3,:)];
end
knn_model = fitcknn(trainingData, trainingAnswer, 'NumNeighbors', nn_k);
model_result = testAnswer'== predict(knn_model, testData);
accuracy = sum(model_result)/length(testAnswer);
thisTestIndexes
total_accuracy = total_accuracy + accuracy;
fprintf('%d : knn model accuracy : %f \n', cnt, accuracy);
end
fprintf('knn model accuracy : %f \n\n', total_accuracy/length(testIndexes));