numpy创建对象

本文介绍了使用numpy库进行随机数生成的方法,包括random.rand()、random.random()、random.randint()、random.randn()和random.normal(),以及如何创建不同维度的随机数组。
摘要由CSDN通过智能技术生成

numpy学习专题

三、numpy创建对象

random.rand()函数 – 生成0~1的随机数

import numpy as np
rd1 = np.random.rand(6,4)
rd2 =np.random.rand()
print(rd2)
print(type(rd2),"\n")
print(rd1)
print(type(rd1))
0.2992310709818873
<class 'float'> 

[[0.86400755 0.15811693 0.94232414 0.50700351]
 [0.14690234 0.88496558 0.65528594 0.83304129]
 [0.04466805 0.36860662 0.68956199 0.91384106]
 [0.68781598 0.06055178 0.91753934 0.14015356]
 [0.58244098 0.51184351 0.13765617 0.37988842]
 [0.34098886 0.38982788 0.32005162 0.59432023]]
<class 'numpy.ndarray'>
import numpy as np
rd1 = np.random.rand(2,4,3)
print(rd1)
print(type(rd1))
[[[0.33431815 0.51472196 0.78083909]
  [0.36694782 0.72296216 0.91703453]
  [0.54013307 0.55117039 0.83127443]
  [0.07533477 0.81357117 0.64013274]]

 [[0.26799894 0.70914831 0.84395563]
  [0.03882194 0.31556394 0.34240176]
  [0.28173342 0.40646934 0.18385237]
  [0.63842974 0.96426054 0.32739565]]]
<class 'numpy.ndarray'>

random.random()函数 – 生成0~1的随机数

与rand函数的区别在于 random函数接受的是元组,而rand函数接受的是多个参数
import numpy as np
arr = np.random.random((2,3))
print(arr,type(arr))
[[0.31124393 0.99102652 0.16446841]
 [0.82123294 0.80343202 0.33497687]] <class 'numpy.ndarray'>

random.randint()函数 --可以设置随机数的范围,但是数据中的随机数都是整数

import numpy as np
arr = np.random.randint(10,100,(2,3))
print(arr)
[[49 96 80]
 [67 41 76]]

random.randn()函数 – 返回一组样本,具有标准的正态分布

import numpy as np
arr = np.random.randn(2,2,2)
print(arr)
[[[ 0.64777235  0.66552344]
  [-0.31685235 -0.15115967]]

 [[-0.53005372 -1.2542614 ]
  [-0.59816852 -0.7173953 ]]]

random.normal()函数 – 生成高斯分布的概率密度随机数

import numpy as np
arr =np.random.normal(loc=2,scale =1,size=(3,4))
print(arr)
[[0.61483494 1.32854497 1.73986031 3.30046067]
 [1.77460589 1.64246624 1.85669721 2.04221075]
 [1.97510797 2.50794697 2.56113673 2.70891053]]

一定要支持作者哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值