numpy.tile用法
先说下在numpy中,个人对array的维度的比较形象的理解:
array的维度就是从最外边的[]出发(可理解为array的声明),一直找到具体数值而经过的[]的数量(含最后的数值,它是最后一维)
比如:
(1) [1,2]的维度是(2,),因为最外层的[]仅仅是声明,穿过该[],直接找到数值,所以是一维的,数量是2.
(2)
[[1,2],
[3,4]] 的维度是(2,2),从最外层[]出发,向里走,有[1,2],[3,4],所以第一维的数量是2,继续往里走,遇到'1,2'和'3,4',所以第二维的数量是2(3)
[[[1,2],
[3,4]],
[[5,6],
[7,8]]]
的维度是(2,2,2),从最外层[],有两个[[]],所以第一维度是2,继续往里走有两个[],所以第二维度是2,继续走是两个数值,达到了最后一维,且最后一维的数量也是2.
import numpy as np
help(np.tile)
Help on function tile in module numpy.lib.shape_base:
tile(A, reps)
Construct an array by repeating A the number of times given by reps.
If `reps` has length ``d``, the result will have dimension of
``max(d, A.ndim)``.
If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
or shape (1, 1, 3) for 3-D replication. If this is not the desired
behavior, promote `A` to d-dimensions manually before calling this
function.
If ``A.ndim > d``, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
(1, 1, 2, 2).
Note : Although tile may be used for broadcasting, it is strongly
recommended to use numpy's broadcasting operations and functions.
Parameters
----------
A : array_like
The input array.
reps : array_like
The number of repetitions of `A` along each axis.
Returns
-------
c : ndarray
The tiled output array.
See Also
--------
repeat : Repeat elements of an array.
broadcast_to : Broadcast an array to a new shape
Examples
--------
>>> a = np.array([0, 1, 2])
>>> 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]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
[[0, 1, 2, 0, 1, 2]]])
>>> b = np.array([[1, 2], [3, 4]])
>>> np.tile(b, 2)
array([[1, 2, 1, 2],
[3, 4, 3, 4]])
>>> np.tile(b, (2, 1))
array([[1, 2],
[3, 4],
[1, 2],
[3, 4]])
>>> c = np.array([1,2,3,4])
>>> np.tile(c,(4,1))
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
np.tile(A,reps)就是根据reps的值,将array A在某些维度上进行复制,而产生新的array.
如果reps的长度等于A.ndim,则根据reps的值对A相应的维度进行复制。如果不相等,最后的结果取A.ndim和reps长度的较大值的维度;
如果A.ndim>len(reps),则先对reps前置若干个'1',使reps长度与ndim匹配,然后再进行各维度的复制;
如果A.ndim<len(reps), 则先对A的维度前置若干个'1',使两者匹配,再进行各维度复制。
- A的维度与reps长度相等:
a=np.array([1,2,3])
a.ndim
1
np.tile(a,2)# 可见,将a在自身的维度下复制2次。
array([1, 2, 3, 1, 2, 3])
b=np.array([[1,2,3],
[4,5,6]])
np.tile(b,(2,3))
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6, 4, 5, 6]])
可见,是先将最后一维在自己的维度复制3遍,然后在第一维复制2遍
- A的维度与reps长度不相等
A.ndim>len(reps)
np.tile(b,2)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
将reps扩展成与b维度一样的即,等效于np.tile(b,(1,2))!
np.tile(b,(1,2)) #在最后一维复制2遍
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
A.ndim<len(reps)
np.tile(b,(3,2,2))
array([[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]])
将A扩展成与b维度一样的,即A先升维为(1,2,3)
bb=b[np.newaxis,:]
bb
array([[[1, 2, 3],
[4, 5, 6]]])
bb.shape
(1, 2, 3)
np.tile(bb,(3,2,2))
array([[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]],
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]])
即先在最后一维复制2次,然后倒数第二维复制2次,最后第一维复制3次。
numpy.tile用法的更多相关文章
- [Python学习] python 科学计算库NumPy—tile函数
在学习knn分类算法的过程中用到了tile函数,有诸多的不理解,记录下来此函数的用法. 函数原型:numpy.tile(A,reps) #简单理解是此函数将A进行重复输出 其中A和reps都是ar ...
- numpy.tile()
numpy.tile()是个什么函数呢,说白了,就是把数组沿各个方向复制 比如 a = np.array([0,1,2]), np.tile(a,(2,1))就是把a先沿x轴(就这样称呼吧)复制 ...
- 数据分析-numpy的用法
一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...
- pytorch中的torch.repeat()函数与numpy.tile()
repeat(*sizes) → Tensor Repeats this tensor along the specified dimensions. Unlike expand(), this fu ...
- numpy数组扩展函数repeat和tile用法
numpy.repeat(a, repeats, axis=None) >>> a = np.arange(3) >>> a array([0, 1, 2]) &g ...
- numpy常用用法总结
numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...
- numpy.where() 用法详解
numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出 ...
- numpy.loadtxt用法
numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None ...
- Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法
1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引 ...
- python3 numpy基本用法归纳总结
安装numpy : pip install numpy numpy数组生成方法总结 In [4]: import numpy as np #使用列表生成一个一维数组 data = [1,2,3,4,5 ...
随机推荐
- Typecho美化之网页底部增加好久不见的底部样式
好久不见之本站同款网站底部样式,效果见本站. 1.修改footer.php首先,在页脚文件footer.php文件的最下面放入以下代码: <!-- 好久不见 --> <div cla ...
- Docker安装与镜像加速器的配置
Docker简介 百科说:Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机 ...
- Hi3516EV200 编译环境配置及交叉编译软件包
基础信息 OS: Ubuntu 16.04 xenial SDK 版本: Hi3516EV200R001C01SPC012 - Hi3516EV200_SDK_V1.0.1.1 SDK 包路径:Hi3 ...
- python xlrd 读取表格 单元格值被覆盖
代码实现顺序: 按行读取 按列读取 满足if条件 单元格值赋值给字典 实现代码: datas = []# 定义一个空列表 for i in range (3,nrows): sheet_data={} ...
- [AI/GPT/综述] AI Agent的设计模式综述
序:文由 其一,随着大模型的发展,通用智能不断迭代升级,应用模式也不断创新,从简单的Prompt应用.RAG(搜索增强生成).再到AI Agent(人工智能代理). 其中AI Agent一直是个火热的 ...
- 关于valueOf的一点思考
官方描述:返回值为该对象的原始值. 来源:Object.prototype,所以所有js对象都继承了此方法,根据犀牛书第六版的描述,对象转换为数字和字符串的时候的过程是不一样的. 对象 -> 字 ...
- 搭建docker swarm集群实现负载均衡
Swarm简介:Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源.Swarm ...
- this和super--java进阶day01
1.this和super的代表 super是父类的标识符,如堆内存中的标志 2.this和super的访问 重点说访问构造方法,super()访问父类构造方法我们已经清楚,但是this()访问本类构造 ...
- 学习unigui【19】unidbgrid的Group By This Field汉化
上面已经将group by this field 汉化. 由于版本的不断更新,不可避免有汉化遗漏.那么说到 如何汉化问题. 根据ExtJSVersion查找你电脑响应目录文件D:\Program Fi ...
- 将本地库上传到 GitHub
4.4.1 上传本地库 在 GitHub 网站上创建仓库 复制仓库地址 在 Idea 中的模块上右键 设置远程地址别名 点击 Push 推送到 GitHub 仓库 上传成功 4.4.2 正常情况下是合 ...