# 【3】数据分析-1-数据的处理--numpy--2--随机数random

## 一、NumPy的random子库

np.random.*
np.random.rand()
np.random.randn()
np.random.randint()

rand(d0,d1,..,dn) 根据d0‐dn创建随机数数组，浮点数，[0,1)，均匀分布
randn(d0,d1,..,dn) 根据d0‐dn创建随机数数组，标准正态分布
randint(low[,high,shape] 据shape创建随机整数或整数数组，范围是[low, high)
seed(s) 随机数种子，s是给定的种子值

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,5))
>>> a
array([[152, 180, 139, 184, 123],
[187, 199, 160, 187, 184],
[127, 182, 131, 123, 161]])


n_genes = 3
n_drugs =5
print np.random.randn(n_genes, n_drugs)

[[ 0.0395099   0.33837761 -0.84218319 -0.04963228 -1.23024536]
[-0.98879267  0.60207459 -0.1595486  -1.28334796 -1.23822008]
[ 0.7172287  -0.48890867  1.6726215   0.54363772  0.44157299]]


# Random numbers between [0,1) of shape 2,2
print(np.random.rand(2,2))

# Normal distribution with mean=0 and variance=1 of shape 2,2
print(np.random.randn(2,2))

# Random integers between [0, 10) of shape 2,2
print(np.random.randint(0, 10, size=[2,2]))

# One random number between [0,1)
print(np.random.random())

# Random numbers between [0,1) of shape 2,2
print(np.random.random(size=[2,2]))

# Pick 10 items from a given list, with equal probability
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10))

# Pick 10 items from a given list with a predefined probability 'p'
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10, p=[0.3, .1, 0.1, 0.4, 0.1]))  # picks more o's

#> [[ 0.84  0.7 ]
#>  [ 0.52  0.8 ]]

#> [[-0.06 -1.55]
#>  [ 0.47 -0.04]]

#> [[4 0]
#>  [8 7]]

#> 0.08737272424956832

#> [[ 0.45  0.78]
#>  [ 0.03  0.74]]

#> ['i' 'a' 'e' 'e' 'a' 'u' 'o' 'e' 'i' 'u']
#> ['o' 'a' 'e' 'a' 'a' 'o' 'o' 'o' 'a' 'o']


# Create the random state
rn = np.random.RandomState(100)

# Create random numbers between [0,1) of shape 2,2
print(rn.rand(2,2))

#> [[ 0.54  0.28]
#>  [ 0.42  0.84]]
# Set the random seed
np.random.seed(100)

# Create random numbers between [0,1) of shape 2,2
print(np.random.rand(2,2))

#> [[ 0.54  0.28]
#>  [ 0.42  0.84]]


uniform(low,high,size)

normal(loc,scale,size)

poisson(lam,size)

## 二、permutation

numpy.random.permutation(length)用来产生一个随机序列作为索引，再使用这个序列从原来的数据集中按照新的随机顺序产生随机数据集。

indices = numpy.random.permutation(data_x.shape[0]) # shape[0]表示第0轴的长度，通常是训练数据的数量
rand_data_x = data_x[indices]
rand_data_y = data_y[indices] # data_y就是标记（label）


a = np.arange(12)
print a
np.random.shuffle(a)
print a
print
a = np.arange(12)
print a
b = np.random.permutation(a)
print b
print a


[ 0  1  2  3  4  5  6  7  8  9 10 11]
[11  6  4 10  3  0  7  1  9  2  5  8]

[ 0  1  2  3  4  5  6  7  8  9 10 11]
[10  4  8 11  1  7  6  2  0  9  5  3]
[ 0  1  2  3  4  5  6  7  8  9 10 11]