Algorithm-Classifier-IsolationForest
view release on metacpan or search on metacpan
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
t/81-sklearn-real-data.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use FindBin ();
use File::Spec;
use List::Util qw(sum);
use Algorithm::Classifier::IsolationForest;
my $DATA = File::Spec->catdir( $FindBin::Bin, 'data' );
my @SETS = qw(glass ionosphere seeds wdbc);
my @SEEDS = ( 1, 7, 42 );
# An Isolation Forest is a random estimator, so two fits of the SAME
# implementation with different seeds already disagree. That seed-to-seed
# spread is the ceiling on any cross-implementation comparison, and it is
# what these checks are stated against.
#
t/81-sklearn-real-data.t view on Meta::CPAN
my @out = `$PYTHON $tmp $csv 2>/dev/null`;
return undef if $? != 0 || !@out;
chomp @out;
return \@out;
} ## end sub live_sklearn
# -----------------------------------------------------------------------
# The datasets
# -----------------------------------------------------------------------
for my $name (@SETS) {
my $csv = File::Spec->catfile( $DATA, "$name.csv" );
my $ref = File::Spec->catfile( $DATA, "$name.sklearn" );
my $lab = File::Spec->catfile( $DATA, "$name.labels" );
unless ( -r $csv && -r $ref && -r $lab ) {
fail("$name: fixture files are missing from t/data");
next;
}
my ( $rows, $names ) = load_csv($csv);
my $sk = load_column($ref);
my $labels = load_column($lab);
t/81-sklearn-real-data.t view on Meta::CPAN
#
# The statistic is the rare class's mean normalised rank, where 0.5 is
# chance and 1.0 would put all nine at the very top. A top-k lift was the
# obvious first choice and is a bad one: with only nine rare samples it is
# quantised to a couple of attainable values -- over a 60-seed sweep it
# returned exactly 1.13x or 2.26x and nothing between, turning on whether
# one specific sample cleared the cut. sklearn scores 2.26x on the same
# data for the same reason, not because it is better. The mean rank moves
# continuously and stays in 0.637-0.725 across those same 60 seeds.
{
my ( $rows, undef ) = load_csv( File::Spec->catfile( $DATA, 'glass.csv' ) );
my $labels = load_column( File::Spec->catfile( $DATA, 'glass.labels' ) );
# Mean normalised rank of the labelled rows: 0 = least anomalous of the
# set, 1 = most.
my $mean_rank = sub {
my ($scores) = @_;
my $n = scalar @$scores;
my @asc = sort { $scores->[$a] <=> $scores->[$b] } 0 .. $n - 1;
my @pct;
$pct[ $asc[$_] ] = $_ / ( $n - 1 ) for 0 .. $n - 1;
my @rare = grep { $labels->[$_] } 0 .. $n - 1;
return sum( @pct[@rare] ) / scalar @rare;
};
my $sk_rank = $mean_rank->( [ map { -$_ } @{ load_column( File::Spec->catfile( $DATA, 'glass.sklearn' ) ) } ] );
for my $seed (@SEEDS) {
my $model = Algorithm::Classifier::IsolationForest->new(
n_trees => 100,
sample_size => 256,
seed => $seed,
);
$model->fit($rows);
my $ours = $mean_rank->( $model->score_samples($rows) );
( run in 2.256 seconds using v1.01-cache-2.11-cpan-007c89162af )