1. 首页
  2. IT资讯

Numpy常用函数和属性(三)

“u003Cdivu003Eu003Cpu003E本篇介绍Numpy中的随机数函数。numpy中使用随机数函数需要用到random子库。u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp9.pstatp.comu002Flargeu002Fdfic-imagehandleru002F7d494cde-ea9a-4ae9-b3bb-72fa4dbb2791″ img_width=”1200″ img_height=”923″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpu003E.random.rand(d0, d1, …, dn)#返回一个随机数组,其形状由参数决定,其元素服从[0, 1)均匀分布u003Cu002Fpu003Eu003Cpreu003E>>> np.random.rand(3,2)u003Cbru003Earray([[0.20573961, 0.91404287], [0.69945553, 0.53085317], [0.05577571, 0.88107057]])u003Cbru003E>>> np.random.rand()#参数为空,则只返回一个随机数0.0161926940289171u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.randn(d0, d1, …, dn)#返回一个随机数组,其形状由参数决定,其元素服从期望为0,方差为1的高斯分布(正态分布)u003Cu002Fpu003Eu003Cpreu003E>>> np.random.randn(2,3)u003Cbru003Earray([[-0.1769781 , 0.24502692, -0.70638158], [ 0.7185526 , 0.3902619 , 0.44282054]])u003Cbru003E>>>np.random.randn()#参数为空,则只返回一个随机数u003Cbru003E1.1701375093070554u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.randint(low, high=None, size=None, dtype=’l’) #返回一个随机整数数组,其形状由size决定,服从 [`low`, `high`)的离散均匀分布u003Cu002Fpu003Eu003Cpreu003E>>> np.random.randint(10)#high=None,服从 [0,low)的离散均匀分布u003Cbru003E7u003Cbru003E>>> np.random.randint(low =10, high =20)#size 为空,则返回单个整数u003Cbru003E13u003Cbru003E>>> np.random.randint(low =10, high =20,size=(2,4))u003Cbru003Earray([[14, 19, 15, 11], [12, 18, 15, 16]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.seed(seed=None)#设置随机数种子u003Cu002Fpu003Eu003Cpreu003E>>> np.random.seed(99999)u003Cbru003E>>> np.random.randn(2,5)u003Cbru003Earray([[ 0.62409405, 1.27496309, -1.65960361, 0.50794989, -0.22091414], [ 0.08732639, -0.76979327, -0.56394527, 0.64313667, -1.85690324]])u003Cbru003E>>> np.random.randn(2,5)u003Cbru003Earray([[ 0.06574732, -0.33484632, -0.14828486, 1.38384557, 0.17160335], [ 0.9140183 , -0.54034406, -1.12732828, -2.24561307, -0.27743505]])u003Cbru003E>>> np.random.seed(99999)#同样的种子可以重复生成随机数组u003Cbru003E>>> np.random.randn(2,5)u003Cbru003Earray([[ 0.62409405, 1.27496309, -1.65960361, 0.50794989, -0.22091414], [ 0.08732639, -0.76979327, -0.56394527, 0.64313667, -1.85690324]])u003Cbru003E>>> np.random.randn(2,5)u003Cbru003Earray([[ 0.06574732, -0.33484632, -0.14828486, 1.38384557, 0.17160335], [ 0.9140183 , -0.54034406, -1.12732828, -2.24561307, -0.27743505]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.choice(a, size=None, replace=True, p=None)#从一维数组a中以概率p抽取元素,形成size形状新数组,replace表示是否可以重用元素,默认为False。u003Cu002Fpu003Eu003Cpreu003E>> aa_milne_arr = [‘pooh’, ‘rabbit’, ‘piglet’, ‘Christopher’]u003Cbru003E>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])u003Cbru003Earray([‘pooh’, ‘pooh’, ‘pooh’, ‘Christopher’, ‘piglet’], dtype=’|S11′)u003Cbru003E>>> np.random.choice(5,3)#若a为单个整数,等效于np.arange(a)u003Cbru003Earray([0, 3, 4])u003Cbru003E>>> np.random.choice(5,(2,3))#p为空,则均匀分布抽样u003Cbru003Earray([[2, 4, 1], [0, 3, 3]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.shuffle(a)#根据数组a的第0轴进行随机排列,打乱并改变数组a ,无返回值u003Cu002Fpu003Eu003Cpreu003E>>> arr = np.arange(10)u003Cbru003E>>> np.random.shuffle(arru003Cbru003E)>>> arr[1 7 5 2 9 4 3 6 0 8]u003Cbru003E>>> arr = np.arange(9).reshape((3, 3))u003Cbru003E>>> arrarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])u003Cbru003E>>> np.random.shuffle(arr)u003Cbru003E>>> arrarray([[3, 4, 5], [6, 7, 8], [0, 1, 2]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.permutation(a) #根据数组a的第0轴进行随机排列, 不改变a,返回一个新的数组u003Cu002Fpu003Eu003Cpreu003E>>> np.random.permutation([1, 4, 9, 12, 15])u003Cbru003E array([15, 1, 9, 4, 12])u003Cbru003E>>> arr = np.arange(9).reshape((3, 3))u003Cbru003E>>> np.random.permutation(arr)u003Cbru003Earray([[6, 7, 8], [0, 1, 2], [3, 4, 5]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.uniform(low=0.0,high=1.0, size=None)#返回服从[low, high)均匀分布的数组。u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002F937de7bf9ef049cb9bf7d64e6cc74e9a” img_width=”174″ img_height=”93″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> np.random.uniform(-2,2,10)u003Cbru003Earray([-1.0108675 , 1.2139926 , -0.46648179, 1.48363953, -1.00934312, 1.50305119, -0.53430239, 1.01847205, 1.45830054, 0.05367493])u003Cbru003E>>> np.random.uniform(10,11)#不指定size,则返回单个随机数10.66833012275534u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.normal(mu=0.0, sigma=1.0, size=None)#返回服从期望为0,标准差为1的高斯分布的随机数组。u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002F67a6cda11198470ba329a280da2cd033″ img_width=”245″ img_height=”74″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cp class=”ql-align-center”u003Eu003Cbru003Eu003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002F9b48baebfc2b4ddea079f08e1af73369″ img_width=”200″ img_height=”170″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> mu, sigma = 0, 0.1 # mean and standard deviationu003Cbru003E>>> np.random.normal(mu, sigma, (3,2))u003Cbru003Earray([[ 0.01940839, 0.12807021], [ 0.12198445, -0.02773643], [-0.0250715 , -0.08589577]])u003Cbru003E>>> np.random.normal(mu, sigma, 10)u003Cbru003Earray([-0.04020967, 0.04701897, -0.08604024, -0.17741818, 0.00268857, 0.06352545, 0.15986825, 0.01114141, -0.1614628 , 0.09045275])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.poisson(lam=1.0, size=None)#产生泊松分布的数组, lam随机事件发生概率,size为形状 。u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002F5444e04b138c4e99b33c79b9d9936204″ img_width=”221″ img_height=”59″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> np.random.poisson(5, 10)u003Cbru003Earray([4, 3, 9, 6, 5, 2, 8, 4, 8, 7])u003Cbru003E>>> np.random.poisson(lam=(3., 500.), size=(5, 2))u003Cbru003Earray([[ 2, 519], [ 2, 487], [ 3, 528], [ 4, 465], [ 1, 479]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.binomial(n, p, size=None)#产生二项分布的数组,n为试验次数,p为概率。u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002F6db174e2440c4057b96e6ea8488aa237″ img_width=”270″ img_height=”95″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> n, p = 10, .5 # 对称抛对称硬币10次,单次正面的概率u003Cbru003E0.5u003Cbru003E>>> np.random.binomial(n, p, 9)u003Cbru003Earray([6, 5, 5, 6, 6, 4, 3, 7, 6])#抛10次中出现正面的次数u003Cbru003E>>> np.random.binomial(n, p, (3,4))u003Cbru003Earray([[6, 4, 7, 7], [2, 4, 5, 4], [5, 7, 4, 7]])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.pareto(a, size=None)#帕累托分布(Pareto II or Lomax distribution)u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002F0c309a2dcbfd43ad8f4cb85a9965083a” img_width=”193″ img_height=”72″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> np.random.pareto(3.0, 10)u003Cbru003Earray([0.27584436, 0.20097631, 0.07832075, 0.60535648, 0.17822113, 0.29401011, 0.10952842, 0.87455492, 0.49542338, 0.49259947])u003Cbru003Eu003Cu002Fpreu003Eu003Cpu003E.random.weibull(a, size=None)#韦伯分布u003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002F05c2e0de68174d389e6c6ca28230feac” img_width=”277″ img_height=”73″ alt=”Numpy常用函数和属性(三)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cpreu003E>>> np.random.weibull(5.0, (2,10))u003Cbru003Earray([[0.83913416, 0.54670166, 1.20978437, 1.12294924, 1.04715573, 0.8790425 , 1.17878101, 1.13276002, 0.76166397, 0.85192145], [0.84105884, 0.86277061, 0.98130684, 1.15309877, 0.80517508, 0.61821871, 0.77178803, 0.96368598, 1.20141363, 0.86801079]])u003Cbru003Eu003Cu002Fpreu003Eu003Cu002Fdivu003E”

原文始发于:Numpy常用函数和属性(三)

主题测试文章,只做测试使用。发布者:玩家L-,转转请注明出处:http://www.cxybcw.com/10602.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code