copyleft() {
cat<<-EOF
nbins: discretize numerics into n bins
Copyright (C) 2004 Tim Menzies
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
EOF
}
Some learners can only handle discrete (a.k.a. nominal or non-numeric) variables. Here's a simple discretizer that finds all numbers in a table of data and discretizes them into bins of size max-min/n.
Of course, this is just the tip-of-the-iceberg in handling the discretization problem. For a more complete analysis, see [Do95]. In fact, this code is just a demonstrator for my data mining students so they can try different discretization methods.
usage() {
cat<<-EOF
Usage: nbins [FLAGs] FILE
Simple (very) discretization of all numeric columns in FILE into n bins
Flags:
-h print this help text
-l copyright notice
-m CHAR missing value character; default="$Q$missing0$Q
-n NUM number of bins; default=$Q$bins0$Q
-s CHAR field separator; default=$Q$sep0$Q
-x run an example
EOF
exit
}
Here's the distribution of numbers in the last column of the file nbinseg.dat:
sirius:~/public_html/dm [769]$ bars -r 2 nbinseg.dat
10| 3| ***
12| 10| **********
14| 40| ****************************************
16| 39| ***************************************
18| 39| ***************************************
20| 40| ****************************************
22| 26| **************************
24| 32| ********************************
26| 36| ************************************
28| 28| ****************************
30| 24| ************************
32| 27| ***************************
34| 18| ******************
36| 13| *************
38| 11| ***********
40| 5| *****
42| 1| *
44| 5| *****
46| 1| *
nbins can bunch these up into (e.g.) 5 bins:
sirius:~/public_html/dm [770]$ nbins -n5 nbinseg.dat | bars -r 2
8| 7| **
16| 124| **************************************
22| 129| ****************************************
30| 92| *****************************
38| 39| ************
46| 7| **
First, if you have installed anything from this site before, save your config file to somewhere safe.
Second, copy the following files to your directory (from
either ~timm/public_html/dm or
http://www.cs.pdx.edu/~timm/dm or
from http://www.cs.pdx.edu/~timm/dm/nbins.zip):
Third, make nbins executable:
chmod +x nbins
Fourth, compare your safe version of config
with the new version you just copied and fix up any paths.
Five, edit this file and config.
The first line of this file should point to your local bash shell.
and you'll need to check at least the #paths section in config
Check that all it works:
nbins -x
If the installation worked, then you should see a file returned with a smaller range of numbers in each column.
Defaults:
sep0="," missing0="?" bins0=10
Paths:
. config
Minor details:
Q="\""
nbinsDemo() {
main nbinseg.dat
}
main() {
$gawk -F"$sep" -f lib.awk -f nbins.awk \
Bins=$bins Missing="$missing" \
pass=1 $1 pass=2 $1
}
BEGIN {
FS=OFS=",";
Missing="?";
Bins=10;
Max; Min; #arrays of min/max numbers in each column
}
FNR==1 && pass==1 { for(I=1;I<=NF;I++) {
Max[I]= -1 * Inf;
Min[I]= Inf};
}
pass==1 {
for(I=1;I<=NF;I++) {
if ($I == "?") continue;
if (! number($I) ) {
Symbol[I];
continue;}
else {
Max[I]=most(Max[I],$I);
Min[I]=least(Min[I],$I);
if (novel(I,$I,Seen)) Range[I]++
}}}
pass==2 {
for(I=1;I<=NF;I++) $I= discretize(I,$I);
print $0;
}
function discretize(col,i, r) {
if ( Symbol[col] ) return i;
if ($I == Missing) return i;
if (Range[col] < Bins) return i;
r=(Max[col]- Min[col])/Bins;
return round(i/r)*r;
}
demo=""
while getopts "hln:s:x" flag
do case "$flag" in
h) usage; exit ;;
l) copyleft; exit;;
m) missing=$OPTARG;;
n) bins=$OPTARG;;
s) sep=$OPTARG;;
x) demo="nbinsDemo nbinseg.dat";;
esac
done
shift $(($OPTIND - 1))
missing=${missing:=$missing0}
bins=${bins:=$bins0}
sep=${sep:=$sep0}
if [ -n "$demo" ]
then $demo
exit
else main $1
fi
Tim Menzies ,
tim@menzies.us,
http://menzies.us
This page generated by Site:
see http://www.cs.pdx.edu/~timm/dm/site.html
This site is built using PerlPod.Style sheet switching method taken from Eddie Traversa's excellent and simple-to-apply tutorial: http://dhtmlnirvana.com/content/styleswitch/styleswitch1.html.
Search engine powered by ATOMZ http://www.atomz.com/search/. Note, the indexes to this site are only updated weekly (heh, its a free service- what more ja want?).
Icons on this site come from http://www.sql-news.de/rubriken/olap.asp and http://www.ifnet.it/webif/centrodi/eng/toolbar.htm.
The JAVA machine learners used at this site come from the extensive data mining libraries found in the University of Waikato's Environment for Knowledge Analysis (the WEKA) http://www.cs.waikato.ac.nz/ml/weka/
Copyright (C) Tim Menzies 2004
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2; see http://www.gnu.org/copyleft/gpl.html. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The content from or through this web page are provided 'as is' and the author makes no warranties or representations regarding the accuracy or completeness of the information. Your use of this web page and information is at your own risk. You assume full responsibility and risk of loss resulting from the use of this web page or information. If your use of materials from this page results in the need for servicing, repair or correction of equipment, you assume any costs thereof. Follow all external links at your own risk and liability.