From f3770b09f9df88156bb6952d06aa926a01dd33c9 Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 2 May 2019 15:14:28 +0200 Subject: [PATCH] ~ schnellere Version mit weniger Weka #SPM-17 add work development 20m --- .../spm/baudas/analysis/ShoppingTimes.java | 46 +++---------------- .../baudas/analysis/ShoppingTimesTest.java | 10 ++-- 2 files changed, 11 insertions(+), 45 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index b904cb4..865517f 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,10 +1,7 @@ package de.hsel.spm.baudas.analysis; -import weka.core.AttributeStats; import weka.core.Instance; import weka.core.Instances; -import weka.filters.Filter; -import weka.filters.unsupervised.attribute.Remove; import java.io.File; import java.util.HashMap; @@ -36,44 +33,15 @@ public class ShoppingTimes implements Analysis> { public Map getResult() { if (result == null) { result = new HashMap<>(); - int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; - int dayIndex = 0; - int hourIndex = 1; - try { - //Remove all other attributes - Remove remove = new Remove(); - remove.setAttributeIndicesArray(keepIndexes); - remove.setInvertSelection(true); - remove.setInputFormat(instances); - instances = Filter.useFilter(instances, remove); - } catch (Exception ex) { - ex.printStackTrace(); + for(Instance instance : instances) { + DayHour dayHour = new DayHour(instance.stringValue(Attribute.SHOPPING_DAY), instance.stringValue(Attribute.SHOPPING_HOUR)); + int tmp = 1; + if(result.containsKey(dayHour)) + tmp = result.get(dayHour) + 1; + + result.put(dayHour, tmp); } - - /* - 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; } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index 1087bad..f6f572f 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -16,12 +16,10 @@ import static org.junit.jupiter.api.Assertions.assertTimeout; * @version 0.1 * @since 0.1 **/ -public class ShoppingTimesTest { - - //TODO: mehr Tests +class ShoppingTimesTest { @Test - public void test100() { + void test100() { URL url = getClass().getClassLoader().getResource("kd100.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); @@ -38,7 +36,7 @@ public class ShoppingTimesTest { } @Test - public void test1000(){ + void test1000(){ URL url = getClass().getClassLoader().getResource("kd1000.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); @@ -55,7 +53,7 @@ public class ShoppingTimesTest { } @Test - public void test10000() { + void test10000() { URL url = getClass().getClassLoader().getResource("kd10000.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile()));