~ schnellere Version mit weniger Weka

#SPM-17 add work development 20m
This commit is contained in:
Johannes Theiner 2019-05-02 15:14:28 +02:00
parent 5541e1b9c8
commit f3770b09f9
2 changed files with 11 additions and 45 deletions

View File

@ -1,10 +1,7 @@
package de.hsel.spm.baudas.analysis; package de.hsel.spm.baudas.analysis;
import weka.core.AttributeStats;
import weka.core.Instance; import weka.core.Instance;
import weka.core.Instances; import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
@ -36,44 +33,15 @@ public class ShoppingTimes implements Analysis<Map<DayHour, Integer>> {
public Map<DayHour, Integer> getResult() { public Map<DayHour, Integer> getResult() {
if (result == null) { if (result == null) {
result = new HashMap<>(); result = new HashMap<>();
int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR};
int dayIndex = 0;
int hourIndex = 1;
try { for(Instance instance : instances) {
//Remove all other attributes DayHour dayHour = new DayHour(instance.stringValue(Attribute.SHOPPING_DAY), instance.stringValue(Attribute.SHOPPING_HOUR));
Remove remove = new Remove(); int tmp = 1;
remove.setAttributeIndicesArray(keepIndexes); if(result.containsKey(dayHour))
remove.setInvertSelection(true); tmp = result.get(dayHour) + 1;
remove.setInputFormat(instances);
instances = Filter.useFilter(instances, remove); result.put(dayHour, tmp);
} catch (Exception ex) {
ex.printStackTrace();
} }
/*
remove all days except one and get the stats
needed because weka has no other way to combine attributes
*/
for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) {
String day = instances.attribute(dayIndex).value(i);
Instances instancesCopy = new Instances(instances, 0, instances.numInstances());
for (int j = instancesCopy.numInstances() - 1; j >= 0; j--) {
Instance instance = instancesCopy.get(j);
if(!instance.stringValue(dayIndex).equals(day))
instancesCopy.delete(j);
}
//get customer count and put into map
AttributeStats stats = instancesCopy.attributeStats(hourIndex);
for (int j = 0; j < instancesCopy.attribute(hourIndex).numValues(); j++) {
DayHour dayHour = new DayHour(day, instancesCopy.attribute(hourIndex).value(j));
result.put(dayHour, stats.nominalCounts[j]);
}
}
} }
return result; return result;
} }

View File

@ -16,12 +16,10 @@ import static org.junit.jupiter.api.Assertions.assertTimeout;
* @version 0.1 * @version 0.1
* @since 0.1 * @since 0.1
**/ **/
public class ShoppingTimesTest { class ShoppingTimesTest {
//TODO: mehr Tests
@Test @Test
public void test100() { void test100() {
URL url = getClass().getClassLoader().getResource("kd100.csv"); URL url = getClass().getClassLoader().getResource("kd100.csv");
assert url != null; assert url != null;
ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); ShoppingTimes overview = new ShoppingTimes(new File(url.getFile()));
@ -38,7 +36,7 @@ public class ShoppingTimesTest {
} }
@Test @Test
public void test1000(){ void test1000(){
URL url = getClass().getClassLoader().getResource("kd1000.csv"); URL url = getClass().getClassLoader().getResource("kd1000.csv");
assert url != null; assert url != null;
ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); ShoppingTimes overview = new ShoppingTimes(new File(url.getFile()));
@ -55,7 +53,7 @@ public class ShoppingTimesTest {
} }
@Test @Test
public void test10000() { void test10000() {
URL url = getClass().getClassLoader().getResource("kd10000.csv"); URL url = getClass().getClassLoader().getResource("kd10000.csv");
assert url != null; assert url != null;
ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); ShoppingTimes overview = new ShoppingTimes(new File(url.getFile()));