numpy.random.uniform

函数原型:

numpy.random.uniform(low,high,size)

功能

从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.

参数介绍:

  • low: 采样下界,float类型,默认值为0;
  • high: 采样上界,float类型,默认值为1;
  • size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出m* n * k个样本,缺省时输出1个值。

返回值

ndarray类型,其形状和参数与size中描述一致。

这里顺便说下ndarray类型,表示一个N维数组对象,其有一个shape(表维度大小)和dtype(说明数组数据类型的对象),使用zeros和ones函数可以创建数据全0或全1的数组,原型:

numpy.ones(shape,dtype=None,order='C')

其中,shape表数组形状(m*n),dtype表类型,order表是以C还是fortran形式存放数据。

类似uniform,还有以下随机数产生函数:

  1. randint: 原型:numpy.random.randint(low, high=None, size=None, dtype=‘l’),产生随机整数;
  2. random_integers: 原型: numpy.random.random_integers(low, high=None, size=None),在闭区间上产生随机整数;
  3. random_sample: 原型: numpy.random.random_sample(size=None),在[0.0,1.0)上随机采样;
  4. random: 原型: numpy.random.random(size=None),和random_sample一样,是random_sample的别名;
  5. rand: 原型: numpy.random.rand(d0, d1, …, dn),产生d0 - d1 - … - dn形状的在[0,1)上均匀分布的float型数。
  6. randn: 原型:numpy.random.randn(d0,d1,…,dn),产生d0 - d1 - … - dn形状的标准正态分布的float型数。

Examples:

例一: “画柱状图”

    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    import numpy as np
     
    s = np.random.uniform(0,1,1200)      # 产生1200个[0,1)的数
    count, bins, ignored = plt.hist(s, 12, normed=True)
     """
     hist原型:
             matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None,
             cumulative=False, bottom=None, histtype='bar', align='mid', 
             orientation='vertical',rwidth=None, log=False, color=None, label=None, 
             stacked=False, hold=None,data=None,**kwargs)
     输入参数很多,具体查看matplotlib.org,本例中用到3个参数,分别表示:s数据源,bins=12表示bin 
     的个数,即画多少条条状图,normed表示是否归一化,每条条状图y坐标为n/(len(x)`dbin),整个条状图积分值为1
     输出:count表示数组,长度为bins,里面保存的是每个条状图的纵坐标值
          bins:数组,长度为bins+1,里面保存的是所有条状图的横坐标,即边缘位置
          ignored: patches,即附加参数,列表或列表的列表,本例中没有用到。
    """
    plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
    plt.show()

绘制结果如下图:
在这里插入图片描述

例二: “画正态分布柱状图”

    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.mlab as MLA
     
    mu, sigma = 10, 10
    x = mu + sigma*np.random.randn(5000)
     
    # the histogram of the data
    n, bins, patches = plt.hist(x, 20, normed=1, facecolor='blue', alpha=0.8)
     
    # add a 'best fit' line
    y = MLA.normpdf( bins, mu, sigma)
    """
    normpdf函数原型:
            matplotlib.mlab.normpdf(x, *args)
    功能:Return the normal pdf evaluated at x; args provides mu, sigma
    """
    l = plt.plot(bins, y, 'g--', linewidth=3)
     
    plt.xlabel('samples')
    plt.ylabel('p')
    plt.title(r'$Normal\ pdf\ m=10,\ \sigma=10$')
    plt.axis([-30, 50, 0, 0.042])
    plt.grid(True)
    plt.show()

结果如下图:
在这里插入图片描述

numpy.random.RandomState介绍:

英文简介:“Container for the Mersenne Twister pseudo-random number generator.”
翻译过来为:它是一个容器,用来存储采用梅森旋转产生伪随机数的算法。

输入参数:seed,可选项{None, int, array_like},没有给定的话,函数随机选一个起始点,这样深度学习的结果可能接近,但不完全相同,如果给定一个seed,则结果是deterministic,是确定的,但给定不给定seed对输出随机数并没有影响,只是相当于给定了一个初始点,后面的数也是基于这个seed而产生。

————————————————
原文链接:https://blog.csdn.net/u013920434/article/details/52507173

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值