/
arff-to-fann-data.pl
executable file
·211 lines (178 loc) · 4.97 KB
/
arff-to-fann-data.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use POSIX;
=head1 NAME
arff-to-fann-data - Converts ARFF files to FANN Data format
=head1 AUTHOR
Syed Reza
=cut
sub die_with_usage { my $error = shift;
print "Usage: $0 -i [input.arff] -o [output.data]\n";
print "------------------------------------------\n";
printf "%-5s\t%s\n", "-attr", "the NAME of the output \@attribute";
printf "%-5s\t%s\n", "", "(defaults to last \@attribute)";
if($error) {
print "\nError: $error\n";
}
exit(1);
}
=head1 DESCRIPTION
This is not a general purpose script. It treats all Numeric values in
the input arff file as input nodes in FANN.
It allows you to specify the target attribute in the ARFF file
for the output node in the NN. By default the target column,
specified with the -attr option, is the name of the last @attribute
If your target column is of type Nominal (as it is in most classification cases)
this script will automatically create log2(number_of_nominals)
=cut
my $arff;
my $data;
my $attr;
GetOptions(
"i=s", \$arff,
"o=s", \$data,
"attr=s", \$attr,
);
unless(-e $arff) {
die_with_usage;
}
if($attr) {
print "Output \@attribute is $attr\n";
print "Searching for \@attribute $attr\n";
} else {
$attr = undef;
print "Output \@attribute is assumed to be the last one\n";
}
my $arff_fh;
my $data_fh;
my $capture = "";
my $attr_name; my $attr_type;
my $target_attr_name = undef;
my $target_attr_type = undef;
my $target_attr_index = undef;
my $count = 0;
my $indata = 0;
my @vector;
my $expected_vector_length = 0;
my $input_node_count = 0;
my $output_node_count = 0;
open($arff_fh, "<$arff");
while(<$arff_fh>) { chomp;
if($_ =~ /^\s*\@data/i) {
$indata = 1;
unless(defined($attr)) {
$target_attr_name = $attr_name;
$target_attr_type = $attr_type;
# we have taken the last attribute to be the output attribute
# we must set the capture to be 0
chop($capture); $capture .= "0";
$target_attr_index = length($capture)-1;
}
$count = 0;
$expected_vector_length = length($capture);
print "TARGET_ATTR_NAME = $target_attr_name\n";
print "TARGET_ATTR_TYPE = $target_attr_type\n";
print "TARGET_ATTR_INDEX = $target_attr_index\n\n";
print "VECTOR LENGTH = ", length($capture), "\n";
# print "CAPTURE = \n$capture";
next;
}
if($indata) {
# last;
if($_ !~ /^\s*$/) {
@vector = split(/\s*\,\s*/);
if(scalar(@vector) == $expected_vector_length) { $count++; }
}
} else {
if($_ =~ /^\s*\@attribute\s+(.+)\s+(.+)\s*$/i) {
$attr_name = $1;
$attr_type = $2;
if($attr && $attr_name eq $attr) {
$target_attr_type = $attr_type;
$target_attr_name = $attr_name;
$target_attr_index = $count;
$capture .= "0";
}
else {
if($attr_type eq "numeric") {
$capture .= "1";
$input_node_count++;
} else {
$capture .= "0";
}
}
$count++;
}
}
}
close($arff_fh);
print "TARGET_ATTR_INDEX = $target_attr_index\n";
print "TARGET_ATTR_NAME = $target_attr_name\n";
print "TARGET_ATTR_TYPE = $target_attr_type\n";
unless(defined($target_attr_index) && defined($target_attr_name) && defined($target_attr_type)) {
die_with_usage("Target Attribute Not Found. \nARFF invalid or \@attribute does not exist");
}
print "\n";
my @target_nominals = undef;
if($target_attr_type eq "numeric") {
print "NOTICE: TARGET ATTRIBUTE TYPE is Numeric.\nAssuming normalized to 0-1 Sigmoid range\n";
$output_node_count = 1;
} elsif($target_attr_type =~ /^\{(.+)\}$/) {
print "NOTICE: TARGET ATTRIBUTE TYPE looks Nominal\n";
print "Fixing to a set of Output Neurons...\n";
@target_nominals = split(/\s*\,\s*/, $1);
$output_node_count = ceil(log2(scalar(@target_nominals)));
}
print "\n\n";
print "NUM_INSTANCES = $count\n";
print "NUM_INPUT_NODES = $input_node_count\n";
print "NUM_OUTPUT_NODES: $output_node_count\n";
open($data_fh, ">$data");
open($arff_fh, "<$arff");
print $data_fh "$count $input_node_count $output_node_count\n";
$indata = 0; $count = 0;
open($arff_fh, "<$arff");
while(<$arff_fh>) { chomp;
if($_ =~ /^\s*\@data/i) { $indata = 1; next;}
if($indata) {
# last;
if($_ !~ /^\s*$/) {
@vector = split(/\s*\,\s*/);
if(scalar(@vector) == $expected_vector_length) {
for(my $i = 0; $i < $expected_vector_length; $i++) {
if(substr($capture, $i, 1)) {
print $data_fh $vector[$i];
print $data_fh " " unless $i == ($expected_vector_length-1);
}
}
print $data_fh "\n";
print $data_fh encode_target($vector[$target_attr_index]), "\n";
$count++;
}
}
} else { next; }
}
close($data_fh);
close($arff_fh);
sub log2 { my $n = shift;
return log($n)/log(2);
}
sub encode_target { my $value = shift;
if(@target_nominals) {
my $index = 0;
++$index until $target_nominals[$index] eq $value or $index > $#target_nominals;
return encode_binary($index, "");
} else {
return $value;
}
}
sub encode_binary { my $num = shift;
my @bits = (); my $p;
for($p = 0; $p < $output_node_count; $p++) {
if($num&(1<<$p)) { push(@bits, 1); }
else { push(@bits, 0); }
}
return join(" ", @bits);
}