Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the numpy.repeat() function in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The repeat() function in Python is simply used to repeat the elements of an input array.

Syntax

The repeat() function takes the syntax below:

numpy.repeat(a, repeats, axis=None)
Syntax for the repeat() function in Python

Parameter value

The repeat() function takes the following parameter values:

  • a: This is the input array. It is a required parameter.
  • repeats: This is the number of times for which the elements of the input array are to be repeated. It is a required parameter.
  • axis: This is the axis along which the repetition is made. It is an optional parameter.

Return value

The repeat() function returns an array of the same shape as the input array.

Example

import numpy as np
# creating an input array
a = np.array([1,2,3,4,5])
# calling the repeat() function
my_array = np.repeat(a, 2)
print(my_array)
Implementing the repeat() function

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an input array, a, using the array() function.
  • Line 6: We call the repeat() function and pass a and 2 as arguments. The function will create an array in which the element a will be repeated twice.
  • Line 9: We print the new array, my_array.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring