randomizer.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pfi-internship2016 作者: hvy 项目源码 文件源码
def rnd(shape, mean=0.0, stddev=0.01):
    """Return a list with the given shape with random float elements.
    The values are generated from a Gaussian distribution.

    Args:
        shape (tuple, list): A shape to generate random floats for. 1 or 2 dimensional.
        mean (float): Mean of the Gaussian distribution.
        stddev (float): Standard deviation of the distribution.

    Returns:
        list: A list with random float elements with the dimensions specified by `shape`.
    """
    if not isinstance(shape, (tuple, list)):
        raise TypeError('Invalid shape. The shape must be a tuple or a list.')

    dimension = len(shape)

    if dimension is 1:
        # Create a 1 dimensional array with random elements
        return [random.gauss(mean, stddev) for _ in range(shape[0])]
    elif dimension is 2:
        # Create a 2 dimensional matrix with random elements
        return [[random.gauss(mean, stddev) for _ in range(shape[1])] for _ in range(shape[0])]
    else:
        raise NotImplementedError('Random generation with dimensions > 2 is not yet implemented.')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号