RandomizedTreesForBigData.pm untested

filter: Statementstmt 0.0% 0 / 464 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 629 CC 49 SCAR 49.2
CC
49
Cov
0%
get_training_data_for_N_trees_balanced
342.0
_digest_training_data_all_trees
210.0
get_training_data_for_N_trees
56.0
CRAP 137.2
1 package Algorithm::RandomizedTreesForBigData;
2  
3 #--------------------------------------------------------------------------------------
4 # Copyright (c) 2017 Avinash Kak. All rights reserved. This program is free
5 # software. You may modify and/or distribute it under the same terms as Perl itself.
6 # This copyright notice must remain attached to the file.
7 #
8 # Algorithm::RandomizedTreesForBigData is a Perl module for inducing multiple decision
9 # trees using randomized selection of samples from a large training data file.
10 # -------------------------------------------------------------------------------------
11  
12 #use lib 'blib/lib', 'blib/arch';
13  
14 #use 5.10.0;
15 use strict;
16 use warnings;
17 use Carp;
18 use List::Util qw(pairmap);
19 use Algorithm::DecisionTree 3.43;
20  
21 our $VERSION = '3.43';
22  
23 ############################################ Constructor ##############################################
24 sub new {
25 my ($class, %args) = @_;
26 my @params = keys %args;
27 my %dtargs = %args;
28 delete $dtargs{how_many_trees};
29 delete $dtargs{how_many_training_samples_per_tree} if exists $dtargs{how_many_training_samples_per_tree};
30 delete $dtargs{looking_for_needles_in_haystack} if exists $dtargs{looking_for_needles_in_haystack};
31 croak "\nYou have used a wrong name for a keyword argument --- perhaps a misspelling\n"
32 if check_for_illegal_params(@params) == 0;
33 bless {
34 _all_trees => {map {$_ => Algorithm::DecisionTree->new(%dtargs)} 0..$args{how_many_trees}-1},
35 _csv_cleanup_needed => $args{csv_cleanup_needed} || 0,
36 _looking_for_needles_in_haystack => $args{looking_for_needles_in_haystack},
37 _how_many_training_samples_per_tree => $args{how_many_training_samples_per_tree},
38 _training_datafile => $args{training_datafile},
39 _csv_class_column_index => $args{csv_class_column_index} || undef,
40 _csv_columns_for_features => $args{csv_columns_for_features} || undef,
41 _how_many_trees => $args{how_many_trees} || die "must specify number of trees",
42 _root_nodes => [],
43 _training_data_for_trees => {map {$_ => []} 0..$args{how_many_trees} - 1},
44 _all_record_ids => [],
45 _training_data_record_indexes => {},
46 _classifications => undef,
47 _debug1 => $args{debug1},
48 }, $class;
49 }
50  
51 ############################################## Methods ################################################
52 sub get_training_data_for_N_trees {
53 my $self = shift;
54 die("Aborted. get_training_data_csv() is only for CSV files") unless $self->{_training_datafile} =~ /\.csv$/;
55 my @all_record_ids;
56 open FILEIN, $self->{_training_datafile} or die "Unable to open $self->{_training_datafile} $!";
57 my $record_index = 0;
58 while (<FILEIN>) {
59 next if /^[ ]*\r?\n?$/;
60 $_ =~ s/\r?\n?$//;
61 my $record = $self->{_csv_cleanup_needed} ? cleanup_csv($_) : $_;
62 push @{$self->{_all_record_ids}}, substr($record, 0, index($record, ','));
63 $record_index++;
64 }
65 close FILEIN;
66 $self->{_how_many_total_training_samples} = $record_index - 1;
67 print "\n\nTotal number of training samples: $self->{_how_many_total_training_samples}\n" if $self->{_debug1};
68 print "\n\nAll record labels: @{$self->{_all_record_ids}}\n" if $self->{_debug1};
69 if ($self->{_looking_for_needles_in_haystack}) {
70 $self->get_training_data_for_N_trees_balanced();
71 } else {
72 $self->get_training_data_for_N_trees_regular();
73 }
74 }
75
76 sub get_training_data_for_N_trees_balanced {
77 my $self = shift;
78 die "You cannot use the contructor option 'how_many_training_samples_per_tree' if you " .
79 "have set the option 'looking_for_needles_in_haystack' " if $self->{_how_many_training_samples_per_tree};
80 my @class_names;
81 my %unique_class_names;
82 my %all_record_ids_with_class_labels;
83 $|++;
84 open FILEIN, $self->{_training_datafile} or die "Unable to open $self->{_training_datafile} $!";
85 my $i = 0;
86 while (<FILEIN>) {
87 next if /^[ ]*\r?\n?$/;
88 $_ =~ s/\r?\n?$//;
89 if ($i == 0) {
90 $i++;
91 next;
92 }
93 my $record = $self->{_csv_cleanup_needed} ? cleanup_csv($_) : $_;
94 my @parts = split /,/, $record;
95 my $classname = $parts[$self->{_csv_class_column_index}];
96 push @class_names, $classname;
97 $unique_class_names{$classname} = 1;
98 my $record_label = shift @parts;
99 $record_label =~ s/^\s*\"|\"\s*$//g;
100 $all_record_ids_with_class_labels{$record_label} = $classname;
101 print "." if $i % 10000 == 0;
102 $i++;
103 }
104 close FILEIN;
105 $|--;
106 my @unique_class_names = sort keys %unique_class_names;
107 my $num_unique_classnames = @unique_class_names;
108 die "\n\n'looking_for_needles_in_haystack' option has only been tested for the case of " .
109 "two data classes. You appear to have $num_unique_classnames data classes. If you know " .
110 "that you have specified only two classes, perhaps you need to use the constructor option " .
111 "'csv_cleanup_needed'. Aborting." if @unique_class_names > 2;
112 print "\n\nunique class names: @unique_class_names\n" if $self->{_debug1};
113 my %hist = map {$_ => 0} @unique_class_names;
114 foreach my $item (@class_names) {
115 foreach my $unique_val (@unique_class_names) {
116 if ($item eq $unique_val) {
117 $hist{$unique_val}++;
118 last;
119 }
120 }
121 }
122 if ($self->{_debug1}) {
123 print "\nhistogram of the values for the field : ";
124 foreach my $key (sort keys %hist) {
125 print "$key => $hist{$key} ";
126 }
127 }
128 my @histvals = values %hist;
129 my @hist_minmax = minmax( \@histvals );
130 my $ max_number_of_trees_possible = int($hist_minmax[1] / $hist_minmax[0]);
131 if ($self->{_debug1}) {
132 print "\n\nmaximum number of trees possible: $max_number_of_trees_possible\n";
133 }
134 die "\n\nYou have asked for more trees than can be supported by the training data. " .
135 "Maxinum number of trees that can be constructed from the training file is: $max_number_of_trees_possible\n"
136 if $self->{_how_many_trees} > $ max_number_of_trees_possible;
137
138 my %class1 = map {$_->[0] => $_->[1]} grep {@$_ > 0} map { $_->[1] eq $unique_class_names[0] ? [$_->[0], $_->[1]] : [] } pairmap {[$a,$b]} %all_record_ids_with_class_labels;
139  
140 my %class2 = map {$_->[0] => $_->[1]} grep {@$_ > 0} map { $_->[1] eq $unique_class_names[1] ? [$_->[0], $_->[1]] : [] } pairmap {[$a,$b]} %all_record_ids_with_class_labels;
141 my %minority_class = scalar(keys %class1) >= scalar(keys %class2) ? %class2 : %class1;
142 my %majority_class = scalar(keys %class1) >= scalar(keys %class2) ? %class1 : %class2;
143 my @minority_records = sort keys %minority_class;
144 my @majority_records = sort keys %majority_class;
145 print "\n\nminority records: @minority_records\n" if $self->{_debug1};
146 $self->{_how_many_training_samples_per_tree} = 2 * @minority_records;
147 $self->{_training_data_record_indexes} = {map {my $t = $_; $t => [map { $majority_records[rand @majority_records] } 0 .. @minority_records - 1]} 0 .. $self->{_how_many_trees} - 1};
148 map {my $t = $_; push @{$self->{_training_data_record_indexes}->{$t}}, @minority_records} 0 .. $self->{_how_many_trees} - 1;
149 if ($self->{_debug1}) {
150 print "\n Displaying records in the different training sets:\n";
151 foreach my $t (sort {$a <=> $b} keys %{$self->{_training_data_record_indexes}}) {
152 print "\n\n$t => @{$self->{_training_data_record_indexes}->{$t}}\n";
153 }
154 }
155 $self->_digest_training_data_all_trees();
156 }
157  
158 sub get_training_data_for_N_trees_regular {
159 my $self = shift;
160 die "You cannot use the contructor option 'looking_for_needles_in_haystack' if you " .
161 "have set the option 'how_many_training_samples_per_tree' " if $self->{_looking_for_needles_in_haystack};
162 $self->{_training_data_record_indexes} = {map {my $t = $_; $t => [map { $self->{_all_record_ids}->[rand @{$self->{_all_record_ids}}] } 0 .. $self->{_how_many_training_samples_per_tree} ] } 0 .. $self->{_how_many_trees} - 1};
163 $self->_digest_training_data_all_trees();
164 }
165  
166 sub _digest_training_data_all_trees {
167 my $self = shift;
168 my $firstline;
169 open FILEIN, $self->{_training_datafile} || die "unable to open $self->{_training_datafile}: $!";
170 my $record_index = 0;
171 while (<FILEIN>) {
172 next if /^[ ]*\r?\n?$/;
173 $_ =~ s/\r?\n?$//;
174 my $record = $self->{_csv_cleanup_needed} ? cleanup_csv($_) : $_;
175 if ($record_index == 0) {
176 $firstline = $record;
177 $record_index++;
178 next;
179 }
180 foreach my $t (keys %{$self->{_training_data_record_indexes}}) {
181 push @{$self->{_training_data_for_trees}->{$t}}, $record
182 if (contained_in(substr($record, 0, index($record, ',')),
183 @{$self->{_training_data_record_indexes}->{$t}}));
184 }
185 $record_index++;
186 }
187 close FILEIN;
188 my $splitup_data_for_trees = {map {my $t = $_; $t => [map {my $record = $_; [split /,/, $record]} @{$self->{_training_data_for_trees}->{$t}}]} 0 .. $self->{_how_many_trees} - 1};
189 my $data_hash_for_all_trees = {map {my $t = $_; $t => {map {my $record = $_; my $record_lbl = shift @{$record}; $record_lbl =~ s/^\s*\"|\"\s*$//g; $record_lbl => $record} @{$splitup_data_for_trees->{$t}}}} 0 .. $self->{_how_many_trees} - 1};
190 if ($self->{_debug1}) {
191 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
192 my @record_labels = keys %{$data_hash_for_all_trees->{$t}};
193 print "\n\nFor tree $t: record labels: @record_labels\n";
194 for my $kee (sort keys %{$data_hash_for_all_trees->{$t}}) {
195 print "$kee -----> @{$data_hash_for_all_trees->{$t}->{$kee}}\n";
196 }
197 }
198 }
199 my @all_feature_names = split /,/, $firstline;
200 my $class_column_heading = $all_feature_names[$self->{_csv_class_column_index}];
201 my @feature_names = map {$all_feature_names[$_]} @{$self->{_csv_columns_for_features}};
202 print "\n\nclass column heading: $class_column_heading\n";
203 print "feature names: @feature_names\n";
204 my $class_for_sample_all_trees = {map {my $t = $_; $t => {map {my $lbl = $_; "sample_$lbl" => "$class_column_heading=$data_hash_for_all_trees->{$t}->{$lbl}->[$self->{_csv_class_column_index} - 1]" } keys %{$data_hash_for_all_trees->{$t}} } } 0 .. $self->{_how_many_trees} - 1};
205 if ($self->{_debug1}) {
206 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
207 my @sample_labels = keys %{$class_for_sample_all_trees->{$t}};
208 print "\n\nFor tree $t: sample labels: @sample_labels\n";
209 for my $kee (sort keys %{$class_for_sample_all_trees->{$t}}) {
210 print "$kee -----> $class_for_sample_all_trees->{$t}->{$kee}\n";
211 }
212 }
213 }
214 my $sample_names_in_all_trees = {map {my $t = $_; $t => [map {"sample_$_"} keys %{$data_hash_for_all_trees->{$t}}]} 0 .. $self->{_how_many_trees} - 1};
215 my $feature_values_for_samples_all_trees = {map {my $t = $_; $t => {map {my $key = $_; "sample_$key" => [map {my $feature_name = $all_feature_names[$_]; "$feature_name=$data_hash_for_all_trees->{$t}->{$key}->[$_-1]"} @{$self->{_csv_columns_for_features}} ] } keys %{$data_hash_for_all_trees->{$t}} } } 0 .. $self->{_how_many_trees} - 1};
216 if ($self->{_debug1}) {
217 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
218 my @sample_labels = keys %{$feature_values_for_samples_all_trees->{$t}};
219 print "\n\nFor tree $t: sample labels: @sample_labels\n";
220 for my $kee (sort keys %{$feature_values_for_samples_all_trees->{$t}}) {
221 print "$kee -----> @{$feature_values_for_samples_all_trees->{$t}->{$kee}}\n";
222 }
223 }
224 }
225 my $features_and_values_all_trees = {map {my $t = $_; $t => {map {my $i = $_; $all_feature_names[$i] => [map {my $key = $_; $data_hash_for_all_trees->{$t}->{$key}->[$i-1]} keys %{$data_hash_for_all_trees->{$t}}]} @{$self->{_csv_columns_for_features} } } } 0 .. $self->{_how_many_trees} - 1};
226 if ($self->{_debug1}) {
227 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
228 my @feature_labels = keys %{$features_and_values_all_trees->{$t}};
229 print "\n\nFor tree $t: feature labels: @feature_labels\n";
230 for my $kee (sort keys %{$features_and_values_all_trees->{$t}}) {
231 print "$kee -----> @{$features_and_values_all_trees->{$t}->{$kee}}\n";
232 }
233 }
234 }
235 my $all_class_names_all_trees = {map {my $t = $_; my %all_class_labels_in_tree = map {$_ => 1} values %{$class_for_sample_all_trees->{$t}}; my @uniques = keys %all_class_labels_in_tree; $t => \@uniques } 0 .. $self->{_how_many_trees} - 1};
236 if ($self->{_debug1}) {
237 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
238 my @unique_class_names_for_tree = @{$all_class_names_all_trees->{$t}};
239 print "\n\nFor tree $t: all unique class names: @unique_class_names_for_tree\n";
240 }
241 }
242 my $numeric_features_valuerange_all_trees = {map {my $t = $_; $t => {}} 0 .. $self->{_how_many_trees} - 1};
243 my $feature_values_how_many_uniques_all_trees = {map {my $t = $_; $t => {}} 0 .. $self->{_how_many_trees} - 1};
244 my $features_and_unique_values_all_trees = {map {my $t = $_; $t => {}} 0 .. $self->{_how_many_trees} - 1};
245 my $numregex = '[+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?';
246 foreach my $t (0 .. $self->{_how_many_trees} - 1) {
247 foreach my $feature (sort keys %{$features_and_values_all_trees->{$t}}) {
248 my %all_values_for_feature = map {$_ => 1} @{$features_and_values_all_trees->{$t}->{$feature}};
249 my @unique_values_for_feature = grep {$_ ne 'NA'} keys %all_values_for_feature;
250 $feature_values_how_many_uniques_all_trees->{$t}->{$feature} = scalar @unique_values_for_feature;
251 my $not_all_values_float = 0;
252 map {$not_all_values_float = 1 if $_ !~ /^$numregex$/} @unique_values_for_feature;
253 if ($not_all_values_float == 0) {
254 my @minmaxvalues = minmax(\@unique_values_for_feature);
255 $numeric_features_valuerange_all_trees->{$t}->{$feature} = \@minmaxvalues;
256 }
257 $features_and_unique_values_all_trees->{$t}->{$feature} = \@unique_values_for_feature;
258 }
259 }
260 if ($self->{_debug1}) {
261 print "\nDisplaying value ranges for numeric features for all trees:\n\n";
262 foreach my $tree_index (keys %{$numeric_features_valuerange_all_trees}) {
263 my %keyval = %{$numeric_features_valuerange_all_trees->{$tree_index}};
264 print "\nFor tree $tree_index =>:\n";
265 foreach my $fname (keys %keyval) {
266 print " $fname => @{$keyval{$fname}}\n";
267 }
268 }
269 print "\nDisplaying number of unique values for each features for each tree:\n\n";
270 foreach my $tree_index (keys %{$feature_values_how_many_uniques_all_trees}) {
271 my %keyval = %{$feature_values_how_many_uniques_all_trees->{$tree_index}};
272 print "\nFor tree $tree_index =>:\n";
273 foreach my $fname (keys %keyval) {
274 print " $fname => $keyval{$fname}\n";
275 }
276 }
277 print "\nDisplaying unique values for all features for all trees:\n\n";
278 foreach my $tree_index (keys %{$features_and_unique_values_all_trees}) {
279 my %keyval = %{$features_and_unique_values_all_trees->{$tree_index}};
280 print "\nFor tree $tree_index =>:\n";
281 foreach my $fname (keys %keyval) {
282 print " $fname => @{$keyval{$fname}}\n";
283 }
284 }
285 }
286 foreach my $t (0..$self->{_how_many_trees}-1) {
287 $self->{_all_trees}->{$t}->{_class_names} = $all_class_names_all_trees->{$t};
288 $self->{_all_trees}->{$t}->{_feature_names} = \@feature_names;
289 $self->{_all_trees}->{$t}->{_samples_class_label_hash} = $class_for_sample_all_trees->{$t};
290 $self->{_all_trees}->{$t}->{_training_data_hash} = $feature_values_for_samples_all_trees->{$t};
291 $self->{_all_trees}->{$t}->{_features_and_values_hash} = $features_and_values_all_trees->{$t};
292 $self->{_all_trees}->{$t}->{_features_and_unique_values_hash} = $features_and_unique_values_all_trees->{$t};
293 $self->{_all_trees}->{$t}->{_numeric_features_valuerange_hash} = $numeric_features_valuerange_all_trees->{$t};
294 $self->{_all_trees}->{$t}->{_feature_values_how_many_uniques_hash} = $feature_values_how_many_uniques_all_trees->{$t};
295 }
296 if ($self->{_debug1}) {
297 foreach my $t (0..$self->{_how_many_trees}-1) {
298 print "\n\n============================= For Tree $t ==================================\n";
299 print "\nAll class names: @{$self->{_all_trees}->{$t}->{_class_names}}\n";
300 print "\nSamples and their feature values for each tree:\n";
301 foreach my $item (sort {sample_index($a) <=> sample_index($b)} keys %{$self->{_all_trees}->{$t}->{_training_data_hash}}) {
302 print "$item => @{$self->{_all_trees}->{$t}->{_training_data_hash}->{$item}}\n";
303 }
304 print "\nclass label for each data sample for each tree:\n";
305 foreach my $item (sort {sample_index($a) <=> sample_index($b)} keys %{$self->{_all_trees}->{$t}->{_samples_class_label_hash}} ) {
306 print "$item => $self->{_all_trees}->{$t}->{_samples_class_label_hash}->{$item}\n";
307 }
308 print "\nfeatures and the values taken by them:\n";
309 foreach my $item (sort keys %{$self->{_all_trees}->{$t}->{_features_and_values_hash}}) {
310 print "$item => @{$self->{_all_trees}->{$t}->{_features_and_values_hash}->{$item}}\n";
311 }
312 print "\nnumeric features and their ranges:\n";
313 foreach my $item (sort keys %{$self->{_all_trees}->{$t}->{_numeric_features_valuerange_hash}}) {
314 print "$item => @{$self->{_all_trees}->{$t}->{_numeric_features_valuerange_hash}->{$item}}\n";
315 }
316 print "\nnumber of unique values in each feature:\n";
317 foreach my $item (sort keys %{$self->{_all_trees}->{$t}->{_feature_values_how_many_uniques_hash}}) {
318 print "$item => $self->{_all_trees}->{$t}->{_feature_values_how_many_uniques_hash}->{$item}\n";
319 }
320 }
321 }
322 }
323  
324 sub show_training_data_for_all_trees {
325 my $self = shift;
326 foreach my $t (0..$self->{_how_many_trees}-1) {
327 print "\n\n============================= For Tree $t ==================================\n";
328 $self->{_all_trees}->{$t}->show_training_data();
329 }
330 }
331  
332 sub calculate_first_order_probabilities {
333 my $self = shift;
334 map $self->{_all_trees}->{$_}->calculate_first_order_probabilities(), 0 .. $self->{_how_many_trees}-1;
335 }
336  
337 sub calculate_class_priors {
338 my $self = shift;
339 map $self->{_all_trees}->{$_}->calculate_class_priors(), 0 .. $self->{_how_many_trees}-1;
340 }
341  
342 sub construct_all_decision_trees {
343 my $self = shift;
344 $self->{_root_nodes} =
345 [map $self->{_all_trees}->{$_}->construct_decision_tree_classifier(), 0 .. $self->{_how_many_trees}-1];
346 }
347  
348 sub display_all_decision_trees {
349 my $self = shift;
350 foreach my $t (0 .. $self->{_how_many_trees}-1) {
351 print "\n\n============================= For Tree $t ==================================\n";
352 $self->{_root_nodes}->[$t]->display_decision_tree(" ");
353 }
354 }
355  
356 sub classify_with_all_trees {
357 my $self = shift;
358 my $test_sample = shift;
359 $self->{_classifications} = [ map $self->{_all_trees}->{$_}->classify($self->{_root_nodes}->[$_], $test_sample), 0 .. $self->{_how_many_trees}-1 ];
360 }
361  
362 sub display_classification_results_for_all_trees {
363 my $self = shift;
364 die "You must first call 'classify_with_with_all_trees()' before invoking 'display_classification_results_for_all_trees()'" unless $self->{_classifications};
365 my @classifications = @{$self->{_classifications}};
366 my @solution_paths = map $_->{'solution_path'}, @classifications;
367 foreach my $t (0 .. $self->{_how_many_trees}-1) {
368 print "\n\n============================= For Tree $t ==================================\n";
369 print "\nnumber of training samples used: $self->{_how_many_training_samples_per_tree}\n";
370 my $classification = $classifications[$t];
371 delete $classification->{'solution_path'};
372 my @which_classes = sort {$classification->{$b} <=> $classification->{$a}} keys %$classification;
373 print "\nClassification:\n\n";
374 print " class probability\n";
375 print " ---------- -----------\n";
376 foreach my $which_class (@which_classes) {
377 my $classstring = sprintf("%-30s", $which_class);
378 my $valuestring = sprintf("%-30s", $classification->{$which_class});
379 print " $classstring $valuestring\n";
380 }
381 print "\nSolution path in the decision tree: @{$solution_paths[$t]}\n";
382 print "\nNumber of nodes created: " . $self->{_root_nodes}->[$t]->how_many_nodes() . "\n";
383  
384 }
385 }
386  
387 sub get_majority_vote_classification {
388 my $self = shift;
389 die "You must first call 'classify_with_all_trees()' before invoking 'get_majority_vote_classifiction()'" unless $self->{_classifications};
390 my @classifications = @{$self->{_classifications}};
391 my %decision_classes = map { $_ => 0 } @{$self->{_all_trees}->{0}->{_class_names}};
392 foreach my $t (0 .. $self->{_how_many_trees}-1) {
393 my $classification = $classifications[$t];
394 delete $classification->{'solution_path'} if exists $classification->{'solution_path'};
395 my @sorted_classes = sort {$classification->{$b} <=> $classification->{$a}} keys %$classification;
396 $decision_classes{$sorted_classes[0]}++;
397 }
398 my @sorted_by_votes_decision_classes = sort {$decision_classes{$b} <=> $decision_classes{$a}} keys %decision_classes;
399 return $sorted_by_votes_decision_classes[0];
400 }
401  
402 ######################################## Utility Routines ##############################################
403 sub sample_index {
404 my $arg = shift;
405 $arg =~ /_(.+)$/;
406 return $1;
407 }
408  
409 # checks whether an element is in an array:
410 sub contained_in {
411 my $ele = shift;
412 my @array = @_;
413 my $count = 0;
414 map {$count++ if $ele eq $_} @array;
415 return $count;
416 }
417  
418 # Returns an array of two values, the min and the max, of an array of floats
419 sub minmax {
420 my $arr = shift;
421 my ($min, $max);
422 foreach my $i (0..@{$arr}-1) {
423 if ( (!defined $min) || ($arr->[$i] < $min) ) {
424 $min = $arr->[$i];
425 }
426 if ( (!defined $max) || ($arr->[$i] > $max) ) {
427 $max = $arr->[$i];
428 }
429 }
430 return ($min, $max);
431 }
432  
433 sub check_for_illegal_params {
434 my @params = @_;
435 my @legal_params = qw / training_datafile
436 entropy_threshold
437 max_depth_desired
438 csv_class_column_index
439 csv_columns_for_features
440 symbolic_to_numeric_cardinality_threshold
441 number_of_histogram_bins
442 how_many_trees
443 how_many_training_samples_per_tree
444 looking_for_needles_in_haystack
445 csv_cleanup_needed
446 debug1
447 /;
448 my $found_match_flag;
449 foreach my $param (@params) {
450 foreach my $legal (@legal_params) {
451 $found_match_flag = 0;
452 if ($param eq $legal) {
453 $found_match_flag = 1;
454 last;
455 }
456 }
457 last if $found_match_flag == 0;
458 }
459 return $found_match_flag;
460 }
461  
462 sub cleanup_csv {
463 my $line = shift;
464 $line =~ tr/\/:?()[]{}'/ /;
465 # my @double_quoted = substr($line, index($line,',')) =~ /\"[^\"]+\"/g;
466 my @double_quoted = substr($line, index($line,',')) =~ /\"[^\"]*\"/g;
467 for (@double_quoted) {
468 my $item = $_;
469 $item = substr($item, 1, -1);
470 $item =~ s/^\s+|,|\s+$//g;
471 $item = join '_', split /\s+/, $item;
472 substr($line, index($line, $_), length($_)) = $item;
473 }
474 my @white_spaced = $line =~ /,(\s*[^,]+)(?=,|$)/g;
475 for (@white_spaced) {
476 my $item = $_;
477 $item =~ s/\s+/_/g;
478 $item =~ s/^\s*_|_\s*$//g;
479 substr($line, index($line, $_), length($_)) = $item;
480 }
481 $line =~ s/,\s*(?=,|$)/,NA/g;
482 return $line;
483 }
484  
485  
486 1;