📄 associationrulesminingmodel.java
字号:
if ( (misExportCharType & EXPORT_MISCHAR_SUPPORT) == EXPORT_MISCHAR_SUPPORT ) useSupp = true;
if ( (misExportCharType & EXPORT_MISCHAR_CONFIDENCE) == EXPORT_MISCHAR_CONFIDENCE ) useConf = true;
if ( (misExportCharType & EXPORT_MISCHAR_COVERAGE) == EXPORT_MISCHAR_COVERAGE ) useCov = true;
if ( (misExportCharType & EXPORT_MISCHAR_LIFT) == EXPORT_MISCHAR_LIFT ) useLift = true;
if ( (misExportCharType & EXPORT_MISCHAR_COSINE) == EXPORT_MISCHAR_COSINE ) useCos = true;
// Create and fill meta data:
MiningDataSpecification metaData = new MiningDataSpecification("association rules");
OrdinalAttribute ruleNumber = new OrdinalAttribute("ruleNumber");
CategoricalAttribute itemId = (CategoricalAttribute)( (AssociationRulesSettings) miningSettings ).getItemId();
CategoricalAttribute conclusionFlag = new CategoricalAttribute("conclusionFlag");
conclusionFlag.setDataType( CategoricalAttribute.BOOLEAN );
metaData.addMiningAttribute(ruleNumber);
metaData.addMiningAttribute(itemId);
metaData.addMiningAttribute(conclusionFlag);
NumericAttribute support = new NumericAttribute("support");
if (useSupp) metaData.addMiningAttribute( support );
NumericAttribute confidence = new NumericAttribute("confidence");
if (useConf) metaData.addMiningAttribute( confidence );
NumericAttribute coverage = new NumericAttribute("coverage");
if (useCov) metaData.addMiningAttribute( coverage );
NumericAttribute lift0 = new NumericAttribute("lift");
if (useLift) metaData.addMiningAttribute( lift0 );
NumericAttribute cosine = new NumericAttribute("cosine");
if (useCos) metaData.addMiningAttribute( cosine );
int nAtt = metaData.getAttributesNumber();
// Create mining input stream and assign meta data:
MiningStoredData msd = new MiningStoredData();
msd.setMetaData(metaData);
// Fill mining input stream:
// Get number of rules:
int nRules = associationRules.size();
// Write all association rules:
double k0 = conclusionFlag.addCategory( new Category("0") );
double k1 = conclusionFlag.addCategory( new Category("1") );
for (int i = 0; i < nRules; i++) {
double ki = ruleNumber.addCategory( new Category( String.valueOf(i) ) );
// Get rule:
RuleSet rs = (RuleSet) associationRules.elementAt(i);
int itemSize = rs.getSize();
// Get characteristics of rule:
double supp = 0;
if (useSupp) supp = rs.getSupport() * 100;
double conf = 0;
if (useConf) conf = rs.getConfidence() * 100;
double cov = 0;
if (useCov) cov = coverage(rs) * 100;
double lift = 0;
if (useLift) lift = lift(rs);
double cos = 0;
if (useCos) cos = cosine(rs);
// Premise part of rule:
ItemSet is = rs.getPremise();
int nprem = rs.getPremise().getSize();
for (int j = 0; j < nprem; j++) {
int pN = is.getItemAt(j);
double[] values = new double[nAtt];
int ind = 0;
values[ind++] = ki;
values[ind++] = pN;
values[ind++] = k0;
if (useSupp) values[ind++] = supp;
if (useConf) values[ind++] = conf;
if (useCov) values[ind++] = cov;
if (useLift) values[ind++] = lift;
if (useCos) values[ind++] = cos;
MiningVector mv = new MiningVector(values);
mv.setMetaData(metaData);
msd.add(mv);
};
// Conclusion part of rule:
for (int j = nprem; j < itemSize; j++) {
int pN = rs.getConclusion().getItemAt(j-nprem);
double[] values = new double[nAtt];
int ind = 0;
values[ind++] = ki;
values[ind++] = pN;
values[ind++] = k1;
if (useSupp) values[ind++] = supp;
if (useConf) values[ind++] = conf;
if (useCov) values[ind++] = cov;
if (useLift) values[ind++] = lift;
if (useCos) values[ind++] = cos;
MiningVector mv = new MiningVector(values);
mv.setMetaData(metaData);
msd.add(mv);
}
}
return msd;
}
/**
* Exports association rules of length 2 (A => B) in non-transactional flat
* format of the following form:
* 1. Attribute - itemId premise,
* 2. Attribute - itemId conclusion,
* [Attributes from set {support, confidence, coverage, lift, cosine}].
* The last included attriutes of rule characteristics depend
* on misExportCharType. <p>
*
* Nice format to be stored in databases or for further selections
* by the Multidimensional stream. Especially important for ITI Collaborative
* Filtering algorithms since there all rules are of length 2.
*
* @return association rules as mining input stream
* @throws MiningException cannot export into mining input stream
*/
public MiningInputStream toMISAssociationRulesFlat() throws MiningException
{
// Required characteristics:
boolean useSupp = false;
boolean useConf = false;
boolean useCov = false;
boolean useLift = false;
boolean useCos = false;
if ( (misExportCharType & EXPORT_MISCHAR_SUPPORT) == EXPORT_MISCHAR_SUPPORT ) useSupp = true;
if ( (misExportCharType & EXPORT_MISCHAR_CONFIDENCE) == EXPORT_MISCHAR_CONFIDENCE ) useConf = true;
if ( (misExportCharType & EXPORT_MISCHAR_COVERAGE) == EXPORT_MISCHAR_COVERAGE ) useCov = true;
if ( (misExportCharType & EXPORT_MISCHAR_LIFT) == EXPORT_MISCHAR_LIFT ) useLift = true;
if ( (misExportCharType & EXPORT_MISCHAR_COSINE) == EXPORT_MISCHAR_COSINE ) useCos = true;
// Create and fill meta data:
MiningDataSpecification metaData = new MiningDataSpecification("association rules flat");
CategoricalAttribute itemId = (CategoricalAttribute)( (AssociationRulesSettings) miningSettings ).getItemId();
CategoricalAttribute itemIdPremise = (CategoricalAttribute) itemId.clone();
itemIdPremise.setName("itemIdPremise");
CategoricalAttribute itemIdConclusion = (CategoricalAttribute) itemId.clone();
itemIdConclusion.setName("itemIdConclusion");
metaData.addMiningAttribute(itemIdPremise);
metaData.addMiningAttribute(itemIdConclusion);
NumericAttribute support = new NumericAttribute("support");
if (useSupp) metaData.addMiningAttribute( support );
NumericAttribute confidence = new NumericAttribute("confidence");
if (useConf) metaData.addMiningAttribute( confidence );
NumericAttribute coverage = new NumericAttribute("coverage");
if (useCov) metaData.addMiningAttribute( coverage );
NumericAttribute lift0 = new NumericAttribute("lift");
if (useLift) metaData.addMiningAttribute( lift0 );
NumericAttribute cosine = new NumericAttribute("cosine");
if (useCos) metaData.addMiningAttribute( cosine );
int nAtt = metaData.getAttributesNumber();
// Create mining input stream and assign meta data:
MiningStoredData msd = new MiningStoredData();
msd.setMetaData(metaData);
// Fill mining input stream:
// Get number of rules:
int nRules = associationRules.size();
// Write all association rules:
for (int i = 0; i < nRules; i++) {
// Get rule:
RuleSet rs = (RuleSet) associationRules.elementAt(i);
int itemSize = rs.getSize();
// Get characteristics of rule:
double supp = 0;
if (useSupp) supp = rs.getSupport() * 100;
double conf = 0;
if (useConf) conf = rs.getConfidence() * 100;
double cov = 0;
if (useCov) cov = coverage(rs) * 100;
double lift = 0;
if (useLift) lift = lift(rs);
double cos = 0;
if (useCos) cos = cosine(rs);
// Add rule:
int prem = rs.getPremise().getItemAt(0); // first item of premise
int conc = rs.getConclusion().getItemAt(0); // first item of conclusion
double[] values = new double[nAtt];
int ind = 0;
values[ind++] = prem;
values[ind++] = conc;
if (useSupp) values[ind++] = supp;
if (useConf) values[ind++] = conf;
if (useCov) values[ind++] = cov;
if (useLift) values[ind++] = lift;
if (useCos) values[ind++] = cos;
MiningVector mv = new MiningVector(values);
mv.setMetaData(metaData);
msd.add(mv);
}
return msd;
}
/**
* Writes association rule model as plain text.
*
* @param writer writer for plain text model
* @exception MiningException cannot write text
*/
public void writePlainText( Writer writer ) throws MiningException
{
try
{
int nLITS = largeItemSets.size();
writer.write( "Number of large itemsets: " + nLITS + "\n" );
for (int i = 0; i < nLITS; i++)
{
ItemSet is = (ItemSet) largeItemSets.elementAt(i);
writer.write( i + ": " );
int itemSize = is.getSize();
for (int j = 0; j < itemSize; j++)
{
int pN = is.getItemAt(j);
writer.write( String.valueOf(pN) + " " );
}
int nTransact = getNumberOfTransactions();
double Support = 100.0 * ((double) is.getSupportCount()) / ((double) nTransact);
writer.write( "\t" + " Support = " + Math.round(Support*100)/100.0 + "\n" );
}
int nRules = associationRules.size();
writer.write( "Number of association rules: " + nRules + "\n" );
for(int i = 0; i < nRules; i++)
{
writer.write( i + ": " );
RuleSet rs = (RuleSet) associationRules.elementAt(i);
int itemSize = rs.getSize();
int nprem = rs.getPremise().getSize();
int nconcl = rs.getConclusion().getSize();
for (int j = 0; j < nprem; j++)
{
int pN = rs.getPremise().getItemAt(j);
writer.write( String.valueOf(pN) + " " );
}
writer.write( " => " );
for (int j = 0; j < nconcl; j++)
{
int pN = rs.getConclusion().getItemAt(j);
writer.write( String.valueOf(pN) + " " );
}
double Support = rs.getSupport() * 100.0;
double Confidence = rs.getConfidence() * 100.0;
writer.write( "\t" + "Support = " + Math.round(Support*100)/100.0 + ", " );
writer.write( "Confidence = " + Math.round(Confidence*100)/100.0 + "\n" );
}
writer.flush();
}
catch( IOException ex)
{
throw new MiningException( "Can't write plain text presentation." );
}
}
/**
* Returns string representation (just few words).
*
* @return string representation
*/
public String toString()
{
return "Association rules mining model";
}
/**
* Returns association rule model as HTML string.
*
* @return model as HTML string
*/
public String toHtmlString()
{
AssociationRulesSettings settings = (AssociationRulesSettings)miningSettings;
CategoricalAttribute transactionId = (CategoricalAttribute)settings.getTransactionId();
int m = getNumberOfTransactions();
CategoricalAttribute itemId = (CategoricalAttribute)settings.getItemId();
Vector largeItemSets = getLargeItemSets();
int n = largeItemSets.size();
Vector associationRules = getAssociationRules();
int rulesNumber = associationRules.size();
String view = "<br><br>Large item sets (" + n + "):" +
"<table width=100% border=1 cellpadding=0>" +
"<tr><th>large item set</th>" +
"<th>items</th></tr>";
for( int i = 0; i < n; i++ )
{
view = view + "<tr>";
ItemSet is = (ItemSet)largeItemSets.elementAt(i);
int itemSize = is.getSize();
double support = (double)is.getSupportCount()/(double)m;
view = view + "<td width=30%> " + "<font color=blue>" + (i+1) +
") length=" + itemSize +
", support=<b>" + support +
"</b></font></td><td>";
for (int j = 0; j < itemSize; j++)
{
view = view + "<a href=#" + itemId.getCategory( is.getItemAt( j ) ) + ">" +
"<font color=blue><b>" + itemId.getCategory( is.getItemAt( j ) ) + "</b></color></a> ";
}
view = view + "</td></tr>";
}
view = view + "</table><br>";
view = view + "<br>Association rules (" + rulesNumber + ")<table width=100% border=1 cellspacing=0 cellpadding=0>";
view = view + "<tr><th>premise</th>" +
"<th><b>>>></b></th>" +
"<th>conclusion</th></tr>";
for( int i = 0; i < rulesNumber; i++ )
{
view = view + "<tr>";
RuleSet rs = (RuleSet) associationRules.elementAt(i);
int itemSize = rs.getSize();
int nprem = rs.getPremise().getSize();
int nconcl = rs.getConclusion().getSize();
view = view + "<td>";
for( int j = 0; j < nprem; j++ )
{
view = view + "<a href=#" + itemId.getCategory( rs.getPremise().getItemAt(j) ) +
"><font color=blue><b>" + itemId.getCategory( rs.getPremise().getItemAt(j) ) + "</b></color></a> " + " ";
}
view = view + "</td>";
double support = rs.getSupport();
double confidence = rs.getConfidence();
view = view + "<td>" + "<font color=blue>support=<b>" +
support + "</b> confidence=<b>" +
confidence + "</b></color></td>";
view = view + "<td>";
for (int j = 0; j < nconcl; j++)
{
view = view + "<a href=#" + itemId.getCategory( rs.getConclusion().getItemAt(j) ) +
"><font color=blue><b>" + itemId.getCategory( rs.getConclusion().getItemAt(j) ) + "</b></color></a> " + " ";
}
view = view + "</td>";
view = view + "</tr>";
}
view = view + "</table>";
return view;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -