NumPy – Using ndarray.repeat() method (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python, offering a powerful N-dimensional array object, and is especially useful for performing mathematical and logical operations on arrays. The ndarray.repeat() method is one of the versatile tools in NumPy that allows for the repetition of elements in an array. In this tutorial, we’ll explore how to use the ndarray.repeat() method in various ways, illustrated with examples ranging from basic to advanced usage.

Syntax & Parameters

The ndarray.repeat() function repeats elements of an array. The repetitions can be specified globally or individually for each element. This method is useful for enlarging datasets, creating test data, or even preparing data for operations that require duplication.

Signature:

ndarray.repeat(repeats, axis=None)

Parameters:

  • repeats: An int or array of ints indicating the number of repetitions for each element.
  • axis: The axis along which to repeat values. If not specified, the input is flattened before repeating.

Now, let’s dive into how we can leverage the ndarray.repeat() method with four detailed examples.

Example 1: Basic Repetition

This example demonstrates the simplest form of repetition, where an element in a 1D array is repeated multiple times.

import numpy as np
# Create a numpy array
array = np.array([1, 2, 3])
# Repeat each element 3 times
repeated_array = array.repeat(3)
print(repeated_array)

Output:

[1, 1, 1, 2, 2, 2, 3, 3, 3]

This repeats each element in the array three times, resulting in a new array three times as large as the original.

Example 2: Repeating Along a Specified Axis

Repeating elements along a specific axis in a multi-dimensional array offers more control over the shape of the output array.

import numpy as np
# Create a 2-dimensional array
array_2d = np.array([[1, 2], [3, 4]])
# Repeat each element 2 times along axis 0
repeated_array_2d = array_2d.repeat(2, axis=0)
print(repeated_array_2d)

Output:

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

This repeats each element in the original array two times along the first axis (rows). This technique is useful for duplicating rows or columns in a dataset.

Example 3: Advanced Repetition with Varying Number of Times

You can also specify the number of repetitions individually for each element in the array by passing an array of integers to the repeats parameter.

import numpy as np
# Create a numpy array
array = np.array([1, 2, 3])
# Repeat first element 1 time, second 2 times, and third 3 times
repeated_array_advanced = array.repeat([1, 2, 3])
print(repeated_array_advanced)

Output:

[1, 2, 2, 3, 3, 3]

This customization provides the ability to have varying repetitions for each element, serving as a powerful feature for data manipulation.

Example 4: Combining with Other NumPy Functions

Here, we’ll combine ndarray.repeat() with other NumPy functions to achieve a more complex result. We’ll create an expanded array and then apply a mathematical operation.

import numpy as np
# Create a numpy array
array = np.array([1, 2, 3])
# Repeat each element 3 times
repeated_array = array.repeat(3)
# Multiply each element by 2
modified_array = repeated_array * 2
print(modified_array)

Output:

[2, 2, 2, 4, 4, 4, 6, 6, 6]

Combining ndarray.repeat() with array operations can significantly streamline data transformation workflows.

Conclusion

The ndarray.repeat() method is a flexible and powerful tool within NumPy for duplicating array elements. Through the examples shown, it’s clear how this method can be applied in simple to complex scenarios, aiding significantly in data manipulation and preparation tasks. With the foundation laid in this tutorial, you’re well-equipped to implement the ndarray.repeat() method in your own Python data science projects.