>> import numpy as np
>> help(np.repeat)
>> help(np.tile)
  • 二者执行的是均是复制操作;
  • np.repeat:复制的是多维数组的每一个元素
  • np.tile:复制的是多维数组本身

1. np.repeat

>> x = np.arange(1, 5).reshape(2, 2)
>> np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])
# 对数组中的每一个元素进行复制
# 除了待重复的数组之外,只有一个额外的参数时,高维数组也会 flatten 至一维

当然将高维 flatten 至一维,并非经常使用的操作,也即更经常地我们在某一轴上进行复制,比如在行的方向上(axis=1),在列的方向上(axis=0):

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

当然更为灵活地也可以在某一轴的方向上(axis=0/1),对不同的行/列复制不同的次数:

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

2. np.tile

Python numpy 下的 np.tile有些类似于 matlab 中的 repmat函数。不需要 axis 关键字参数,仅通过第二个参数便可指定在各个轴上的复制倍数。

>> a = np.arange(3)
>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]]) >> b = np.arange(1, 5).reshape(2, 2)
>> np.tile(b, 2)
array([[1, 2, 1, 2],
[3, 4, 3, 4]]) # 等价于
>> np.tile(b, (1, 2))

numpy 辨异(四)—— np.repeat 与 np.tile的更多相关文章

  1. np.tile(), np.repeat() 和 tf.tile()

    以上三个函数,主要区别在于能够拓展维度上和重复方式: np.tile() 能够拓展维度,并且整体重复: a = np.array([0,1,2]) np.tile(a,(2,2)) # out # a ...

  2. numpy 辨异(三)—— hstack/column_stack,linalg.eig/linalg.eigh

    1. np.hstack np.column_stack >>> np.hstack([np.array([1, 2, 3]), np.array([4, 5, 6])]) arra ...

  3. numpy 辨异(二) —— np.identity()/np.eye()

    import numpy as np; 两者在创建单位矩阵上,并无区别,两者的区别主要在接口上: np.identity(n, dtype=None):只能获取方阵,也即标准意义的单位阵: np.ey ...

  4. np.repeat 与 np.tile

    1.Numpy的 tile() 函数,就是将原矩阵横向.纵向地复制.tile 是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来. 举个例子,原矩阵: import numpy as np ...

  5. np.repeat函数

    np.repeat用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me np.repeat用于将numpy数组重复 一维数组重复三次 import numpy as np # 随机生成[0,5 ...

  6. np.repeat()

    np.repeat()用于将numpy数组重复. numpy.repeat(a, repeats, axis=None); 参数: axis=0,沿着y轴复制,实际上增加了行数axis=1,沿着x轴复 ...

  7. numpy中np.c_和np.r_

    np.r_:按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat() np.c_:按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的mer ...

  8. numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)

    1. np.asarray -- numpy 风格的类型转换 从已有多维数组创建新的多维数组,数据类型可重新设置 >> B = np.asarray(A, dtype='int32') 2 ...

  9. numpy中np.array()与np.asarray的区别以及.tolist

    array 和 asarray 都可以将 结构数据 转化为 ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会. 1.输 ...

随机推荐

  1. easyui datagrid editor checkbox 单击事件

    Easyui datagrid treegrid中能够为行追加checkbox元素.比如: $('#tt').treegrid({ url:'get_data.php', idField:'id', ...

  2. Windows服务安装命令:

    sc create YY.SmsPlatform.RemoteDataCenter binPath= "E:\YY.SmsPlatform\YY.SmsPlatform.RemoteData ...

  3. flask的使用(一)

    1.程序基本的说明 #-*-encoding=utf--*- 从flask中引入类 from flask import Flask ,render_template import config 初始化 ...

  4. ivotal-tc-Server与Tomcat区别

    Pivotal-tc-Server之前叫做SpringSource tc Server,包含三个版本分别是:Spring版.标准版和开发版,但其中只有开发版是免费的.比如在STS中包含的版本就是开发板 ...

  5. Hibernate与代理模式

    代理模式:当须要调用某个对象的时候.不须要关心拿到的是不是一定是这个对象,它须要的是,我拿到的这个对象能够完毕我想要让它完毕的任务就可以,也就是说.这时调用方能够拿到一个代理的一个对象,这个对象能够调 ...

  6. SDL2源码分析1:初始化(SDL_Init())

    ===================================================== SDL源码分析系列文章列表: SDL2源码分析1:初始化(SDL_Init()) SDL2源 ...

  7. Spring boot(三) springboot 定时任务

    这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...

  8. JS生成一个种子随机数(伪随机数)

    原文链接:https://geniuspeng.github.io/2016/09/12/js-random/ 最近有一个需求,需要生成一个随机数,但是又不能完全随机,就是说需要一个种子seed,se ...

  9. [AngularFire 2] Protect Write Access Using Security Rules

    We cannot allow un-auth user to change the database data as they want, for Firebase, it is easy just ...

  10. php 过滤js字符串代码

    $str = '<script type="text/javascript" src="dd.js"></script> 测试php正则 ...