From 6ebf46b65c9a0d080de9ffb9885c0423fcf3e087 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 19 Apr 2019 13:58:02 +0200 Subject: [PATCH] ~ tried understanding weka further, no luck so far Signed-off-by: Johannes Theiner #SPM-17 development 1h --- .../hsel/spm/baudas/analysis/Attribute.java | 39 +++++++++++++++ .../de/hsel/spm/baudas/analysis/DayHour.java | 16 ++++++ .../spm/baudas/analysis/ShoppingTimes.java | 49 +++++++++++++------ .../baudas/analysis/ShoppingTimesTest.java | 18 +++++-- 4 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/Attribute.java create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/DayHour.java diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java new file mode 100644 index 0000000..375714b --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java @@ -0,0 +1,39 @@ +package de.hsel.spm.baudas.analysis; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class Attribute { + + static final int SEX = 0; + static final int AGE = 1; + static final int KIDS = 2; + static final int MARITAL_STATUS = 3; + static final int EMPLOYED = 4; + static final int SHOPPING_DAY = 5; + static final int SHOPPING_HOUR = 6; + static final int RESIDENCE = 7; + static final int HOUSEHOLD_INCOME = 8; + static final int REGULAR = 9; + static final int PURCHASE_AMOUNT = 10; + static final int POWER_TOOLS = 11; + static final int HARDWARE = 12; + static final int HAND_TOOLS = 13; + static final int SANITARY = 14; + static final int LOCKING = 15; + static final int TILES = 16; + static final int COLORS = 17; + static final int FITTINGS = 18; + static final int RENOVATION = 19; + static final int ELECTRICAL = 20; + static final int BUILDING_MATERIAL = 21; + static final int WOOD = 22; + static final int FLOORING = 23; + static final int LIGHTS = 24; + static final int GARDENING_TOOLS = 25; + + + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java new file mode 100644 index 0000000..054032e --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java @@ -0,0 +1,16 @@ +package de.hsel.spm.baudas.analysis; + +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@Data +public class DayHour { + + private String day; + private String hour; +} \ No newline at end of file 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 bca82b1..bc68fea 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,6 +1,5 @@ package de.hsel.spm.baudas.analysis; -import javafx.util.Pair; import weka.core.Instances; import weka.filters.Filter; import weka.filters.unsupervised.attribute.Remove; @@ -13,10 +12,10 @@ import java.util.Map; * @version 0.1 * @since 0.1 **/ -public class ShoppingTimes implements Analysis, Integer>>{ +public class ShoppingTimes implements Analysis> { private Instances instances; - private Map, Integer> result; + private Map result; public ShoppingTimes(File file) { instances = load(file); @@ -24,14 +23,14 @@ public class ShoppingTimes implements Analysis, Integer @Override - public Map, Integer> getResult() { - if(result == null) { + public Map getResult() { + if (result == null) { + int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; int dayIndex = 0; int hourIndex = 1; - int[] keepIndexes = new int[]{5, 6}; - Remove remove = new Remove(); try { + Remove remove = new Remove(); remove.setAttributeIndicesArray(keepIndexes); remove.setInvertSelection(true); remove.setInputFormat(instances); @@ -42,14 +41,36 @@ public class ShoppingTimes implements Analysis, Integer - - /*Map, Integer> hours = new HashMap<>(); - AttributeStats stats = instances.attributeStats(hourIndex); - for(int i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { - Pair pair = new Pair<>(instances.attribute(hourIndex).value(i), instances.attribute(0).value(i)); - hours.put(pair, stats.nominalCounts[i]); + /*try { + SubsetByExpression byExpression = new SubsetByExpression(); + byExpression.setExpression("(1 == Montag)"); + byExpression.setInputFormat(instances); + instances = Filter.useFilter(instances, byExpression); + } catch (Exception e) { + e.printStackTrace(); } - result = hours;*/ + + + Enumeration dayValues = instances.attribute(dayIndex).enumerateValues(); + int i = 0; + while (dayValues.hasMoreElements()) { + System.out.println(dayValues.nextElement() + " " + instances.attributeStats(dayIndex).nominalCounts[i]); + i++; + } + + Enumeration hourValues = instances.attribute(hourIndex).enumerateValues(); + i = 0; + while (hourValues.hasMoreElements()) { + System.out.println(hourValues.nextElement() + " " + instances.attributeStats(hourIndex).nominalCounts[i]); + i++; + }*/ + + + + /* AttributeStats stats = instances.attributeStats(hourIndex); + for (i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { + System.out.println(instances.attribute(hourIndex).value(i) + " " + stats.nominalCounts[i]); + }*/ } 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 c0d419b..fdc4d29 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -1,6 +1,5 @@ package de.hsel.spm.baudas.analysis; -import javafx.util.Pair; import org.junit.jupiter.api.Test; import java.io.File; @@ -26,9 +25,9 @@ public class ShoppingTimesTest { assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); - AtomicReference, Integer>> results = new AtomicReference<>(); + AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(450), () -> results.set(overview.getResult())); + assertTimeout(Duration.ofMillis(500), () -> results.set(overview.getResult())); /*for(Pair pair : results.get().keySet()) { System.out.println(pair); }*/ @@ -37,4 +36,17 @@ public class ShoppingTimesTest { //assertEquals(21, results.get().get(new Pair<>("Montag", ">17 Uhr"))); //TODO: mehr Tests } + + @Test + public void test10000() { + URL url = getClass().getClassLoader().getResource("kd10000.csv"); + assert url != null; + ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); + + AtomicReference> results = new AtomicReference<>(); + + assertTimeout(Duration.ofMillis(550), () -> results.set(overview.getResult())); + + //TODO: mehr Tests + } } \ No newline at end of file