-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_nearest_neighbor.py
493 lines (418 loc) · 20.5 KB
/
k_nearest_neighbor.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import math
import copy
import Term
def calculate_cosine_similarity(essay_test_list, essay_training_list):
# Penggambaran Output:
# 1 Array berisi tiga element yang disebut weightingMethod : TF-IDF, TF-CHI, TF-RF
# Setiap weightingMethod berisi X elemen yang disebut dataTest, index i merepresentasikan ini data uji keberapa
# Setiap dataTest berisi Y elemen yang disebut cosineSimilarity, ini menyimpan hasil perhitungan data uji i dengan
# data latih j, j merepresentasikan data latih ke bereapa
all_test_data_cs_tfidf = []
all_test_data_cs_tfchi = []
all_test_data_cs_tfrf = []
for essay_test in essay_test_list:
all_train_data_cs_tfidf = []
all_train_data_cs_tfchi = []
all_train_data_cs_tfrf = []
for essay_training in essay_training_list:
term_list = get_train_test_term_for_cs(essay_test.term_list, essay_training.term_list)
dot_product = calculate_dot_product(term_list)
train_vector_length = calculate_vector_length(term_list['train'])
test_vector_length = calculate_vector_length(term_list['test'])
#cosine similarity TF-IDF
if dot_product['tfidf'] == 0 or train_vector_length['tfidf'] == 0 or test_vector_length['tfidf'] == 0:
cs_tfidf = float(0)
else:
cs_tfidf = float(dot_product['tfidf']) / (float(train_vector_length['tfidf']) * float(test_vector_length['tfidf']))
#cosine similarity TF-CHI
if dot_product['tfchi'] == 0 or train_vector_length['tfchi'] == 0 or test_vector_length['tfchi'] == 0:
cs_tfchi = float(0)
else:
cs_tfchi = float(dot_product['tfchi']) / (float(train_vector_length['tfchi']) * float(test_vector_length['tfchi']))
#cosine similarity TF-RF
if dot_product['tfrf'] == 0 or train_vector_length['tfrf'] == 0 or test_vector_length['tfrf'] == 0:
cs_tfrf = float(0)
else:
cs_tfrf = float(dot_product['tfrf']) / (float(train_vector_length['tfrf']) * float(test_vector_length['tfrf']))
all_train_data_cs_tfidf.append(cs_tfidf)
all_train_data_cs_tfchi.append(cs_tfchi)
all_train_data_cs_tfrf.append(cs_tfrf)
all_test_data_cs_tfidf.append(all_train_data_cs_tfidf)
all_test_data_cs_tfchi.append(all_train_data_cs_tfchi)
all_test_data_cs_tfrf.append(all_train_data_cs_tfrf)
return {'cs_tfidf': all_test_data_cs_tfidf, 'cs_tfchi': all_test_data_cs_tfchi, 'cs_tfrf': all_test_data_cs_tfrf}
def get_train_test_term_for_cs(test_term_list, train_term_list):
test_term_content = convert_to_array_string(test_term_list)
train_term_content = convert_to_array_string(train_term_list)
cur_train_term_list = []
for term in test_term_list:
if term.term_content in train_term_content:
cur_train_term_list.append(copy.deepcopy(train_term_list[train_term_content.index(term.term_content)]))
else:
t = Term.Term(term.term_content)
t.set_tf_idf(0)
t.set_tf_chi(0)
t.set_tf_rf(0)
cur_train_term_list.append(t)
cur_test_term_list = copy.deepcopy(test_term_list)
for term in train_term_list:
if term.term_content not in test_term_content:
t = Term.Term(term.term_content)
t.set_tf_idf(0)
t.set_tf_chi(0)
t.set_tf_rf(0)
cur_test_term_list.append(t)
cur_train_term_list.append(copy.deepcopy(train_term_list[train_term_content.index(term.term_content)]))
return {'train': cur_train_term_list, 'test': cur_test_term_list}
def convert_to_array_string(term_list):
array_string = []
for term in term_list:
array_string.append(term.term_content)
return array_string
def calculate_dot_product(term_list):
dot_product = {'tfidf': float(0), 'tfchi': float(0), 'tfrf': float(0)}
train_term_list = term_list['train']
test_term_list = term_list['test']
for index in range(len(test_term_list)):
dot_product['tfidf'] += float(test_term_list[index].tf_idf) * float(train_term_list[index].tf_idf)
dot_product['tfchi'] += float(test_term_list[index].tf_chi) * float(train_term_list[index].tf_chi)
dot_product['tfrf'] += float(test_term_list[index].tf_rf) * float(train_term_list[index].tf_rf)
return dot_product
def calculate_vector_length(term_list):
vector_length = {'tfidf': float(0), 'tfchi': float(0), 'tfrf': float(0)}
for index in range(len(term_list)):
vector_length['tfidf'] += float(math.pow(term_list[index].tf_idf, 2))
vector_length['tfchi'] += float(math.pow(term_list[index].tf_chi, 2))
vector_length['tfrf'] += float(math.pow(term_list[index].tf_rf, 2))
vector_length['tfidf'] = math.sqrt(vector_length['tfidf'])
vector_length['tfchi'] = math.sqrt(vector_length['tfchi'])
vector_length['tfrf'] = math.sqrt(vector_length['tfrf'])
return vector_length
def knn_classification(cosine_similarity_list, essay_training_list, essay_test_list, score_category):
temp = get_essay_training_id_score(essay_training_list)
essay_training_id_list = temp['id_list']
essay_training_score_list = temp['score_list']
classification_result = []
for i in range(len(cosine_similarity_list)): #looping untuk memberikan nilai bagi setiap data uji
essay_test = essay_test_list[i]
result = {'essay_id': essay_test.essay_id, 'actual_score': essay_test.human_rater_score,
'k1_predicted_score': None, 'k3_predicted_score': None, 'k5_predicted_score': None,
'k7_predicted_score': None, 'k9_predicted_score': None}
# buat tuple (cosine similarity, essay_train_id, essay_train_score)
sorted_data_training = sorted(zip(cosine_similarity_list[i], essay_training_id_list, essay_training_score_list),
reverse=True)
result['k1_predicted_score'] = sorted_data_training[0][2]
result['k3_predicted_score'] = k_neighbor_classification(3, sorted_data_training[:3])
result['k5_predicted_score'] = k_neighbor_classification(5, sorted_data_training[:5])
result['k7_predicted_score'] = k_neighbor_classification(7, sorted_data_training[:7])
result['k9_predicted_score'] = k_neighbor_classification(9, sorted_data_training[:9])
classification_result.append(result)
return calculate_accuracy(classification_result)
def get_essay_training_id_score(essay_training_list):
essay_training_id_list = []
essay_training_score_list = []
for essay in essay_training_list:
essay_training_id_list.append(essay.essay_id)
essay_training_score_list.append(essay.human_rater_score)
return {'id_list': essay_training_id_list, 'score_list': essay_training_score_list}
def k_neighbor_classification(k, top_k_similar_data):
list_score = ["A", "B", "C", "D"]
class_vote = [0, 0, 0, 0]
for i in range(k):
human_score = top_k_similar_data[i][2] #struktur tuple: (nilai cosine similarity, essay id, essay score)
if human_score == "A":
class_vote[0] += 1
elif human_score == "B":
class_vote[1] += 1
elif human_score == "C":
class_vote[2] += 1
elif human_score == "D":
class_vote[3] += 1
max_vote = max(class_vote)
sorted_class_vote = sorted(zip(class_vote, list_score), reverse=True)
if k == 3:
if max_vote == 1:
return top_k_similar_data[0][2]
else:
return list_score[class_vote.index(max_vote)]
if k == 5:
if max_vote == 2:
return get_score_with_greater_prior(sorted_class_vote, max_vote, top_k_similar_data)
else:
return list_score[class_vote.index(max_vote)]
if k == 7:
if max_vote == 2 or max_vote == 3:
return get_score_with_greater_prior(sorted_class_vote, max_vote, top_k_similar_data)
else:
return list_score[class_vote.index(max_vote)]
if k == 9:
if max_vote == 3 or max_vote == 4:
return get_score_with_greater_prior(sorted_class_vote, max_vote, top_k_similar_data)
else:
return list_score[class_vote.index(max_vote)]
def get_score_with_greater_prior(sorted_class_vote, max_vote, top_k_similar_data):
list_possible_score = []
for i in range (0, len(sorted_class_vote)):
if sorted_class_vote[i][0] == max_vote:
list_possible_score.append(sorted_class_vote[i][1])
if len(list_possible_score) == 1:
return sorted_class_vote[0][1]
else:
score = ""
for i in range(0,len(top_k_similar_data)):
if top_k_similar_data[i][2] in list_possible_score:
score = top_k_similar_data[i][2]
break
return score
def k3_neighbor_classification(top_three_similar_data):
list_score = ["A", "B", "C", "D"]
class_vote = [0, 0, 0, 0]
for j in range(3):
human_score = top_three_similar_data[j][2] #struktur tuple: (nilai cosine similarity, essay id, essay score)
if human_score == "A":
class_vote[0] += 1
elif human_score == "B":
class_vote[1] += 1
elif human_score == "C":
class_vote[2] += 1
elif human_score == "D":
class_vote[3] += 1
max_vote = max(class_vote)
if max_vote == 1:
return top_three_similar_data[0][2]
else:
return list_score[class_vote.index(max_vote)]
def k5_neighbor_classification(top_five_similar_data):
list_score = ["A", "B", "C", "D"]
class_vote = [0, 0, 0, 0]
for j in range(5):
human_score = top_five_similar_data[j][2]
if human_score == "A":
class_vote[0] += 1
elif human_score == "B":
class_vote[1] += 1
elif human_score == "C":
class_vote[2] += 1
elif human_score == "D":
class_vote[3] += 1
max_vote = max(class_vote)
if max_vote == 1:
return top_five_similar_data[0][2]
elif max_vote == 2:
sorted_class_vote = sorted(zip(class_vote, list_score), reverse=True)
if sorted_class_vote[0][0] == sorted_class_vote[1][0]:
# Apabila ada 2 nilai yang masing-masing kemunculannya 2x
for j in range(5):
score = top_five_similar_data[j][2]
if score == sorted_class_vote[0][1] or score == sorted_class_vote[1][1]:
#ambil nilai yang memiliki kemunculan 2x dan nilai cosine similaritynya tertinggi
return score
else:
#apabila hanya ada 1 nilai yang kemunculannya 2x
return list_score[class_vote.index(max_vote)]
else:
return list_score[class_vote.index(max_vote)]
def calculate_accuracy(classification_result):
k1_correct = 0
k3_correct = 0
k5_correct = 0
k7_correct = 0
k9_correct = 0
total_data_test = len(classification_result)
for result in classification_result:
print "Klasifikasi data uji ID: {0}, Actual score: {1}, 1-NN score: {2}, 3-NN score: {3}, 5-NN score: {4}, " \
"7-NN score: {5}, 9-NN score: {6}".format(
result['essay_id'], result['actual_score'], result['k1_predicted_score'], result['k3_predicted_score'],
result['k5_predicted_score'], result['k7_predicted_score'], result['k9_predicted_score'])
if result['actual_score'] == result['k1_predicted_score']:
k1_correct += 1
if result['actual_score'] == result['k3_predicted_score']:
k3_correct += 1
if result['actual_score'] == result['k5_predicted_score']:
k5_correct += 1
if result['actual_score'] == result['k7_predicted_score']:
k7_correct += 1
if result['actual_score'] == result['k9_predicted_score']:
k9_correct += 1
print "Hasil Correct Prediction 1-NN: {0}, 3-NN: {1}, 5-NN: {2}, 7-NN:{3}, 9-NN: {4}, jumlah data uji: {5}".\
format(k1_correct, k3_correct, k5_correct, k7_correct, k9_correct, total_data_test)
k1_accuracy = (float(k1_correct) / float(total_data_test)) * 100.0
k3_accuracy = (float(k3_correct) / float(total_data_test)) * 100.0
k5_accuracy = (float(k5_correct) / float(total_data_test)) * 100.0
k7_accuracy = (float(k7_correct) / float(total_data_test)) * 100.0
k9_accuracy = (float(k9_correct) / float(total_data_test)) * 100.0
print "Akurasi k1, {0}, k3: {1}, k5: {2}, k7: {3}, k9: {4}".format(k1_accuracy, k3_accuracy ,k5_accuracy, k7_accuracy, k9_accuracy)
accuracy = {'k1_accuracy': k1_accuracy, 'k3_accuracy': k3_accuracy, 'k5_accuracy': k5_accuracy, 'k7_accuracy': k7_accuracy,
'k9_accuracy': k9_accuracy}
return accuracy
#========================= ONE SIDED SAMPLING ================================================#
def calculate_cosine_similarity_tfidf(essay_test_list, essay_training_list):
essay_test = essay_test_list[0]
cosine_similarity_result = []
for essay_training in essay_training_list:
term_list = get_train_test_term_for_cs(essay_test.term_list, essay_training.term_list)
dot_product = calculate_dot_product(term_list)
train_vector_length = calculate_vector_length(term_list['train'])
test_vector_length = calculate_vector_length(term_list['test'])
#cosine similarity TF-IDF
if dot_product['tfidf'] == 0 or train_vector_length['tfidf'] == 0 or test_vector_length['tfidf'] == 0:
cs_tfidf = float(0)
else:
cs_tfidf = float(dot_product['tfidf']) / (float(train_vector_length['tfidf']) * float(test_vector_length['tfidf']))
cosine_similarity_result.append(cs_tfidf)
return cosine_similarity_result
def one_nn_classification(cosine_similarity_result, essay_training_list, essay_test_list):
temp = get_essay_training_id_score(essay_training_list)
essay_training_id_list = temp['id_list']
essay_training_score_list = temp['score_list']
essay_test = essay_test_list[0]
result = {'essay_id': essay_test.essay_id, 'actual_score': essay_test.human_rater_score,
'k1_predicted_score': None}
# buat tuple (cosine similarity, essay_train_id, essay_train_score)
top_one_similar_data = sorted(zip(cosine_similarity_result, essay_training_id_list, essay_training_score_list),
reverse=True)[:1]
result['k1_predicted_score'] = top_one_similar_data[0][2]
print result
if result['actual_score'] != result['k1_predicted_score']:
return True
else:
return False
#=============================================================================================#
def create_confusion_matrix(classification_result):
"""
contoh confusion matrix
[P/A======Nilai A Nilai B Nilai C Nilai D]
[Nilai A [0,0] [0,1] [0,2] [0,3]
[Nilai B [1,0] [1,1] [1,2] [1,3]
[Nilai C [2,0] [2,1] [2,2] [2,3]
[Nilai D [3,0] [3,1] [3,2] [3,3]
Dimana untuk True Positif adalah nilai diagonal
False Negatif adalah nilai column
False Positif adalah nilai row
"""
k3_confusion_matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
k5_confusion_matrix = [[0, 0, 0, 0], [0, 0, 0], 0, [0, 0, 0, 0], [0, 0, 0, 0]]
for result in classification_result:
actual = result['actual_score']
k3_predicted = result['k3_predicted_score']
k5_predicted = result['k5_predicted_score']
if actual == "A":
if k3_predicted == "A":
k3_confusion_matrix[0][0] += 1
elif k3_predicted == "B":
k3_confusion_matrix[1][0] += 1
elif k3_predicted == "C":
k3_confusion_matrix[2][0] += 1
else:
k3_confusion_matrix[3][0] += 1
if k5_predicted == "A":
k5_confusion_matrix[0][0] += 1
elif k5_predicted == "B":
k5_confusion_matrix[1][0] += 1
elif k5_predicted == "C":
k5_confusion_matrix[2][0] += 1
else:
k5_confusion_matrix[3][0] += 1
elif actual == "B":
if k3_predicted == "A":
k3_confusion_matrix[0][1] += 1
elif k3_predicted == "B":
k3_confusion_matrix[1][1] += 1
elif k3_predicted == "C":
k3_confusion_matrix[2][1] += 1
else:
k3_confusion_matrix[3][1] += 1
if k5_predicted == "A":
k5_confusion_matrix[0][1] += 1
elif k5_predicted == "B":
k5_confusion_matrix[1][1] += 1
elif k5_predicted == "C":
k5_confusion_matrix[2][1] += 1
else:
k5_confusion_matrix[3][1] += 1
elif actual == "C":
if k3_predicted == "A":
k3_confusion_matrix[0][2] += 1
elif k3_predicted == "B":
k3_confusion_matrix[1][2] += 1
elif k3_predicted == "C":
k3_confusion_matrix[2][2] += 1
else:
k3_confusion_matrix[3][2] += 1
if k5_predicted == "A":
k5_confusion_matrix[0][2] += 1
elif k5_predicted == "B":
k5_confusion_matrix[1][2] += 1
elif k5_predicted == "C":
k5_confusion_matrix[2][2] += 1
else:
k5_confusion_matrix[3][2] += 1
else:
if k3_predicted == "A":
k3_confusion_matrix[0][3] += 1
elif k3_predicted == "B":
k3_confusion_matrix[1][3] += 1
elif k3_predicted == "C":
k3_confusion_matrix[2][3] += 1
else:
k3_confusion_matrix[3][3] += 1
if k5_predicted == "A":
k5_confusion_matrix[0][3] += 1
elif k5_predicted == "B":
k5_confusion_matrix[1][3] += 1
elif k5_predicted == "C":
k5_confusion_matrix[2][3] += 1
else:
k5_confusion_matrix[3][0] += 1
return {'k3_conf_matrix': k3_confusion_matrix, 'k5_conf_matrix': k5_confusion_matrix}
def performance_evaluation(conf_matrix, score_category):
tp_a = conf_matrix[0][0]
fp_a = conf_matrix[0][1] + conf_matrix[0][2] + conf_matrix[0][3]
fn_a = conf_matrix[1][0] + conf_matrix[2][0] + conf_matrix[3][0]
if tp_a == 0:
precision_a = 0
recall_a = 0
else:
precision_a = (float(tp_a) / float(tp_a + fp_a)) * 100.0
recall_a = (float(tp_a) / float(tp_a + fn_a)) * 100.0
tp_b = conf_matrix[1][1]
fp_b = conf_matrix[1][0] + conf_matrix[1][2] + conf_matrix[1][3]
fn_b = conf_matrix[0][1] + conf_matrix[2][1] + conf_matrix[3][1]
if tp_b == 0:
precision_b = 0
recall_b = 0
else:
precision_b = (float(tp_b) / float(tp_b + fp_b)) * 100.0
recall_b = (float(tp_b) / float(tp_b + fn_b)) * 100.0
if score_category < 3:
sum_precision = precision_a + precision_b
sum_recall = recall_a + recall_b
else:
tp_c = conf_matrix[2][2]
fp_c = conf_matrix[2][0] + conf_matrix[2][1] + conf_matrix[2][3]
fn_c = conf_matrix[0][2] + conf_matrix[1][2] + conf_matrix[3][2]
if tp_c == 0:
precision_c = 0
recall_c = 0
else:
precision_c = (float(tp_c) / float(tp_c + fp_c)) * 100.0
recall_c = (float(tp_c) / float(tp_c + fn_c)) * 100.0
if score_category == 4:
tp_d = conf_matrix[3][3]
fp_d = conf_matrix[3][0] + conf_matrix[3][1] + conf_matrix[3][2]
fn_d = conf_matrix[0][3] + conf_matrix[1][3] + conf_matrix[2][3]
if tp_d == 0:
precision_d = 0
recall_d = 0
else:
precision_d = (float(tp_d) / float(tp_d + fp_d)) * 100.0
recall_d = (float(tp_d) / float(tp_d + fn_d)) * 100.0
sum_precision = precision_a + precision_b + precision_c + precision_d
sum_recall = recall_a + recall_b + recall_c + recall_d
else:
sum_precision = precision_a + precision_b + precision_c
sum_recall = recall_a + recall_b + recall_c
macro_avg_precision = 1.0 / (float(score_category) * sum_precision)
macro_avg_recall = 1.0 / (float(score_category) * sum_recall)
f_measure = (2 * macro_avg_precision * macro_avg_recall) / (macro_avg_precision + macro_avg_recall)
return {'macro_avg_precision': macro_avg_precision, 'macro_avg_recall': macro_avg_recall, 'f_measure': f_measure}