python(22)-numpy-随机数

一维
    1.生成1-10个()范围内的序列整数
    2.生成1-N个()范围内的随机整数
    3.生成1-N个()范围内的随机浮点数

多维
     1.均匀分布          rand(d0,d1,..,dn)
     2.标准正态分布   randn(d0,d1,..,dn)
     3.随机整数或者整数数组,范围[low,high]  randint(low[,high,shape])
     4.种子 seed(s)

  数组变化


一维
    1.生成1-10个()范围内的序列整数
    2.生成1-N个()范围内的随机整数
    3.生成1-N个()范围内的随机浮点数

  num_arr=np.arange(1,10)       #1.生成1-10个()范围内的随机整数
    """
    [1 2 3 4 5 6 7 8 9]
    """
    random_arr_int=np.random.randint(1, 7, size=10)#2.生成1-N个()范围内的随机整数
    """
    [4 5 3 6 2 1 5 5 5 5]
    """
    random_arr_float=np.random.uniform(100, 1000,size=(1,6)) #3. 生成1-N个 范围内的随机浮点数
    """
    [[ 283.95510351  247.53991888  276.40475545  405.37086063  611.22097477
   161.4635404 ]]
    """


多维
     1.均匀分布          rand(d0,d1,..,dn)
     2.标准正态分布   randn(d0,d1,..,dn)
     3.随机整数或者整数数组,范围[low,high]  randint(low[,high,shape])
     4.种子 seed(s)

 

a = np.random.rand(3, 4, 5)
b = np.random.randn(3, 4, 5)
c = np.random.randint(100, 200, (3, 4))

np.random.seed(10)
np.random.randint(100, 200, (3 ,4))
np.random.randint(100 ,200, (3, 4))
#种子 两次的随机数都是一样的

数组变化

shuffle(a)  根据数组a的第1轴(也就是最外层的维度)进行随排列,改变数组x
permutation(a) 根据数组a的第1轴产生一个新的乱序数组,不改变数组x
choice(a[,size,replace,p]) 从一维数组a中以概率p抽取元素,形成size形状新数组replace表示是否可以重用元素,默认为False
a = np.random.randint(100, 200, (3, 4))
np.random.shuffle(a)
np.random.shuffle(a)
a 变化

b = np.random.randint(100, 200, (3, 4))
np.random.permutation(b)
b
b 没变

c = np.random.randint(100, 200, (8,))
np.random.choice(c, (3, 2)) #默认可以出现重复值
np.random.choice(c, (3, 2), replace=False) #不允许出现重复值
np.random.choice(c, (3, 2),p=c/np.sum(c)) #指定每个值出现的概率
 uniform(low, high, size)    产生具有均匀分布的数组, low起始值, high结束值, size形状
 normal(loc, scale, size)    产生具有正态分布的数组, loc均值, scale标准差, size形状
 poisson(lam, size)    产生具有泊松分布的数组, lam随机事件发生率, size形状

 u = np.random.uniform(0, 10, (3, 4))
    n = np.random.normal(10, 5, (3, 4))
    p = np.random.poisson(2.0, (3, 4))
posted @ 2019-12-25 20:00  jasmineTang  阅读(115)  评论(0)    收藏  举报