AI-ML

 view release on metacpan or  search on metacpan

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

        		$reg = ($lambda / (2 * $m)) * sum( $thetas->slice(x0 => 1) ** 2 );
        		$cost = (-1 / $m) * sum($y * log($h) + (1 - $y) * log(1-$h)) + $reg;

        		push @cost_values, $cost->get_element(0,0) if exists $self->{cost};

        		$reg_thetas = ($lambda / $m) * $thetas;
        		$reg_thetas->set_element(0,0,0);

        		$grad = ($x->T x ($h - $y)) / $m;

        		$thetas = $thetas - $alpha * ( $grad + $reg_thetas );
      	}
    }
    else {
      	for my $i (1 .. $iters) {
        	$h = sigmoid($x x $thetas);
        	$cost = (-1 / $m)*sum(($y * log($h)) + ((1-$y) * log(1-$h)));

        	push @cost_values, $cost->get_element(0,0) if exists $self->{cost};

        	$grad = ($x->T x ($h - $y)) / $m;
        	$thetas = $thetas - $alpha * $grad;
      	}
    }
		AI::ML::Expr::plot_cost($self->{cost}, @cost_values) if exists $self->{cost};
		$self->{thetas} = $thetas;
}


=head2 classification

=cut
sub classification {
    my ($self, $x) = @_;
    $x = (M->ones($x->rows,1))->append($x);
    $self->{classification} = sigmoid($x x $self->{thetas});
}


=head2 prediction

=cut
sub prediction {
    my ($self, $x, %opts) = @_;
    $x = Math::Lapack::Matrix::concatenate(
        M->ones($x->rows,1),
        $x
    );
    my $h = sigmoid($x x $self->{thetas});
    $self->{yatt} =  AI::ML::Expr::prediction($h, %opts);
}



=head2 accuracy

=cut
sub accuracy {
		my ($self, $y) = @_;
		unless( exists $self->{yatt} ) {
				print STDERR "You should first predict the values!\n";
				exit;
		}
		return AI::ML::Expr::accuracy($y, $self->{yatt});
}


=head2 precision 

=cut
sub precision {
		my ($self, $y) = @_;
		unless( exists $self->{yatt} ) {
				print STDERR "You should first predict the values!\n";
				exit;
		}
		return AI::ML::Expr::precision($y, $self->{yatt});
}


=head2 recall 

=cut
sub recall {
		my ($self, $y) = @_;
		unless( exists $self->{yatt} ) {
				print STDERR "You should first predict the values!\n";
				exit;
		}
		return AI::ML::Expr::recall($y, $self->{yatt});
}



=head2 f1

=cut
sub f1 {
		my ($self, $y) = @_;
		unless( exists $self->{yatt} ) {
				print STDERR "You should first predict the values!\n";
				exit;
		}
		return AI::ML::Expr::f1($y, $self->{yatt});
}

1;



( run in 0.608 second using v1.01-cache-2.11-cpan-39bf76dae61 )