Wednesday, September 13, 2017

An example for Apriori coded in Perl

#!/usr/bin/perl -w
use Data::Mining::Apriori;
$|=1;

$apriori = new Data::Mining::Apriori;

$apriori->{metrics}{minSupport}=0.0155; # The minimum support(required), default value is 0.01(1%)

$apriori->{metrics}{minConfidence}=0.0155; # The minimum confidence(required), default value is 0.10(10%)

$apriori->{metrics}{minLift}=1; # The minimum lift(optional)

$apriori->{metrics}{minLeverage}=0; # The minimum leverage(optional)

$apriori->{metrics}{minConviction}=0; # The minimum conviction(optional)

$apriori->{metrics}{minCoverage}=0; # The minimum coverage(optional)

$apriori->{metrics}{minCorrelation}=0; # The minimum correlation(optional)

$apriori->{metrics}{minCosine}=0; # The minimum cosine(optional)

$apriori->{metrics}{minLaplace}=0; # The minimum laplace(optional)

$apriori->{metrics}{minJaccard}=0; # The minimum jaccard(optional)

#$apriori->{output}=1;
# The output type(optional): 1 - Export to text file delimited by tab; 2 - Export to excel file with chart.

#$apriori->{pathOutputFiles}='data/'; # The path to output files(optional)

$apriori->{messages}=1; # A value boolean to display the messages(optional)

$apriori->{keyItemsDescription}{'101'}='MILK'; # Hash table reference to add items by key and description
$apriori->{keyItemsDescription}{102}='BREAD';
$apriori->{keyItemsDescription}{'103'}='CEREAL';

@items=(103,101);
$apriori->insert_key_items_transaction(\@items); # Insert key items by transaction
$apriori->insert_key_items_transaction([103,102]);
$apriori->insert_key_items_transaction([103,101]);
$apriori->insert_key_items_transaction([103,101,102]);
$apriori->insert_key_items_transaction([101,102]);
$apriori->insert_key_items_transaction([103,101,102]);
$apriori->insert_key_items_transaction([103,101]);
$apriori->insert_key_items_transaction([103,102]);
$apriori->insert_key_items_transaction([103,101,102]);
$apriori->insert_key_items_transaction([103,101,102]);

print "\n${\$apriori->quantity_possible_rules}"; # Show the quantity of possible rules

$apriori->{limitRules}=10; # The limit of rules(optional)

$apriori->{limitSubsets}=12; # The limit of subsets(optional)

$apriori->generate_rules;
# Generate association rules to no longer meet the minimum support, confidence, lift, leverage, conviction, coverage, correlation, cosine, laplace, jaccard or limit of rules

print "\n@{$apriori->{frequentItemset}}\n"; # Show frequent items


exit(1);

Monday, September 4, 2017

An example for Decision Tree with Perl

#!/usr/bin/perl -w
#Refer to http://search.cpan.org/~kwilliams/AI-DecisionTree-0.11/lib/AI/DecisionTree.pm
use AI::DecisionTree;
$dtree = new AI::DecisionTree;
 
$dtree->add_instance(attributes => {outlook     => 'sunny',
                    temperature => 'hot',
                    humidity    => 'high'},
     result => 'no');
 
$dtree->add_instance(attributes => {outlook     => 'sunny',
                    temperature => 'hot',
                    humidity    => 'normal'},
     result => 'yes');

$dtree->add_instance(attributes => {outlook     => 'overcast',
                    temperature => 'cold',
                    humidity    => 'normal'},
     result => 'no');

$dtree->add_instance(attributes => {outlook     => 'sunny',
                    temperature => 'cold',
                    humidity    => 'normal'},
     result => 'yes');


$dtree->train;
 
$result = $dtree->get_result(attributes => {outlook     => 'sunny',
                    temperature => 'hot',
                    humidity    => 'normal'});

print $result."\n";
exit(1);