87
|
1 #!/usr/bin/env python
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 """
|
|
5 generate a random dataset
|
|
6 """
|
|
7
|
|
8 import csv
|
|
9 import random
|
|
10 import sys
|
|
11 from .cli import DatasetGenerationParser
|
|
12
|
|
13
|
|
14 class RandomDataset(object):
|
|
15
|
|
16 def __init__(self, N, D, bounds=(0., 1.)):
|
|
17 assert len(bounds) == 2
|
|
18 self.N = N
|
|
19 self.D = D
|
|
20 self.bounds = bounds
|
|
21 self.length = bounds[-1] - bounds[0]
|
|
22
|
|
23 self.points = [self() for _ in xrange(self.N)]
|
|
24
|
|
25 def translate(self, fraction):
|
|
26 return self.length*fraction + self.bounds[0]
|
|
27
|
|
28 def __call__(self):
|
|
29 """generate one `D`-dimensional point of data"""
|
|
30
|
|
31 return [self.translate(random.random()) for i in xrange(self.D)]
|
|
32
|
|
33
|
|
34 def main(args=sys.argv[1:]):
|
|
35 """CLI"""
|
|
36
|
|
37 # parse command line
|
|
38 parser = DatasetGenerationParser(description=__doc__)
|
|
39 options = parser.parse_args(args)
|
|
40
|
|
41 # make some random points
|
|
42 points = RandomDataset(N=options.number, D=options.dimensions)
|
|
43
|
|
44 # output them
|
|
45 parser.writer().writerows(points.points)
|
|
46
|
|
47
|
|
48 if __name__ == '__main__':
|
|
49 main()
|