view tvii/dataset/rand.py @ 87:9d5a5e9f5c3b

add kmeans + dataset
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:05:57 -0800
parents
children
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
generate a random dataset
"""

import csv
import random
import sys
from .cli import DatasetGenerationParser


class RandomDataset(object):

    def __init__(self, N, D, bounds=(0., 1.)):
        assert len(bounds) == 2
        self.N = N
        self.D = D
        self.bounds = bounds
        self.length = bounds[-1] - bounds[0]

        self.points = [self() for _ in xrange(self.N)]

    def translate(self, fraction):
        return self.length*fraction + self.bounds[0]

    def __call__(self):
        """generate one `D`-dimensional point of data"""

        return [self.translate(random.random()) for i in xrange(self.D)]


def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line
    parser = DatasetGenerationParser(description=__doc__)
    options = parser.parse_args(args)

    # make some random points
    points = RandomDataset(N=options.number, D=options.dimensions)

    # output them
    parser.writer().writerows(points.points)


if __name__ == '__main__':
    main()