Merge branch 'SPM-13'

This commit is contained in:
Johannes Theiner 2019-05-06 18:32:03 +02:00
commit b0d886ab15
2 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,101 @@
package de.hsel.spm.baudas.analysis;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Cluster Analysis.
*
* @author Johannes Theiner
* @version 0.1
*/
public class Cluster implements Analysis<Map<Integer, Map<String, String>>> {
private Instances instances;
private Map<Integer, Map<String, String>> result;
public Cluster(File file) {
instances = load(file);
}
/**
* get result of cluster analysis.
*
* @return Result
*/
@Override
public Map<Integer, Map<String, String>> getResult() {
if(result == null) {
result = new HashMap<>();
//TODO: anpassen wenn #SPM-17 gemerged ist.
int[] keepIndexes = new int[]{0, 1, 3, 5, 6, 7};
Remove remove = new Remove();
try {
remove.setAttributeIndicesArray(keepIndexes);
remove.setInvertSelection(true);
remove.setInputFormat(instances);
instances = Filter.useFilter(instances, remove);
} catch (Exception e) {
e.printStackTrace();
}
//creating a single cluster to get average, weka has no way to get that from a bigger one.
SimpleKMeans fullMeans = new SimpleKMeans();
Instances fullCentroids = null;
try {
fullMeans.setNumClusters(1);
fullMeans.setPreserveInstancesOrder(true);
fullMeans.buildClusterer(instances);
fullCentroids = fullMeans.getClusterCentroids();
} catch (Exception ex) {
ex.printStackTrace();
}
assert fullCentroids != null;
//creating real cluster
SimpleKMeans kMeans = new SimpleKMeans();
try {
kMeans.setNumClusters(5);
kMeans.setPreserveInstancesOrder(true);
kMeans.buildClusterer(instances);
int count = 0;
count = putIntoMap(fullCentroids, count);
Instances centroids = kMeans.getClusterCentroids();
putIntoMap(centroids, count);
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* puts data into map.
*
* @param centroids cluster analysis result
* @param count current insert count
* @return count increment
*/
private int putIntoMap(Instances centroids, int count) {
for (int i = 0; i < centroids.numInstances(); i++) {
Map<String, String> map = new HashMap<>();
for (int j = 0; j < centroids.numAttributes(); j++) {
map.put(centroids.attribute(j).name(), centroids.instance(i).stringValue(j));
}
result.put(count, map);
count++;
}
return count;
}
}

View File

@ -0,0 +1,64 @@
package de.hsel.spm.baudas.analysis;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.net.URL;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTimeout;
/**
* test for Cluster Analysis.
*
* @author Julian Hinxlage
* @version 0.1
*/
class ClusterTest {
@Test
void test100() {
URL url = getClass().getClassLoader().getResource("kd100.csv");
assert url != null;
Cluster cluster = new Cluster(new File(url.getFile()));
AtomicReference<Map<Integer, Map<String, String>>> results = new AtomicReference<>();
assertTimeout(Duration.ofMillis(2000), () -> results.set(cluster.getResult()));
assertEquals("m", results.get().get(0).get("Geschlecht"));
}
@Test
void test1000() {
URL url = getClass().getClassLoader().getResource("kd1000.csv");
assert url != null;
Cluster cluster = new Cluster(new File(url.getFile()));
AtomicReference<Map<Integer, Map<String, String>>> results = new AtomicReference<>();
assertTimeout(Duration.ofMillis(2000), () -> results.set(cluster.getResult()));
assertEquals("m", results.get().get(0).get("Geschlecht"));
}
@Test
void test10000() {
URL url = getClass().getClassLoader().getResource("kd10000.csv");
assert url != null;
Cluster cluster = new Cluster(new File(url.getFile()));
AtomicReference<Map<Integer, Map<String, String>>> results = new AtomicReference<>();
assertTimeout(Duration.ofMillis(2000), () -> results.set(cluster.getResult()));
assertEquals("m", results.get().get(0).get("Geschlecht"));
}
}