Algorithm-Classifier-IsolationForest

 view release on metacpan or  search on metacpan

benchmarking/bench-sklearn-scoring.pl  view on Meta::CPAN

# timing starts.
#
# Method correspondence:
#   Perl score_samples         <-->  clf.score_samples(X)      (same formula, opposite sign)
#   Perl predict               <-->  clf.predict(X)            (same semantics, 0/1 vs -1/+1)
#   Perl score_predict_samples <-->  (no sklearn equivalent)
#   Perl score_predict_split   <-->  (no sklearn equivalent)
#   Perl path_lengths          <-->  (no sklearn equivalent)
#   (no Perl equivalent)       <-->  clf.decision_function(X)  (threshold-shifted score)
#
# The ratio column shows sklearn ops/s / best-Perl ops/s.
# >1 means sklearn is faster; <1 means Perl is faster.
#
# Unavailable backends (Inline::C not installed, OpenMP not linked,
# scikit-learn not installed) are omitted from the table.
#
# Run with:
#   perl -Ilib benchmarking/bench-sklearn-scoring.pl

use strict;
use warnings;

benchmarking/bench-sklearn-scoring.pl  view on Meta::CPAN

			|| ( $c_key   && $HAS_C )
			|| ( $omp_key && $HAS_OPENMP )
			|| ( $sk_key  && $sk );

		my $pure_rate = $pure_key                   ? $pure_pl{$key}{$pure_key} : undef;
		my $c_rate    = ( $c_key && $HAS_C )        ? $c_pl{$key}{$c_key}       : undef;
		my $omp_rate  = ( $omp_key && $HAS_OPENMP ) ? $omp_pl{$key}{$omp_key}   : undef;
		my $sk_rate   = ( $sk_key && $sk )          ? $sk->{$key}{$sk_key}      : undef;

		# Best available Perl rate for the ratio denominator
		my $best = $omp_rate // $c_rate // $pure_rate;

		my @cols = ( sprintf( "  %-*s", $MW, $label ) );
		push @cols, sprintf( "  %*s", $NW, fmt_rate($pure_rate) );
		push @cols, sprintf( "  %*s", $NW, fmt_rate($c_rate) )   if $HAS_C;
		push @cols, sprintf( "  %*s", $NW, fmt_rate($omp_rate) ) if $HAS_OPENMP;

		if ( defined $sk ) {
			push @cols, sprintf( "  %*s", $SW, fmt_rate($sk_rate) );
			my $ratio = ( $best && $sk_rate ) ? sprintf( '%.2f', $sk_rate / $best ) : '--';
			push @cols, sprintf( "  %*s", $RW, $ratio );
		}

		print join( '', @cols ), "\n";
	} ## end for my $row (@rows)
	print "\n";
} ## end sub print_point

# -----------------------------------------------------------------------
# Banner

benchmarking/bench-sklearn-scoring.pl  view on Meta::CPAN

	+ ( $HAS_OPENMP ? 2 + $NW           : 0 )
	+ ( defined $sk ? 2 + $SW + 2 + $RW : 0 );

my $backends = 'pure-Perl' . ( $HAS_C ? ', C serial' : '' ) . ( $HAS_OPENMP ? ', C+OpenMP' : '' );

print '=' x $TW, "\n";
printf " Perl (%s) vs scikit-learn -- scoring speed (ops/s)\n", $backends;
print '=' x $TW, "\n";
printf " Training: %d samples, n_trees=%d, sample_size=%d\n", $N_TRAIN, $N_TREES, $PSI;
printf " Each measurement: %.0fs wall-clock with warmup\n", $BENCH_SECS;
print " ratio = sklearn ops/s / best-Perl ops/s  (>1 = sklearn faster)\n";
print " --  = no equivalent or backend not available\n";
print " packed = pre-packed input (skips per-call AoA walk; C backend only)\n";
print "\n";

unless ( defined $sk ) {
	print " (scikit-learn not available; showing Perl results only)\n\n";
}

# ---- Query-size sweep -----------------------------------------------
print '#' x $TW, "\n";

benchmarking/bench-voting.pl  view on Meta::CPAN

#      (the paper's "stop at majority"), whereas mean aggregation always
#      walks every tree.  score_samples() has no such early exit (the
#      vote fraction needs the full count), so it is shown as a contrast.
#      Timed across n_trees, query-set size, and feature count, under the
#      default backend (C + OpenMP when available).
#
#      How much predict() saves depends on the data: early exit triggers
#      sooner when points are clearly inliers or clearly outliers, later
#      when they sit near the decision boundary.  The gaussian-cluster +
#      planted-outlier data here is fairly separable, so this is closer
#      to a best case than to a worst case.
#
#      The speed timings run on the SERIAL C backend (use_openmp => 0).
#      The majority-vote win is algorithmic -- it walks fewer trees per
#      point -- and forcing serial isolates that from OpenMP scheduling:
#      early exit makes different points finish after different numbers
#      of trees, so an OpenMP `parallel for` sees uneven per-point work
#      and its load-imbalance jitter would otherwise swamp the effect
#      being measured (wall_cmpthese times a single window, unlike
#      wall_rate's windowed median -- see BenchAccel).  The fewer-walks
#      saving carries over to the OpenMP path; it is just far noisier to

examples/contamination-threshold.pl  view on Meta::CPAN

printf "Flagged with a fixed 0.5 threshold : %d / %d points (%.1f%%)\n", $n_fixed, scalar @data, 100 * $n_fixed / @data;
printf "\n(true anomaly rate baked into the data: %.1f%%)\n",            100 * 20 / @data;

print <<'NOTE';

Takeaways:
  * decision_threshold() exposes whatever fit() learned (undef if you never set
    contamination).
  * predict() with no threshold uses that learned cutoff; pass an explicit
    threshold to override it for a single call.
  * Set contamination to your best guess of the anomaly fraction -- it doesn't
    have to be exact, it just calibrates where the cutoff lands.
NOTE



( run in 1.079 second using v1.01-cache-2.11-cpan-9581c071862 )