先说下在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用法的更多相关文章

  1. [Python学习] python 科学计算库NumPy—tile函数

    在学习knn分类算法的过程中用到了tile函数,有诸多的不理解,记录下来此函数的用法.   函数原型:numpy.tile(A,reps) #简单理解是此函数将A进行重复输出 其中A和reps都是ar ...

  2. numpy.tile()

    numpy.tile()是个什么函数呢,说白了,就是把数组沿各个方向复制 比如 a = np.array([0,1,2]),    np.tile(a,(2,1))就是把a先沿x轴(就这样称呼吧)复制 ...

  3. 数据分析-numpy的用法

    一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...

  4. pytorch中的torch.repeat()函数与numpy.tile()

    repeat(*sizes) → Tensor Repeats this tensor along the specified dimensions. Unlike expand(), this fu ...

  5. numpy数组扩展函数repeat和tile用法

    numpy.repeat(a, repeats, axis=None) >>> a = np.arange(3) >>> a array([0, 1, 2]) &g ...

  6. numpy常用用法总结

    numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...

  7. numpy.where() 用法详解

    numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出 ...

  8. numpy.loadtxt用法

    numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None ...

  9. Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法

    1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引 ...

  10. python3 numpy基本用法归纳总结

    安装numpy : pip install numpy numpy数组生成方法总结 In [4]: import numpy as np #使用列表生成一个一维数组 data = [1,2,3,4,5 ...

随机推荐

  1. Java 实现 Excel(XLS/ XLSX)和 HTML 格式之间的转换

    Excel 是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言.虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,以便更好地利用和展示数据.本文 ...

  2. 用Logseq记日报和管理文献

    优缺点浅评 Logseq是一款双链笔记软件,其优点结合使用场景概括来说包括 开箱即用的极简界面,非常适合用来写日报 灵活的双链,强大的PDF标注,适合构建文献库 使用markdown格式来本地存储笔记 ...

  3. Live2D - 模型预览图

    chitose epsilon2_1       haru-01   haru-02 hijiki tororo vert_normal vert_swimwear ryoufuku seifuku ...

  4. 有关算法与数据结构的考题解答参考汇总 [C++] [链表] · 第三篇

    早先年考研的主考科目正是[算法与数据结构],复习得还算可以.也在当时[百度知道]上回答了许多相关问题,现把他们一起汇总整理一下,供读者参考. [1] 原题目地址:https://zhidao.baid ...

  5. 【MATLAB习题】工程测试时域、频域分析

    代码: classdef App_uoye < matlab.apps.AppBase % Properties that correspond to app components proper ...

  6. RCE_STUDY

    概念 RCE(Remote code execution)远程代码执行漏洞,RCE又分命令执行和代码执行. RCE-远程代码执行:远程执行PHP代码 RCE-远程命令执行:远程执行Linux或者Win ...

  7. 【Bug记录】defineEmits 在 TS 项目报错

    前言 单文件组件 <script setup> 是 Vue3.2 版本后非常流行的写法. 但是在开发过程中,按照官网使用 defineEmits ,结果项目报错,无法运行. 错误截图 Un ...

  8. ModuleNotFoundError: No module named '_bz2'

    前言 运行 python 报错:ModuleNotFoundError: No module named '_bz2' when building python 解决 安装在 Ubuntu/Debia ...

  9. PaddleOCR学习笔记2-初步识别服务

    今天初步实现了网页,上传图片,识别显示结果到页面的服务.后续再完善. 采用flask + paddleocr+ bootstrap快速搭建OCR识别服务. 代码结构如下: 模板页面代码文件如下: up ...

  10. mysql 导出数据的命令

    博客地址:https://www.cnblogs.com/zylyehuo/ # 1.数据库备份与恢复 # mysqldump命令用于备份数据库数据 [root@localhost ~]# mysql ...