Search in sources :

Example 1 with AssociationRule

use of org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule in project knime-core by knime.

the class SubgroupMinerModel2 method createAssociationRulesOutput.

private BufferedDataTable createAssociationRulesOutput(final DataTableSpec inputSpec, final ExecutionContext exec, final AprioriAlgorithm apriori, final List<DataCell> nameMapping) {
    DataTableSpec outSpec = createAssociationRulesSpec(inputSpec);
    BufferedDataContainer ruleRows = exec.createDataContainer(outSpec);
    assert nameMapping != null;
    List<AssociationRule> associationRules = apriori.getAssociationRules(m_confidence.getDoubleValue());
    // for every association rule
    int rowKeyCounter = 0;
    for (AssociationRule r : associationRules) {
        // get the support
        double support = r.getSupport();
        // get the confidence
        double confidence = r.getConfidence();
        // get lift
        double lift = r.getLift();
        // get the antecedence (which is one item) -> cell
        FrequentItemSet antecedent = r.getAntecedent();
        // get the consequence
        FrequentItemSet consequent = r.getConsequent();
        DataCell[] allCells = new DataCell[6];
        allCells[0] = new DoubleCell(support);
        allCells[1] = new DoubleCell(confidence);
        allCells[2] = new DoubleCell(lift);
        // consequent is always only one item -> access with get(0) ok
        if (nameMapping.size() > consequent.getItems().get(0)) {
            allCells[3] = nameMapping.get(consequent.getItems().get(0));
        } else {
            allCells[3] = new StringCell("Item" + consequent.getItems().get(0));
        }
        allCells[4] = new StringCell("<---");
        Set<DataCell> allcells = new HashSet<DataCell>();
        for (int i = 0; i < antecedent.getItems().size() && i < m_maxItemSetLength.getIntValue() + 5; i++) {
            if (nameMapping.size() > antecedent.getItems().get(i)) {
                allcells.add(nameMapping.get(antecedent.getItems().get(i)));
            } else {
                allcells.add(new StringCell("Item" + antecedent.getItems().get(i)));
            }
        }
        allCells[5] = CollectionCellFactory.createSetCell(allcells);
        if (antecedent.getItems().size() > 0) {
            DataRow row = new DefaultRow("rule" + (rowKeyCounter++), allCells);
            ruleRows.addRowToTable(row);
        }
    }
    ruleRows.close();
    return ruleRows.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) DataRow(org.knime.core.data.DataRow) AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet)

Example 2 with AssociationRule

use of org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule in project knime-core by knime.

the class ArrayApriori method getAssociationRules.

/*
     * new idea for mining association rules: getFrequentitemsets -> have to do
     * the mapping again create possible candidates: for each item i in the set
     * s create the set without this item s' go down the tree for both s and s'
     * compute the confidence with getCounterFor(last item in s) /
     * getCounterFor(last item in s') if confidence is large enough - create
     * association rule (i, s', counterFor(s), confidence) store it
     */
/**
 * {@inheritDoc}
 */
@Override
public List<AssociationRule> getAssociationRules(final double confidence) {
    List<FrequentItemSet> frequentItemSets = getFrequentItemSets(FrequentItemSet.Type.CLOSED);
    List<AssociationRule> associationRules = new ArrayList<AssociationRule>();
    /*
         * handle always frequent items seperately: since they are always
         * frequent each association rule of the itemset -> item must have
         * confidence = 1 and support = dbsize go once through the list and
         * create an association rule for every item x, like
         * {alwaysFrequentItems\x}-> x
         */
    for (Integer i : m_alwaysFrequentItems) {
        List<Integer> withoutI = new ArrayList<Integer>(m_alwaysFrequentItems);
        withoutI.remove(i);
        List<Integer> iList = new ArrayList<Integer>(1);
        iList.add(i);
        AssociationRule rule = new AssociationRule(new FrequentItemSet(Integer.toString(m_idCounter++), withoutI, 1.0), new FrequentItemSet(Integer.toString(m_idCounter++), iList, 1.0), 1.0, 1.0, 1.0);
        associationRules.add(rule);
    }
    // for each itemset s in frequentitemsets
    for (FrequentItemSet s : frequentItemSets) {
        if (s.getItems().size() > 1) {
            double supportS = s.getSupport();
            for (Iterator<Integer> iterator = s.iterator(); iterator.hasNext(); ) {
                Integer i = iterator.next();
                List<Integer> sWithoutI = new ArrayList<Integer>(s.getItems());
                sWithoutI.remove(i);
                // now go down the tree for both s and s'
                double newSupport = getSupportFor(sWithoutI);
                // logger.debug("support(s'): " + newSupport);
                double c = supportS / newSupport;
                if (c >= confidence) {
                    // create association rule (i, s', counterFor(s),
                    // confidence)
                    // AssociationRule rule = new AssociationRule(i,
                    // sWithoutI, c, supportS);
                    List<Integer> iList = new ArrayList<Integer>();
                    iList.add(i);
                    AssociationRule rule = new AssociationRule(new FrequentItemSet(Integer.toString(m_idCounter++), sWithoutI, newSupport), new FrequentItemSet(Integer.toString(m_idCounter++), iList, getSupportFor(iList)), s.getSupport(), c, c / getSupportFor(iList));
                    associationRules.add(rule);
                // logger.debug("found association rule: " + rule);
                }
            }
        }
    }
    return associationRules;
}
Also used : AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) ArrayList(java.util.ArrayList) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet)

Example 3 with AssociationRule

use of org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule in project knime-core by knime.

the class TIDApriori method getAssociationRules.

/**
 * {@inheritDoc}
 */
@Override
public List<AssociationRule> getAssociationRules(final double confidence) {
    List<FrequentItemSet> frequentItemSets = getFrequentItemSets(FrequentItemSet.Type.CLOSED);
    List<AssociationRule> associationRules = new ArrayList<AssociationRule>();
    // handle always frequent items seperately
    List<Integer> alwaysFrequentIds = new ArrayList<Integer>();
    for (TIDItem item : m_alwaysFrequentItems) {
        alwaysFrequentIds.add(item.getId());
    }
    for (TIDItem item : m_alwaysFrequentItems) {
        // create for each item an association
        // rule with the rest of them in them in the antecendent
        // support = dbsize, confidence = 1
        List<Integer> rest = new ArrayList<Integer>(alwaysFrequentIds);
        // we want to remove the object with the value and not at position
        // thus we the argument needs to be an object!
        rest.remove(Integer.valueOf(item.getId()));
        List<Integer> itemList = new ArrayList<Integer>();
        itemList.add(item.getId());
        AssociationRule rule = new AssociationRule(new FrequentItemSet(Integer.toString(m_idCounter++), rest, 1.0), new FrequentItemSet(Integer.toString(m_idCounter++), itemList, 1.0), 1.0, 1.0, 1.0);
        associationRules.add(rule);
    }
    // for each itemset
    for (FrequentItemSet s : frequentItemSets) {
        if (s.getItems().size() > 1) {
            // for each item
            for (Integer i : s.getItems()) {
                // create the set without the item
                List<Integer> sWithoutI = new ArrayList<Integer>(s.getItems());
                sWithoutI.remove(i);
                // create an empty TIDItemSet
                TIDItemSet itemSet = TIDItemSet.createEmptyTIDItemSet("" + m_idCounter, m_dbsize);
                for (Integer item : sWithoutI) {
                    int index = m_frequentItems.indexOf(new TIDItem(item));
                    TIDItem tidItem = m_frequentItems.get(index);
                    itemSet.addItem(tidItem);
                }
                double newSupport = itemSet.getSupport();
                double oldSupport = s.getSupport();
                double c = oldSupport / newSupport;
                if (c >= confidence) {
                    List<Integer> iList = new ArrayList<Integer>();
                    iList.add(i);
                    int index = m_frequentItems.indexOf(new TIDItem(i));
                    TIDItem tidItem = m_frequentItems.get(index);
                    if (tidItem == null) {
                    // TODO: what if ???
                    }
                    AssociationRule rule = new AssociationRule(new FrequentItemSet(Integer.toString(m_idCounter++), sWithoutI, newSupport), new FrequentItemSet(Integer.toString(m_idCounter++), iList, // TODO: support of single item
                    tidItem.getSupport()), s.getSupport(), c, c / tidItem.getSupport() * m_dbsize);
                    associationRules.add(rule);
                }
            }
        }
    }
    return associationRules;
}
Also used : AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) ArrayList(java.util.ArrayList) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) TIDFrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.TIDFrequentItemSet)

Example 4 with AssociationRule

use of org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule in project knime-core by knime.

the class PMMLAssociationRuleHandler method startElement.

/**
 * {@inheritDoc}
 */
@Override
public void startElement(final String uri, final String localName, final String name, final Attributes atts) throws SAXException {
    // start element -> extract minimum support, confidence, nr of items
    if (name.equals("AssociationModel") || /* In order to support association rule PMML models previously
               written by KNIME the wrong model name is still parsed. */
    name.equals("AssociationRuleModel")) {
        // all required attributes
        m_nrOfTransactions = Integer.parseInt(atts.getValue("numberOfTransactions"));
        m_minSupport = Double.parseDouble(atts.getValue("minimumSupport"));
        m_minConfidence = Double.parseDouble(atts.getValue("minimumConfidence"));
        m_nrOfItems = Integer.parseInt(atts.getValue("numberOfItems"));
        m_nrOfItemsets = Integer.parseInt(atts.getValue("numberOfItemsets"));
        m_nrOfRules = Integer.parseInt(atts.getValue("numberOfRules"));
    } else if (name.equals("Item")) {
        // get the id and the value
        String id = atts.getValue("id");
        String value = atts.getValue("value");
        if (!m_items.containsKey(id)) {
            m_items.put(id, value);
        }
        // ignore the mapped value!
        if (atts.getValue("mappedValue") != null) {
            LOGGER.warn("Ignoring mapped value: " + atts.getValue("mappedValue"));
        }
        // and the weight
        if (atts.getValue("weight") != null) {
            LOGGER.warn("Ignoring weight of item " + id + "/" + value);
        }
    } else if (name.equals("Itemset")) {
        String id = atts.getValue("id");
        if (m_currentItemSet == null) {
            m_currentItemSet = new FrequentItemSet(id);
        }
        if (!id.equals(m_currentItemSet.getId())) {
            m_itemsets.add(m_currentItemSet);
            m_currentItemSet = new FrequentItemSet(id);
        }
        if (atts.getValue("support") != null) {
            m_currentItemSet.setSupport(Double.parseDouble(atts.getValue("support")));
        }
    } else if (name.equals("ItemRef")) {
        // get the referenced item id
        String itemId = atts.getValue("itemRef");
        // find the item:
        if (!m_items.containsKey(itemId)) {
            throw new SAXException("Referenced item " + itemId + " in itemset " + m_currentItemSet.getId() + " cannot be found in items!");
        }
        // TODO: also support String ids
        m_currentItemSet.add(Integer.parseInt(itemId));
    } else if (name.equals("AssociationRule")) {
        double support = Double.parseDouble(atts.getValue("support"));
        double confidence = Double.parseDouble(atts.getValue("confidence"));
        String antecedentId = atts.getValue("antecedent");
        String consequentId = atts.getValue("consequent");
        // The lift attribute is optional
        String value = atts.getValue("lift");
        double lift = 0.0;
        if (value != null) {
            lift = Double.parseDouble(value);
        }
        FrequentItemSet antecedent = null;
        FrequentItemSet consequent = null;
        for (FrequentItemSet set : m_itemsets) {
            if (set.getId().equals(antecedentId)) {
                antecedent = set;
            } else if (set.getId().equals(consequentId)) {
                consequent = set;
            }
        }
        if (consequent == null || antecedent == null) {
            throw new SAXException("One of the referenced itemsets " + antecedentId + " or " + consequentId + " in association rule could not be found.");
        }
        m_rules.add(new AssociationRule(antecedent, consequent, support, confidence, lift));
    }
}
Also used : AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) SAXException(org.xml.sax.SAXException)

Example 5 with AssociationRule

use of org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule in project knime-core by knime.

the class PMMLAssociationRuleHandler method writeRules.

private void writeRules(final TransformerHandler handler) throws SAXException {
    for (AssociationRule rule : m_rules) {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute(null, null, "support", CDATA, "" + rule.getSupport());
        atts.addAttribute(null, null, "confidence", CDATA, "" + rule.getConfidence());
        atts.addAttribute(null, null, "lift", CDATA, "" + rule.getLift());
        atts.addAttribute(null, null, "antecedent", CDATA, rule.getAntecedent().getId());
        atts.addAttribute(null, null, "consequent", CDATA, rule.getConsequent().getId());
        handler.startElement(null, null, "AssociationRule", atts);
        handler.endElement(null, null, "AssociationRule");
    }
}
Also used : AssociationRule(org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule) AttributesImpl(org.xml.sax.helpers.AttributesImpl)

Aggregations

AssociationRule (org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule)6 FrequentItemSet (org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet)5 ArrayList (java.util.ArrayList)2 DataCell (org.knime.core.data.DataCell)2 DataRow (org.knime.core.data.DataRow)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 DefaultRow (org.knime.core.data.def.DefaultRow)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 StringCell (org.knime.core.data.def.StringCell)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 HashSet (java.util.HashSet)1 TIDFrequentItemSet (org.knime.base.node.mine.subgroupminer.freqitemset.TIDFrequentItemSet)1 SAXException (org.xml.sax.SAXException)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1