📄 sequentialminingmodel.java
字号:
*/
public int getExportTransactItemNames()
{
return exportTransactItemNames;
}
/**
* Sets type how item, transaction, and position IDs are handled in PMML.
* This is because of an incompleteness in PMML 20: transaction, item, and
* position ID are not specially denoted in the mining schema.
* This makes PMML20 sequence models not really applicable
* to new data (except you use agreed names for the IDs). <p>
*
* There are two ways to handle this problem:
* 1. Do nothing: conform with PMML 2.0 but lose of functionality,
* 2. Use XELOPES PMML Extension: to SequenceModel three new
* attributes 'itemIdName' (itemId), 'transactIdName' (transactionId),
* and 'positionIdName' (itemIndex) are added.
*
* @param exportTransactItemNames PMML export type of item, transaction,
* and position IDs
*/
public void setExportTransactItemNames(int exportTransactItemNames)
{
this.exportTransactItemNames = exportTransactItemNames;
}
/**
* Returns export type to mining input stream (method toMiningInputStream).
*
* @return export type to mining input stream
*/
public int getMisExportType()
{
return misExportType;
}
/**
* 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 sequences.
*
* @exception MiningException cannot build hashtable for large sequences
*/
public void buildLargeSequences() throws MiningException {
slits = new Hashtable();
int num = sequentialRules.size();
int nTransact = getNumberOfTransactions();
for (int i = 0 ; i < num; i++)
{
ItemSetSeq slit = (ItemSetSeq) sequentialRules.elementAt(i);
Double Supp = (Double) slits.get(slit);
if (Supp == null)
{
double supp = (double) slit.getSupportCount() / (double) nTransact;
slits.put(slit, new Double(supp) );
};
};
}
/**
* Calculates coverage of sequence 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 rss sequence rule
* @return coverage number from interval [0,1]
* @exception MiningException cannot calculate coverage
*/
public double coverage(RuleSetSeq rss) throws MiningException {
double supp = rss.getSupport();
double conf = rss.getConfidence();
if ( Math.abs(conf) <= 1.0e-20 )
return 0;
return supp/conf;
}
/**
* Calculates lift of sequence 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 sequences. In this case, the method
* 'buildLargeSequences' is used.
*
* @param rss sequence rule
* @return lift value
* @exception MiningException missing lift value in rule and empty
* hashtable of large sequences
*/
public double lift(RuleSetSeq rss) throws MiningException {
double lift = rss.getLift();
if ( !Category.isMissingValue(lift) )
return lift;
if (slits == null)
buildLargeSequences();
double conf = rss.getConfidence();
ItemSetSeq conc = rss.getConclusion();
Double Conc = (Double)slits.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 sequence 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 rss sequence rule
* @return cosine value
* @exception MiningException cannot calculate lift
*/
public double cosine(RuleSetSeq rss) throws MiningException {
double lift = lift(rss);
double supp = rss.getSupport();
double cos = Math.sqrt(lift*supp);
return cos;
}
/**
* Reorganize sequence rule model for beeing used as
* recommendation engine via the apply method.
*
* @exception MiningException cannot build recommendations
*/
public void buildRecommendations() throws MiningException {
int num = sequenceRules.size();
Hashtable h = new Hashtable();
for(int i=0;i<num;i++)
{
RuleSetSeq rs = (RuleSetSeq)sequenceRules.get(i);
ItemSetSeq 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()) {
ItemSetSeq pr = (ItemSetSeq)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++)
{
RuleSetSeq rs = (RuleSetSeq)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++)
{
RuleSetSeq rs = (RuleSetSeq)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 sequence 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 empty hashtable for recommendations
*/
public RuleSetSeq apply(Vector items) throws MiningException {
if(recs == null) throw new MiningException("recommendations not builded");
SequentialSettings arset = (SequentialSettings)miningSettings;
CategoricalAttribute itemId = (CategoricalAttribute)arset.getItemId();
int num = items.size();
ItemSetSeq is = new ItemSetSeq();
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()+")");
RuleSetSeq rs = (RuleSetSeq)recs.get(is);
return rs;
}
/**
* Applies function of sequential 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 sequntial rules mining model to an itemset or mining vector
* of items; i.e. in a non-transactional representation. Returns
* RuleSetSeq of recommended items or null if no sequence rules
* contain the antecedent.
*
* Before using this method, buildRecommendations must be called.
*
* @param miningData itemset or mining vector of items
* @return sequential 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 ItemSetSeq) && !(miningData instanceof MiningVector) )
throw new MiningException("miningData neither ItemSetSeq nor miningVector");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -