📄 apriori.java
字号:
try {
text.append("\n\nApriori options:\n\n");
text.append("-t <training file>\n");
text.append("\tThe name of the training file.\n");
Enumeration enum = apriori.listOptions();
while (enum.hasMoreElements()) {
Option option = (Option)enum.nextElement();
text.append(option.synopsis()+'\n');
text.append(option.description()+'\n');
}
trainFileString = Utils.getOption('t', options);
if (trainFileString.length() == 0)
throw new Exception("No training file given!");
apriori.setOptions(options);
reader = new BufferedReader(new FileReader(trainFileString));
apriori.buildAssociations(new Instances(reader));
System.out.println(apriori);
} catch(Exception e) {
log.error(e.getStackTrace().toString());
log.error("\n"+e.getMessage()+text);
}
}
private void pmmlDocument (int depth) {
try{
Element versionElement = pmmlIntro();
Element headerElement = header ();
Element dataDictionaryElement = dataDictionary () ;
Element associationModelElement = associationModel ();
versionElement.addContent(headerElement);
versionElement.addContent(dataDictionaryElement);
versionElement.addContent(associationModelElement);
// Create the XML document
DocType dtd = new DocType("pmml_2_0.dtd");
Document pmmlDocument = new Document (versionElement,dtd);
}
catch (Exception e){
log.error("PMML Document Exception: " + e);
log.error(e.getStackTrace().toString());
}
}
/**
* Creates the pmml Element
*/
private Element pmmlIntro () throws Exception {
Element pmmlIntro = new Element ("PMML");
return pmmlIntro;
}
/**
* Create the Header Element
*/
private Element header () throws Exception {
Element header = new Element ("Header");
String headerString = " Association Rule Model for ";
headerString = "The Association Rule Model for the Data Mined Data";
header.setAttribute("copyright", "issel.ee.auth.gr");
header.setAttribute("description", headerString);
Element applicationNameElement = new Element ("Application");
applicationNameElement.setAttribute("name", "Agent Academy Data Miner");
applicationNameElement.setAttribute("version", "0.3");
header.addContent(applicationNameElement);
return header;
}
/**
* Create the DataDictionary for the pre - specified XML file
*/
private Element dataDictionary () throws Exception {
Element dataDictionary = new Element ("DataDictionary");
Element dataFieldElement;
Element attributeValueElement;
FastVector headerVector = m_instances.getVector();
String attributeName;
String attributeType;
String attributeValueString;
int numberOfFields = m_instances.numAttributes();
String numberOfFieldsString = String.valueOf(numberOfFields);
dataDictionary.setAttribute("numberOfFields", numberOfFieldsString );
for (int i=0; i < headerVector.size();i++){
dataFieldElement = new Element ("DataField");
org.agentacademy.modules.dataminer.core.Attribute a = null;
a = (org.agentacademy.modules.dataminer.core.Attribute) headerVector.elementAt(i);
attributeName = a.name();
dataFieldElement.setAttribute("name", attributeName);
if (a.isNominal()){
attributeType = "categorical";
dataFieldElement.setAttribute("optype",attributeType);
//
Enumeration enumerateValues = a.enumerateValues();
while (enumerateValues.hasMoreElements()){
attributeValueString = (String)enumerateValues.nextElement();
attributeValueElement = new Element ("Value");
attributeValueElement.setAttribute("value", attributeValueString);
dataFieldElement.addContent(attributeValueElement);
}
}
else if (a.isNumeric()){
attributeType = "continuous";
dataFieldElement.setAttribute("optype",attributeType);
}
else if (a.isRegular()){
attributeType = "ordinal";
dataFieldElement.setAttribute("optype",attributeType);
}
else {
attributeType = "string";
dataFieldElement.setAttribute("optype",attributeType);
}
dataDictionary.addContent(dataFieldElement);
}
return dataDictionary;
}
/**
* Returns the association Model Element
*/
private Element associationModel (){
Element associationElement = new Element ("AssociationModel");
Element assocInputStatsElement = assocInputStats ();
Element associationItemElement;
Element associationItemSetElement;
Element associationItemRefElement;
Element assocRuleElement;
associationElement.addContent(assocInputStatsElement);
FastVector assocItemVector = new FastVector();
String assocItemItemSetString;
int counter = 1;
String counterString;
// Enumeration attributeEnumeration = m_instances.enumerateAttributes();
int numberOfAttributes = m_instances.numAttributes();
Hashtable mapping = new Hashtable ();
for (int attributeCounter = 0; attributeCounter < numberOfAttributes; attributeCounter ++){
org.agentacademy.modules.dataminer.core.Attribute alpha = (org.agentacademy.modules.dataminer.core.Attribute)m_instances.attribute(attributeCounter);
String attributeName = alpha.name();
log.debug("Attribute name = " +attributeName);
for (int numberOfValues =0; numberOfValues < alpha.numValues(); numberOfValues ++){
assocItemItemSetString = attributeName + "=" + alpha.value(numberOfValues);
//XML document : The AssocItem Elements
associationItemElement = new Element ("AssocItem");
counterString = String.valueOf(counter);
associationItemElement.setAttribute("id" , counterString);
associationItemElement.setAttribute("value",assocItemItemSetString);
associationElement.addContent(associationItemElement);
// Create a Hash Table in order to refer to Items by ids
mapping.put(assocItemItemSetString,counterString);
counter++;
}
// }
}
// Create a Hash Table in order to refer to ItemSet by ids
Hashtable itemSetMap = new Hashtable ();
String itemSetCompare;
String itemSetForHash;
int itemSetCounter = 0;
String itemSetCounterString = null;
// Parse the m_Ls in order to find the ItemSets
for (int kappa = 0; kappa < m_Ls.size(); kappa++){
assocItemVector = (FastVector) m_Ls.elementAt(kappa);
for (int lamda = 0; lamda < assocItemVector.size(); lamda ++){
ItemSet assocItemRef = (ItemSet)assocItemVector.elementAt(lamda);
itemSetCompare = (assocItemRef.toString(m_instances));
int spaceIndex;
String itemSetCreationString;
itemSetCompare = itemSetCompare.substring(0,itemSetCompare.lastIndexOf(" "));
// System.out.println(" Weka ItemSet = " + itemSetCompare);
spaceIndex = itemSetCompare.indexOf(" ", 0);
if (spaceIndex == -1) {
itemSetCounter ++;
itemSetCounterString = String.valueOf(itemSetCounter);
// System.out.println("itemSetCounter = " + itemSetCounterString);
itemSetCreationString = (String)mapping.get(itemSetCompare);
//System.out.println(itemSetCreationString);
itemSetForHash = itemSetCompare;
itemSetMap.put(itemSetForHash,itemSetCounterString);
// XML Document : Create the AssocItemSet Elements for all L(1) ItemSets
associationItemSetElement = new Element ("AssocItemSet");
associationItemSetElement.setAttribute("id",itemSetCounterString);
associationItemSetElement.setAttribute("support", "1.0");
associationItemSetElement.setAttribute("numberOfItems", "1");
associationItemRefElement = new Element ("AssocItemRef");
associationItemRefElement.setAttribute("itemRef",itemSetCreationString);
associationItemSetElement.addContent(associationItemRefElement);
}
else{
int fromIndex = 0;
int toIndex = 0;
String itemSetCompareBefore = null;
String itemSetCompareAfter = null;
itemSetForHash = "";
itemSetCounter ++;
itemSetCounterString = String.valueOf(itemSetCounter);
// System.out.println("ItemSetCounter = " + itemSetCounterString);
// XML Document : Create the AssocItemSet Elements for all the rest ItemSets
associationItemSetElement = new Element ("AssocItemSet");
associationItemSetElement.setAttribute("id",itemSetCounterString);
associationItemSetElement.setAttribute("support", "1.0");
int numberOfItemsCounter = 0;
while (toIndex != -1){
numberOfItemsCounter ++;
toIndex = itemSetCompare.indexOf(" ");
if (toIndex !=-1){
itemSetCompareBefore = itemSetCompare.substring(0, toIndex);
itemSetCompareAfter = itemSetCompare.substring(toIndex + 1,itemSetCompare.length());
// XML Document : Create the AssocItemRef Elements for the AssocItemSet Elements
itemSetCreationString = (String)mapping.get(itemSetCompareBefore);
associationItemRefElement = new Element ("AssocItemRef");
associationItemRefElement.setAttribute("itemRef",itemSetCreationString);
associationItemSetElement.addContent(associationItemRefElement);
// Update Counters
fromIndex = toIndex;
itemSetCompare = itemSetCompareAfter;
itemSetForHash += itemSetCompareBefore;
itemSetForHash +=" ";
// System.out.println("Rule =" + itemSetCompareBefore);
}
else {
itemSetCreationString = (String)mapping.get(itemSetCompare);
associationItemRefElement = new Element ("AssocItemRef");
associationItemRefElement.setAttribute("itemRef",itemSetCreationString);
associationItemSetElement.addContent(associationItemRefElement);
itemSetForHash += itemSetCompare;
// System.out.println("Rule =" + itemSetCompare);
}
}
itemSetMap.put(itemSetForHash,itemSetCounterString);
associationItemSetElement.setAttribute("numberOfItems", String.valueOf(numberOfItemsCounter));
}
associationElement.addContent(associationItemSetElement);
}
}
// Create the XML rules for using the ItemSet HashTable...
for (int iota = 0; iota < m_allTheRules[0].size(); iota++) {
String antecedent, consequent, antecedentSubString, consequentSubString, antecedentString, consequentString,
antecedentSupportString, consequentSupportString, ruleSupportString, ruleConfidenceString;
int antecedentSupport, consequentSupport;
double ruleSupport;
antecedent = ((ItemSet)(m_allTheRules[0].elementAt(iota))).toString(m_instances);
antecedentSubString = antecedent.substring(0,antecedent.lastIndexOf(" "));
antecedentSupportString = antecedent.substring(antecedent.lastIndexOf(" ")+1, antecedent.length());
antecedentSupport = Integer.parseInt(antecedentSupportString);
consequent = ((ItemSet)(m_allTheRules[1].elementAt(iota))).toString(m_instances);
consequentSubString = consequent.substring(0,consequent.lastIndexOf(" "));
consequentSupportString = consequent.substring(consequent.lastIndexOf(" ")+1, consequent.length());
consequentSupport = Integer.parseInt(consequentSupportString);
ruleSupport = consequentSupport/antecedentSupport;
ruleSupportString = Utils.doubleToString(ruleSupport,2);
ruleConfidenceString = Utils.doubleToString(((Double)m_allTheRules[2].elementAt(iota)).doubleValue(),2);
antecedentString = (String)itemSetMap.get(antecedentSubString);
consequentString = (String)itemSetMap.get(consequentSubString);
assocRuleElement = new Element ("AssocRule");
assocRuleElement.setAttribute("support", ruleSupportString);
assocRuleElement.setAttribute("confidence", ruleConfidenceString);
assocRuleElement.setAttribute("antecedent", antecedentString);
assocRuleElement.setAttribute("consequent", consequentString);
associationElement.addContent(assocRuleElement);
}
return associationElement;
}
/**
* Returns the Element that carries the assocInputStats Element
*/
private Element assocInputStats (){
Element assocInputStatsElement = new Element("AssocInputStats");
String numberOfTransactionsString, numberOfItemsString, minimumSupportString,
minimumConfidenceString, numberOfItemsetsString, numberOfRulesString;
FastVector assocItemVector = (FastVector) m_Ls.elementAt(0);
int itemSetCounter = 0;
for (int i = 0; i < m_Ls.size(); i++) {
itemSetCounter += ((FastVector)m_Ls.elementAt(i)).size();
}
numberOfTransactionsString = String.valueOf(m_instances.numInstances());
numberOfItemsString = String.valueOf(assocItemVector.size());
minimumSupportString = Utils.doubleToString(m_minSupport,2);
minimumConfidenceString = Utils.doubleToString(m_minMetric,2);
numberOfItemsetsString =String.valueOf(itemSetCounter);
numberOfRulesString = String.valueOf(m_numRules);
assocInputStatsElement.setAttribute("numberOfTransactions",numberOfTransactionsString);
assocInputStatsElement.setAttribute("numberOfItems",numberOfItemsString);
assocInputStatsElement.setAttribute("minimumSupport",minimumSupportString);
assocInputStatsElement.setAttribute("minimumConfidence",minimumConfidenceString);
assocInputStatsElement.setAttribute("numberOfItemsets",numberOfItemsetsString);
assocInputStatsElement.setAttribute("numberOfRules",numberOfRulesString);
return assocInputStatsElement;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -