~ tried understanding weka further, no luck so far

Signed-off-by: Johannes Theiner <j.theiner@live.de>
#SPM-17 development 1h
This commit is contained in:
Johannes Theiner 2019-04-19 13:58:02 +02:00
parent 094e539c76
commit 6ebf46b65c
4 changed files with 105 additions and 17 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<Map<Pair<String, String>, Integer>>{
public class ShoppingTimes implements Analysis<Map<DayHour, Integer>> {
private Instances instances;
private Map<Pair<String, String>, Integer> result;
private Map<DayHour, Integer> result;
public ShoppingTimes(File file) {
instances = load(file);
@ -24,14 +23,14 @@ public class ShoppingTimes implements Analysis<Map<Pair<String, String>, Integer
@Override
public Map<Pair<String, String>, Integer> getResult() {
public Map<DayHour, Integer> 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<Map<Pair<String, String>, Integer
/*Map<Pair<String, String>, Integer> hours = new HashMap<>();
AttributeStats stats = instances.attributeStats(hourIndex);
for(int i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) {
Pair<String, String> 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<Object> dayValues = instances.attribute(dayIndex).enumerateValues();
int i = 0;
while (dayValues.hasMoreElements()) {
System.out.println(dayValues.nextElement() + " " + instances.attributeStats(dayIndex).nominalCounts[i]);
i++;
}
Enumeration<Object> 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]);
}*/
}

View File

@ -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<Map<Pair<String, String>, Integer>> results = new AtomicReference<>();
AtomicReference<Map<DayHour, Integer>> results = new AtomicReference<>();
assertTimeout(Duration.ofMillis(450), () -> results.set(overview.getResult()));
assertTimeout(Duration.ofMillis(500), () -> results.set(overview.getResult()));
/*for(Pair<String, String> 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<Map<DayHour, Integer>> results = new AtomicReference<>();
assertTimeout(Duration.ofMillis(550), () -> results.set(overview.getResult()));
//TODO: mehr Tests
}
}