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 ...
随机推荐
- Github Copilot的使用方法和快捷键
GitHub Copilot是一款由GitHub和OpenAI共同开发的代码智能补全工具,它使用机器学习模型来为你提供代码建议和自动完成,可以加快开发过程并提高代码质量.下面是使用GitHub Cop ...
- LCP 11. 期望个数统计
地址:https://leetcode-cn.com/problems/qi-wang-ge-shu-tong-ji/ <?php /** 某互联网公司一年一度的春招开始了,一共有 n 名面试者 ...
- vue练习用免费开源api大全
1. 网易云api 网易云api是网上一位大神工具网易云获取的,数据都是真实的网易云数据 2. api大全 这是csdn一个兄弟收集的,种类挺多,就是有一些需要money,不过大部分还是免费的 3. ...
- 腾讯元宝登顶App Store免费榜榜首!国产AI APP混战升级
最近,国产AI APP市场热闹非凡,竞争愈发激烈.其中,腾讯元宝的表现格外亮眼,它在短短一周内,借助微信的强大助力,一路逆袭,成功登上苹果App Store免费榜榜首.这一变化不仅展现了腾讯在AI领域 ...
- 文件上传fuzz工具-Upload_Auto_Fuzz
一.工具介绍 在日常遇到文件上传时,如果一个个去测,会消耗很多时间,如果利用工具去跑的话就会节省很多时间,本Burp Suite插件专为文件上传漏洞检测设计,提供自动化Fuzz测试,共300+条p ...
- PHP的回调函数
所谓的回调函数,就是指调用函数时并不是向函数中传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中,这个作为参数的函数就是回调函数.通俗的来说,回调函数也是一个我们定义的函数,但是不 ...
- docker搭建本地仓库
环境准备: 服务器:9.134.130.35 私有仓库服务器,运行registry容器 客户端:9.208.244.175 测试客户端,用于上传.下载镜像文件 测试搭建本地仓库 mkdir /dock ...
- AI Agent爆火后,MCP协议为什么如此重要!
什么是MCP? 模型上下文协议(Model Context Protocol, MCP)是一种专为机器学习模型服务设计的通信协议,旨在高效管理模型推理过程中的上下文信息(如会话状态.环境变量.动态配置 ...
- 常见的 AI 模型格式
来源:博客链接 过去两年,开源 AI 社区一直在热烈讨论新 AI 模型的开发.每天都有越来越多的模型在 Hugging Face 上发布,并被用于实际应用中.然而,开发者在使用这些模型时面临的一个挑战 ...
- 阅读IDEA生成的equals方法--java进阶day05
1.IDEA生成的equals方法 虽然我们之前写了equals方法,但IDEA中可以快速生成equals方法,因此,我们要能看懂IDEA生成的equals方法 1.if(this==o) 2.if( ...