use of org.dmg.pmml.association.AssociationModel in project jpmml-sparkml by jpmml.
the class AssociationRulesTest method evaluateFPGrowthShopping.
@Test
public void evaluateFPGrowthShopping() throws Exception {
Predicate<ResultField> predicate = (resultField -> true);
Equivalence<Object> equivalence = getEquivalence();
try (SparkMLTestBatch batch = (SparkMLTestBatch) createBatch(FP_GROWTH, SHOPPING, predicate, equivalence)) {
PMML pmml = batch.getPMML();
Model model = Iterables.getOnlyElement(pmml.getModels());
assertTrue(model instanceof AssociationModel);
}
}
use of org.dmg.pmml.association.AssociationModel 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