📄 associationrules.pm
字号:
=pod
=head2 generate_rules($file_prefix, $support_threshold, $max_n)
Given
=over
=item 0 a file prefix
=item 0 a support threshold (optional)
=item 0 a confidence threshold (optional)
=item 0 maximum frequent set size to look for (optional)
=back
create a file with all association rules in it. The output file is of
the form:
support-count confidence left-hand-set-size right-hand-set-size frequent-set-size left-hand-set => right-hand-set
=cut
sub generate_rules {
my $file_prefix = shift;
my $support_threshold = shift;
my $confidence_threshold = shift;
my $max_n = shift;
$support_threshold = 1 if !defined($support_threshold);
$confidence_threshold = 0 if !defined($confidence_threshold);
my $num_rules = 0;
# Read in frequent set supports
my %frequent_set;
read_frequent_sets(\%frequent_set, $file_prefix, $support_threshold, $max_n);
die "Found no frequent sets from file prefix $file_prefix support $support_threshold " if (0 == int(keys(%frequent_set)));
# Go through the sets computing stats
my $rulefile = $file_prefix . '-support-' . $support_threshold . '-conf-' .
$confidence_threshold . '-rules.txt';
open(RULES, ">$rulefile") or die "Couldn't open $rulefile: $!\n";
while (my ($set, $count) = each %frequent_set) {
# Traverse all subsets (save full and empty)
my $support = $frequent_set{$set};
die "Couldn't find frequent set '$set'" if !defined($support);
my @set = split('\s+', $set);
for my $lhs_selector (1..(1<<int(@set))-2) {
my @lhs_set = @set[grep $lhs_selector&1<<$_, 0..$#set];
my $all_ones = (1<<int(@set))-1;
my $rhs_selector = $all_ones ^ $lhs_selector;
my @rhs_set = @set[grep $rhs_selector&1<<$_, 0..$#set];
# print "lhs_selector $lhs_selector 1<<int(@set) ", 1<<int(@set), " rhs_selector $rhs_selector\n";
# print "lhs_set @lhs_set ";
# print "rhs_set @rhs_set\n";
my $lhs_set = join(' ', @lhs_set);
my $rhs_set = join(' ', @rhs_set);
# Spit out rule
my $lhs_support = $frequent_set{$lhs_set};
#my $rhs_support = $frequent_set{$rhs_set};
die "Couldn't find frequent set '$lhs_set'" if !defined($lhs_support);
#die "Couldn't find frequent set '$rhs_set'" if !defined($rhs_support);
# For rule A => B, support = T(AB), conf = T(AB) / T(A)
my $conf = $support / $lhs_support;
if ($conf >= $confidence_threshold) {
$num_rules++;
print RULES "$support ", sprintf("%.3f ", $conf),
int(@lhs_set), ' ', int(@rhs_set), ' ', int(@set), ' ',
"$lhs_set => $rhs_set\n";
}
}
}
close(RULES);
print STDERR "$num_rules rules\n" if $debug;
}
sub set_debug {
$debug = $_[0];
}
1;
=head1 DESCRIPTION
This module contains some functions to do association rule mining from
text files. This sounds obscure, but really measures beautifully
simple things through counting.
=head2 FREQUENT SETS
Frequent sets answer the question, "Which events occur together more
than N times?"
=head3 The detail
The 'transaction file' contains items in transactions. A set of items
has 'support' s if all the items occur together in at least s
transactions. (In many papers, support is a number between 0 and 1
representing the fraction of total transactions. I found the absolute
number itself more interesting, so I use that instead. Sorry for the
confusion.) For an itemset "A B C", the support is sometimes notated
"T(A B C)" (the number of 'T'ransactions).
A set of items is called a 'frequent set' if it has support at least
the given support threshold. Generating frequent set produces all
frequent sets, and some information about each set (e.g., its
support).
=head2 RULES
Association rules answer the (related) question, "When these events
occur, how often do those events also occur?"
=head3 The detail
A rule has a left-hand set of items and a right-hand set
of items. A rule "LHS => RHS" with a support s and 'confidence' c means
that the underlying frequent set (LHS + RHS) occured together in at
least s transactions, and for all the transactions LHS occurred in,
RHS also occured in at least the fraction c (a number from 0 to 1).
Generating rules produces all rules with support at least the given
support threshold, and confidence at least the given confidence
threshold. The confidence is sometimes notated "conf(LHS => RHS) =
T(LHS + RHS) / T(LHS)". There is also related data with each rule
(e.g., the size of its LHS and RHS, the support, the confidence,
etc.).
=head3 FREQUENT SETS AND ASSOCIATION RULES GENERALLY USEFUL
Although association rule mining is often described in commercial
terms like "market baskets" or "transactions" (collections of events)
and "items" (events), one can imagine events that make this sort of
counting useful across many domains. Events could be
=over
=item 0 stock market went down at time t
=item 0 patient had symptom X
=item 0 flower petal length was > 5mm
=back
For this reason, I believe counting frequent sets and looking at
association rules to be a fundamental tool of any data miner, someone
who is looking for patterns in pre-existing data, whether commercial
or not.
=head1 EXAMPLES
Given the following input file:
234 Orange
463 Strawberry
53 Apple
234 Banana
412 Peach
467 Pear
234 Pear
147 Pear
141 Orange
375 Orange
Generating frequent sets at support threshold 1 (a.k.a. 'at support 1')
produces three files:
The 1-sets:
1 Strawberry
1 Banana
1 Apple
3 Orange
1 Peach
3 Pear
The 2-sets:
1 Banana Orange
1 Banana Pear
1 Orange Pear
The 3-sets:
1 Banana Orange Pear
Generating the rules at support 1 produces the following:
1 0.333 1 1 2 Orange => Pear
1 0.333 1 1 2 Pear => Orange
1 1.000 1 2 3 Banana => Orange Pear
1 0.333 1 2 3 Orange => Banana Pear
1 1.000 2 1 3 Banana Orange => Pear
1 0.333 1 2 3 Pear => Banana Orange
1 1.000 2 1 3 Banana Pear => Orange
1 1.000 2 1 3 Orange Pear => Banana
1 1.000 1 1 2 Banana => Orange
1 0.333 1 1 2 Orange => Banana
1 1.000 1 1 2 Banana => Pear
1 0.333 1 1 2 Pear => Banana
Generating frequent sets at support 2 produces one file:
3 Orange
3 Pear
Generating rules at support 2 produces nothing.
Generating rules at support 1 and confidence 0.5 produces:
1 1.000 1 2 3 Banana => Orange Pear
1 1.000 2 1 3 Banana Orange => Pear
1 1.000 2 1 3 Banana Pear => Orange
1 1.000 2 1 3 Orange Pear => Banana
1 1.000 1 1 2 Banana => Orange
1 1.000 1 1 2 Banana => Pear
Note all the lower confidence rules are gone.
=head1 ALGORITHM
=head2 Generating frequent sets
Generating frequent sets is straight-up Apriori. See for example:
http://www.almaden.ibm.com/software/quest/Publications/papers/vldb94_rj.pdf
I have not optimized. It depends on having the transactions all in
memory. However, given that, it still might scale decently (millions
of transactions).
=head2 Generating rules
Generating rules is a very vanilla implementation. It requires
reading all the frequent sets into memory, which does not scale at
all. Given that, since computers have lots of memory these days, you
might still be able to get away with millions of frequent sets (which
is <<millions of transactions).
=head1 BUGS
There is an existing tool (written in C) to mine frequent sets I kept
running across:
http://fuzzy.cs.uni-magdeburg.de/~borgelt/software.html#assoc
I should check it out to see if it is easy or desirable to be
file-level compatible with it.
One could imagine wrapping it in Perl, but the Perl-C/C++ barrier is
where I have encountered all my troubles in the past, so I wouldn't
personally pursue that.
=head1 VERSION
This document describes Data::Mining::AssociationRules version 0.1.
=head1 AUTHOR
Dan Frankowski
dfrankow@winternet.com
http://www.winternet.com/~dfrankow
Hey, if you download this module, drop me an email! That's the fun
part of this whole open source thing.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included
in the distribution and available in the CPAN listing for
Data::Mining::AssociationRules (see www.cpan.org or search.cpan.org).
=head1 DISCLAIMER
To the maximum extent permitted by applicable law, the author of this
module disclaims all warranties, either express or implied, including
but not limited to implied warranties of merchantability and fitness
for a particular purpose, with regard to the software and the
accompanying documentation.
=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -