Numpy 函数总结 (不断更新)
2019-01-20 05:46
926 查看
本篇主要收集一些平时见到的 Numpy 函数。
numpy.random.seed & numpy.random.RandomState
np.random.seed()和
np.random.RandomState都用于生成随机数种子,
np.random.seed()是可以直接调用的方法,而
np.random.RandomState则是一个产生随机数的容器,使用时需要创建实例对象,进而调用实例方法,如
np.random.RandomState(42).uniform()。
随机数种子
seed只有一次有效,在下一次调用产生随机数函数前没有设置
seed,则还是产生随机数。如果需要每次都产生随机数,则可以将随机数
seed设置成
None,或者不设置。
>>> import numpy as np >>> np.random.seed(42) >>> np.random.randint(1, 10, 5) # array([5, 1, 2, 6, 1]) >>> np.random.seed(42) >>> np.random.randint(1, 10, 5) # array([5, 1, 2, 6, 1]) >>> np.random.randint(1, 10, 5) # array([8, 8, 3, 6, 5])
>>> from numpy.random import RandomState >>> r = RandomState(42) >>> r.randint(1, 10, 5) # array([9, 9, 7, 3, 9]) >>> r = RandomState(42) >>> r.randint(1, 10, 5) # array([9, 9, 7, 3, 9]) >>> r = RandomState(None) >>> r.randint(1, 10, 5) # array([8, 3, 2, 6, 5])
>>> import random # 使用Python的Random模块 >>> random.seed(42) >>> random.sample(range(10), 5) # [1, 0, 4, 9, 6] >>> random.sample(range(10), 5) # [6, 9, 1, 4, 5]
numpy.tile
numpy.tile(A, n)用于将一整个数组 A 重复 n 次。 下面是一个简单的例子:
>>> a = [1,2,3,4] >>> np.tile(a, 3) # array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
然而如果 n 的长度大于 1,则情况就略复杂了。下面看个例子:
>>> a = np.array([1,2,3]) >>> np.tile(a, (3, 3)) array([[1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3]])
上面的原始数组 a 为一维,n 的长度为 2,则 tile 函数会将原来的一维拓展为 2 维,再在每一维上重复相应的数组,相当于下面两步:
>>> a = np.array([1,2,3]) >>> a = np.expand_dims(a, axis=0) # a 为 array([[1, 2, 3]]) >>> np.tile(a, (3, 3))
上面的情况是 n 的长度大于 a 的维度,另一种情况是 n 的长度小于 a 的维度:
>>> b = np.array([[1,2,3], [4,5,6]]) >>> np.tile(b, 2) array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]])
上面的情况是 b 的维度为 2,n 的长度为1,则同样 n 会被扩展为 2,不足的维度用 1 填充,即变成 (1, 2),所以上例中 b 的第一维没有被复制,被复制的是第二维。最后按惯例是一个复杂点的例子:
>>> c = np.arange(27).reshape((3,3,3)) array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) >>> np.tile(c, (2,2,2)) array([[[ 0, 1, 2, 0, 1, 2], [ 3, 4, 5, 3, 4, 5], [ 6, 7, 8, 6, 7, 8], [ 0, 1, 2, 0, 1, 2], [ 3, 4, 5, 3, 4, 5], [ 6, 7, 8, 6, 7, 8]], [[ 9, 10, 11, 9, 10, 11], [12, 13, 14, 12, 13, 14], [15, 16, 17, 15, 16, 17], [ 9, 10, 11, 9, 10, 11], [12, 13, 14, 12, 13, 14], [15, 16, 17, 15, 16, 17]], [[18, 19, 20, 18, 19, 20], [21, 22, 23, 21, 22, 23], [24, 25, 26, 24, 25, 26], [18, 19, 20, 18, 19, 20], [21, 22, 23, 21, 22, 23], [24, 25, 26, 24, 25, 26]], [[ 0, 1, 2, 0, 1, 2], [ 3, 4, 5, 3, 4, 5], [ 6, 7, 8, 6, 7, 8], [ 0, 1, 2, 0, 1, 2], [ 3, 4, 5, 3, 4, 5], [ 6, 7, 8, 6, 7, 8]], [[ 9, 10, 11, 9, 10, 11], [12, 13, 14, 12, 13, 14], [15, 16, 17, 15, 16, 17], [ 9, 10, 11, 9, 10, 11], [12, 13, 14, 12, 13, 14], [15, 16, 17, 15, 16, 17]], [[18, 19, 20, 18, 19, 20], [21, 22, 23, 21, 22, 23], [24, 25, 26, 24, 25, 26], [18, 19, 20, 18, 19, 20], [21, 22, 23, 21, 22, 23], [24, 25, 26, 24, 25, 26]]])
最后出来的结果其实非常具有对称的美感。
另外与
numpy.tile()有密切联系的函数为
numpy.repeat(),其功能是对应元素重复:
>>> np.repeat(13, 5) # array([13, 13, 13, 13, 13])
numpy.repeat()可以制定要重复的轴 (axis),但如果不指定,则将原数组拉伸为 1 维数组后再对应元素重复:
>>> a = np.array([[1,2], [3,4]]) >>> np.repeat(a, 3) # array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]) >>> np.repeat(a, 3, axis=1) array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]])
/
相关文章推荐
- 一些头文件中常用的函数做题总结,不断更新
- python常见函数修饰符总结(不断更新)
- 水晶报表常用函数总结(不断更新中.......)
- C和C++语言编程里面常用函数或者编程技巧总结(不断更新)
- numpy库常用函数记录(不断更新)
- 函数的用法(不断更新,总结)
- STM32 I2C总线 自我总结学习 不断更新中
- (不断更新)总结cocos2d-x在安卓上各种崩溃原因
- php 手工函数收集,小函数收集 不断更新中。。。。
- 工作中遇到的svn 命令总结----不断更新
- 总结点mysql的相关 函数 不持续更新~~
- ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)
- ubuntu基本使用总结(不断更新中。。。
- STM32 485通信 自我学习总结 控制380V变频器去控制380V电机 不断更新中
- mysql学习经验与总结不断更新中
- Android编程规范-学习方法-心得总结(不断更新)
- 【LaTeX】Yap的使用总结-不断更新
- struts2 错误总结 自己会不断更新中。。。。。
- 面试笔试题刷题总结 不断更新中。。。。。。
- iphone程序崩溃原因总结(不断更新中。。。)