-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classifier.java
187 lines (157 loc) · 6.04 KB
/
Classifier.java
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
package com.dead.acctivi_classification;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.dead.acctivi_classification.distanceAlgorithm.DistanceAlgorithm;
import com.dead.acctivi_classification.distanceAlgorithm.EuclideanDistance;
public class Classifier {
private int K;
private double splitRatio;
private double accuracy = 0;
private DistanceAlgorithm distanceAlgorithm;
private List<DataPoint> listDataPoint;
private List<DataPoint> listTrainData;
private List<DataPoint> listTestData;
private List<DataPoint> listTestValidator;
private List<Double> listDistance;
public Classifier(){
K = 11;
splitRatio = 0.8;
distanceAlgorithm = new EuclideanDistance();
listDataPoint = new ArrayList<>();
listTrainData = new ArrayList<>();
listTestData = new ArrayList<>();
listTestValidator = new ArrayList<>();
}
public int getK() {
return K;
}
public void setK(int k) {
K = k;
}
public double getSplitRatio() {
return splitRatio;
}
public void setSplitRatio(double splitRatio) {
this.splitRatio = splitRatio;
}
public List<DataPoint> getListDataPoint() {
return listDataPoint;
}
public void setListDataPoint(List<DataPoint> listDataPoint) {
this.listDataPoint.clear();
this.listDataPoint.addAll(listDataPoint);
}
public List<DataPoint> getListTrainData() {
return listTrainData;
}
public List<DataPoint> getListTestData() {
return listTestData;
}
public DistanceAlgorithm getDistanceAlgorithm() {
return distanceAlgorithm;
}
public void setDistanceAlgorithm(DistanceAlgorithm distanceAlgorithm) {
this.distanceAlgorithm = distanceAlgorithm;
}
public double getAccuracy() {
return accuracy;
}
public void splitData(){
listTestData.clear();
listTrainData.clear();
int trainSize = (int)(listDataPoint.size() * splitRatio);
int testSize = listDataPoint.size() - trainSize;
Collections.shuffle(listDataPoint);
for (int i = 0;i < trainSize; i++)
listTrainData.add(listDataPoint.get(i));
for (int i = 0; i < testSize; i++){
DataPoint dataPointTest = new DataPoint(listDataPoint.get(i + trainSize));
DataPoint dataPointValidator = new DataPoint(dataPointTest);
dataPointTest.setCategory(Category.TEST);
listTestData.add(dataPointTest);
listTestValidator.add(dataPointValidator);
}
}
private List<Double> calculateDistances(DataPoint point){
List<Double> listDistance = new ArrayList<>();
for (DataPoint dataPoint:listTrainData){
double distance = distanceAlgorithm.calculateDistance(point.getMY(), point.getVY(),point.getSDY(),point.getMZ(), point.getVZ(),point.getSDZ(),
dataPoint.getMY(), dataPoint.getVY(),dataPoint.getSDY(),dataPoint.getMZ(), dataPoint.getVZ(),dataPoint.getSDZ());
listDistance.add(distance);
}
return listDistance;
}
// NOT SURE WHATS HAPPENING
private Category getMaxCategory(HashMap<Category, Integer> hashMap){
Iterator<Map.Entry<Category, Integer>> iterator = hashMap.entrySet().iterator();
int maxCategory = Integer.MIN_VALUE;
Category category = null;
while (iterator.hasNext()) {
Map.Entry<Category, Integer> item = iterator.next();
if (item.getValue() > maxCategory){
category = item.getKey();
}
}
return category;
}
private Category classifyDataPoint(DataPoint point){
HashMap<Category, Integer> hashMap = new HashMap<>();
listDistance = calculateDistances(point);
for (int i = 0; i < K; i++){
double min = Double.MAX_VALUE;
int minIndex = -1;
for (int j = 0; j < listDistance.size(); j++){
if (listDistance.get(j) < min){
min = listDistance.get(j);
minIndex = j;
}
}
Category category = listTrainData.get(minIndex).getCategory();
if (hashMap.containsKey(category)){
hashMap.put(category, hashMap.get(category) + 1);
}else{
hashMap.put(category, 1);
}
listDistance.set(minIndex, Double.MAX_VALUE);
}
return getMaxCategory(hashMap);
}
public void classify(){
accuracy = 0;
for (int i = 0;i < listTestData.size(); i++){
DataPoint dataPoint = listTestData.get(i);
Category category = classifyDataPoint(dataPoint);
if (isCorrect(category, listTestValidator.get(i).getCategory()))
accuracy++;
dataPoint.setCategory(category);
}
accuracy /= listTestData.size();
}
Category predictNew(double mY, double vY, double sdY, double mZ, double vZ, double sdZ){
DataPoint dataPoint = new DataPoint(mY,vY,sdY,mZ,vZ,sdZ,Category.values()[4]);
dataPoint.setCategory(Category.TEST);
Category category = classifyDataPoint(dataPoint);
return category;
}
void addTrainData(){
listTestData.clear();
listTrainData.clear();
int trainSize = (int)(listDataPoint.size() * 1);
Collections.shuffle(listDataPoint);
for (int i = 0;i < trainSize; i++){
listTrainData.add(listDataPoint.get(i));
}
}
private boolean isCorrect(Category predictedCategory, Category trueCategory){
return predictedCategory.equals(trueCategory);
}
public void reset() {
listDataPoint.clear();
listTestData.clear();
listTrainData.clear();
}
}