NumpyPython学习Tips

python numpy-tile函数

2017-09-12  本文已影响0人  KardelShaw
本文所有代码均可在Pycharm编译运行
Python版本:3.6.2

俗话说,新手看博客,高手看文档,那我们先按高手的套路走一波文档:help(numpy.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]])

简单来说,tile函数的作用是让某个数组(其实不局限于数组,但我们这里只讨论数组),以某种方式重复,构造出新的数组,所以返回值也是个数组。

例1
a = array([0, 1, 2])
b = tile(a, 2)
print(b)

//结果为
[0, 1, 2, 0, 1, 2]

这个例子肯定很简单了,我用来说明tile的两个参数,第一个参数是数组,第二个参数可以是一个数字,更多情况可能是一个元组(这种情况可能更容易让人懵逼)。下面讨论当是元组时tile函数是如何执行的。

tile怎么执行的,有两个要点

(1)比较数组维度d和元组维度reps,如果d<reps,�在需要时对数组补中括号 [] 来增加维度。
(2)元组数字从右到左,数组维度从最深维度到最低维度。�假设元组为(a,b,c,d,e,f),则数组最深维度重复f次;然后次深维度重复e次;接着次次深维度重复d次;再然后次次次深维度重复c次…… 以此类推,直到对最低维度重复a次,结束,得到结果

例1说明:数组维度d=1,元组维度reps=1,所以对数组的维度进行重复时,不需要补中括号。元组元素从右到左看,第一个元素是2,数组最深维度是1。所以得到

[0, 1, 2, 0, 1, 2]

可以预见,如果只重复1次,会得到原数组本身

[0, 1, 2]
例2
a = array([0, 1, 2])
b = tile(a, (2, 1))
print(b)

//结果为
[[0, 1, 2],  
 [0, 1, 2]]

例2说明:数组只有1个维度,比较数组维度d和元组维度reps,d<reps,所以需要补中括号。元组从右到左看,而数组维度从最深的开始看。对数组a的最深维度重复1次,得到[[0, 1, 2]],然后再对数组a次深维度重复2次,得到[[0, 1, 2], [0, 1, 2]]

现在你应该可以按着上面的思路,得出以下3个函数结果,判断完后再试着在编译器中输出看是否与你预想的一致。

a = array([0, 1, 2])

①tile(a, (2, 2))

②tile(a, (2, 1, 1))

③tile(a, (2, 2, 1))






答案:


Numbers作图


Numbers作图

Numbers作图

能理解吗?欢迎留下您的见解和建议!

上一篇下一篇

猜你喜欢

热点阅读