Skip to content

Commit 64aea5e

Browse files
committed
no message
1 parent a35fbd2 commit 64aea5e

File tree

7 files changed

+84
-177
lines changed

7 files changed

+84
-177
lines changed

src/main/java/com/github/felipexw/classifiers/neighbors/SimpleKNNClassifier.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected List<LabeledInstance> getInstancesthatMaximizeAccuracy() {
5252
for (List<LabeledInstance> labeled : partitionedInstances) {
5353
for (LabeledInstance instance : labeled) {
5454
if (i != testIndex) {
55-
Neighbor neighbor = new Neighbor(instance, -1d, featureExtractor);
55+
Neighbor neighbor = new Neighbor(instance, -1d, null);
5656
List<Neighbor> neighbors = getNeighborsWithDistanceFromARootNeighboor(neighbor, k);
5757
features.put(neighbor, neighbors);
5858
} else
@@ -128,7 +128,7 @@ protected void setUpForTraining(List<LabeledInstance> instances) {
128128
protected void calculateFeatureSimilarities() {
129129
for (int i = 0; i < instances.size(); i++) {
130130
LabeledInstance instance = instances.get(i);
131-
Neighbor neighbor = new Neighbor(instance, -1d, featureExtractor);
131+
Neighbor neighbor = new Neighbor(instance, -1d, null);
132132

133133
List<Neighbor> neighbors = getNeighborsWithDistanceFromARootNeighboor(neighbor, this.k);
134134
features.put(neighbor, neighbors);
@@ -188,19 +188,18 @@ public List<Prediction> predict(List<Instance> instances) {
188188
}
189189

190190
protected List<Neighbor> getAllNeighbors(Instance labeledInstance) {
191-
throw new UnsupportedOperationException("Continue the refactoring.");
192-
/*
191+
192+
193193
List<Neighbor> neighborses = new ArrayList<>();
194194
for (short i = 0; i < instances.size(); i++) {
195195
LabeledInstance trainingInstance = instances.get(i);
196196
double distance = similarityCalculator.calculate(labeledInstance.getFeatures(), trainingInstance.getFeatures());
197197

198-
Neighbor neighbor = new Neighbor(trainingInstance, distance);
198+
Neighbor neighbor = new Neighbor(trainingInstance, distance, featureExtractor);
199199
neighborses.add(neighbor);
200200
}
201201

202202
return neighborses;
203-
*/
204203
}
205204

206205
@Override

src/main/java/com/github/felipexw/core/Instance.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
/**
66
* Created by felipe.appio on 23/08/2016.
77
*/
8-
public class Instance<F> {
8+
public abstract class Instance<F> {
99
protected List<F> features;
1010

11-
public List<F> getFeatures() {
12-
return features;
13-
}
11+
public abstract List<F> getFeatures();
1412
}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
package com.github.felipexw.core;
22

3+
import java.util.List;
34
import java.util.Objects;
45

56
/**
67
* Created by felipe.appio on 23/08/2016.
78
*/
8-
public class LabeledInstance<L extends Label, F extends Model> extends Instance<String> {
9+
public class LabeledInstance<L extends Label, F extends Model> extends Instance<F> {
910
protected L label;
1011
private int count;
1112

12-
public LabeledInstance(L label) {
13+
public LabeledInstance(List<F> features,L label) {
1314
this.label = label;
15+
this.features = features;
1416
}
1517

1618
public L getLabel() {
1719
return this.label;
1820
}
1921

20-
@Override
21-
public String toString() {
22-
return label.toString();
23-
}
24-
2522
public void setCount(int count) {
2623
this.count = count;
2724
}
2825

26+
@Override
27+
public List<F> getFeatures() {
28+
29+
return null;
30+
}
31+
2932
public int getCount() {
3033
return count;
3134
}
35+
36+
@Override
37+
public boolean equals(Object o) {
38+
if (this == o) return true;
39+
if (o == null || getClass() != o.getClass()) return false;
40+
41+
LabeledInstance<?, ?> that = (LabeledInstance<?, ?>) o;
42+
43+
return label.equals(that.label);
44+
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return label.hashCode();
50+
}
3251
}

src/main/java/com/github/felipexw/core/Model.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
*/
1111
public class Model implements FeatureVector {
1212

13-
private FeatureExtractor featureExtractor;
13+
protected FeatureExtractor featureExtractor;
14+
protected List features;
1415

1516
public Model(FeatureExtractor featureExtractor) {
1617
this.featureExtractor = featureExtractor;
1718
}
1819

1920
@Override
20-
public List getData(List source) {
21-
return featureExtractor.extract(source);
21+
public List getData() {
22+
return featureExtractor.extract(features);
2223
}
24+
2325
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.github.felipexw.core.extraction;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
45
import java.util.List;
56
import java.util.Objects;
67

78
/**
89
* Created by felipe.appio on 31/08/2016.
910
*/
10-
public class DoubleFeatureExtractor implements FeatureExtractor<Double>{
11+
public class DoubleFeatureExtractor implements FeatureExtractor<Double> {
1112

1213
@Override
1314
public List<Double> extract(List<Object> source) {
14-
return Arrays.asList(new Double(1d), new Double(2d), new Double(3d), new Double(4d));
15+
List<Double> target = new ArrayList<>();
16+
source.forEach(s -> target.add(Double.parseDouble(String.valueOf(s))));
17+
return target;
1518
}
1619
}

src/main/java/com/github/felipexw/core/extraction/FeatureVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
public interface FeatureVector<T> {
99

10-
List<T> getData(List<Object> source);
10+
List<T> getData();
1111
}

0 commit comments

Comments
 (0)