参考:Numpy学习——数组填充np.pad()函数的应用


举例说明:

import numpy as np
a = np.zeros((3, 4), dtype=int)
aarray([[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]])# pad(array, pad_width, mode, **kwargs)
# array: array_like
# pad_width: ((1, 2), (3, 4))
#    1: width of top
#    2: width of bottom
#    3: width of left
#    4: width of right
# mode: str or function
#    'constant': Pads with a constant value
# constant_values: Used in 'constant'. The values to set the padded values for each axis.
#    ((1, 2), (3, 4))
#    1: value of top
#    2: value of bottom
#    3: vlaue of left
#    4: vlaue of right
np.pad(a, ((1, 2), (3, 4)), 'constant', constant_values=((1, 2), (3, 4)))array([[3, 3, 3, 1, 1, 1, 1, 4, 4, 4, 4],[3, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4],[3, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4],[3, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4],[3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4],[3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4]])# (1, 2)
# top:bottom=1:2
# left:right=1:2
np.pad(a, (1, 2), 'constant', constant_values=(1, 2))array([[1, 1, 1, 1, 1, 2, 2],[1, 0, 0, 0, 0, 2, 2],[1, 0, 0, 0, 0, 2, 2],[1, 0, 0, 0, 0, 2, 2],[1, 2, 2, 2, 2, 2, 2],[1, 2, 2, 2, 2, 2, 2]])a = [1, 2, 3, 4, 5]
np.pad(a, (2, 3), 'constant', constant_values=(4, 6))array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6])# mode: 'edge': Pads with the edge values of array
np.pad(a, (2, 3), 'edge')array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])np.pad(a, (2, 3), 'linear_ramp', end_values=(5, 1))array([5, 3, 1, 2, 3, 4, 5, 4, 2, 1])np.pad(a, (2, 3), 'maximum')array([5, 5, 1, 2, 3, 4, 5, 5, 5, 5])np.pad(a, (2, 3), 'mean')array([3, 3, 1, 2, 3, 4, 5, 3, 3, 3])np.pad(a, (2, 3), 'median')array([3, 3, 1, 2, 3, 4, 5, 3, 3, 3])a = [[1, 2], [3, 4]]
np.pad(a, (2, 3), 'minimum')array([[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[3, 3, 3, 4, 3, 3, 3],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1]])


语法说明:

>>> help(np.pad)
Help on function pad in module numpy.lib.arraypad:pad(array, pad_width, mode, **kwargs)Pads an array.Parameters----------array : array_like of rank NInput arraypad_width : {sequence, array_like, int}Number of values padded to the edges of each axis.((before_1, after_1), ... (before_N, after_N)) unique pad widthsfor each axis.((before, after),) yields same before and after pad for each axis.(pad,) or int is a shortcut for before = after = pad width for allaxes.mode : str or functionOne of the following string values or a user supplied function.'constant'Pads with a constant value.'edge'Pads with the edge values of array.'linear_ramp'Pads with the linear ramp between end_value and thearray edge value.'maximum'Pads with the maximum value of all or part of thevector along each axis.'mean'Pads with the mean value of all or part of thevector along each axis.'median'Pads with the median value of all or part of thevector along each axis.'minimum'Pads with the minimum value of all or part of thevector along each axis.'reflect'Pads with the reflection of the vector mirrored onthe first and last values of the vector along eachaxis.'symmetric'Pads with the reflection of the vector mirroredalong the edge of the array.'wrap'Pads with the wrap of the vector along the axis.The first values are used to pad the end and theend values are used to pad the beginning.<function>Padding function, see Notes.stat_length : sequence or int, optionalUsed in 'maximum', 'mean', 'median', and 'minimum'.  Number ofvalues at edge of each axis used to calculate the statistic value.((before_1, after_1), ... (before_N, after_N)) unique statisticlengths for each axis.((before, after),) yields same before and after statistic lengthsfor each axis.(stat_length,) or int is a shortcut for before = after = statisticlength for all axes.Default is ``None``, to use the entire axis.constant_values : sequence or int, optionalUsed in 'constant'.  The values to set the padded values for eachaxis.((before_1, after_1), ... (before_N, after_N)) unique pad constantsfor each axis.((before, after),) yields same before and after constants for eachaxis.(constant,) or int is a shortcut for before = after = constant forall axes.Default is 0.end_values : sequence or int, optionalUsed in 'linear_ramp'.  The values used for the ending value of thelinear_ramp and that will form the edge of the padded array.((before_1, after_1), ... (before_N, after_N)) unique end valuesfor each axis.((before, after),) yields same before and after end values for eachaxis.(constant,) or int is a shortcut for before = after = end value forall axes.Default is 0.reflect_type : {'even', 'odd'}, optionalUsed in 'reflect', and 'symmetric'.  The 'even' style is thedefault with an unaltered reflection around the edge value.  Forthe 'odd' style, the extended part of the array is created bysubtracting the reflected values from two times the edge value.Returns-------pad : ndarrayPadded array of rank equal to `array` with shape increasedaccording to `pad_width`.Notes-----.. versionadded:: 1.7.0For an array with rank greater than 1, some of the padding of lateraxes is calculated from padding of previous axes.  This is easiest tothink about with a rank 2 array where the corners of the padded arrayare calculated by using padded values from the first axis.The padding function, if used, should return a rank 1 array equal inlength to the vector argument with padded values replaced. It has thefollowing signature::padding_func(vector, iaxis_pad_width, iaxis, kwargs)wherevector : ndarrayA rank 1 array already padded with zeros.  Padded values arevector[:pad_tuple[0]] and vector[-pad_tuple[1]:].iaxis_pad_width : tupleA 2-tuple of ints, iaxis_pad_width[0] represents the number ofvalues padded at the beginning of vector whereiaxis_pad_width[1] represents the number of values padded atthe end of vector.iaxis : intThe axis currently being calculated.kwargs : dictAny keyword arguments the function requires.Examples-------->>> a = [1, 2, 3, 4, 5]>>> np.pad(a, (2,3), 'constant', constant_values=(4, 6))array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6])>>> np.pad(a, (2, 3), 'edge')array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])>>> np.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))array([ 5,  3,  1,  2,  3,  4,  5,  2, -1, -4])>>> np.pad(a, (2,), 'maximum')array([5, 5, 1, 2, 3, 4, 5, 5, 5])>>> np.pad(a, (2,), 'mean')array([3, 3, 1, 2, 3, 4, 5, 3, 3])>>> np.pad(a, (2,), 'median')array([3, 3, 1, 2, 3, 4, 5, 3, 3])>>> a = [[1, 2], [3, 4]]>>> np.pad(a, ((3, 2), (2, 3)), 'minimum')array([[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1],[3, 3, 3, 4, 3, 3, 3],[1, 1, 1, 2, 1, 1, 1],[1, 1, 1, 2, 1, 1, 1]])>>> a = [1, 2, 3, 4, 5]>>> np.pad(a, (2, 3), 'reflect')array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])>>> np.pad(a, (2, 3), 'reflect', reflect_type='odd')array([-1,  0,  1,  2,  3,  4,  5,  6,  7,  8])>>> np.pad(a, (2, 3), 'symmetric')array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])>>> np.pad(a, (2, 3), 'symmetric', reflect_type='odd')array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])>>> np.pad(a, (2, 3), 'wrap')array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])>>> def pad_with(vector, pad_width, iaxis, kwargs):...     pad_value = kwargs.get('padder', 10)...     vector[:pad_width[0]] = pad_value...     vector[-pad_width[1]:] = pad_value...     return vector>>> a = np.arange(6)>>> a = a.reshape((2, 3))>>> np.pad(a, 2, pad_with)array([[10, 10, 10, 10, 10, 10, 10],[10, 10, 10, 10, 10, 10, 10],[10, 10,  0,  1,  2, 10, 10],[10, 10,  3,  4,  5, 10, 10],[10, 10, 10, 10, 10, 10, 10],[10, 10, 10, 10, 10, 10, 10]])>>> np.pad(a, 2, pad_with, padder=100)array([[100, 100, 100, 100, 100, 100, 100],[100, 100, 100, 100, 100, 100, 100],[100, 100,   0,   1,   2, 100, 100],[100, 100,   3,   4,   5, 100, 100],[100, 100, 100, 100, 100, 100, 100],[100, 100, 100, 100, 100, 100, 100]])

转载于:https://www.cnblogs.com/alex-bn-lee/p/10740554.html

【400】numpy.pad 为数组加垫(迷宫类题目)相关推荐

  1. python输入数组并计算_利用Python进行数据分析——Numpy基础:数组和矢量计算

    利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...

  2. 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算

    第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说,用numpy的主要目的在于应用矢量化运算.Numpy并没有多么高级的数据分析功能,理解Numpy ...

  3. 【15分】C. 最胖的加菲(类与对象+数组)

    [15分]C. 最胖的加菲(类与对象+数组) 题目描述 有一群猫猫,每只猫都有自己的名称和体重. 用类来描述猫,名称和体重都是私有属性,要求加入属性的get方法.其他函数根据需要自己定义 创建一个动态 ...

  4. python 数据分析5 NumPy 基础:数组和矢量计算

    系列文章:Python 数据分析 文章目录 五.NumPy 基础:数组和矢量计算 1.n维数组对象 ndarray 创建 dtype 数组运算 基本的索引和切片 一维数组 二维数组 布尔型索引 花式索 ...

  5. Python数据分析学习四 NumPy基础:数组和矢量计算

    Python数据分析学习系列四NumPy基础:数组和矢量计算 资料转自(GitHub地址):https://github.com/wesm/pydata-book 有需要的朋友可以自行去github下 ...

  6. numpy.pad使用详解

    numpy中pad的用法 在机器学习中经常用到pad进行数据的预处理,对原本的矩阵进行填充,具体用法如下: numpy.pad(array, pad_width, mode='constant', * ...

  7. python数组排序-python - 按列在NumPy中对数组进行排序

    python - 按列在NumPy中对数组进行排序 如何在第n列中对NumPy中的数组进行排序? 例如, a = array([[9, 2, 3], [4, 5, 6], [7, 0, 5]]) 我想 ...

  8. python中numpy.pad()函数的使用

      在卷积神经网络中,为了避免因为卷积运算导致输出图像缩小和图像边缘信息丢失,常常采用图像边缘填充技术,即在图像四周边缘填充0,使得卷积运算后图像大小不会缩小,同时也不会丢失边缘和角落的信息.在Pyt ...

  9. Python Numpy多维数组.sum(axis=0/1/2...) 详解

    Python Numpy多维数组.sum(axis=0/1/2-) 详解 numpy中axis取值的说明 首先对numpy中axis取值进行说明:一维数组时axis=0,二维数组时axis=0,1,维 ...

最新文章

  1. 【转】makefile写法2
  2. python之禅星号_Python基础1
  3. linux常用架构,Linux常用到的一些命令-Go语言中文社区
  4. php 接口日志,PHP 开发 APP 接口--错误日志接口
  5. PowerDesigner逆向工程导入MYSQL数据库总结
  6. solve det(I + uv^t)
  7. (引)XPath 示例
  8. php 是否存在,php判断常量是否存在
  9. Linux实训项目——第八章:配置网络接口及远程管理
  10. python的ols_工具方法 | 6行代码教你用Python做OLS回归(内附CFPS实例)
  11. 分享几个程序员接活赚外快的平台
  12. opencv之subtract
  13. 智能家居项目开发: 设计模式(工厂模式)+ 线程池 + Socket (持续更新中)
  14. R可视乎 | 散点图系列(2)
  15. 电子签章如何申请(电子签章类型)
  16. Android获取手机IMEI号和IMSI号
  17. win7x64下实现进程保护
  18. 工科生的信仰 ESP32 + HaaS Python 打造HaaS收音机
  19. 将 5G 应用于工业物联网
  20. 微信小程序:炫酷手持滚动弹幕生成小工具

热门文章

  1. 京东方拟收购法国零售物联网领域公司50.1%以上股份
  2. CentOS下LAMP一键yum安装脚本
  3. Fiddler的学习
  4. UITableView性能提升和优化(第3章) 之一
  5. 【Thread】- ReentrantLock、ReentrantReadWriteLock
  6. 小程序中获取高度以及设备的方法
  7. 新生代Eden与两个Survivor区的解释
  8. Linux中mysql的操作
  9. DELL服务器装2003系统
  10. csu1356 :判断一个环是否为奇数环