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 package org.openimaj.ml.clustering.spectral;
31
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.apache.log4j.Logger;
36 import org.openimaj.experiment.evaluation.cluster.ClusterEvaluator;
37 import org.openimaj.experiment.evaluation.cluster.analyser.FullMEAnalysis;
38 import org.openimaj.experiment.evaluation.cluster.analyser.FullMEClusterAnalyser;
39 import org.openimaj.experiment.evaluation.cluster.processor.Clusterer;
40 import org.openimaj.feature.DoubleFVComparison;
41 import org.openimaj.knn.DoubleNearestNeighboursExact;
42 import org.openimaj.ml.clustering.SpatialClusterer;
43 import org.openimaj.ml.clustering.SpatialClusters;
44 import org.openimaj.ml.clustering.dbscan.DistanceDBSCAN;
45 import org.openimaj.ml.clustering.dbscan.DoubleDBSCANClusters;
46 import org.openimaj.ml.clustering.dbscan.DoubleNNDBSCAN;
47 import org.openimaj.ml.clustering.dbscan.SparseMatrixDBSCAN;
48 import org.openimaj.ml.dataset.WineDataset;
49 import org.openimaj.util.function.Function;
50 import org.openimaj.util.pair.DoubleObjectPair;
51
52 import ch.akuhn.matrix.SparseMatrix;
53 import ch.akuhn.matrix.Vector;
54 import ch.akuhn.matrix.eigenvalues.AllEigenvalues;
55 import ch.akuhn.matrix.eigenvalues.Eigenvalues;
56
57
58
59
60
61
62 public class WineDatasetExperiment {
63 private static final int MAXIMUM_DISTANCE = 300;
64 private static Logger logger = Logger.getLogger(WineDatasetExperiment.class);
65
66
67
68
69 public static void main(String[] args) {
70 WineDataset ds = new WineDataset(2,3);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 logger.info("Clustering using modified spectral clustering");
92 DoubleSpectralClustering clustCSP = prepareCSPSpectralClustering(ds);
93 Function<List<double[]>,SparseMatrix> func = new RBFSimilarityDoubleClustererWrapper<double[]>(new DummyExtractor());
94 evaluate(ds, clustCSP, func);
95 }
96
97 private static DoubleSpectralClustering prepareCSPSpectralClustering(WineDataset ds) {
98 SpatialClusterer<? extends SpatialClusters<double[]>, double[]> cl = null;
99
100 SpectralClusteringConf<double[]> conf = new SpectralClusteringConf<double[]>(cl );
101 conf.eigenChooser = new EigenChooser() {
102
103 @Override
104 public Eigenvalues prepare(SparseMatrix laplacian) {
105 Eigenvalues eig = new AllEigenvalues(laplacian);
106 return eig;
107 }
108
109 @Override
110 public int nEigenVectors(Iterator<DoubleObjectPair<Vector>> vals, int totalEigenVectors) {
111
112 return 0;
113 }
114 };
115 DoubleSpectralClustering clust = new DoubleSpectralClustering(conf);
116 return clust;
117 }
118
119 private static SparseMatrixDBSCAN prepareDBScane() {
120
121 double epss = 0.5;
122 SparseMatrixDBSCAN inner = new DistanceDBSCAN(epss, 1);
123 return inner;
124 }
125
126 private static DoubleSpectralClustering prepareSpectralClustering() {
127
128 double epss = 0.6;
129 SpatialClusterer<DoubleDBSCANClusters,double[]> inner = new DoubleNNDBSCAN(epss, 2,new DoubleNearestNeighboursExact.Factory(DoubleFVComparison.EUCLIDEAN));
130 SpectralClusteringConf<double[]> conf = new SpectralClusteringConf<double[]>(
131 inner
132 );
133
134 conf.eigenChooser = new HardCodedEigenChooser(10);
135 DoubleSpectralClustering clust = new DoubleSpectralClustering(conf);
136 return clust;
137 }
138
139 private static void evaluate(WineDataset ds, Clusterer<SparseMatrix> clust, Function<List<double[]>, SparseMatrix> func) {
140 ClusterEvaluator<SparseMatrix, FullMEAnalysis> eval = new ClusterEvaluator<SparseMatrix, FullMEAnalysis>(clust,ds,func,new FullMEClusterAnalyser());
141 int[][] evaluate = eval.evaluate();
142 logger.info("Expected Classes: " + ds.size());
143 logger.info("Detected Classes: " + evaluate.length);
144 FullMEAnalysis res = eval.analyse(evaluate);
145 System.out.println(res.getSummaryReport());
146 }
147 }