- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathKNN.java
More file actions
Latest commit
113 lines (96 loc) · 3.21 KB
/
Copy pathKNN.java
File metadata and controls
113 lines (96 loc) · 3.21 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
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
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclassKNN {
privateintk;
privateList<double[]> X_train;
privateList<Integer> y_train;
publicKNN(intk) {
this.k = k;
X_train = newArrayList<>();
y_train = newArrayList<>();
}
publicvoidfit(List<double[]> X, List<Integer> y) {
X_train = X;
y_train = y;
}
publicList<Integer> predict(List<double[]> X) {
List<Integer> y_pred = newArrayList<>();
for (double[] x : X) {
intpredictedLabel = _predict(x);
y_pred.add(predictedLabel);
}
returny_pred;
}
privateint_predict(double[] x) {
double[] distances = newdouble[X_train.size()];
for (inti = 0; i < X_train.size(); i++) {
distances[i] = euclideanDistance(x, X_train.get(i));
}
int[] indices = argsort(distances);
Map<Integer, Integer> labelCount = newHashMap<>();
for (inti = 0; i < k; i++) {
intlabel = y_train.get(indices[i]);
labelCount.put(label, labelCount.getOrDefault(label, 0) + 1);
}
intmostCommonLabel = -1;
intmaxCount = -1;
for (Map.Entry<Integer, Integer> entry : labelCount.entrySet()) {
intlabel = entry.getKey();
intcount = entry.getValue();
if (count > maxCount) {
maxCount = count;
mostCommonLabel = label;
}
}
returnmostCommonLabel;
}
privatedoubleeuclideanDistance(double[] x1, double[] x2) {
doublesum = 0.0;
for (inti = 0; i < x1.length; i++) {
sum += Math.pow(x1[i] - x2[i], 2);
}
returnMath.sqrt(sum);
}
privateint[] argsort(double[] array) {
int[] indices = newint[array.length];
for (inti = 0; i < indices.length; i++) {
indices[i] = i;
}
for (inti = 0; i < indices.length - 1; i++) {
for (intj = i + 1; j < indices.length; j++) {
if (array[indices[i]] > array[indices[j]]) {
inttemp = indices[i];
indices[i] = indices[j];
indices[j] = temp;
}
}
}
returnindices;
}
publicstaticvoidmain(String[] args) {
List<double[]> X_train = newArrayList<>();
X_train.add(newdouble[] { 1, 2 });
X_train.add(newdouble[] { 1.5, 1.8 });
X_train.add(newdouble[] { 5, 8 });
X_train.add(newdouble[] { 8, 8 });
X_train.add(newdouble[] { 1, 0.6 });
X_train.add(newdouble[] { 9, 11 });
List<Integer> y_train = newArrayList<>();
y_train.add(0);
y_train.add(0);
y_train.add(1);
y_train.add(1);
y_train.add(0);
y_train.add(1);
KNNknn = newKNN(3);
knn.fit(X_train, y_train);
List<double[]> X_test = newArrayList<>();
X_test.add(newdouble[] { 1, 2.5 });
X_test.add(newdouble[] { 3, 4 });
X_test.add(newdouble[] { 8, 9 });
List<Integer> y_pred = knn.predict(X_test);
System.out.println(y_pred);
}
}