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]]
一定要支持作者哦