📄 customersequentialminingmodel.java
字号:
// -----------------------------------------------------------------------
/**
* Exports large customer sequences or customer sequence rules in
* transactional format. Depending on the misExportType one of the
* following methods is called: toMISLargeSequences or
* toMISSequenceRules. <p>
*
* Nice format to be stored in databases or for further selections
* by the Multidimensional stream.
*
* @return customer sequence rules as mining input stream
* @throws MiningException cannot export as mining input stream
*/
public MiningInputStream toMiningInputStream() throws MiningException
{
if (misExportType == EXPORT_MIS_LARGE_SEQUENCES)
return toMISLargeSequences() ;
else if (misExportType == EXPORT_MIS_SEQUENCE_RULES)
return toMISSequenceRules();
else
throw new MiningException("invalid misExportType specified");
}
/**
* Exports large customer sequences in transactional format of the following
* form:
* 1. Attribute - customerSequenceNumber,
* 2. Attribute - sequenceNumber,
* 3. Attribute - itemId,
* [4. Attribute - support].
* Inclusion of support attriute depends on misExportCharType. <p>
*
* Nice format to be stored in databases or for further selections
* by the Multidimensional stream.
*
* @return large customer sequences as mining input stream
* @throws MiningException cannot export as mining input stream
*/
public MiningInputStream toMISLargeSequences() throws MiningException
{
// Required characteristics:
boolean useSupp = false;
if ( (misExportCharType & EXPORT_MISCHAR_SUPPORT) == EXPORT_MISCHAR_SUPPORT ) useSupp = true;
// Create and fill meta data:
MiningDataSpecification metaData = new MiningDataSpecification("large customer sequences");
OrdinalAttribute customerSequenceNumber = new OrdinalAttribute("customerSequenceNumber");
OrdinalAttribute sequenceNumber = new OrdinalAttribute("sequenceNumber");
CategoricalAttribute itemId = (CategoricalAttribute)( (CustomerSequentialSettings) miningSettings ).getItemId();
metaData.addMiningAttribute(customerSequenceNumber);
metaData.addMiningAttribute(sequenceNumber);
metaData.addMiningAttribute(itemId);
NumericAttribute support = new NumericAttribute("support");
if (useSupp) metaData.addMiningAttribute(support);
int nAtt = metaData.getAttributesNumber();
// Create mining input stream and assign meta data:
MiningStoredData msd = new MiningStoredData();
msd.setMetaData(metaData);
// Fill mining input stream:
// Write all customer sequences:
int nTrans = getNumberOfTransactions();
Vector seqRules = getSequentialRules();
int nSeq = seqRules.size();
for (int i = 0; i < nSeq; i++) {
double ki = customerSequenceNumber.addCategory( new Category( String.valueOf(i) ) );
// Get customer sequence:
CustomSequence cs = (CustomSequence) seqRules.elementAt(i);
int seqSize = cs.getSize();
// Get support of sequence:
double supp = 0;
if (useSupp) {
supp = cs.getSupportCount();
if (nTrans > 0) supp = 100*supp/nTrans;
}
// Add customer sequence to MIS:
for (int j = 0; j < seqSize; j++) {
Category cat = new Category( String.valueOf(j) );
double kj = sequenceNumber.getKey(cat);
if ( Category.isMissingValue(kj) )
kj = sequenceNumber.addCategory(cat);
ItemSet is = (ItemSet) cs.getItemSet(j);
// Go through sequence:
for (int k = 0; k < is.getSize(); k++) {
int pN = is.getItemAt(k);
double[] values = new double[nAtt];
values[0] = ki;
values[1] = kj;
values[2] = pN;
if (useSupp) values[3] = supp;
MiningVector mv = new MiningVector(values);
mv.setMetaData(metaData);
msd.add(mv);
};
};
}
return msd;
}
/**
* Exports customer sequence rules in transactional format of the following
* form:
* 1. Attribute - ruleNumber,
* 2. Attribute - sequenceNumber,
* 3. Attribute - itemId,
* 4. Attribute - isConclusion,
* [Attributes from set {support, confidence, coverage, lift}].
* 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.
*
* @return sequence rules as mining input stream
* @throws MiningException cannot export rules as mining input stream
*/
public MiningInputStream toMISSequenceRules() 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("customer sequence rules");
OrdinalAttribute ruleNumber = new OrdinalAttribute("ruleNumber");
OrdinalAttribute sequenceNumber = new OrdinalAttribute("sequenceNumber");
CategoricalAttribute itemId = (CategoricalAttribute)( (CustomerSequentialSettings) miningSettings ).getItemId();
CategoricalAttribute conclusionFlag = new CategoricalAttribute("conclusionFlag");
conclusionFlag.setDataType( CategoricalAttribute.BOOLEAN );
metaData.addMiningAttribute(ruleNumber);
metaData.addMiningAttribute(sequenceNumber);
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:
Vector rules = getSequenceRules();
int nRules = rules.size();
// Write all customer sequence 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 and show rule:
CustomRuleSetSeq crss = (CustomRuleSetSeq) rules.elementAt(i);
int itemSetSize = crss.getSize();
// Get characteristics of rule:
double supp = 0;
if (useSupp) supp = crss.getSupport() * 100;
double conf = 0;
if (useConf) conf = crss.getConfidence() * 100;
double cov = 0;
if (useCov) cov = coverage(crss) * 100;
double lift = 0;
if (useLift) lift = lift(crss);
double cos = 0;
if (useCos) cos = cosine(crss);
// Premise part of rule:
CustomSequence cs = (CustomSequence) crss.getPremise();
int seqSize = cs.getSize();
// Go through all itemsets of premise sequence:
for (int j = 0; j < seqSize; j++) {
Category cat = new Category( String.valueOf(j) );
double kj = sequenceNumber.getKey(cat);
if ( Category.isMissingValue(kj) )
kj = sequenceNumber.addCategory(cat);
ItemSet is = (ItemSet) cs.getItemSet(j);
// Go through sequence:
for (int k = 0; k < is.getSize(); k++) {
int pN = is.getItemAt(k);
double[] values = new double[nAtt];
int ind = 0;
values[ind++] = ki;
values[ind++] = kj;
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:
cs = crss.getConclusion();
seqSize = cs.getSize();
// Go through all itemsets of conclusion sequence:
for (int j = 0; j < seqSize; j++) {
Category cat = new Category( String.valueOf(j) );
double kj = sequenceNumber.getKey(cat);
if ( Category.isMissingValue(kj) )
kj = sequenceNumber.addCategory(cat);
ItemSet is = (ItemSet) cs.getItemSet(j);
// Go through sequence:
for (int k = 0; k < is.getSize(); k++) {
int pN = is.getItemAt(k);
double[] values = new double[nAtt];
int ind = 0;
values[ind++] = ki;
values[ind++] = kj;
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;
}
/**
* Return string representation of customer sequential model.
*
* @return string representation of customer sequential model
*/
public String toString()
{
return "Customer sequential mining model";
}
/**
* Write customer sequential model to HTML string.
*
* @return HTML string of customer sequential model
*/
public String toHtmlString()
{
Vector sequences = getSequentialRules();
int size = sequences.size();
CategoricalAttribute itemId = (CategoricalAttribute)((CustomerSequentialSettings)miningSettings).getItemId();
CustomerSequentialSettings seqset = (CustomerSequentialSettings)miningSettings;
CategoricalAttribute cattr = (CategoricalAttribute)seqset.getCustomerId();
int transNum = getNumberOfTransactions();
String html = "<br><br>sequential rules (" + size + "):" +
"<table width=100% border=1 cellpadding=0>" +
"<tr><th>sequential rule</th>" +
"<th>sequence</th></tr>";
for( int i=0; i < size; i++)
{
CustomSequence cs = (CustomSequence)sequences.get(i);
html = html + "<tr>";
html = html + "<td width=30%><font color=blue>" + (i+1) + "; length=" + cs.getSize() + "; " +
"support=" + ((double)cs.getSupportCount()/(double)transNum) + "</font></td>";
html = html + "<td>";
for( int j = 0; j < cs.getSize(); j++ )
{
ItemSet is = cs.getItemSet(j);
html = html + ">>>";
for(int k=0; k < is.getSize(); k++)
{
Category category = (Category)itemId.getCategory((double)is.getItemAt(k));
html = html + "<a href=#" + category.getDisplayValue() + ">" +
"<font color=blue><b>" + category.getDisplayValue() + "</b></color></a>" + " ";
}
}
html = html + "</td>";
}
html = html + "</tr></table>";
return html;
}
/**
* Write customer sequential model as plain text.
*
* @param writer writer for model
* @exception MiningException cannot write text object
*/
public void writePlainText( Writer writer ) throws MiningException
{
Vector sequences = getSequentialRules();
int size = sequences.size();
CategoricalAttribute itemId = (CategoricalAttribute)((CustomerSequentialSettings)miningSettings).getItemId();
try {
writer.write("\nModel contains "+size+" sequential rules\n");
for(int i=0;i<size;i++)
{
CustomSequence cs = (CustomSequence)sequences.get(i);
writer.write("Sequence with "+cs.getSize()+" itemsets:\n");
for(int j=0;j<cs.getSize();j++)
{
ItemSet is = cs.getItemSet(j);
writer.write("\tItemSet #"+j+": ");
for(int k=0;k<is.getSize();k++)
{
Category category = (Category)itemId.getCategory((double)is.getItemAt(k));
writer.write(category.getDisplayValue()
+" ");
}
writer.write("\n");
}
}
writer.flush();
} catch(IOException ex) {
throw new MiningException("I/O exception while writing to plain text");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -