-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample12.java
More file actions
64 lines (44 loc) · 1.86 KB
/
Example12.java
File metadata and controls
64 lines (44 loc) · 1.86 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
package applications.ml;
import datastructs.interfaces.IVector;
import datastructs.maths.DenseMatrixSet;
import maths.ConfusionMatrix;
import maths.functions.distances.EuclideanVectorCalculator;
import ml.classifiers.KNNClassifier;
import utils.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** Category: Machine Learning
* ID: Example11
* Description: Using the ```ConfusionMatrix``` class
* Taken From:
* Taken From:
* Details:
* TODO
*/
public class Example12 {
public static void main(String[] args) throws IOException, IllegalArgumentException{
// load the data set
Pair<DenseMatrixSet<Double>, List<Integer>> data = DataSetLoader.loadIrisDataSet();
KNNClassifier<Double, DenseMatrixSet<Double>,
EuclideanVectorCalculator<Double>,
ClassificationVoter> classifier = new KNNClassifier<Double, DenseMatrixSet<Double>,
EuclideanVectorCalculator<Double>, ClassificationVoter>(2, false);
classifier.setDistanceCalculator(new EuclideanVectorCalculator<Double>());
classifier.setMajorityVoter(new ClassificationVoter());
classifier.train(data.first, data.second);
List<Pair<Integer, Integer>> results = new ArrayList<>();
for(int rowIdx=0; rowIdx<data.first.m(); ++rowIdx){
IVector<Double> row = data.first.getRow(rowIdx);
Integer classIdx = data.second.get(rowIdx);
Integer predictedIdx = classifier.predict(row);
results.add(PairBuilder.makePair(classIdx, predictedIdx ));
}
List<String> names = new ArrayList<>();
names.add("Iris-setosa");
names.add("Iris-versicolor");
names.add("Iris-virginica");
ConfusionMatrix confusionMatrix = new ConfusionMatrix(results, names);
System.out.println(confusionMatrix.toString());
}
}