Last active
March 11, 2016 00:06
-
-
Save jdp/6e902095599e0877bbe7 to your computer and use it in GitHub Desktop.
Generate a population following a distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkpop -i pokemon 100 zipf | sort | uniq -c | sort -rn | |
68 hoopa | |
12 diancie | |
7 zygarde | |
3 yveltal | |
2 xerneas | |
1 volcanion | |
1 trevenant | |
1 pumpkaboo | |
1 noivern | |
1 noibat | |
1 gourgeist | |
1 bergmite | |
1 avalugg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import sys | |
import numpy | |
def zipf_command(args): | |
for item in numpy.random.zipf(args.a, args.size): | |
yield item | |
def main(args): | |
values = args.infile.readlines() | |
mapping = {} | |
for item in args.func(args): | |
if item not in mapping: | |
mapping[item] = values.pop() | |
args.outfile.write(mapping[item]) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--input', dest='infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | |
parser.add_argument('-o', '--output', dest='outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout) | |
parser.add_argument('size', type=int) | |
subparsers = parser.add_subparsers() | |
parser_zipf = subparsers.add_parser('zipf') | |
parser_zipf.add_argument('-a', type=float, default=2) | |
parser_zipf.set_defaults(func=zipf_command) | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment