Listing 1
//Get Connection factory object from the JNDI server
javax.datamining.resource.ConnectionFactory jdmCFactory =
(ConnectionFactory) jndiContext.lookup("java:comp/env/jdm/MyServer");
//Create a data mining server connection specification
ConnectionSpec svrConnSpec = (javax.datamining.resource.ConnectionSpec)
jdmCFactory.getConnectionSpec();
svrConnSpec.setName( ìuserî );
svrConnSpec.setPassword( ìpasswordî );
svrConnSpec.setURI( ìserverURIî );
//Create a Data Mining Engine (DME) Connection
javax.datamining.resource.Connection dmeConn =
(javax.datamining.resource.Connection)jdmCFactory.getConnection( svrConnSpec );
Listing 2
//Get PhysicalDataSetFactory
PhysicalDataSetFactory pdsFactory =
(PhysicalDataSetFactory)dmeConn.getFactory(
ìjavax.datamining.data.PhysicalDataSetî);
//Create PhysicalDataSet by settings import flag set to true
//to import the physical attributes details derived from
//the meta-data of the table.
PhysicalDataSet pdsBuild = pdsFactory.create(
ìDMUSER.CHURN_BUILD_TABLEî, true);
//Set the role of the CUSTOMER_ID column as caseId
PhysicalAttribute caseId = pdsBuild.getAttribute(ìCUSTOMER_IDî);
//Save the PhysicalDataSet in the DME
dmeConn.saveObject(pdsBuild, ìCHURN_BUILD_PDSî);
Listing 3
//Get LogicalDataFactory
LogicalDataFactory ldFactory =
(LogicalDataFactory)dmeConn.getFactory(
ìjavax.datamining.data.LogicalDataî);
//Create LogicalData by specifying BUILD_PDS as input
LogicalData ldBuild = ldFactory.create(ìBUILD_PDSî);
//Set the attribute types of all the logical attributes
LogicalAttribute ageAttr = ldBuild.getAttribute(ìAGEî);
ageAttr.setAttributeType(AttributeType.numerical);
LogicalAttribute workClassAttr = ldBuild.getAttribute(ìWORKCLASSî);
ageAttr.setAttributeType(AttributeType.categorical);
. . . .
//Save LogicalData in the DME
dmeConn.saveObject(ldBuild, ìCHURN_LDî);
Listing 4
//Create a classification settings factory
ClassificationSettingsFactory clasFactory =
(ClassificationSettingsFactory)dmeConn.getFactory(
"javax.datamining.supervised.classification.ClassificationSettings");
//Create a ClassificationSettings object
ClassificationSettings clas = clasFactory.create();
//Set Logical data
clas.setLogicalDataName(ìCHURN_LDî);
//Set target attribute name
clas.setTargetAttributeName(ìCHURNî);
//Create a TreeSettingsFactory
TreeSettingsFactory treeFactory =
(TreeSettingsFactory)dmeConn.getFactory(
"javax.datamining.algorithm.tree.TreeSettings");
//Create TreeSettings instance
TreeSettings treeAlgo = treeFactory.create();
treeAlgo.setBuildHomogeneityMetric(TreeHomogeneityMetric.entropy);
treeAlgo.setMaxDepth(10);
treeAlgo.setMinNodeSize( 10, SizeUnit.count );
//Set algorithm settings in the classification settings
clas.setAlgorithmSettings(treeAlgo);
//Save the build settings object in the database
dmeConn.saveObject(ìCHURN_BUILD_SETTINGSî, clas, true);
Listing 5
//Create BuildTaskFactory
BuildTaskFactory buildTaskFactory = BuildTaskFactory)dmeConn.getFactory(
ìjavax.datamining.task.BuildTaskî);
//Create BuildTask object
BuildTask buildTask = buildTaskFactory.create(
ìCHURN_BUILD_PDSî,îCHURN_BUILD_SETTINGSî,îCHURN_MODELî);
//Save BuildTask object
dmeConn.saveObject(ìCHURN_BUILD_TASKî, buildTask, true);
//Execute build task asynchronously in the database
ExecutionHandle execHandle = dmeConn.execute(ìCHURN_BUILD_TASKî);
//Wait for completion of the task
ExecutionStatus execStatus =
execHandle.waitForCompletion(Integer.MAX_VALUE);
Listing 6
//Create & save PhysicalDataSpecification
PhysicalDataSet testData = pdsFactory.create(
ìDMUSER.CHURN_TEST_TABLEî, false );
testData.addAttribute(caseId);
m_dmeConn.saveObject( ìCHURN_TEST_PDSî, testData, true );
//Create ClassificationTestTaskFactory
ClassificationTestTaskFactory testTaskFactory =
(ClassificationTestTaskFactory)dmeConn.getFactory(
ìjavax.datamining.supervised.classification.ClassificationTestTaskî );
//Create, store & execute Test Task
ClassificationTestTask testTask = testTaskFactory.create(
"CHURN_TEST_PDS", "CHURN_MODEL", "CHURN_TESTMETRICS" );
//Set lift metrics
testTask.setNumberOfLiftQuantiles(10);]
testTask.setPositiveTargetValue(new Integer(1));
//Save TestTask object
dmeConn.saveObject(ìCHURN_TEST_TASKî, testTask, true);
//Execute test task asynchronously in the database
ExecutionHandle execHandle = dmeConn.execute(ìCHURN_TEST_TASKî);
//Wait for completion of the task
ExecutionStatus execStatus = execHandle.waitForCompletion(Integer.MAX_VALUE);
//Explore the test metrics after successful completion of the task
if(ExecutionState.success.equals(execStatus.getState())) {
//Retrieve the test metrics object
ClassificationTestMetrics testMetrics =
(ClassificationTestMetrics)dmeConn.getObject(ìCHURN_TESTMETRICSî);
//Retrieve confusion matrix and accuracy
Double accuracy = testMetrics.getAccuracy();
ConfusionMatrix cfm = testMetrics.getConfusionMatrix();
//Retrieve lift
Lift lift = testMetrics.getLift();
}
Listing 7
//Create & save PhysicalDataSpecification
PhysicalDataSet applyData = pdsFactory.create(
"CHURN_APPLY_TABLE ", false );
applyData.addAttribute( caseId );
m_dmeConn.saveObject( "CHURN_APPLY_PDS", applyData, true );
//Create ClassificationApplySettingsFactory
ClassificationApplySettingsFactory applySettingsFactory =
(ClassificationApplySettingsFactory)dmeConn.getFactory(
ìjavax.datamining.supervised.classification.ClassificationApplySettingsî);
//Create & save ClassificationApplySettings
ClassificationApplySettings clasAS = applySettingsFactory.create();
clasAS.mapTopPrediction(ClassificationApplyContent.predictedCategory,
ìPREDICTED_CHURNî);
HashMap srcDestMap = new HashMap();
srcDestMap.put(ìCUSTOMER_IDî, ìCUSTOMER_IDî);
clasAS.setSourceDestinationMap(srcDestMap);
m_dmeConn.saveObject( "CHURN_APPLY_SETTINGS", clasAS, true);
//Create DataSetApplyTaskFactory
DataSetApplyTaskFactory applyTaskFactory =
(DataSetApplyTaskFactory)dmeConn.getFactory(
ìjavax.datamining.task.apply.DataSetApplyTaskî);
//Create, store & execute apply Task
DataSetApplyTask applyTask = m_dsApplyFactory.create(
"CHURN_APPLY_PDS ", "CHURN_MODEL", "CHURN_APPLY_SETTINGS ",
"CHURN_APPLY_RESULTS");
//Save ApplyTask object
dmeConn.saveObject(ìCHURN_APPLY_TASKî, applyTask, true);
//Execute test task asynchronously in the database
ExecutionHandle execHandle = dmeConn.execute(ìCHURN_APPLY_TASKî);
//Wait for completion of the task
ExecutionStatus execStatus =
execHandle.waitForCompletion(Integer.MAX_VALUE);
Listing 8
import java.util.*;
import javax.datamining.*;
import javax.datamining.association.*;
import javax.datamining.data.*;
import javax.datamining.resource.*;
import javax.datamining.task.*;
import javax.naming.*;
// JDM API Imports
/**
* This class demonstrates the market basket analysis using JDM API.
* Execute of this class requires a JDM compatible Data Mining Engine (DME)
* that supports association rules.
*
* Before executing this follow these steps:
*
* Step-1: Connection Setup
* Ensure that DME ConnectionFactory is registered with the JNDI server.
* Change the connection details in the example accordingly. JDM vendors
* could provide the vendor specific ConnectionFactory class that can
* be used to simplify the execution of this example code.
*
* Step-2: PRODUCT_TRANSACTIONS table setup
* This example cannot be run without the data. For convenience, a sample data
* is printed here in comma separated format that can be imported as a table
* in the database or vendor specific data format.
Transaction_Id,Product_name,Purchased
1,Pepperoni Pizza,1
1,Diet Coke,1
1,Buffalo wings,1
2,Buffalo wings,1
2,Diet Coke,1
3,Pepperoni Pizza,1
3,Diet Coke,1
4,Diet Coke,1
4,French Fries,1
5,Diet Coke,1
5,Buffalo wings,1
*
* Step-3: Classpath setup
* Ensure that to run this example you have all the jar files inclusing the
* jdm.jar file in your classpath. Refer JDM Vendor product documentation
* to find the appropriate dependent libraries to execute this program.
* Step-4: Execute the class
* Using
* java MarketBasket
*/
public class MarketBasket
{
//Connection related data members
private Connection m_dmeConn = null;
private ConnectionFactory m_dmeConnFactory = null;
//Object factories used in this demo program
private PhysicalDataSetFactory m_pdsFactory = null;
private PhysicalAttributeFactory m_paFactory = null;
private AssociationSettingsFactory m_assoFactory = null;
private RulesFilterFactory m_filterFactory = null;
private BuildTaskFactory m_buildFactory = null;
public static void main( String args[] )
{
try
{
MarketBasket marketBasket = new MarketBasket();
//Login to DME
marketBasket.login();
//Initialize object factories
marketBasket.initFactories();
//Build Market Basket Model & explore the model rules
marketBasket.buildModel();
}
catch(Exception anyExp) {
anyExp.printStackTrace(System.out);
}
finally {
try {
//ogout from the Data Mining Engine
m_dmeConn.close();
} catch(Exception anyExp1) { }//Ignore
}
}
/**
* Constructor initializes all the object factories
* Get all the factory objects from the connection to create the JDM objects.
*/
public void initFactories() throws JDMException
{
m_pdsFactory = (PhysicalDataSetFactory)m_dmeConn.getFactory(
"javax.datamining.data.PhysicalDataSet");
m_paFactory = (PhysicalAttributeFactory)m_dmeConn.getFactory(
"javax.datamining.data.PhysicalAttribute");
m_assoFactory = (AssociationSettingsFactory)m_dmeConn.getFactory(
"javax.datamining.association.AssociationSettings");
m_buildFactory = (BuildTaskFactory)m_dmeConn.getFactory(
"javax.datamining.task.BuildTask");
m_filterFactory = (RulesFilterFactory)m_dmeConn.getFactory(
"javax.datamining.association.RulesFilter");
}
public void login() throws JDMException, NamingException
{
//1. Create or Get DME ConnectionFactory.
// Here one could create vendor specific connection factory to
// simplify the execution of the demo or use the JNDI to get
// the connection factory in vendor neutral approach
//Create JNDI initial context
Hashtable jndiEnv = new Hashtable ();
//Set JNDI server specific environment variables
//and authentication details
jndiEnv.put( Context.INITIAL_CONTEXT_FACTORY,
"com.myCompany.jndi.InitialContextFactoryImpl" );
jndiEnv.put( Context.PROVIDER_URL, "http://myHost:myPort/myService" );
jndiEnv.put( Context.SECURITY_PRINCIPAL, "user" );
jndiEnv.put( Context.SECURITY_CREDENTIALS, "password" );
InitialContext jndiContext = new InitialContext(jndiEnv);
//Perform JNDI lookup to obtain the connection factory
javax.datamining.resource.ConnectionFactory jdmCFactory =
(ConnectionFactory) jndiContext.lookup("java:comp/env/jdm/MyServer");
//Create a data mining server connection
ConnectionSpec svrConnSpec = (javax.datamining.resource.ConnectionSpec)
jdmCFactory.getConnectionSpec();
//If the DME authentication details are note registered specify the DME
//authentication details. Refer to JDM vendor product documentation for
//DME authentication details
svrConnSpec.setName( "user" );
svrConnSpec.setPassword( "password" );
svrConnSpec.setURI( "serverURI" );
m_dmeConn = jdmCFactory.getConnection( svrConnSpec );
}
/**
* This method builds a market basket model using PRODUCT_TRANSACTIONS table.
* Refer to JDM Vendor product documentation to see the data URI representation.
*/
public void buildModel() throws JDMException
{
//1. Create & save PhysicalDataSet
PhysicalDataSet buildData = m_pdsFactory.create(
"PRODUCT_TRANSACTIONS", false );//Import meta-data is set to false
//Specify the transactional format data attribute roles
PhysicalAttribute transId = m_paFactory.create(
"Transaction_Id", AttributeDataType.integerType,
PhysicalAttributeRole.caseId );
PhysicalAttribute productName = m_paFactory.create(
"Product_name", AttributeDataType.stringType,
PhysicalAttributeRole.attributeName );
PhysicalAttribute purchasedFlag = m_paFactory.create(
"Purchased", AttributeDataType.integerType,
PhysicalAttributeRole.attributeValue );
buildData.addAttribute(transId);
buildData.addAttribute(productName);
buildData.addAttribute(purchasedFlag);
m_dmeConn.saveObject( "MarketBasket_DATA", buildData, false );
//2. Create & save Mining Function Settings
//Create ClassificationSettings
AssociationSettings buildSettings = m_assoFactory.create();
buildSettings.setMinSupport( 0.1 );
buildSettings.setMinConfidence( 0.51 );
m_dmeConn.saveObject( "MarketBasket_SETTINGS", buildSettings, true );
//3. Create, save & execute Build Task
BuildTask buildTask = m_buildFactory.create(
"MarketBasket_DATA", //Build data
"MarketBasket_SETTINGS", //Build settings name
"MarketBasket_MODEL" //Mining model name
);
m_dmeConn.saveObject("MarketBasket_TASK", buildTask, true);
ExecutionHandle execHandle = m_dmeConn.execute("MarketBasket_TASK");
System.out.print("MarketBasket_TASK is started, please wait. ");
//Wait for completion of the task
ExecutionStatus status = execHandle.waitForCompletion(Integer.MAX_VALUE);
//Check the status of the task after completion
boolean isTaskSuccess = status.getState().equals(ExecutionState.success);
if( isTaskSuccess ) {
//Task completed successfully
System.out.println("MarketBasket_TASK is successful.");
} else {//Task failed
System.out.println("MarketBasket_TASK is failed.\nFailure Description: " +
status.getDescription() );
}
//4. Restore the model from the DME and explore the details of the model
AssociationModel model =
(AssociationModel)m_dmeConn.retrieveObject(
"MarketBasket_MODEL", NamedObject.model);
//5. Display the rules
displayAssociationRules( model );
}
/**
* This method displays rules from association model.
*
* @param model An association model
*/
private void displayAssociationRules(AssociationModel model)
throws JDMException
{
// specify the rule selection criteria
// retrieve rules with selection criteria
RulesFilter filter = (RulesFilter) m_filterFactory.create();
filter.setMaxNumberOfRules(10);
filter.setOrderingCondition(
new RuleProperty[] { RuleProperty.support, RuleProperty.confidence },
new SortOrder[] { SortOrder.descending, SortOrder.descending }
);
filter.setRange( RuleProperty.support, 0.15, 1.0 );
// filter.setRange( RuleProperty.confidence, 0.55, 1.0 );
// retrieve rules
Collection rules = model.getRules(filter);
Iterator iRules = rules.iterator();
int ruleNumber = 1;
while( iRules.hasNext() )
{
AssociationRule rule = (AssociationRule) iRules.next();
System.out.println("Rule-" + ruleNumber++);
Itemset antecedent = rule.getAntecedent();
Object[] ante_items = antecedent.getItems();
// sort the items in antecedent to produce deterministic order of items
TreeSet sortedSet = new TreeSet();
for( int i=0; i<ante_items.length; i++ )
sortedSet.add( ante_items[i] );
Iterator sortedI = sortedSet.iterator();
while( sortedI.hasNext() )
System.out.print( sortedI.next() + " " );
Itemset consequent = rule.getConsequent();
Object[] cons_items = consequent.getItems();
System.out.println( "==> " + cons_items[0] + " (support=" +
rule.getSupport() + ", confidence=" + rule.getConfidence() + ")" );
}
}
}