import numpy as np
import random
import random
'''
numpy.random.rand(d0,d1,…,dn)
rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
'''
data=np.random.rand(3,2)
print "ttt:\n %s" % data
numpy.random.rand(d0,d1,…,dn)
rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
'''
data=np.random.rand(3,2)
print "ttt:\n %s" % data
'''
numpy.random.randint(low, high=None, size=None, dtype=’l’)
返回随机整数,范围半开区间为[low,high),包含low,不包含high
numpy.random.random_integers(low, high=None, size=None)
返回随机整数,范围闭区间为[low,high],包含low和high
'''
data1=np.random.random_integers(10,size=(2,2))
print "the random_integers is:\n %s" % data1
返回随机整数,范围闭区间为[low,high],包含low和high
'''
data1=np.random.random_integers(10,size=(2,2))
print "the random_integers is:\n %s" % data1
'''
生成[0,1)之间的浮点数
numpy.random.random_sample(size=None)
numpy.random.random(size=None)
numpy.random.ranf(size=None)
numpy.random.sample(size=None)
'''
data2=np.random.random_sample(size=(2,2))
print "the random_sample is:\n %s" % data2
'''
numpy.random.choice(a, size=None, replace=True, p=None)
从给定的一维数组中生成随机数
参数: a为一维数组类似数据或整数;size为数组维度;p为数组中的数据出现的概率
a为整数时,对应的一维数组为np.arange(a)
'''
data3=np.random.choice(10,3)
print "the choice1 is:\n %s" % data3
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
data4=np.random.choice(demo_list,size=(2,2))
print "the choice2 is:\n %s" % data4
data4=np.random.choice(demo_list,size=(2,2))
print "the choice2 is:\n %s" % data4
'''
排序:
shuffle(x)现场修改序列,改变自身内容。(类似洗牌,打乱顺序)
permutation(x)返回一个随机排列
'''
arr1=np.arange(10)
print "the arr1 is:\n %s" % arr1
np.random.shuffle(arr1)
print "after shuffle the arr1 is:\n %s" % arr1
arr2 = np.arange(9).reshape((3, 3))
print "the arr2 is:\n %s" % arr2
np.random.permutation(arr2)
print "after shuffle the arr2 is:\n %s" % arr2
print "the arr2 is:\n %s" % arr2
np.random.permutation(arr2)
print "after shuffle the arr2 is:\n %s" % arr2
'''
随机数生成器
np.random.seed()的作用:使得随机数据可预测。
当我们设置相同的seed,每次生成的随机数相同。
如果不设置seed,则每次会生成不同的随机数
'''
随机数生成器
np.random.seed()的作用:使得随机数据可预测。
当我们设置相同的seed,每次生成的随机数相同。
如果不设置seed,则每次会生成不同的随机数
'''
data5=np.random.seed(0);np.random.rand(5)
print "the same seed data5 is:\n %s" % data5
data6=np.random.seed(0);np.random.rand(5)
print "the same seed data6 is:\n %s" % data6
data7=np.random.rand(5)
print "the not same seed data7 is:\n %s" % data7
'''
分布
randn( )返回一个具有标准正态分布样本
normal()返回一个高斯(正态)分布样本
binomial()返回一个二项分布样本
'''