use of org.dmg.pmml.association.Item in project jpmml-sparkml by jpmml.
the class FPGrowthModelConverter method ensureItemset.
private static Itemset ensureItemset(Feature feature, List<String> values, Map<List<String>, Itemset> itemsets, Map<String, Item> items) {
Itemset itemset = itemsets.get(values);
if (itemset == null) {
itemset = new Itemset(String.valueOf(itemsets.size() + 1));
for (String value : values) {
Item item = items.get(value);
if (item == null) {
item = new Item(String.valueOf(items.size() + 1), value).setField(feature.getName());
items.put(value, item);
}
itemset.addItemRefs(new ItemRef(item.getId()));
}
List<ItemRef> itemRefs = itemset.getItemRefs();
if (itemRefs.size() > 1) {
Comparator<ItemRef> comparator = new Comparator<ItemRef>() {
@Override
public int compare(ItemRef left, ItemRef right) {
int leftId = Integer.parseInt(left.getItemRef());
int rightId = Integer.parseInt(right.getItemRef());
return Integer.compare(leftId, rightId);
}
};
Collections.sort(itemRefs, comparator);
}
itemsets.put(values, itemset);
}
return itemset;
}
use of org.dmg.pmml.association.Item in project jpmml-sparkml by jpmml.
the class SparkMLEncoder method encodePMML.
@Override
public PMML encodePMML(Model model) {
PMML pmml = super.encodePMML(model);
Visitor visitor = new AbstractVisitor() {
@Override
public VisitorAction visit(Item item) {
item.setField(null);
return super.visit(item);
}
};
visitor.applyTo(pmml);
return pmml;
}
use of org.dmg.pmml.association.Item in project jpmml-sparkml by jpmml.
the class FPGrowthModelConverter method encodeModel.
@Override
public AssociationModel encodeModel(Schema schema) {
FPGrowthModel model = getTransformer();
List<? extends Feature> features = schema.getFeatures();
SchemaUtil.checkSize(1, features);
Feature feature = features.get(0);
Map<String, Item> items = new LinkedHashMap<>();
Map<List<String>, Itemset> itemsets = new LinkedHashMap<>();
List<AssociationRule> associationRules = new ArrayList<>();
List<Row> associationRuleRows = (model.associationRules()).collectAsList();
for (Row associationRuleRow : associationRuleRows) {
List<String> antecedent = JavaConversions.seqAsJavaList((Seq) associationRuleRow.apply(0));
List<String> consequent = JavaConversions.seqAsJavaList((Seq) associationRuleRow.apply(1));
Double confidence = (Double) associationRuleRow.apply(2);
// XXX
Double lift = 0d;
Double support = 0d;
Itemset antecedentItemset = ensureItemset(feature, antecedent, itemsets, items);
Itemset consequentItemset = ensureItemset(feature, consequent, itemsets, items);
AssociationRule associationRule = new AssociationRule().setAntecedent(antecedentItemset.getId()).setConsequent(consequentItemset.getId());
associationRule = associationRule.setConfidence(confidence).setLift(lift).setSupport(support);
associationRules.add(associationRule);
}
// XXX
int numberOfTransactions = 0;
MiningField transactionMiningField = ModelUtil.createMiningField(FieldName.create("transaction"), MiningField.UsageType.GROUP);
MiningSchema miningSchema = new MiningSchema().addMiningFields(transactionMiningField);
AssociationModel associationModel = new AssociationModel(MiningFunction.ASSOCIATION_RULES, numberOfTransactions, model.getMinSupport(), model.getMinConfidence(), items.size(), itemsets.size(), associationRules.size(), miningSchema);
(associationModel.getItems()).addAll(items.values());
(associationModel.getItemsets()).addAll(itemsets.values());
(associationModel.getAssociationRules()).addAll(associationRules);
return associationModel;
}
Aggregations