AI-NaiveBayes1
view release on metacpan or search on metacpan
Revision history for Perl extension AI::NaiveBayes1.
2.012 Sat May 29 11:00:47 ADT 2021
- Removing non-ASCII character in "Bohmer" last name to avoid testing
issues, "Daniel B\"ohmer" name changed to "Daniel Bohmer" for
documentation purposes
2.011 Fri May 28 07:55:50 ADT 2021
- merged corrections from Daniel Bohmer
2.010 Tue May 25 18:30:47 ADT 2021
- fixing bugs reported by perltesters on some platforms.
It seems to be due to running tests in a parallel way, and
that using the same name of the temporary file is the problem.
2.009 Sun May 23 09:23:06 ADT 2021
- fixed bug with t/auxfunctions.pl not being located during
testing (hopefully fixed because it requires a different
environment to reproduce)
- made Changes reverse chronological
2.008 Fri May 21 10:56:33 ADT 2021
- GitHub release
- documentation improvements
2.007 Wed Jan 29 05:58:37 AST 2020
- documentation update
- fixed bug: import_from_YAML and import_from_YAML_file now
return bless-ed references (they did not return before)
2.006 Tue Mar 15 06:17:04 ADT 2011
- reduced precision in \d{4}.\d+ to \d{4}.\d{10} avoid testing
problems on different platforms
2.005 Tue Mar 8 06:35:43 AST 2011
- reduced precision in comparison to avoid testing problems on
different platforms (from 14 to 12 digits after decimal point)
2.004 Sat Mar 5 12:39:35 AST 2011
- added missing files to MANIFEST (t/7-3.out and t/7-5.out)
2.003 Fri Mar 4 06:43:31 AST 2011
- fixed more testing problems due to differences in precision
2.002 Wed Mar 2 06:29:06 AST 2011
- fixed some testing problems due to differences in precision
2.001 Mon Feb 28 11:18:22 AST 2011
- fixed some testing problems due to differences in precision
2.000 Tue Feb 22 09:58:04 AST 2011
- fixing lexical sorting of version numbers
1.10 Tue Feb 22 09:31:12 AST 2011
- fixed testing problems due to differences in precision
- fixed podchecker warning (some space)
- better test error reporting in 2.t
- added t/pod.t, thanks to Michael Stevens
1.9 Tue Aug 31 09:27:51 ADT 2010
- fixed testing problems due to differences in precision in t/2.t
1.8 Fri Aug 21 06:36:34 ADT 2009
- fixed a pod documentation error
1.7 Thu Aug 20 14:20:15 ADT 2009
- improvements in documentation
- added method add_csv_file
- added method drop_attributes
- removed real_attr and added attribute_type field
1.6 Wed Aug 19 09:09:57 ADT 2009
- improved an error message
- fixed some testing problems due to whitespace
- small improvement in generating documentation
1.5 Wed Jan 30 08:06:22 AST 2008
- fixed testing problems due to differences in the lowest
significant digit on different platforms
1.4 Fri Dec 14 11:42:16 AST 2007
- added test skipping if YAML module is not available
- removing deprecated "our" reserved word
1.3 Fri Dec 7 07:26:47 AST 2007
- added reading from a table format (add_table)
- fixed some warnings reported during testing
- added option "with counts" to print_model
- added smoothing option for attributes
1.2 Mon Mar 14 08:03:16 AST 2005
- addition to documentation
- fixing a minor bug and a warning
1.1 Tue Apr 20 08:07:33 ADT 2004
- added several more test
- implemented optional Gaussian distribution for numerical
attributes
1.0 Thu Sep 4 08:01:19 ADT 2003
- bug fix in testing (attributes and labels are sorted now in
print_model)
0.03 Wed Sep 3 08:25:43 ADT 2003
- bug fix
- import and export to/from string and file (using YAML)
0.02 Fri May 9 15:53:53 ADT 2003
- bug fix
0.01 Fri May 9 14:48:53 ADT 2003
Makefile.PL view on Meta::CPAN
WriteMakefile
( 'NAME' => $module,
'VERSION_FROM' => $name, # finds $VERSION
'dist' => { COMPRESS=>"gzip",
SUFFIX=>"gz",
#PREOP=>('starfish README; '.
# "cp -f README $dir-\$(VERSION); "
# ),
},
'clean' => {FILES => "tmp* testfiles/tmp1 AI *~ t/tmp1 t/*~ t/tmp6 t/tmp6-2"},
'PREREQ_PM' => { YAML => '0.0' },
'PL_FILES' => {},
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => $name, # retrieve abstract from module
# <? echo " AUTHOR => 'Vlado Keselj $VladoURL')" !>#+
AUTHOR => 'Vlado Keselj https://web.cs.dal.ca/~vlado')
#-
: ()
),
($ExtUtils::MakeMaker::VERSION ge '6.30_00'?
NaiveBayes1.pm view on Meta::CPAN
=head1 THANKS
I would like to thank Daniel Bohmer for documentation corrections,
Yung-chung Lin (cpan:xern) for the implementation of the Gaussian model
for continuous variables, and the following people for bug reports, support,
and comments (in no particular order):
Michael Stevens, Tom Dyson, Dan Von Kohorn, Craig Talbert,
Andrew Brian Clegg,
and CPAN-testers, including: Andreas Koenig, Alexandr Ciornii, jlatour,
Jost.Krieger, tvmaly, Matthew Musgrove, Michael Stevens, Nigel Horne,
Graham Crookham, David Cantrell (dcantrell).
=head1 AUTHOR
Copyright 2003-21 Vlado Keselj L<https://web.cs.dal.ca/~vlado>.
In 2004 Yung-chung Lin provided implementation of the Gaussian model for
continous variables.
This script is provided "as is" without expressed or implied warranty.
like the following:
pod2html NaiveBayes1.pm
INSTALLATION
To install this module type the following:
perl Makefile.PL
make
make test
make install
DEPENDENCIES
The methods for importing to and exporting from YAML format require
module YAML.
COPYRIGHT AND LICENCE
Copyright 2003-21 Vlado Keselj https://web.cs.dal.ca/~vlado
#!/usr/bin/perl
use Test::More tests => 4;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
$nb->add_instances(attributes=>{model=>'H',place=>'B'},label=>'repairs=Y',cases=>30);
$nb->add_instances(attributes=>{model=>'H',place=>'B'},label=>'repairs=N',cases=>10);
$nb->add_instances(attributes=>{model=>'H',place=>'N'},label=>'repairs=Y',cases=>18);
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/1-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/1-1.out');
#putfile('t/1-2.out', $nb->export_to_YAML());
#is($nb->export_to_YAML(), getfile('t/1-2.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 1.t" if $@;
$nb->export_to_YAML_file('t/tmp1');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp1');
$printedmodel = &shorterdecimals($nb1->print_model);
#is("Model:\n" . $printedmodel, getfile('t/1-1.out'));
&compare_by_line("Model:\n" . $printedmodel, 't/1-1.out');
my $tmp = $nb->export_to_YAML();
#!/usr/bin/perl
use Test::More tests => 6;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
# @relation spam-b
#
# @attribute morning {Y,N}
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/2-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/2-1.out', __FILE__ , __LINE__);
#putfile('t/2-2.out', $nb->export_to_YAML());
#is($nb->export_to_YAML(), getfile('t/2-2.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 2.t" if $@;
$nb->export_to_YAML_file('t/tmp2');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp2');
$printedmodel = "Model:\n" . $nb1->print_model;
$printedmodel = &shorterdecimals($printedmodel);
&compare_by_line($printedmodel, 't/2-1.out', __FILE__ , __LINE__);
#is("Model:\n" . $nb1->print_model, getfile('t/2-1.out'));
my $tmp = $nb->export_to_YAML();
my $nb2 = AI::NaiveBayes1->import_from_YAML($tmp);
#!/usr/bin/perl
use Test::More tests => 6;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
# Example from Witten I. and Frank E. book "Data Mining" (the WEKA
# book), page82
#
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/3-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/3-1.out', __FILE__, __LINE__);
#putfile('t/3-2.out', $nb->export_to_YAML());
#is($nb->export_to_YAML(), getfile('t/3-2.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 3.t" if $@;
$nb->export_to_YAML_file('t/tmp3');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp3');
&compare_by_line("Model:\n" . &shorterdecimals($nb1->print_model),
't/3-1.out', __FILE__, __LINE__);
my $tmp = $nb->export_to_YAML();
my $nb2 = AI::NaiveBayes1->import_from_YAML($tmp);
&compare_by_line("Model:\n" . &shorterdecimals($nb2->print_model),
't/3-1.out', __FILE__, __LINE__);
#!/usr/bin/perl
use Test::More tests => 6;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
# Example from Witten I. and Frank E. book "Data Mining" (the WEKA
# book), page 86
#
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/4-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/4-1.out', __FILE__, __LINE__);
#putfile('t/4-2.out', $nb->export_to_YAML());
#is($nb->export_to_YAML(), getfile('t/4-2.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 4.t" if $@;
$nb->export_to_YAML_file('t/tmp4');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp4');
&compare_by_line("Model:\n" . &shorterdecimals($nb1->print_model),
't/4-1.out', __FILE__, __LINE__);
my $tmp = $nb->export_to_YAML();
my $nb2 = AI::NaiveBayes1->import_from_YAML($tmp);
&compare_by_line("Model:\n" . &shorterdecimals($nb2->print_model),
't/4-1.out', __FILE__, __LINE__);
#!/usr/bin/perl
use Test::More tests => 6;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
# @relation spam
#
# @attribute morning {Y,N}
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/5-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/5-1.out', __FILE__, __LINE__);
#putfile('t/5-2.out', $nb->export_to_YAML());
#is($nb->export_to_YAML(), getfile('t/5-2.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 5.t" if $@;
$nb->export_to_YAML_file('t/tmp5');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp5');
&compare_by_line("Model:\n" . &shorterdecimals($nb1->print_model),
't/5-1.out', __FILE__, __LINE__);
my $tmp = $nb->export_to_YAML();
my $nb2 = AI::NaiveBayes1->import_from_YAML($tmp);
&compare_by_line("Model:\n" . &shorterdecimals($nb2->print_model),
't/5-1.out', __FILE__, __LINE__);
#!/usr/bin/perl
use Test::More tests => 9;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
$nb->add_instances(attributes=>{C=>'Y',F=>2},label=>'S=Y',cases=>30);
$nb->add_instances(attributes=>{C=>'Y',F=>2},label=>'S=N',cases=>10);
$nb->add_instances(attributes=>{C=>'Y',F=>0},label=>'S=Y',cases=>18);
$nb->train;
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/6-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/6-1.out', __FILE__, __LINE__);
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 6.t" if $@;
$nb->export_to_YAML_file('t/tmp6');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp6');
&compare_by_line("Model:\n" . &shorterdecimals($nb1->print_model),
't/6-1.out', __FILE__, __LINE__);
my $p = $nb->predict(attributes=>{C=>'Y',F=>0});
#putfile('t/6-2.out', YAML::Dump($p));
ok(abs($p->{'S=N'} - 0.580) < 0.001);
#!/usr/bin/perl
use Test::More tests => 12;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb = AI::NaiveBayes1->new;
$nb->add_instances(attributes=>{C=>'Y',F=>2},label=>'S=Y',cases=>30);
$nb->add_instances(attributes=>{C=>'Y',F=>2},label=>'S=N',cases=>10);
$nb->add_instances(attributes=>{C=>'Y',F=>0},label=>'S=Y',cases=>18);
$nb->train;
my $printedmodel = "Model:\n" . $nb->print_model;
$printedmodel = &shorterdecimals($printedmodel);
#putfile('t/7-1.out', $printedmodel);
&compare_by_line($printedmodel, 't/7-1.out', __FILE__, __LINE__);
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 7.t" if $@;
$nb->export_to_YAML_file('t/tmp7');
my $nb1 = AI::NaiveBayes1->import_from_YAML_file('t/tmp7');
&compare_by_line("Model:\n" . &shorterdecimals($nb1->print_model),
't/7-1.out', __FILE__, __LINE__);
my $p = $nb->predict(attributes=>{C=>'Y',F=>0});
#putfile('t/7-2.out', YAML::Dump($p));
ok(abs($p->{'S=N'} - 0.580) < 0.001);
#!/usr/bin/perl
use Test::More tests => 2;
use_ok("AI::NaiveBayes1");
use lib '.';
require 't/auxfunctions.pl';
my $nb;
$nb = AI::NaiveBayes1->new;
$nb->add_table(
" Tp Wp W Wf T count
-------------------------------
$nb->{smoothing}{W} = 'unseen count=0.5';
$nb->{smoothing}{Wp} = 'unseen count=0.5';
$nb->{smoothing}{Wf} = 'unseen count=0.5';
$nb->train;
my $printedmodel = "Model:\n" . $nb->print_model('with counts');
putfile('t/8-1.out', $printedmodel);
is($printedmodel, getfile('t/8-1.out'));
eval "require YAML;";
plan skip_all => "YAML module required for the remaining tests in 8.t" if $@;
use YAML;
# duck ducks fly flies
my $p = $nb->predict(attributes=>{Tp=>'PREV',Wp=>'PREV',W=>'duck',Wf=>'ducks'});
putfile('t/8-2.out', YAML::Dump($p));
#ok(abs($p->{'S=N'} - 0.580) < 0.001);
#ok(abs($p->{'S=Y'} - 0.420) < 0.001);
my $ptotal = $p->{'T=N'};
$p = $nb->predict(attributes=>{Tp=>'N',Wp=>'duck',W=>'ducks',Wf=>'fly'});
t/auxfunctions.pl view on Meta::CPAN
#!/usr/bin/perl
sub compare_by_line {
my $got = shift;
my $file = shift;
my $testfile = @_ ? shift @_ : '';
my $testline = @_ ? shift @_ : '';
my $expected = getfile($file);
if ($got eq $expected) { pass; return }
my $flag = '';
while ($got ne '' or $expected ne '') {
my $a=$got; if ($a =~ /\s*\n/) { $a = $`; $got = $'; }
my $b=$expected; if ($b =~ /\s*\n/) { $b = $`; $expected = $'; }
if ($a ne $b) {
if ($flag eq '')
{ print STDERR "\n$testfile:$testline: Failed comparison with $file!\n"; $flag = 1; }
print STDERR " Got: $a\n".
"Expected: $b\n";
}
}
if ($flag eq '') { pass } else { fail }
}
sub shorterdecimals {
local $_ = shift;
s/(\d{4}\.\d{10})\d+/$1/g;
#!/usr/bin/perl -w
use strict;
use Test::More;
eval "use Test::Pod 1.00";
plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
all_pod_files_ok();
( run in 0.621 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )