AI-ML

 view release on metacpan or  search on metacpan

lib/AI/ML/Expr.pm  view on Meta::CPAN

sub plot_cost{
    my ($file, @costs) = @_;
    my @iters = (1 .. scalar(@costs));

    my $chart = Chart::Gnuplot->new(
            output     => $file,
            title     => "Cost",
            xlabel  => "Iter",
            ylabel     => "Cost"
    );
    $chart->png;
    my $data = Chart::Gnuplot::DataSet->new(
            xdata     => \@iters,
            ydata     => \@costs,
            style     => "linespoints"
    );
    $chart->plot2d($data);

}

1;

t/04-linear-regression.t  view on Meta::CPAN

use Test2::V0;

use Math::Lapack::Matrix;
use AI::ML::LinearRegression;


my $x = Math::Lapack::Matrix->new([[12.39999962],[14.30000019],[14.5],[14.89999962],[16.10000038],[16.89999962],[16.5],[15.39999962],[17],[17.89999962],[18.79999924],[20.29999924],[22.39999962],[19.39999962],[15.5],[16.70000076],[17.29999924],[18.399...

my $y = Math::Lapack::Matrix->new([[11.19999981],[12.5],[12.69999981],[13.10000038],[14.10000038],[14.80000019],[14.39999962],[13.39999962],[14.89999962],[15.60000038],[16.39999962],[17.70000076],[19.60000038],[16.89999962],[14],[14.60000038],[15.100...

my $m = AI::ML::LinearRegression->new(plot => "../../plot.png");

$m->train($x, $y);

my $t = $m->{thetas};

is($t->rows, 2, "Right number of rows");
is($t->columns, 1, "Right number of columns");
_float($t->get_element(0,0), 0.43458449, "Normal Equation - Right value of theta 0,0");
_float($t->get_element(1,0), 0.85114404, "Normal Equation - Right value of theta 1,0");

my $m1 = AI::ML::LinearRegression->new(
    cost     => "../../cost1.png",
    gradient => "foo",
    plot     => "../../plot1.png",
    n        => 50,
    alpha    => 0.001
);


$m1->train($x, $y);
is($m1->{thetas}->rows, 2, "Right number of rows");
is($m1->{thetas}->columns, 1, "Right number of columns");
_float($m1->{thetas}->get_element(0,0), 0.86412871, "Normal Equation - Right value of theta 0,0");
_float($m1->{thetas}->get_element(1,0), 0.8269897, "Normal Equation - Right value of theta 1,0");


my $n = AI::ML::LinearRegression->new(
                                             lambda   => 1,
                                             cost     => "../../cost2.png",
                                             gradient => "foo",
                                             plot     => "../../plot2.png",
                                             n        => 50,
                                             alpha    => 0.001);
$n->train($x, $y);
is($n->{thetas}->rows, 2, "Right number of rows");
is($n->{thetas}->columns, 1, "Right number of columns");

_float($n->{thetas}->get_element(0,0), 0.78473628, "Normal Equation - Right value of theta 0,0");
_float($n->{thetas}->get_element(1,0), 0.83133813, "Normal Equation - Right value of theta 1,0");
### FIXME: if the tests generate files, you should test them.
##         and delete them afterwads

t/05-logistic-regression.t  view on Meta::CPAN

is($x->get_element(2,2),0, "Right element 2,2 of x");
is($y->get_element(3,0),1, "Right element 3,0 of y");
is($y->get_element(7,0),2, "Right element 7,0 of y");

$x->norm_std_deviation();
$y = $y - 1;

my $m = AI::ML::LogisticRegression->new(
				n 		=> 10000,
				alpha => 0.5,
				cost 	=> "../../logistcost.png"
);


$m->train($x, $y);
my $thetas = $m->{thetas};
_float($thetas->get_element(0,0), -1.07661735,"Right vale of theta 0,0");
_float($thetas->get_element(1,0), 0.21463009,"Right vale of theta 1,0");
_float($thetas->get_element(2,0), -0.03173973,"Right vale of theta 2,0");
_float($thetas->get_element(3,0), 0.63483062,"Right vale of theta 3,0");

t/05-logistic-regression.t  view on Meta::CPAN








my $n = AI::ML::LogisticRegression->new(
				n 		=> 10000,
				alpha => 0.5,
				cost 	=> "../../logistcost_reg.png",
				reg 	=> 2
);


$n->train($x, $y);
$thetas = $n->{thetas};
_float($thetas->get_element(0,0), -1.07368839, "Right vale of theta 0,0");
_float($thetas->get_element(1,0), 0.204271, "Right vale of theta 1,0");
_float($thetas->get_element(2,0), -0.02933972, "Right vale of theta 2,0");
_float($thetas->get_element(3,0), 0.60950995, "Right vale of theta 3,0");



( run in 0.725 second using v1.01-cache-2.11-cpan-df04353d9ac )