Algorithm-FeatureSelection
view release on metacpan or search on metacpan
lib/Algorithm/FeatureSelection.pm view on Meta::CPAN
package Algorithm::FeatureSelection;
use strict;
use warnings;
use List::Util qw(sum);
our $VERSION = '0.02';
sub new {
my $class = shift;
my $self = bless {@_}, $class;
return $self;
}
sub pmi {
my $self = shift;
$self->pairewise_mutual_information(@_);
}
sub ig {
my $self = shift;
$self->information_gain(@_);
}
sub igr {
my $self = shift;
$self->information_gain_ratio(@_);
}
sub pairwise_mutual_information {
my $self = shift;
my $features = shift;
## -----------------------------------------------------------------
##
## The argument is expected as below.
##
## $features = {
## feature_1 => {
## class_a => 10,
## class_b => 2,
## },
## feature_2 => {
## class_b => 11,
## class_d => 32
## },
## .
## .
## .
## };
##
## -----------------------------------------------------------------
##
## Pairewise Mutual Information
##
## PMI(w, c) = log ( P( Xw = 1, C = c ) / P( Xw=1 )P( C=c ) )
##
## c.f. w = feature
## c = Class
##
## -----------------------------------------------------------------
my $feature_count;
my $class_count;
my $co_occur_count;
my $all_features_num;
while ( my ( $feature, $ref ) = each %$features ) {
while ( my ( $class, $count ) = each %$ref ) {
$feature_count->{$feature} += $count;
$class_count->{$class} += $count;
$co_occur_count->{ $class . "\t" . $feature } += $count;
( run in 0.467 second using v1.01-cache-2.11-cpan-5b529ec07f3 )