Uniform sampling of a data set
Sometimes you may need to sample a dataset. You may want to get a uniformly sampled subset out of a datatset stored in a file. The perlscript below does the job for you.
#!/usr/bin/perl -w
if ( $#ARGV!=1 ) {
print "Wrong number of arguments\\n\\t".
"uniform-sampler.pl <file> <sample_proportion>\\n";
}
else {
srand();
open(FILE,$ARGV[0]) or die "File $ARGV[0] could not be open";
while($line=<FILE>) {
if ( rand()<$ARGV[1] ) {
print $line;
}
}
close FILE;
}
1;
About this entry
You’re currently reading “Uniform sampling of a data set,” an entry on Xavier Llorà
- Published:
- Friday, May 11th, 2007 at 8:20 am
- Author:
- Xavier
- Category:
- Notes
No comments
Jump to comment form | comments rss | trackback uri