| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # MLPerl, K Nearest Neighbors 2D, Demo Driver | ||
| 4 | # Load training points, find K nearest neighbors to classify test points | ||
| 5 | |||
| 6 | use RPerl; use strict; use warnings; | ||
| 7 | our $VERSION = 0.007_000; | ||
| 8 | |||
| 9 | |||
| 10 | use MLPerl::PythonShims qw(concatenate for_range); | ||
| 11 | use MLPerl::Classifier::KNeighbors; | ||
| 12 | use Time::HiRes qw(time); | ||
| 13 | |||
| 14 | # [ BEGIN INPUT DATA SIZES ] | ||
| 15 | my string_arrayref $data_sizes_all = [ | ||
| 16 | # '25 25 50', | ||
| 17 | # '50 50 100', | ||
| 18 | # '125 125 250', | ||
| 19 | '250 250 500', | ||
| 20 | # '500 500 1000', | ||
| 21 | # '1250 1250 2500', | ||
| 22 | # '2500 2500 5000', | ||
| 23 | # '5000 5000 10000', | ||
| 24 | # '12500 12500 25000', | ||
| 25 | # '25000 25000 50000', | ||
| 26 | # '50000 50000 100000' | ||
| 27 | ]; | ||
| 28 | foreach my string $data_sizes (@{$data_sizes_all}) { | ||
| 29 | my string $data_sizes_underscores = $data_sizes; | ||
| 30 | $data_sizes_underscores =~ s/\ /_/g; | ||
| 31 | my string $file_name = 'script/demo/k_nearest_neighbors_2D_data_' . ($data_sizes_underscores) . '.pl'; | ||
| 32 | |||
| 33 | # read external data | ||
| 34 | (open my filehandleref $FILE_HANDLE, '<', $file_name) and (print 'Opened file ' . q{'} . $file_name . q{'} . ' for reading' . "\n") | ||
| 35 | or (die 'ERROR EMLKNN2D10: Cannot open file ' . q{'} . $file_name . q{'} . ' for reading, ' . $OS_ERROR . ', dying' . "\n"); | ||
| 36 | read $FILE_HANDLE, my string $file_lines, -s $FILE_HANDLE; | ||
| 37 | close $FILE_HANDLE | ||
| 38 | or die 'ERROR EMLKNN2D11: Cannot close file ' . q{'} . $file_name . q{'} . ' after reading, ' . $OS_ERROR . ', dying' . "\n"; | ||
| 39 | |||
| 40 | # initialize local variables to hold external data | ||
| 41 | my number_arrayref_arrayref $train_data_A = undef; | ||
| 42 | my number_arrayref_arrayref $train_data_B = undef; | ||
| 43 | my number_arrayref_arrayref $test_data = undef; | ||
| 44 | |||
| 45 | # load external data | ||
| 46 | eval($file_lines); | ||
| 47 | |||
| 48 | # format train data, concatenate all train data arrays | ||
| 49 | my number_arrayref_arrayref $train_data = concatenate($train_data_A, $train_data_B); | ||
| 50 | |||
| 51 | # generate train data classifications | ||
| 52 | my string_arrayref $train_classifications = concatenate(for_range('0', (scalar @{$train_data_A})), for_range('1', (scalar @{$train_data_B}))); | ||
| 53 | |||
| 54 | # create KNN classifier | ||
| 55 | my integer $k = 3; | ||
| 56 | my object $knn = MLPerl::Classifier::KNeighbors->new(); | ||
| 57 | $knn->set_n_jobs(1); $knn->set_algorithm('brute'); $knn->set_n_neighbors($k); $knn->set_metric('euclidean'); | ||
| 58 | |||
| 59 | # fit KNN classifier to training data | ||
| 60 | $knn->fit($train_data, $train_classifications); | ||
| 61 | |||
| 62 | # [ BEGIN TIMING REPETITIONS ] | ||
| 63 | my integer_arrayref $timing_repetitions_all = [ 1, 5, 10, 25, 50, 100, 250, 500 ]; | ||
| 64 | foreach my integer $timing_repetitions (@{$timing_repetitions_all}) { | ||
| 65 | |||
| 66 | # generate and display KNN classifier's predictions | ||
| 67 | # run repeatedly for timing purposes | ||
| 68 | my number $time_start = time(); | ||
| 69 | my string_arrayref $tests_classifications; | ||
| 70 | for (my integer $i = 0; $i < $timing_repetitions; $i++) { | ||
| 71 | $tests_classifications = $knn->predict($test_data); } | ||
| 72 | my number $time_total = time() - $time_start; | ||
| 73 | print $timing_repetitions . q{ } . $data_sizes . q{ } . sprintf("%.3f", $time_total) . "\n"; | ||
| 74 | # foreach my string $test_classifications (@{$tests_classifications}) { print $test_classifications, "\n"; } | ||
| 75 | } # [ END TIMING REPETITIONS ] | ||
| 76 | |||
| 77 | print "\n"; | ||
| 78 | } # [ END INPUT DATA SIZES ] |