numpy.tile() – The NumPy tile Python Function

Introduction to NumPy tile

The numpy.tile() function creates a new array by repeating array – ‘arr’, the number of repeats we wish to repeat. The size of the resulting array will be max(arr.ndim, repetitions), where repetitions is the number of repetitions. Reps is promoted to arr.ndim by prepending 1’s to it if arr.ndim > repetitions. Reps is promoted to arr.ndim by pre-pending new axis if arr.ndim < repetitions.

Syntax:

numpy.tile(arr, repetitions)

Parameters:

  • array : [array_like]Input array.
  • repetitions: No. of repetitions of arr along each axis.

Return: An array with repetitions of an array – arr as per d, the number of times we want to repeat arr

Code Examples of NumPy tile

Example 01: Working on a 1D array

# welcome to softhunt.net
# Python Program illustrating
# numpy.tile()

import numpy as np

#Working on 1D
arr = np.arange(5)
print("arr : \n", arr)

repetitions = 2
print("Repeating arr 2 times : \n", np.tile(arr, repetitions))

repetitions = 3
print("\nRepeating arr 3 times : \n", np.tile(arr, repetitions))
# [0 1 2 ..., 2 3 4] means [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]
# since it was long output, so it uses [ ... ]

Output:

arr : 
 [0 1 2 3 4]
Repeating arr 2 times : 
 [0 1 2 3 4 0 1 2 3 4]

Repeating arr 3 times : 
 [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02:

# welcome to softhunt.net
# Python Program illustrating
# numpy.tile()

import numpy as np

arr = np.arange(3)
print("arr : \n", arr)

a = 2
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

a = 3
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

a = 2
b = 3
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

Output:

arr : 
 [0 1 2]

Repeating arr : 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]]
arr Shape : 
 (2, 6)

Repeating arr : 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]
 [0 1 2 0 1 2]]
arr Shape : 
 (3, 6)

Repeating arr : 
 [[0 1 2 0 1 2 0 1 2]
 [0 1 2 0 1 2 0 1 2]]
arr Shape : 
 (2, 9)

Example 03: (repetitions == arr.ndim) == 0

# welcome to softhunt.net
# Python Program illustrating
# numpy.tile()

import numpy as np

arr = np.arange(4).reshape(2, 2)
print("arr : \n", arr)

a = 2
b = 1
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

a = 3
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

a = 2
b = 3
repetitions = (a, b)
print("\nRepeating arr : \n", np.tile(arr, repetitions))
print("arr Shape : \n", np.tile(arr, repetitions).shape)

Output:

arr : 
 [[0 1]
 [2 3]]

Repeating arr : 
 [[0 1]
 [2 3]
 [0 1]
 [2 3]]
arr Shape : 
 (4, 2)

Repeating arr : 
 [[0 1 0 1]
 [2 3 2 3]
 [0 1 0 1]
 [2 3 2 3]
 [0 1 0 1]
 [2 3 2 3]]
arr Shape : 
 (6, 4)

Repeating arr : 
 [[0 1 0 1 0 1]
 [2 3 2 3 2 3]
 [0 1 0 1 0 1]
 [2 3 2 3 2 3]]
arr Shape : 
 (4, 6)

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How can Clone or Duplicate a Row Using np.tile

Let’s assume that you have a 1-dimensional input (a 1D NumPy array or a list). To duplicate the row, you’ll use np.tile with reps = [2,1].

# welcome to softhunt.net
# Python Program illustrating
# numpy.tile()

import numpy as np

np_array_1d = np.array([1,2,3,4])
ans = np.tile(A = np_array_1d, reps = [2,1])
print(ans)

Output:

[[1 2 3 4]
 [1 2 3 4]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Does np.tile Work in More Than 2 Dimensions

Yes, see the example below to get knowledge of that.

# welcome to softhunt.net
# Python Program illustrating
# numpy.tile()

import numpy as np

ans = np.tile(A = [1,2], reps = [2,2,2])
print(ans)

Output:

[[[1 2 1 2]
  [1 2 1 2]]

 [[1 2 1 2]
  [1 2 1 2]]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. Splitting Arrays in NumPy Python Functions

Leave a Comment