++ed by:
TOMMY YZHERNAND

2 PAUSE users
2 non-PAUSE users.

Author image William N. Braswell, Jr.

NAME

MLPerl

Machine Learning in Perl, Using the RPerl Optimizing Compiler

DESCRIPTION

MLPerl is a collection of algorithms and data structures used for building machine learning applications in Perl. For general info:

http://mlperl.org

http://rperl.org

http://perlcommunity.org

SYNOPSIS, KNN 2D BRUTE FORCE

Filename script/demo/k_nearest_neighbors_2D.pl:

    #!/usr/bin/env perl
    
    # MLPerl, K Nearest Neighbors 2D, Demo Driver
    # Load training points, find K nearest neighbors to classify test points

    use RPerl;  use strict;  use warnings;
    our $VERSION = 0.007_000;

    use MLPerl::PythonShims qw(concatenate for_range);
    use MLPerl::Classifier::KNeighbors;

    # read external data
    my string $file_name = $ARGV[0];
    open my filehandleref $FILE_HANDLE, '<', $file_name
        or die 'ERROR EMLKNN2D10: Cannot open file ' . q{'} . $file_name . q{'} . ' for reading, ' . $OS_ERROR . ', dying' . "\n";
    read $FILE_HANDLE, my string $file_lines, -s $FILE_HANDLE;
    close $FILE_HANDLE
        or die 'ERROR EMLKNN2D11: Cannot close file ' . q{'} . $file_name . q{'} . ' after reading, ' . $OS_ERROR . ', dying' . "\n";

    # initialize local variables to hold external data
    my number_arrayref_arrayref $train_data_A = undef;
    my number_arrayref_arrayref $train_data_B = undef;
    my number_arrayref_arrayref $test_data = undef;

    # load external data
    eval($file_lines);

    # format train data, concatenate all train data arrays
    my number_arrayref_arrayref $train_data = concatenate($train_data_A, $train_data_B);

    # generate train data classifications
    my string_arrayref $train_classifications = concatenate(for_range('0', (scalar @{$train_data_A})), for_range('1', (scalar @{$train_data_B})));

    # create KNN classifier
    my integer $k = 3;
    my object $knn = MLPerl::Classifier::KNeighbors->new();  $knn->set_n_neighbors($k);  $knn->set_metric('euclidean');

    # fit KNN classifier to training data
    $knn->fit($train_data, $train_classifications);

    # generate and display KNN classifier's predictions
    my string_arrayref $tests_classifications = $knn->predict($test_data);
    foreach my string $test_classifications (@{$tests_classifications}) { print $test_classifications, "\n"; }

SYNOPSIS, KNN 2D BRUTE FORCE, EXECUTE

    $ ./script/demo/k_nearest_neighbors_2D.pl ./script/demo/k_nearest_neighbors_2D_data_25_25_50.pl

SYNOPSIS, KNN 2D BRUTE FORCE, COMPILE & EXECUTE

    $ export RPERL_DEBUG=1 && export RPERL_VERBOSE=1
    $ rperl -V lib/MLPerl/Classifier/KNeighbors.pm
    $ ./script/demo/k_nearest_neighbors_2D.pl ./script/demo/k_nearest_neighbors_2D_data_25_25_50.pl

SEE ALSO

RPerl

rperl

AUTHOR

William N. Braswell, Jr.

mailto:wbraswell@NOSPAM.cpan.org