np.random下的4个随机函数

# -*- coding: utf-8 -*-
"""
Created on Wed May 23 16:38:58 2018

@author: Loulch C.C
"""
from numpy import np

"""
np.random.rand(d0, d1, ..., dn)
    
    Random values in a given shape.
    随机生成一给定形状的n维数组,dtype=ndarray
    Create an array of the given shape and populate it with
    random samples from a uniform distribution
    over ``[0, 1)``.
    创建一个给定形状的数组,并用从[0, 1)均匀分布中用随机样本填充它。
    Parameters
    ----------
    d0, d1, ..., dn : int, optional
        The dimensions of the returned array, should all be positive.
        If no argument is given a single Python float is returned.
    由于每个参数指定的是对应维数的长度,所以应该是正数,若没有参数给定,返回一个浮点数
    Returns
    -------
    out : ndarray, shape ``(d0, d1, ..., dn)``
        Random values.
    输出类型是n维数组
    Notes
    -----
    This is a convenience function. If you want an interface that
    takes a shape-tuple as the first argument, refer to
    np.random.random_sample .
    
"""
np.random.rand()
#0.0004465318551577502
np.random.rand(3,2,2)
#    array([[[0.33349897, 0.07612993],
#        [0.86019193, 0.38230735]],
#
#       [[0.46780106, 0.55711774],
#        [0.44259951, 0.67058414]],
#
#       [[0.673428  , 0.90087974],
#        [0.70143593, 0.77487263]]])
"""
np.random.random_sample(size=None)
    Return random floats in the half-open interval [0.0, 1.0).
    在前开后比的区间[0.0, 1.0)返回一个浮点数
    Results are from the "continuous uniform" distribution over the
    stated interval.  To sample :math:`Unif[a, b), b > a` multiply
    the output of `random_sample` by `(b-a)` and add `a`::
    
      (b - a) * random_sample() + a
    结果是在规定的间隔中从“连续均匀”分布中取值。
    Parameters
    ----------
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  Default is None, in which case a
        single value is returned.
    输入是整数或整数型元组,若用默认参数None,返回一个浮点数
    Returns
    -------
    out : float or ndarray of floats
        Array of random floats of shape `size` (unless ``size=None``, in which
        case a single float is returned).
    
"""
np.random.random_sample(2)
#0.6028707437254377
np.random.random_sample((3,2,2))
#array([[[0.64652491, 0.65078447],
#        [0.56590835, 0.90413143]],
#
#       [[0.7356992 , 0.83405403],
#        [0.79705633, 0.93500609]],
#
#       [[0.06099415, 0.88193611],
#        [0.18557931, 0.25294683]]])
"""
np.random.randn(d0, d1, ..., dn)
    
    Return a sample (or samples) from the "standard normal" distribution.
    返回一个服从正态分布的样本,注意输入参数不能是元组
    If positive, int_like or int-convertible arguments are provided,
    `randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
    with random floats sampled from a univariate "normal" (Gaussian)
    distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
    floats, they are first converted to integers by truncation). A single
    float randomly sampled from the distribution is returned if no
    argument is provided.
    
    This is a convenience function.  If you want an interface that takes a
    tuple as the first argument, use `numpy.random.standard_normal` instead.
    
    Parameters
    ----------
    d0, d1, ..., dn : int, optional
        The dimensions of the returned array, should be all positive.
        If no argument is given a single Python float is returned.
    由于每个参数指定的是对应维数的长度,所以应该是正数,若没有参数给定,返回一个浮点数
    Returns
    -------
    Z : ndarray or float
        A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from
        the standard normal distribution, or a single such float if
        no parameters were supplied.
        从标准正态分布中得到样本为浮点型,形状为(d0, d1, ..., dn)的数组,或者如果没有
        指定参数,返回一个浮点数。
    See Also
    --------
    random.standard_normal : Similar, but takes a tuple as its argument.
    random.standard_normal和np.random.randn功能相似,但输入参数是元组
    
    Notes
    -----
    For random samples from :math:`N(\mu, \sigma^2)`, use:
    
    ``sigma * np.random.randn(...) + mu``
"""
np.random.randn()
#1.1959465924275063
np.random.randn(3,2,2)
#array([[[ 0.17616821,  0.71155665],
#        [-0.19112895,  0.43024993]],
#
#       [[ 1.00172339,  0.04432509],
#        [ 0.35799857, -0.09941122]],
#
#       [[ 0.93664108,  0.12910895],
#        [-0.07058416,  0.78139332]]])
"""
np.random.standard_normal(size=None)

    Draw samples from a standard Normal distribution (mean=0, stdev=1).
    返回一个服从正态分布的样本
    Parameters
    ----------
    size : int or tuple of ints, optional 输入参数size是整数或整数型元组
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  Default is None, in which case a
        single value is returned.
    
    Returns
    -------
    out : float or ndarray  返回一个浮点数和n维数组
        Drawn samples.
    
"""
np.random.standard_normal()
# -0.2502682581746278
np.random.standard_normal(8)
#array([-0.56042949, -0.49409946, -1.54906932,  0.83449251,  0.81294393,
#        0.8207554 ,  0.08094755,  0.35935617])
np.random.standard_normal(size=(3, 2, 2))
#array([[[-0.2995678 , -1.51831344],
#        [ 0.76354373,  1.91338345]],
#
#       [[ 1.26671755, -0.63408626],
#        [-0.20556799,  0.04380744]],
#
#       [[-0.82423503, -0.42493808],
#        [ 0.29523695, -1.13595518]]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
np.random.normal和np.random.randn()函数都可以用来生成服从正态分布的随机样本,但它们有一些区别。 np.random.normal函数的语法为:np.random.normal(loc, scale, size),其中loc表示均值,scale表示标准差,size表示要生成的样本数目。该函数生成的样本符合指定均值和标准差的正态分布。 np.random.randn()函数用于生成服从标准正态分布(均值为0,标准差为1)的样本。该函数的语法为:np.random.randn(d0, d1, ..., dn),其中d0, d1, ..., dn表示生成样本的维度。生成的样本数目由这些维度决定。 总结来说,np.random.normal函数可以通过指定均值和标准差来生成符合正态分布的随机样本,而np.random.randn()函数生成的样本均值为0,标准差为1。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [『Python学习笔记』np.random.rand()函数np.random.randn()函数](https://blog.csdn.net/abc13526222160/article/details/86423754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [np.random一系列(np.random.normal()、np.random.randint、np.random.randn、np.random.rand)](https://download.csdn.net/download/weixin_38733382/13745169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值