+Shopping cart analysis

#SPM-23: add work 2h development
This commit is contained in:
Julian Hinxlage 2019-05-05 10:38:12 +02:00
parent 5e5e8101bf
commit 221c2aea92
2 changed files with 123 additions and 11 deletions

View File

@ -1,26 +1,87 @@
package de.hsel.spm.baudas.analysis; package de.hsel.spm.baudas.analysis;
import weka.associations.Apriori;
import org.jetbrains.annotations.Nullable; import weka.associations.AssociationRule;
import weka.associations.Item;
import weka.core.Instances; import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NumericCleaner;
import weka.filters.unsupervised.attribute.NumericToNominal;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ShoppingCart implements Analysis<Map<String, List<String>>>{ /**
* The shopping-cart Analysis.
*
* @author Julian Hinxlage
* @version 0.1
*/
public class ShoppingCart implements Analysis<Map<List<String>, List<String>>> {
private Instances instances;
@Nullable
@Override @Override
public Instances load(File file) { public Map<List<String>, List<String>> getResult() {
return null; Map<List<String>, List<String>> result = new HashMap<>();
Apriori model = new Apriori();
for (int i = 0; i < 11; i++) {
instances.deleteAttributeAt(0);
} }
@Override //alle artikel anzahlen auf 0 oder 1 setzen
public Map<String, List<String>> getResult() { NumericCleaner nc = new NumericCleaner();
Map<String, List<String>> result = new HashMap<>(); nc.setMaxThreshold(1.0);
nc.setMaxDefault(1.0);
try {
nc.setInputFormat(instances);
instances = Filter.useFilter(instances, nc);
} catch (Exception e) {
e.printStackTrace();
}
//zahl werte zu nomial werten umwandeln
NumericToNominal num2nom = new NumericToNominal();
num2nom.setAttributeIndices("first-last");
try {
num2nom.setInputFormat(instances);
instances = Filter.useFilter(instances, num2nom);
} catch (Exception e) {
e.printStackTrace();
}
model.setTreatZeroAsMissing(true);
try {
model.buildAssociations(instances);
} catch (Exception e) {
e.printStackTrace();
}
for(int i = 0; i < model.getAssociationRules().getRules().size();i++){
AssociationRule rule = model.getAssociationRules().getRules().get(i);
List<String> l1 = new ArrayList<>();
for(Item item : rule.getPremise()){
l1.add(item.getAttribute().name());
}
List<String> l2 = new ArrayList<>();
for(Item item : rule.getConsequence()){
l2.add(item.getAttribute().name());
}
result.put(l1,l2);
}
return result; return result;
} }
public ShoppingCart(File file) {
instances = load(file);
}
} }

View File

@ -0,0 +1,51 @@
package de.hsel.spm.baudas.analysis;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* Test class for shopping-cart analysis.
*
* @author Julian Hinxlage
* @version 0.1
*/
class ShoppingCartTest {
@Test
void test100(){
URL url = getClass().getClassLoader().getResource("kd100.csv");
assert url != null;
ShoppingCart cart = new ShoppingCart(new File(url.getFile()));
Map<List<String>, List<String>> result = cart.getResult();
System.out.println(result);
}
@Test
void test1000(){
URL url = getClass().getClassLoader().getResource("kd1000.csv");
assert url != null;
ShoppingCart cart = new ShoppingCart(new File(url.getFile()));
Map<List<String>, List<String>> result = cart.getResult();
System.out.println(result);
}
@Test
void test10000(){
URL url = getClass().getClassLoader().getResource("kd10000.csv");
assert url != null;
ShoppingCart cart = new ShoppingCart(new File(url.getFile()));
Map<List<String>, List<String>> result = cart.getResult();
System.out.println(result);
}
}