📄 associationrulesminingmodel.java
字号:
}
/**
* Sets export type to mining input stream (method toMiningInputStream).
*
* @param misExportType new export type to mining input stream
*/
public void setMisExportType(int misExportType)
{
this.misExportType = misExportType;
}
/**
* Returns rule characteristics (supp, conf, ...) export type to
* mining input stream.
*
* @return rule characteristics export type to mining input stream
*/
public int getMisExportCharType()
{
return misExportCharType;
}
/**
* Sets rule characteristics (supp, conf, ...) export type to
* mining input stream.
*
* @param misExportCharType rule characteristics export type to mining input stream
*/
public void setMisExportCharType(int misExportCharType)
{
this.misExportCharType = misExportCharType;
}
// -----------------------------------------------------------------------
// Application of model to new data
// -----------------------------------------------------------------------
/**
* Builds hashtable for fast access to large itemsets.
*
* @exception MiningException cannot build hashtable for large itemsets
*/
public void buildLargeItemSets() throws MiningException {
if(largeItemSets==null) throw new MiningException("No large itemsets available");
lits = new Hashtable();
int num = largeItemSets.size();
int nTransact = getNumberOfTransactions();
for (int i = 0 ; i < num; i++)
{
ItemSet lit = (ItemSet) largeItemSets.elementAt(i);
Double Supp = (Double) lits.get(lit);
if (Supp == null)
{
double supp = (double) lit.getSupportCount() / (double) nTransact;
lits.put(lit, new Double(supp) );
};
};
}
/**
* Calculates coverage of association rule. Coverage is the proportion
* of transactions covered by the premise itemset. Equivalentely,
* this is the quotient of support and confidence of the rule.
*
* @param rs association rule
* @return coverage number from interval [0,1]
* @exception MiningException cannot calculate coverage
*/
public double coverage(RuleSet rs) throws MiningException {
double supp = rs.getSupport();
double conf = rs.getConfidence();
if ( Math.abs(conf) <= 1.0e-20 )
return 0;
return supp/conf;
}
/**
* Calculates lift of association rule. Lift is the confidence
* divided by the proportion of all examples that are covered by
* the conclusion. <p>
*
* If no valid lift value is contained in the rule, the method
* calculates it from the large itemsets. In this case, the method
* 'buildLargeItemSets' must have been called before.
*
* @param rs association rule
* @return lift value
* @exception MiningException missing lift value in rule and empty
* hashtable of large itemsets
*/
public double lift(RuleSet rs) throws MiningException {
double lift = rs.getLift();
if ( !Category.isMissingValue(lift) )
return lift;
if (lits == null)
buildLargeItemSets();
double conf = rs.getConfidence();
ItemSet conc = rs.getConclusion();
Double Conc = (Double)lits.get(conc);
if (Conc == null)
return 0.0;
double cc = Conc.doubleValue();
lift = 0.0;
if ( Math.abs(cc) > 1.0e-20 )
lift = conf / cc;
return lift;
}
/**
* Calculates cosine measure of association rule. Cosine is the square
* root of the product of times and lift values. Cosine is very similar to
* lift but in the range [0,1]. It comes from collaborative filtering
* methods. <p>
*
* @param rs association rule
* @return cosine value
* @exception MiningException cannot calculate lift
*/
public double cosine(RuleSet rs) throws MiningException {
double lift = lift(rs);
double supp = rs.getSupport();
double cos = Math.sqrt(lift*supp);
return cos;
}
/**
* Reorganize association rule model for beeing used as
* recommendation engine via the apply method.
*
* @exception MiningException cannot build recommendations
*/
public void buildRecommendations() throws MiningException {
int num = associationRules.size();
Hashtable h = new Hashtable();
for(int i=0;i<num;i++)
{
RuleSet rs = (RuleSet)associationRules.get(i);
ItemSet pr = rs.getPremise();
Vector v = (Vector)h.get(pr);
if(v == null)
{
v = new Vector();
h.put(pr,v);
}
v.add(rs);
}
recs = new Hashtable();
Enumeration keys = h.keys();
while(keys.hasMoreElements()) {
ItemSet pr = (ItemSet)keys.nextElement();
Vector v = (Vector)h.get(pr); // all conclusions for premise pr
int size = v.size();
if(size == 1)
{
recs.put(pr,v.get(0));
}
else // not one conclusion - find those with max support
{
double max = 0;
Vector r = new Vector();
for(int i=0;i<size;i++)
{
RuleSet rs = (RuleSet)v.get(i);
double supp = rs.getSupport();
if(max < supp)
{
r.clear();
r.add(rs);
max = supp;
}
else
if(max == supp) r.add(rs);
}
size = r.size();
if(size == 1)
{
recs.put(pr,r.get(0));
}
else
{
v = new Vector();
max = 0;
for(int i=0;i<size;i++)
{
RuleSet rs = (RuleSet)r.get(i);
double conf = rs.getConfidence();
if(max < conf)
{
v.clear();
v.add(rs);
max = conf;
}
else if(max == conf) v.add(rs);
}
recs.put(pr,v.get(0));
}
}
}
}
/**
* Applies association rules to new item sets. Returns rule set of
* recommended categories or null if no matching rule could be found.
* Before using this method, buildRecommendations must be called. <p>
*
* When some rules are found for a recommendation, one rule is
* selected according to the following strategy:
* <ol>
* <li> Rule with highest support.
* <li> If equal supports, rule with highest confidence.
* <li> If also equal confidences, randomly.
* </ol>
*
* @param items vector of categories (Category objects)
* @return rule set of recommended categories, null if no proper rule found
* @exception MiningException recommendations not builded
*/
public RuleSet apply(Vector items) throws MiningException {
if(recs == null) throw new MiningException("recommendations not builded");
AssociationRulesSettings arset = (AssociationRulesSettings)miningSettings;
CategoricalAttribute itemId = (CategoricalAttribute)arset.getItemId();
int num = items.size();
ItemSet is = new ItemSet();
for(int i=0;i<num;i++)
{
Category category = (Category)items.get(i);
is.addItem((int)itemId.getKey(category));
}
System.out.println("Finding recommendation for "+is+"("+is.hashCode()+")");
RuleSet rs = (RuleSet)recs.get(is);
return rs;
}
/**
* Applies function of association rules mining model to a mining vector.
* Currently not supported.
*
* @param miningVector mining vector where the model should be applied
* @return function value of the mining vector
* @throws MiningException always thrown
*/
public double applyModelFunction(MiningVector miningVector)
throws MiningException {
throw new MiningException("Not implemented.");
}
/**
* Applying the association rules mining model to an itemset or mining vector
* of items; i.e. in a non-transactional representation. Returns
* RuleSet of recommended items or null if no association rules
* contain the antecedent.
*
* Before using this method, buildRecommendations must be called.
*
* @param miningData itemset or mining vector of items
* @return rule set of recommended items or null
* @throws MiningException thrown if miningData not a mining vector
* or something else went wrong
*/
public MiningMatrixElement applyModel(MiningMatrixElement miningData)
throws MiningException {
if ( !(miningData instanceof ItemSet) && !(miningData instanceof MiningVector) )
throw new MiningException("miningData neither ItemSet nor miningVector");
if (recs == null) throw new MiningException("recommendations not builded");
// Form itemset:
ItemSet is = null;
if (miningData instanceof ItemSet)
is = (ItemSet) miningData;
else {
AssociationRulesSettings arset = (AssociationRulesSettings)miningSettings;
CategoricalAttribute itemId = (CategoricalAttribute)arset.getItemId();
MiningVector mv = (MiningVector) miningData;
MiningDataSpecification metaData = mv.getMetaData();
for (int i = 0; i < metaData.getAttributesNumber(); i++) {
CategoricalAttribute catAtt = (CategoricalAttribute) metaData.getMiningAttribute(i);
Category cat = catAtt.getCategory( mv.getValue(i) );
is.addItem((int)itemId.getKey(cat));
};
};
// Find recommendations:
System.out.println("Finding recommendation for "+is+"("+is.hashCode()+")");
RuleSet rs = (RuleSet)recs.get(is);
return rs;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Creates PMML document of association rule model.
* PMMLs AssociationModel is used.
*
* @param writer writer for PMML model
* @exception MiningException cannot write PMML model
* @see com.prudsys.pdm.Adapters.PmmlVersion20.AssociationModel
*/
public void writePmml( Writer writer ) throws MiningException
{
PMML pmml = new PMML();
pmml.setVersion( "2.0" );
pmml.setHeader( (Header)PmmlUtils.getHeader() );
// Add data and transformation dictionary:
MiningDataSpecification metaData = miningSettings.getDataSpecification();
CategoricalAttribute transactId = (CategoricalAttribute) metaData.getMiningAttribute(transactIdName);
if (! exportTransactIds) transactId.setUnboundedCategories(true);
if ( metaData.isTransformed() )
{
pmml.setDataDictionary( (DataDictionary)metaData.getPretransformedMetaData().createPmmlObject() );
pmml.setTransformationDictionary( (com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary)metaData.getMiningTransformationActivity().createPmmlObject() );
}
else
{
pmml.setDataDictionary( (DataDictionary)metaData.createPmmlObject() );
};
// Add association rules model:
AssociationModel[] associationModel = new AssociationModel[1];
associationModel[0] = (AssociationModel)createPmmlObject();
pmml.setAssociationModel( associationModel );
// Add encoding and write to document:
PmmlUtils.setEncoding();
PmmlUtils.marshalPmml(writer, pmml);
}
/**
* Creates PMML element AssociationModel.
*
* @return PMML object AssociationModel
* @exception MiningException cannot create PMML object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -