python之numpy模块random,切片,复制,广播机制

一篇优秀的文章——资源无处不在https://www.cnblogs.com/chenhuabin/p/11412818.html

完整的学习文档https://www.osgeo.cn/numpy/user/quickstart.html

1、np.random.random()系列函数:

(1) np.random.rand():随机生成均匀分布的[0,1]的随机小数

(2) np.random.randint()、np.random.ranom.random_integers():生成均匀分布的整数。根据所传的参数,前者范围为[start,end),后者为[start,end]

(3) np.random.random()、np.random.sanmple() 、np.random.random_sanmple() 、np.random.randf():生成随机浮点数,取值范围:[0,1)

(4) np.random.randn():随机生成服从正态分布~N(0,1)的浮点数

2、numpy的字符串切片,非常清晰明了的图

在这里插入图片描述

3、浅复制copy与==的区别

"==",联动

>>> a = np.arange(12)
>>> b = a # 赋值
>>> a is b
True
>>> b.shape = (3, 4)
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

copy,岿然不动

>>> a = np.arange(12)
>>> b = a.view() # 使用视图
>>> a is b
False
>>> b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b.shape = (3, 4) # 改变b的形状
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> b[0] = 0
>>> a
array([ 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b
array([[ 0, 0, 0, 0],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

4、广播机制,当两个矩阵shape不一,但要进行运算时

在这里插入图片描述

发布了70 篇原创文章 · 获赞 5 · 访问量 3516

猜你喜欢

转载自blog.csdn.net/qq_42647903/article/details/103137218