Example of automatic variable selection

The following script presents the functionality of Variable Selection 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 28.1. Variable selection

# variable selection example
if not tableExists('german_credit'):
 raise "Table 'german_credit' does not exists. Please run german_credit.gy script from data directory first"

targetName = 'Class'
dataName = 'german_credit'
binTable = 'german_credit_bin'
obligatory = ['duration','existing_credits','age']


#------ Binarize data transformation ------

orig_pd = PhysicalData(dataName)
orig_ld  = LogicalData(orig_pd)
save(dataName + '_pd', orig_pd)
save(dataName + '_ld', orig_ld)

transformationSettings = BinarizeSettings()
transformationSettings.setLogicalData(orig_ld)
transformationSettings.getAttributeUsageSet().getAttribute(targetName).usage = TransformationUsageOption.copy
save('binarize_settings', transformationSettings)

bt = TransformationBuildTask()
bt.transformationName = 'binarize_build'
bt.transformationSettingsName = 'binarize_settings'
bt.physicalDataName = dataName + '_pd'

save('binarize_bt', bt)
execute('binarize_bt')

save("pd_out",PhysicalData(binTable))
at = TransformationApplyTask()
at.replaceExistingData = TRUE 
at.setSourceDataName(dataName + '_pd')
at.setTargetDataName("pd_out") 
at.setTransformationName('binarize_build')
save('binarize_apply', at)
execute('binarize_apply')

#-----------------------------------------


pd = PhysicalData(binTable)
ld = LogicalData(pd)
save(binTable + '_pd', pd)
save(binTable + '_ld', ld)


#------ Function and Algorithm settings ------

fs = ClassificationFunctionSettings()
fs.logicalData = ld
fs.targetAttributeName = targetName


as = LogisticRegressionSettings()
as.estimationMethod = LogisticEstimationMethod.newton
as.linkFunctionType = LinkFunctionType.logit
as.preselection = TRUE
as.intercept = TRUE

opt = OptimizationAlgorithmSettings()
opt.convergenceCriterion = ConvergenceCriterion.likelihood
opt.convergenceThreshold = 0.1
opt.iterMax = 20


#------ Variable Selection settings ------

attrUS = fs.attributeUsageSet
for attr in obligatory:
           attrUS.getAttribute(attr).setUsage(UsageOption.obligatory);

vss = VariableSelectionSettings()
vss.variableSelectionMethod = VariableSelectionMethod.forward
vss.modelEntryLevel = 0.1
vss.maxModelSize = 30
vss.groupMode = TRUE

#-----------------------------------------


as.variableSelectionSettings = vss
as.optimizationAlgorithmSettings = opt
fs.algorithmSettings = as
save('model_fs', fs)


#----- building logistic model
bt = MiningBuildTask(binTable + '_pd', 'model_fs', 'model')
save('model_bt', bt)
execute('model_bt')

#------ printing results of variable selection
binTab = tableRead(binTable)

varNames = load('model').getModelStatistics().varNames

print "%30s%40s"%("Variable","Selected to model")
for i in range(0,len(binTab[0])):
    print "%-60s"%binTab[0][i],
    if binTab[0][i] in varNames: 
        print "yes"
    else:
        print "no"

Output

                      Variable                       Selected to model
num_dependents                                               no
residence_since                                              no
installment_commitment                                       no
duration                                                     no
Class                                                        no
age                                                          no
existing_credits                                             no
credit_amount                                                no
__employment__unemployed                                     no
__employment__greater_7                                      no
__employment__4_less_X_less_7                                no
__employment__1_less_X_less_4                                no
__employment__less_1                                         no
__housing__for_free                                          no
__housing__rent                                              no
__housing__own                                               no
__property_magnitude__car                                    no
__property_magnitude__life_insurance                         no
__property_magnitude__real_estate                            no
__property_magnitude__no_known_property                      no
__job__skilled                                               no
__job__high_qualif_self_emp_mgmt                             no
__job__unskilled_resident                                    no
__job__unemp_unskilled_non_res                               no
__own_telephone__yes                                         no
__own_telephone__none                                        no
__purpose__other                                             yes
__purpose__new_car                                           yes
__purpose__radio_tv                                          yes
__purpose__domestic_appliance                                yes
__purpose__retraining                                        yes
__purpose__furniture_equipment                               yes
__purpose__education                                         yes
__purpose__used_car                                          yes
__purpose__repairs                                           yes
__purpose__business                                          yes
__personal_status__female_div_dep_mar                        no
__personal_status__male_mar_wid                              no
__personal_status__male_div_sep                              no
__personal_status__male_single                               no
__savings_status__less_100                                   no
__savings_status__100_less_X_less_500                        no
__savings_status__greater_1000                               no
__savings_status__no_known_savings                           no
__savings_status__500_less_X_less_1000                       no
__checking_status__greater_200                               yes
__checking_status__0_less_X_less_200                         yes
__checking_status__less_0                                    yes
__checking_status__no_checking                               yes
__other_parties__co_applicant                                no
__other_parties__none                                        no
__other_parties__guarantor                                   no
__foreign_worker__yes                                        no
__foreign_worker__no                                         no
__other_payment_plans__none                                  no
__other_payment_plans__bank                                  no
__other_payment_plans__stores                                no
__credit_history__delayed_previously                         no
__credit_history__all_paid                                   no
__credit_history__no_credits_all_paid                        no
__credit_history__critical_other_existing_credit             no
__credit_history__existing_paid                              no