Search in sources :

Example 6 with FrequentItemSet

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

the class TIDApriori method getClosedItemSets.

private List<FrequentItemSet> getClosedItemSets() {
    List<FrequentItemSet> freqSets = new ArrayList<FrequentItemSet>();
    for (TIDItemSet i : m_repository) {
        FrequentItemSet s = i.toFrequentItemSet();
        if (s != null) {
            s.setClosed(true);
            freqSets.add(s);
        }
    }
    return freqSets;
}
Also used : ArrayList(java.util.ArrayList) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) TIDFrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.TIDFrequentItemSet)

Example 7 with FrequentItemSet

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

the class PMMLAssociationRuleHandler method writeItemsets.

private void writeItemsets(final TransformerHandler handler) throws SAXException {
    for (FrequentItemSet set : m_itemsets) {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute(null, null, "id", CDATA, set.getId());
        atts.addAttribute(null, null, "support", CDATA, "" + set.getSupport());
        atts.addAttribute(null, null, "numberOfItems", CDATA, "" + set.getItems().size());
        handler.startElement(null, null, "Itemset", atts);
        // add here the ItemRefs to the items (use index of item as id)
        for (Integer item : set) {
            atts = new AttributesImpl();
            atts.addAttribute(null, null, "itemRef", CDATA, "" + item);
            handler.startElement(null, null, "ItemRef", atts);
            handler.endElement(null, null, "ItemRef");
        }
        handler.endElement(null, null, "Itemset");
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet)

Example 8 with FrequentItemSet

use of org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet 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 9 with FrequentItemSet

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

the class SubgroupMinerModel method createFrequentItemsetOutput.

/*
     * @return a DataTable cotaining the frequent itemsets and their support
     */
private BufferedDataTable createFrequentItemsetOutput(final ExecutionContext exec) throws CanceledExecutionException {
    List<FrequentItemSet> freqSets = m_apriori.getFrequentItemSets(FrequentItemSet.Type.valueOf(m_itemSetType.getStringValue()));
    List<FrequentItemSetRow> rows = new LinkedList<FrequentItemSetRow>();
    // iterate over set list
    int rowKeyCounter = 0;
    for (FrequentItemSet set : freqSets) {
        exec.setProgress((double) rowKeyCounter / (double) m_nrOfRows, "creating output table. Row number: " + rowKeyCounter);
        List<String> itemList = new ArrayList<String>();
        for (int i = 0; i < set.getItems().size(); i++) {
            if (i > m_maxItemSetLength.getIntValue()) {
                break;
            }
            Integer item = set.getItems().get(i);
            // for every item look at the referring column name
            String itemName;
            if (m_nameMapping != null && m_nameMapping.size() > item) {
                itemName = m_nameMapping.get(item);
            } else {
                itemName = "item" + item;
            }
            itemList.add(itemName);
        }
        // create for every set a row
        FrequentItemSetRow row = new FrequentItemSetRow(new RowKey("item set " + rowKeyCounter++), itemList, m_maxItemSetLength.getIntValue(), set.getSupport());
        rows.add(row);
    }
    DataTableSpec outSpec = createItemsetOutputSpec();
    FrequentItemSetTable result = new FrequentItemSetTable(rows, outSpec);
    result.sortBy(FrequentItemSetTable.Sorter.valueOf(m_sorter.getStringValue()));
    return exec.createBufferedDataTable(result, exec);
}
Also used : FrequentItemSetTable(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSetTable) DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) ArrayList(java.util.ArrayList) FrequentItemSet(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LinkedList(java.util.LinkedList) FrequentItemSetRow(org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSetRow)

Example 10 with FrequentItemSet

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

the class SubgroupMinerModel2 method createFrequentItemsetOutput.

/**
 * @param apriori
 * @return a DataTable containing the frequent item sets and their support
 */
private BufferedDataTable createFrequentItemsetOutput(final DataTableSpec spec, final ExecutionContext exec, final AprioriAlgorithm apriori, final List<DataCell> nameMapping) throws CanceledExecutionException {
    DataTableSpec outSpec = createItemsetOutputSpec(spec);
    BufferedDataContainer itemRows = exec.createDataContainer(outSpec);
    assert nameMapping != null;
    List<FrequentItemSet> freqSets = apriori.getFrequentItemSets(FrequentItemSet.Type.valueOf(m_itemSetType.getStringValue()));
    // iterate over set list
    int rowKeyCounter = 0;
    for (FrequentItemSet set : freqSets) {
        exec.setProgress((double) rowKeyCounter / (double) freqSets.size(), "creating output table. Row number: " + rowKeyCounter);
        exec.checkCanceled();
        Set<DataCell> itemList = new HashSet<DataCell>();
        for (int i = 0; i < set.getItems().size(); i++) {
            Integer item = set.getItems().get(i);
            // for every item look at the referring column name
            DataCell itemName;
            if (nameMapping.size() > item) {
                itemName = nameMapping.get(item);
            } else {
                itemName = new StringCell("item" + item);
            }
            itemList.add(itemName);
        }
        // create for every set a row
        DataCell supp = new DoubleCell(set.getSupport());
        DataCell itemCell = CollectionCellFactory.createSetCell(itemList);
        DataRow row = new DefaultRow("item set " + (rowKeyCounter++), supp, itemCell);
        itemRows.addRowToTable(row);
    }
    itemRows.close();
    return itemRows.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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet)

Aggregations

FrequentItemSet (org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSet)14 ArrayList (java.util.ArrayList)7 AssociationRule (org.knime.base.node.mine.subgroupminer.freqitemset.AssociationRule)5 TIDFrequentItemSet (org.knime.base.node.mine.subgroupminer.freqitemset.TIDFrequentItemSet)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 DataCell (org.knime.core.data.DataCell)3 DataRow (org.knime.core.data.DataRow)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 StringCell (org.knime.core.data.def.StringCell)3 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FrequentItemSetRow (org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSetRow)1 FrequentItemSetTable (org.knime.base.node.mine.subgroupminer.freqitemset.FrequentItemSetTable)1 RowKey (org.knime.core.data.RowKey)1 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)1 SAXException (org.xml.sax.SAXException)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1