In the process of learning KNN classification algorithm using the tile function, there are many do not understand, recorded the use of this function.
Function prototype: Numpy.tile (a,reps) #简单理解是此函数将A进行重复输出
Where a and reps are parameters of array_like, a can be: Array,list,tuple,dict,matrix and basic data type int,string,float and bool type, reps type can be tuple,list , Dict,array,int,bool, but cannot be a float,string,matrix type.
There are two kinds of common forms, which is a simple one-dimensional repetitive output, and a two-dimensional repetition after the output.
One-dimensional repetition:
1 ImportNumPy as NP2A = [[1,2,3],[4,5,5]]3b = Np.tile (A,3)4 Print(b)5 6 #output to7 #[[1 2 3 1 2 3 1 2 3]8 #[4 5 5 4 5 5 4 5 5]]
Two-dimensional repetition: #上面的一维重复相当于 B = np.tile (a,[1,3])
1 ImportNumPy as NP2A = [[1,2,3],[4,5,5]]3b = Np.tile (A,[2,3])4 Print(b)5 6 #the output is:7 #[[1 2 3 1 2 3 1 2 3]8 #[4 5 5 4 5 5 4 5 5]9 #[1 2 3 1 2 3 1 2 3]Ten # [4 5 5 4 5 5 4 5 5]]
Python Scientific Computing Library numpy-tile function