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加上心知天气-网页天气插件
给你的博客添加个知心天气的天气预报,代码看下面 <!-- 知心天气--> <div id="tp-weather-widget" class="navb ...
- MySQL - [20] 事务
题记部分 一.什么是ACID (1)Atomicity 原子性 某个操作,要么全部执行完毕,要么全部回滚. (2)Consistency 一致性 数据库中的数据全都符合现实世界中的约束,则这些数据就符 ...
- 浅谈Tox之一
本文分享自天翼云开发者社区<浅谈Tox之一>,作者:Moonriver What is tox? tox是通用的virtualenv管理和测试命令行工具,可用于: 使用不同的Python版 ...
- 全源最短路——Johnson 算法
一.问题引入 目前我们所知道的一些常见的最短路算法有 dijkstra.spfa.floyd. dijkstra 和 spfa 是单源最短路,floyd 是多源最短路. 如果我们需要在 \(O(nm) ...
- 2个月搞定计算机二级C语言——真题(8)解析
1. 前言 本篇我们讲解2个月搞定计算机二级C语言--真题8 2. 程序填空题 2.1 题目要求 2.2 提供的代码 #include <stdio.h> #define N 3 #def ...
- Oracle存储过程里操作BLOB的字节数据的办法
一.缘由 BLOB是指二进制大对象,也就是英文Binary Large Object的缩写. 在很多时候,我们是通过其他编程语言(如Java)访问BLOB的字节数据,进行字节级的操作的. 但是有些时候 ...
- Liunx配置sudo使oracle用户有root权限执行脚本
1. vi /etc/sudoers 将%wheel 两行前的注释# 删除 2. vi /etc/group 将oracle用户 加入 wheel组
- SOA架构和微服务架构的区别
1.SOA架构和微服务架构的区别 首先SOA和微服务架构一个层面的东西,而对于ESB和微服务网关是一个层面的东西,一个谈到是架构风格和方法,一个谈的是实现工具或组件. 1.SOA(Service Or ...
- 解决VuePress中的”Error from chokidar : Error: EBUSY“问题
.title { padding: 10px; background-color: rgba(3, 169, 244, 1); font-size: 16px; color: rgba(255, 25 ...
- Git Bash 无法输入中文
场景重现 有个小伙伴的电脑上 Git Bash 里死活无法输入中文, 导致 git 提交信息没法用中文写... git commit -m "无法输入中文" 解决办法 在 Git ...