-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
knn.sg
50 lines (35 loc) · 1.44 KB
/
knn.sg
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
CSVFile f_feats_train("../data/classifier_4class_2d_linear_features_train.dat")
CSVFile f_feats_test("../data/classifier_4class_2d_linear_features_test.dat")
CSVFile f_labels_train("../data/classifier_4class_2d_linear_labels_train.dat")
CSVFile f_labels_test("../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
RealFeatures features_train(f_feats_train)
RealFeatures features_test(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
#![create_features]
#![choose_distance]
EuclideanDistance distance(features_train, features_test)
#![choose_distance]
#![create_instance]
KNN knn(3, distance, labels_train)
#![create_instance]
#![train_and_apply]
knn.train()
Labels labels_predict = knn.apply(features_test)
#![train_and_apply]
# integration testing variables
MulticlassAccuracy eval()
Real accuracy = eval.evaluate(labels_predict, labels_test)
RealVector output = labels_test.get_values()
RealMatrix mat = features_train.get_feature_matrix()
####################################################
# Integration testing: Store all numerical results.#
####################################################
#119 is char for 'w'
SerializableAsciiFile f_results("knn_results.txt", 119)
DynamicObjectArray results()
results.append_element_wrapped(output, "output")
results.append_element_wrapped(mat, "mat")
results.append_element_wrapped(accuracy, "accuracy")
results.save_serializable(f_results)