Discriminant Analysis

Example A.5. 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], output=[train_data, test_data], seed =1124)

    # 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 the 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"

Output

Classification model build: OK
Classification model apply: OK
Please refer to "output" dataset to see the results of this example
Classification model test: OK