The numpy.tile() function constructs a new array by repeating array - 'arr', the number of times we want to repeat as per repetitions. The resulted array will have dimensions max(arr.ndim, repetitions) where, repetitions is the length of repetitions. If arr.ndim > repetitions, reps is promoted to arr.ndim by pre-pending 1’s to it. If arr.ndim < repetitions, reps is promoted to arr.ndim by pre-pending new axis. 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 array - arr as per d, number of times we want to repeat arr
Code 1 :
Python
# Python Program illustrating
# numpy.tile()
import numpy as geek
#Working on 1D
arr = geek.arange(5)
print("arr : \n", arr)
repetitions = 2
print("Repeating arr 2 times : \n", geek.tile(arr, repetitions))
repetitions = 3
print("\nRepeating arr 3 times : \n", geek.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 ..., 2 3 4]
Code 2 :
Python
# Python Program illustrating
# numpy.tile()
import numpy as geek
arr = geek.arange(3)
print("arr : \n", arr)
a = 2
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.tile(arr, repetitions).shape)
a = 3
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.tile(arr, repetitions).shape)
a = 2
b = 3
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.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]]
arr Shape :
(2, 9)
Code 3 : (repetitions == arr.ndim) == 0
Python
# Python Program illustrating
# numpy.tile()
import numpy as geek
arr = geek.arange(4).reshape(2, 2)
print("arr : \n", arr)
a = 2
b = 1
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.tile(arr, repetitions).shape)
a = 3
b = 2
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.tile(arr, repetitions).shape)
a = 2
b = 3
repetitions = (a, b)
print("\nRepeating arr : \n", geek.tile(arr, repetitions))
print("arr Shape : \n", geek.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)
References : https://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html Note : These codes won’t run on online IDE's. Please run them on your systems to explore the working .
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read