【python】numpy数组(array)扩充(复制)方法repeat和tile的使用
2016-10-25 17:01
831 查看
但是很多情况下不需要使用,在进行矩阵加减运算的时候会自动复制
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> b
array([1, 2])
>>> a-b
array([[0, 0],
[2, 2],
[4, 4]])
>>>
用repeat和tile扩充数组元素,例如
[python] view
plain copy
<span style="font-size:14px;">>>> import numpy as np
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.repeat(5)
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9,
9, 9, 9, 9])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.tile(a,2)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> </span>
官方例子:
[python] view
plain copy
>>> from numpy import *
>>> a=array([10,20])
>>> tile(a,(3,2)) #构造3X2个copy
array([[10, 20, 10, 20],
[10, 20, 10, 20],
[10, 20, 10, 20]])
>>> tile(42.0,(3,2))
array([[ 42., 42.],
[ 42., 42.],
[ 42., 42.]])
>>>
[python] view
plain copy
>>> from numpy import *
>>> repeat(7.,4)
array([ 7., 7., 7., 7.])
>>> a=array([10,20])
>>> a.repeat([3,2])
array([10, 10, 10, 20, 20])
>>> repeat(a,[3,2])
array([10, 10, 10, 20, 20])
>>> a=array([[10,20],[30,40]])
>>> a.repeat([3,2],axis=0)
array([[10, 20],
[10, 20],
[10, 20],
[30, 40],
[30, 40]])
>>> a.repeat([3,2],axis=1)
array([[10, 10, 10, 20, 20],
[30, 30, 30, 40, 40]])
>>>
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> b
array([1, 2])
>>> a-b
array([[0, 0],
[2, 2],
[4, 4]])
>>>
用repeat和tile扩充数组元素,例如
[python] view
plain copy
<span style="font-size:14px;">>>> import numpy as np
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.repeat(5)
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9,
9, 9, 9, 9])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.tile(a,2)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> </span>
官方例子:
[python] view
plain copy
>>> from numpy import *
>>> a=array([10,20])
>>> tile(a,(3,2)) #构造3X2个copy
array([[10, 20, 10, 20],
[10, 20, 10, 20],
[10, 20, 10, 20]])
>>> tile(42.0,(3,2))
array([[ 42., 42.],
[ 42., 42.],
[ 42., 42.]])
>>>
[python] view
plain copy
>>> from numpy import *
>>> repeat(7.,4)
array([ 7., 7., 7., 7.])
>>> a=array([10,20])
>>> a.repeat([3,2])
array([10, 10, 10, 20, 20])
>>> repeat(a,[3,2])
array([10, 10, 10, 20, 20])
>>> a=array([[10,20],[30,40]])
>>> a.repeat([3,2],axis=0)
array([[10, 20],
[10, 20],
[10, 20],
[30, 40],
[30, 40]])
>>> a.repeat([3,2],axis=1)
array([[10, 10, 10, 20, 20],
[30, 30, 30, 40, 40]])
>>>
相关文章推荐
- 【Python】Numpy扩充数组函数之repeat和tile用法
- Python之数组(array)使用方法总结与Numpy中的数组用法
- 【Python】用Numpy扩充数组元素之repeat和tile
- Python之数组(array)使用方法总结与Numpy中的数组用法
- Python之数组(array)使用方法总结与Numpy中的数组用法
- 【Python】Numpy扩充数组函数之repeat和tile用法
- python的numpy包中shape,tile,argsort函数与sorted函数使用方法
- 用Numpy扩充数组元素之repeat和tile
- 对python numpy数组中冒号的使用方法详解
- Python numpy.array() 生成相同元素数组的方法
- numpy扩充数组函数之repeat和tile用法
- 记录一下PHP数组数据分页的方法 array_slice() 函数的使用
- 使用System.arraycopy()实现数组之间的复制
- 【转】使用System.arraycopy()实现数组之间的复制
- Java数组复制的方法和System.arraycopy
- Java中数组的使用方法? 初始化,二维数组,数组复制
- 使用System.arraycopy()实现数组之间的复制
- 使用sortedArrayUsingSelector:方法排序数组中的元素
- 使用System.arraycopy()实现数组之间的复制
- js中的数组Array定义与sort方法使用示例