Giter Site home page Giter Site logo

raku-algorithm-libsvm's Introduction

Actions Status

NAME

Algorithm::LibSVM - A Raku bindings for libsvm

SYNOPSIS

EXAMPLE 1

use Algorithm::LibSVM;
use Algorithm::LibSVM::Parameter;
use Algorithm::LibSVM::Problem;
use Algorithm::LibSVM::Model;

my $libsvm = Algorithm::LibSVM.new;
my Algorithm::LibSVM::Parameter $parameter .= new(svm-type => C_SVC,
                                                  kernel-type => RBF);
# heart_scale is here: https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/heart_scale
my Algorithm::LibSVM::Problem $problem = Algorithm::LibSVM::Problem.from-file('heart_scale');
my @r = $libsvm.cross-validation($problem, $parameter, 10);
$libsvm.evaluate($problem.y, @r).say; # {acc => 81.1111111111111, mse => 0.755555555555556, scc => 1.01157627463546}

EXAMPLE 2

use Algorithm::LibSVM;
use Algorithm::LibSVM::Parameter;
use Algorithm::LibSVM::Problem;
use Algorithm::LibSVM::Model;

sub gen-train {
  my $max-x = 1;
  my $min-x = -1;
  my $max-y = 1;
  my $min-y = -1;
  my @x;
  my @y;
  do for ^300 {
      my $x = $min-x + rand * ($max-x - $min-x);
      my $y = $min-y + rand * ($max-y - $min-y);

      my $label = do given $x, $y {
          when ($x - 0.5) ** 2 + ($y - 0.5) ** 2 <= 0.2 {
              1
          }
          when ($x - -0.5) ** 2 + ($y - -0.5) ** 2 <= 0.2 {
              -1
          }
          default { Nil }
      }
      if $label.defined {
          @y.push: $label;
          @x.push: [$x, $y];
      }
  }
  (@x, @y)
}

my (@train-x, @train-y) := gen-train;
my @test-x = 1 => 0.5e0, 2 => 0.5e0;
my $libsvm = Algorithm::LibSVM.new;
my Algorithm::LibSVM::Parameter $parameter .= new(svm-type => C_SVC,
                                                  kernel-type => LINEAR);
my Algorithm::LibSVM::Problem $problem = Algorithm::LibSVM::Problem.from-matrix(@train-x, @train-y);
my $model = $libsvm.train($problem, $parameter);
say $model.predict(features => @test-x)<label> # 1

DESCRIPTION

Algorithm::LibSVM is a Raku bindings for libsvm.

METHODS

cross-validation

Defined as:

method cross-validation(Algorithm::LibSVM::Problem $problem, Algorithm::LibSVM::Parameter $param, Int $nr-fold --> List)

Conducts $nr-fold-fold cross validation and returns predicted values.

train

Defined as:

method train(Algorithm::LibSVM::Problem $problem, Algorithm::LibSVM::Parameter $param --> Algorithm::LibSVM::Model)

Trains a SVM model.

  • $problem The instance of Algorithm::LibSVM::Problem.

  • $param The instance of Algorithm::LibSVM::Parameter.

DEPRECATED load-problem

Defined as:

multi method load-problem(\lines --> Algorithm::LibSVM::Problem)
multi method load-problem(Str $filename --> Algorithm::LibSVM::Problem)

Loads libsvm-format data.

load-model

Defined as:

method load-model(Str $filename --> Algorithm::LibSVM::Model)

Loads libsvm model.

evaluate

Defined as:

method evaluate(@true-values, @predicted-values --> Hash)

Evaluates the performance of the three metrics (i.e. accuracy, mean squared error and squared correlation coefficient)

  • @true-values The array that contains ground-truth values.

  • @predicted-values The array that contains predicted values.

nr-feature

Defined as:

method nr-feature(--> Int:D)

Returns the maximum index of all the features.

ROUTINES

parse-libsvmformat

Defined as:

sub parse-libsvmformat(Str $text --> List) is export

Is a helper routine for handling libsvm-format text.

CAUTION

DON'T USE PRECOMPUTED KERNEL

As a workaround for RT130187, I applied the patch programs (e.g. src/3.22/svm.cpp.patch) for the sake of disabling random access of the problematic array.

Sadly to say, those patches drastically increase the complexity of using PRECOMPUTED kernel.

SEE ALSO

AUTHOR

titsuki [email protected]

COPYRIGHT AND LICENSE

Copyright 2016 titsuki

This library is free software; you can redistribute it and/or modify it under the terms of the MIT License.

libsvm ( https://github.com/cjlin1/libsvm ) by Chih-Chung Chang and Chih-Jen Lin is licensed under the BSD 3-Clause License.

raku-algorithm-libsvm's People

Contributors

samcv avatar titsuki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

samcv jamesalbert

raku-algorithm-libsvm's Issues

Algorithm::LibSVM.train with the probability option doesn't seem to work correctly

  • parameters:
C_SVC
LINEAR
probability => True
  • train.txt:
1 1:0.25 2:0.20
1 1:0.25 2:0.25
1 1:0.20 2:0.20
1 1:0.20 2:0.25
2 1:0.25 2:-0.20
2 1:0.25 2:-0.25
2 1:0.20 2:-0.20
2 1:0.20 2:-0.25
3 1:-0.25 2:-0.20
3 1:-0.25 2:-0.25
3 1:-0.20 2:-0.20
3 1:-0.20 2:-0.25
4 1:-0.25 2:0.20
4 1:-0.25 2:0.25
4 1:-0.20 2:0.20
4 1:-0.20 2:0.25
  • model diff between libsvm script and this bindings under the same parameters:
< probA -1.90899 -3.00429 -1.90899 -1.77258 -2.58883 2.87862
< probB -1.63715e-16 -7.31008e-17 -4.85579e-18 -0.00591705 -0.374202 0.217818
---
> probA -1.77279 4.48214 2.88179 -1.77234 -2.51077 1.84732
> probB -0.00590905 -0.545341 -0.216282 0.00590746 0.314892 -1.27049e-09

"Cannot resolve caller head(Any:U:) error" rarely occurs

t/04-oneclass.t .. Cannot resolve caller head(Any:U: ); none of these signatures match:
    (Any:D: *%_)
    (Any:D: Callable:D $w, *%_)
    (Any:D: $n, *%_)
  in method _load-problem at /home/itoyota/Programs/raku-Algorithm-LibSVM/lib/Algorithm/LibSVM.pm6 (Algorithm::LibSVM) line 67
  in method load-problem at /home/itoyota/Programs/raku-Algorithm-LibSVM/lib/Algorithm/LibSVM.pm6 (Algorithm::LibSVM) line 53
  in block <unit> at t/04-oneclass.t line 41

t/04-oneclass.t .. Dubious, test returned 1 (wstat 256, 0x100)
No subtests run 
t/05-epssvr.t .... ok   
t/06-nusvr.t ..... ok   
t/07-parse.t ..... ok  
$ perl6 --version
This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.

Patch fails on MacOSX environment

See: https://github.com/titsuki/raku-Algorithm-LibSVM/runs/6229978601?check_suite_focus=true

Something goes wrong on MacOSX:

Aborting due to build failure: Algorithm::LibSVM:ver<0.0.15> (use --force-build to override)
[Algorithm::LibSVM] The spawned command 'patch 3.25/svm.h 3.25/svm.h.patch -o svm.h' exited unsuccessfully (exit code: 1, signal: 0)
[Algorithm::LibSVM]   in method build at /Users/runner/work/raku-Algorithm-LibSVM/raku-Algorithm-LibSVM/lib/Algorithm/LibSVM/CustomBuilder.pm[6](https://github.com/titsuki/raku-Algorithm-LibSVM/runs/6229827657?check_suite_focus=true#step:6:6) (Algorithm::LibSVM::CustomBuilder) line 1[8](https://github.com/titsuki/raku-Algorithm-LibSVM/runs/6229827657?check_suite_focus=true#step:6:8)
[Algorithm::LibSVM]   in block <unit> at -e line 1
===> Building [FAIL]: Algorithm::LibSVM:ver<0.0.15>

Make load-problem more intuitively

Current implementation requires array of Str (i.e., libsvm formatted lines) as an input but it's time consuming and stressful.
This method should allow naive raku data structures like a simple array of Num.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.