码迷,mamicode.com
首页 > 其他好文 > 详细

基于numpy的随机数构造

时间:2018-05-29 14:06:10      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:may   rank   python   put   osi   shape   维度   like   种子   


class numpy.random.RandomState(seed=None)
  RandomState 是一个基于Mersenne Twister算法的伪随机数生成类
  RandomState 包含很多生成 概率分布的伪随机数 的方法。

  如果指定seed值,那么每次生成的随机数都是一样的。即对于某一个伪随机数发生器,只要该种子相同,产生的随机数序列就是相同的。


numpy.random.RandomState.rand(d0, d1, ..., dn)
  Random values in a given shape.
  Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
  rand()函数产生 [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.

numpy.random.RandomState.uniform(low=0.0, high=1.0, size=None)
  Draw samples from a uniform distribution.
  Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.
  uniform()函数产生 [low,high)间的 均匀分布的指定维度的 伪随机数
  Parameters:
  low : float or array_like of floats, optional
    Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.
  high : float or array_like of floats
    Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.
  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.
    If size is None (default), a single value is returned if low and high are both scalars. Otherwise, np.broadcast(low, high).size samples are drawn.

  Returns:
    out : ndarray or scalar
      Drawn samples from the parameterized uniform distribution.

 

有时候我们需要自己模拟构造 输入数据(矩阵),那么这种随机数的生成是一种很好的方式。

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Tue May 29 12:14:11 2018
 4 
 5 @author: Frank
 6 """
 7 
 8 import numpy as np
 9 
10 #基于seed产生随机数
11 rng = np.random.RandomState(seed)
12 print(type(rng))
13 
14 #生成[0,1)间的 32行2列矩阵
15 X=rng.rand(32, 2)
16 print("X.type{}".format(type(X)))
17 print(X)
18 
19 #生成[0,1)间的 一个随机数
20 a1 = rng.rand()
21 print("a1.type{}".format(type(a1)))
22 print(a1)
23 
24 #生成[0,1)间的 一个包含两个元素的随机数组
25 a2 = rng.rand(2)
26 print("a2.type{}".format(type(a2)))
27 print(a2)
28 
29 #生成[1,2)间的随机浮点数
30 X1 = rng.uniform(1,2)
31 print("X1.type{}".format(type(X1)))
32 print(X1)
33 
34 #生成[1,2)间的随机数,一维数组且仅含1个数
35 X2 = rng.uniform(1,2,1)
36 print("X2.type{}".format(type(X2)))
37 print(X2)
38 
39 #生成[1,2)间的随机数,一维数组且仅含2个数
40 X3 = rng.uniform(1,2,2)
41 print("X3.type{}".format(type(X3)))
42 print(X3)
43 
44 #生成[1,2)间的随机数,2行3列矩阵
45 X4 = rng.uniform(1,2,(2,3))
46 print("X4.type{}".format(type(X4)))
47 print(X4)

 

基于numpy的随机数构造

标签:may   rank   python   put   osi   shape   维度   like   种子   

原文地址:https://www.cnblogs.com/black-mamba/p/9104546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!