1. 程式人生 > >np.tile 和np.newaxis

np.tile 和np.newaxis

 output
array([[ 0.24747071, -0.43886742],
       [-0.03916734, -0.70580089],
       [ 0.00462337, -0.51431584],
       ..., 
       [ 0.15071507, -0.57029653],
       [ 0.06246116, -0.33766761],
       [ 0.08218585, -0.59906501]], dtype=float32)

ipdb> np.shape(output)
(64, 2)

ipdb> np.max(output, axis=1)[:,np.newaxis]
array([[ 0.24747071],
       [-0.03916734],
       [ 0.00462337],
       ..., 
       [ 0.15071507],
       [ 0.06246116],
       [ 0.08218585]], dtype=float32)

ipdb> np.tile(np.max(output, axis=1)[:,np.newaxis], [1,2]))
*** SyntaxError: invalid syntax (<stdin>, line 1)

ipdb> np.tile(np.max(output, axis=1)[:,np.newaxis], [1,2])
array([[ 0.24747071,  0.24747071],
       [-0.03916734, -0.03916734],
       [ 0.00462337,  0.00462337],
       ..., 
       [ 0.15071507,  0.15071507],
       [ 0.06246116,  0.06246116],
       [ 0.08218585,  0.08218585]], dtype=float32)

ipdb> output
array([[ 0.24747071, -0.43886742],
       [-0.03916734, -0.70580089],
       [ 0.00462337, -0.51431584],
       ..., 
       [ 0.15071507, -0.57029653],
       [ 0.06246116, -0.33766761],
       [ 0.08218585, -0.59906501]], dtype=float32)

ipdb> np.max(output, axis=1)
array([ 0.24747071, -0.03916734,  0.00462337, ...,  0.15071507,
        0.06246116,  0.08218585], dtype=float32)
ipdb> np.exp(output - np.tile(np.max(output, axis=1)[:,np.newaxis], [1,2]))
array([[ 1.        ,  0.50341612],
       [ 1.        ,  0.51343411],
       [ 1.        ,  0.59515154],
       ..., 
       [ 1.        ,  0.48626012],
       [ 1.        ,  0.67023373],
       [ 1.        ,  0.50598365]], dtype=float32)

1- np.newaxis

np.newaxis的功能是插入新維度,看下面的例子:

a=np.array([1,2,3,4,5])
print a.shape

print a

輸出結果

(5,)
[1 2 3 4 5]

可以看出a是一個一維陣列,

x_data=np.linspace(-1,1,300)[:,np.newaxis]
a=np.array([1,2,3,4,5])
b=a[np.newaxis,:]
print a.shape,b.shape
print a

print b

輸出結果:

(5,) (1, 5)
[1 2 3 4 5]
[[1 2 3 4 5]]

x_data=np.linspace(-1,1,300)[:,np.newaxis]
a=np.array([1,2,3,4,5])
b=a[:,np.newaxis]
print a.shape,b.shape
print a
print b

輸出結果

(5,) (5, 1)
[1 2 3 4 5]
[[1]
 [2]
 [3]
 [4]
 [5]]

可以看出np.newaxis分別是在行或列上增加維度,原來是(6,)的陣列,在行上增加維度變成(1,6)的二維陣列,在列上增加維度變為(6,1)的二維陣列

2. np.tile

函式原型:numpy.tile(A,reps) #簡單理解是此函式將A進行重複輸出

 其中A和reps都是array_like的引數,A可以是:array,list,tuple,dict,matrix以及基本資料型別int,string,float以及bool型別,reps的型別可以是tuple,list,dict,array,int,bool,但不可以是float,string,matrix型別。

計較常用的形式有兩種,是將A簡單進行一維重複輸出,和將A進行二維重複後輸出。
一維重複:

import numpy as np
a = [[1,2,3],[4,5,5]]
b = np.tile(a,3)
print(b)

#輸出為
#[[1 2 3 1 2 3 1 2 3]
# [4 5 5 4 5 5 4 5 5]]

二維重複:#上面的一維重複相當於 b = np.tile(a,[1,3])

import numpy as np
a = [[1,2,3],[4,5,5]]
b = np.tile(a,[2,3])
print(b)

#輸出為:
#[[1 2 3 1 2 3 1 2 3]
# [4 5 5 4 5 5 4 5 5]
# [1 2 3 1 2 3 1 2 3]
# [4 5 5 4 5 5 4 5 5]]

2.1 np.tile

numpy.tile()是個什麼函式呢,說白了,就是把陣列沿各個方向複製

比如 a = np.array([0,1,2]),    np.tile(a,(2,1))就是把a先沿x軸(就這樣稱呼吧)複製1倍,即沒有複製,仍然是 [0,1,2]。 再把結果沿y方向複製2倍,即最終得到

 array([[0,1,2],

             [0,1,2]])

同理:

>>> b = np.array([[1, 2], [3, 4]])
>>> np.tile(b, 2) #沿X軸複製2倍
array([[1, 2, 1, 2],
       [3, 4, 3, 4]])
>>> np.tile(b, (2, 1))#沿X軸複製1倍(相當於沒有複製),再沿Y軸複製2倍
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])

numpy.tile()具體細節,如下:

numpy.tile(Areps)

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 < dA 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 > dreps is promoted to A.ndim by pre-pending 1’s to it. Thus for an A of shape (2, 3, 4, 5), a repsof (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 elements of an array.
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]])