Mercurial > hg > tvii
comparison tvii/dataset/line.py @ 87:9d5a5e9f5c3b
add kmeans + dataset
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 14:05:57 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:b56d329c238d | 87:9d5a5e9f5c3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 """ | |
4 generate a line segment | |
5 """ | |
6 | |
7 import sys | |
8 from .cli import DatasetGenerationParser | |
9 | |
10 | |
11 def x_range(x_min, x_max, n): | |
12 """1-D range function""" | |
13 | |
14 dx = (x_max - x_min)/(n - 1.) | |
15 return [x_min + dx*i for i in range(n)] | |
16 | |
17 | |
18 class Line(object): | |
19 """linear dataset""" | |
20 | |
21 # TODO: should woprk in `D` dimensions | |
22 def __init__(self, W, b): | |
23 self.W = W | |
24 self.b = b | |
25 | |
26 def y(self, x): | |
27 return self.W*x + self.b | |
28 | |
29 def __call__(self, *x): | |
30 return [self.y(_x) for _x in x] | |
31 | |
32 | |
33 def main(args=sys.argv[1:]): | |
34 """CLI""" | |
35 | |
36 # parse command line | |
37 parser = DatasetGenerationParser(description=__doc__) | |
38 parser.add_argument('W', type=float, | |
39 help="slope") | |
40 parser.add_argument('b', type=float, | |
41 help="offset") | |
42 parser.add_argument('-x', dest='x_range', type=float, | |
43 nargs=2, metavar=('MIN', 'MAX'), default=(0., 1.), | |
44 help="range of `x`") | |
45 options = parser.parse_args(args) | |
46 | |
47 # instantiate line generator | |
48 line = Line(options.W, options.b) | |
49 | |
50 # generate `x` | |
51 x = x_range(options.x_range[0], options.x_range[1], options.number) | |
52 | |
53 # generate `y` | |
54 y = line(*x) | |
55 | |
56 # output points | |
57 parser.writer().writerows(zip(x, y)) |