The following script presents the functionality of the Discriminant Analysis module in AdvancedMiner. Please note that this script does not include the data required to run the script in AdvancedMiner Client. The full source with the data can be found in the Examples appendix.
Example 32.1. Discriminant analysis
# Creates a dataset for testing purposes
table 'discriminant_test' :
pa pb pc pd Class
5.1 3.5 1.4 0.2 'ClassA'
4.7 3.2 1.3 0.2 'ClassA'
5.0 3.6 1.4 0.2 'ClassA'
4.6 3.4 1.4 0.3 'ClassA'
4.4 2.9 1.4 0.2 'ClassA'
5.4 3.7 1.5 0.2 'ClassA'
4.8 3.0 1.4 0.1 'ClassA'
5.8 4.0 1.2 0.2 'ClassA'
5.4 3.9 1.3 0.4 'ClassA'
5.7 3.8 1.7 0.3 'ClassA'
5.4 3.4 1.7 0.2 'ClassA'
4.6 3.6 1.0 0.2 'ClassA'
4.8 3.4 1.9 0.2 'ClassA'
5.0 3.4 1.6 0.4 'ClassA'
5.2 3.4 1.4 0.2 'ClassA'
4.8 3.1 1.6 0.2 'ClassA'
5.2 4.1 1.5 0.1 'ClassA'
4.9 3.1 1.5 0.1 'ClassA'
4.9 3.1 1.5 0.1 'ClassA'
5.1 3.4 1.5 0.2 'ClassA'
4.5 2.3 1.3 0.3 'ClassA'
4.4 3.2 1.3 0.2 'ClassA'
5.1 3.8 1.9 0.4 'ClassA'
4.8 3.0 1.4 0.3 'ClassA'
5.1 3.8 1.6 0.2 'ClassA'
4.6 3.2 1.4 0.2 'ClassA'
5.3 3.7 1.5 0.2 'ClassA'
5.0 3.3 1.4 0.2 'ClassA'
6.4 3.2 4.5 1.5 'ClassB'
5.5 2.3 4.0 1.3 'ClassB'
5.7 2.8 4.5 1.3 'ClassB'
4.9 2.4 3.3 1.0 'ClassB'
5.2 2.7 3.9 1.4 'ClassB'
5.9 3.0 4.2 1.5 'ClassB'
6.1 2.9 4.7 1.4 'ClassB'
6.7 3.1 4.4 1.4 'ClassB'
5.8 2.7 4.1 1.0 'ClassB'
5.6 2.5 3.9 1.1 'ClassB'
6.1 2.8 4.0 1.3 'ClassB'
6.1 2.8 4.7 1.2 'ClassB'
6.6 3.0 4.4 1.4 'ClassB'
6.7 3.0 5.0 1.7 'ClassB'
5.7 2.6 3.5 1.0 'ClassB'
5.5 2.4 3.7 1.0 'ClassB'
6.0 2.7 5.1 1.6 'ClassB'
5.6 2.7 4.2 1.3 'ClassB'
5.7 3.0 4.2 1.2 'ClassB'
5.7 2.9 4.2 1.3 'ClassB'
6.2 2.9 4.3 1.3 'ClassB'
5.1 2.5 3.0 1.1 'ClassB'
5.7 2.8 4.1 1.3 'ClassB'
6.0 3.4 4.5 1.6 'ClassB'
6.3 2.3 4.4 1.3 'ClassB'
5.5 2.5 4.0 1.3 'ClassB'
6.1 3.0 4.6 1.4 'ClassB'
5.0 2.3 3.3 1.0 'ClassB'
# load and set the test dataset names
train_data='discriminant_test_trn'
test_data='discriminant_test_tst'
# use the built-in splitting table procedure to split the dataset into
# training and testing data
tableSplit('discriminant_test', split=[4, 6], seed=1250, output=[train_data, test_data])
# create a PhysicalData object for these datasets
pd_trn = PhysicalData(train_data)
pd_tst = PhysicalData(test_data)
# save the objects into MR (MetadataRepository)
save('pd_trn', pd_trn)
save('pd_tst', pd_tst)
# create classification function settings
fs = ClassificationFunctionSettings()
# create algorithm settings for discriminant classification
as = DiscriminantSettings()
# create logical data for the training object
ld = LogicalData(pd_trn)
# sets the prepared algoritim settings to classification function settings
fs.setAlgorithmSettings(as)
# sets the prepared logical data object to classification function settings
fs.setLogicalData(ld)
# the 'Class' 'Class' will be used as the target of classification
fs.getAttributeUsageSet().getAttribute('Class').setUsage(UsageOption.target)
# save the prepared classification function settings to MR
save('fs', fs)
# create a mining build task, then save and execute it
bt = MiningBuildTask('pd_trn', 'fs', 'discrim_model')
save('discrim_bt', bt)
execute('discrim_bt')
print "Classification model build: OK"
# create a mining apply task to score the test data
at = MiningApplyTask()
# the name of miningModel generated using classification build task is 'model' - now
# this miningModel is assigned to apply task
at.setModelName('discrim_model')
# set apply source data name to PhysicalData which is the testing dataset
at.setSourceDataName('pd_tst')
# prepare PhysicalData to store the data in the applying process
pd1 = PhysicalData('output')
save('pd_out', pd1)
at.setTargetDataName('pd_out')
# prepare apply output for classification
cao = ClassificationApplyOutput()
# there will be two apply output items - both Classification Category Items, which
# will give the information about the probability of fit to one of the two classes
cci1 = ClassificationCategoryItem()
cci1.setDestinationName('ClassA')
cci1.setOutputType(ClassificationOutputType.probability)
cci1.setSelectedTargetCategory('ClassA')
cci2 = ClassificationCategoryItem()
cci2.setDestinationName('ClassB')
cci2.setOutputType(ClassificationOutputType.probability)
cci2.setSelectedTargetCategory('ClassB')
# adds the prepared apply output items to the item list
cao.item.add(cci1)
cao.item.add(cci2)
at.setApplyOutput(cao)
# adds a direct mapping of the original target
asi = ApplySourceItem()
asi.setDestinationName('Class')
asi.setSourceName('Class')
at.directMapping.add(asi)
# if the output table already exists overwrite it.
at.setReplaceExistingData(TRUE)
# save and execute apply task.
save('discrim_at', at)
execute('discrim_at')
print "Classification model apply: OK"
print "Please refer to \"output\" dataset to see results of this example"
# testing of the classification model
tt = ClassificationTestTask('pd_tst', 'discrim_model', 'discrim_out')
tt.testDataTargetAttributeName = 'Class'
tt.positiveTargetValue = 'ClassA'
save('discrim_tt', tt)
execute('discrim_tt')
print "Classification model test: OK"
print "Results of this example:\n"
print 'Class ClassA ClassB'
trans None<-'output':
print Class,"%.4f"%ClassA,"%.4f"%ClassB
Output:
Classification model build: OK Classification model apply: OK Please refer to "output" dataset to see results of this example Classification model test: OK Results of this example: Class ClassA ClassB ClassA 112.1758 0.0000 ClassA 89.8505 0.0000 ClassA 118.3799 0.0000 ClassA 88.7906 0.0000 ClassA 66.0818 0.0000 ClassA 88.7847 0.0000 ClassA 153.5090 0.0000 ClassA 116.6557 0.0000 ClassA 120.6522 0.0000 ClassA 104.1476 0.0000 ClassA 119.9023 0.0000 ClassA 76.3064 0.0000 ClassA 105.9716 0.0000 ClassA 80.4579 0.0000 ClassA 166.2009 0.0000 ClassA 95.2905 0.0000 ClassA 95.2905 0.0000 ClassA 13.4212 0.0000 ClassA 87.8038 0.0000 ClassA 62.6096 0.0000 ClassA 88.1054 0.0000 ClassA 97.7208 0.0000 ClassB 0.0000 139.3288 ClassB 0.0000 108.8467 ClassB 0.0000 93.3899 ClassB 0.0000 71.5368 ClassB 0.0000 97.6359 ClassB 0.0000 100.8035 ClassB 0.0000 72.7283 ClassB 0.0000 89.9913 ClassB 0.0000 159.3261 ClassB 0.0000 78.7979 ClassB 0.0000 98.7718 ClassB 0.0000 96.4234 ClassB 0.0000 91.4814 ClassB 0.0000 104.5953 ClassB 0.0000 106.4954