当前位置:网站首页>Numpy duplicate data

Numpy duplicate data

2022-06-09 20:52:00 Eager to learn

numpy Medium main use tile Functions and repeat Function to realize data duplication

tile function

tile The function is defined as follows :

def tile(A, reps)

The function mainly has two parameters , Parameters A Specifies a duplicate array , Parameters reps The number of times the order is repeated .
Be careful : If reps The length of is d, Then the dimension of the last repeated array is max(d, A.ndim), Take away d And A The maximum value of the dimension . If A.ndim < d, By adding a new axis A Turn into d dimension . For example, for shape by (3,) adopt 2 Dimensional repetition shape Turn into (1,3), adopt 3 Dimensional repetition shape Turn into (1,1,3).

Example :
 Insert picture description here
 Insert picture description here
 Insert picture description here

repeat function

The function is defined as follows :

repeat(a, repeats, axis=None):

repeat The main functions are 3 Parameters , Parameters a Specify the array elements that need to be repeated , Parameters repeats Specify the number of repetitions , Parameters axis Specifies which axis to repeat along .

  • axis=None, It will be flatten Current matrix , In fact, it becomes a line vector
  • axis=0, Along y Axis copy , Actually increased the number of lines
  • axis=1, Along x Axis copy , Actually increase the number of columns

 Insert picture description here

>>> np.repeat(x, 3, axis=0)
array([[1, 2],
       [1, 2],
       [1, 2],
       [3, 4],
       [3, 4],
       [3, 4]])

summary :
The main difference between these two functions is :tile The function is to repeat the operation on the array ,repeat The function is to repeat the operation on each element in the array .

原网站

版权声明
本文为[Eager to learn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092044478839.html