搜索
写经验 领红包
 > 科技

umpy数学函数(umpy主要函数的使用方法)

导语:深度学习入门系列-Numpy函数精讲-numpy.repeat

每天一个numpy函数

numpy.repeat(a, repeats, axis=None)

函数功能:通过按照指定的重复次数和重复的坐标值重复复制array中的元素来扩展ndarray。

函数参数:

a: 输入array。

repeats:单个int或者ints构成的列表。一个数字则表示都遵循相同的重复次数,多个数字则指定对应元素的复制次数。

eg 1. repeat=1 实现ndarray 的flatten

>>> import numpy as np>>> x = np.array([[1,2], [3,4]])>>> np.repeat(x, 1)array([1, 2, 3, 4])

eg 2. repeat != 1和ints数组情形下 axis=0 针对nd array进行重复

>>> import numpy as np>>> x = np.array([[1,2], [3,4]])>>> np.repeat(x, 2, axis = 0)array([[1, 2],       [1, 2],       [3, 4],       [3, 4]])>>> np.repeat(x, (3,4), axis = 0)array([[1, 2],       [1, 2],       [1, 2],       [3, 4],       [3, 4],       [3, 4],       [3, 4]])

eg 3. axis == 1

>>> x = np.array([[1,2],[3,4]])>>> np.repeat(x, 3, axis=1)array([[1, 1, 1, 2, 2, 2],       [3, 3, 3, 4, 4, 4]])

eg 4. repeat为int列表,按照索引和axis指定对应元素复制列表

>>> import numpy as np>>> x = np.array([[1,2], [3,4]])>>> np.repeat(x, (3,4), axis = 0)array([[1, 2],       [1, 2],       [1, 2],       [3, 4],       [3, 4],       [3, 4],       [3, 4]])

axis:int, 可选。指定元素复制所在的维度,会导致该维的dim发生变化。

axis=None时会flatten当前输入(将输入ndarray转换为一个行向量),然后按指定次数进行复制。
>>> import numpy as np>>> x = np.array([[1,2], [3,4]])>>> np.repeat(x, 2)array([1, 1, 2, 2, 3, 3, 4, 4])
axis不为Node的示例可以参考repeat参数说明中给出的例子。

返回

重复复制之后的数组,除了指定的axis处维度,返回ndarray的其他维度保持不变。

免责声明:本站部份内容由优秀作者和原创用户编辑投稿,本站仅提供存储服务,不拥有所有权,不承担法律责任。若涉嫌侵权/违法的,请与我联系,一经查实立刻删除内容。本文内容由快快网络小荣创作整理编辑!