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;
}
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");
}
}
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));
}
}
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);
}
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();
}
Aggregations