python中的tile函数和sum函数

时间:2021-04-07 21:20:37

numpy.tile(A,reps)定义如下:

Construct an array by repeating A the number of times given by reps.

Ifrepshaslengthd,theresultwillhavedimensionofmax(d, A.ndim).

IfA.ndim < d,Aispromotedtobed-dimensionalbyprependingnewaxes.Soashape(3,)arrayispromotedto (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote Ato d-dimensions manually before calling this function.

IfA.ndim > d,repsispromotedtoA.ndimbypre-pending1’stoit.ThusforanAofshape(2,3,4,5),arepsof (2, 2) is treated as (1, 1, 2, 2).

Parameters
: array_like

The input array.reps : array_like

The number of repetitions of along each axis.Returns

: ndarray
The tiled output array.

See Also: 

repeat

Repeat elements of an array.

Examples

NumPy Reference, Release 1.8.0

>>> np.vsplit(x, 2)[array([[[ 0., 1.],

        [ 2.,  3.]]]),
 array([[[ 4.,  5.],

[ 6., 7.]]])]

>>> np.array([012])>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])>>> np.tile(a, (22))array([[0, 1, 2, 0, 1, 2],

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

>>> np.tile(a, (212))array([[[0, 1, 2, 0, 1, 2]],

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

>>> np.array([[12], [34]])>>> np.tile(b, 2)
array([[1, 2, 1, 2], 


设A为数组,且A为x维。reps的长度为y,则,numpy.tile(A,reps)表示如下:

另A=(A1,A2,A3....,Ax),reps=(Y1,Y2,Y3,.......,Ym).

如果m<x:则reps=(1,1,.....,Y1,Y2,...,Ym)

对A从低维到高维,每个维度按照Ym,Ym-1,.......,1的倍数进行复制。其中一个中括号[]代表一个维度。

如果m>x:则

对A从低维到高维,每个维度按照Ym,Ym-1,.....,Y1的倍数进行复制。其中一个中括号[]代表一个维度



numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)

Sum of array elements over a given axis. 

设A.ndim=x,axis=y

则A.sum(axis=y),意味着A按照(x-y)的维度进行求和。例如:

>>> p=numpy.array([[[1,2],[9,10]]])

>>> p

array([[[ 1,  2],

        [ 9, 10]]])


>>> p.sum(axis=2)

array([[ 3, 19]])


>>> p.sum(axis=0)

array([[ 1,  2],

       [ 9, 10]])


>>> p.sum(axis=1)

array([[10, 12]])