python中的tile函数和sum函数

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

page487image10288

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

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

[ 6., 7.]]])]

page487image13048 page487image13472 page487image13896 page487image14320

>>> 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]])



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值