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

Overview

The atleast_3d() function in Python converts the inputs to a given array(s) which has at least two dimensions.

Syntax

numpy.atleast_3d(*arys)
The syntax for the atleast_2d()

Parameter value

The atleast_3d()function takes one or more input arrays, arys.

Return value

The atleast_3d() function returns an array with a.ndim >= 3.

Example

import numpy as np
# creating the input values
a = np.arange(8).reshape(2,4)
print("Original:",a)
# converting the input values to an array
myarray = np.atleast_3d(a)
print("Converted",myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We assign numerical values for the variable, x, starting from 0 to 7 using the arange() function with a shape of 2 rows and 4 columns.
  • Line 6: We call the atleast_3d() function and pass the x variable as its argument. The result is assigned to a variable, myarray.
  • Line 8: We print the myarray array.

Free Resources