numpy.atleast_3d() – The NumPy atleast_3d Python Function

Introduction to NumPy atleast_3d

When we wish to convert inputs to arrays with at least three dimensions, we utilize the numpy.atleast_3d() function. Higher-dimensional inputs are maintained while scalar, 1, and 2-dimensional inputs are transformed to 3-dimensional arrays.

Scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays are all possible inputs.

Syntax:

numpy.atleast_3d(*arrays)

Parameters:

  • arrays1, arrays2, … : [array_like] One or more array-like sequences. Non-array inputs are convert to arrays. Arrays that already have three or more dimensions are preserved.

Return: An array, or list of arrays, each with arr.ndim >= 3. Copies are avoided where possible, and views with three or more dimensions are returned. For example, a 1-D array of shape (N, ) becomes a view of shape (1, N, 1), and a 2-D array of shape (M, N) becomes a view of shape (M, N, 1).

Code Examples of NumPy atleast_3d

Example 01:

# welcome to softhunt.net
# Python program explaining
# numpy.atleast_3d() function

import numpy as np
num = 15

print ("Input number : ", num)
	
	
out_arr = np.atleast_3d(num)
print ("output 3d array from input number : ", out_arr)

Output:

Input number :  15
output 3d array from input number :  [[[15]]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# numpy.atleast_3d() function

import numpy as np

my_list = [[1, 2, 3],
		[4, 5, 6]]

print ("Input list : ", my_list)
	
out_arr = np.atleast_3d(my_list)
print ("output array : \n", out_arr)

Output:

Input list :  [[1, 2, 3], [4, 5, 6]]
output array : 
 [[[1]
  [2]
  [3]]

 [[4]
  [5]
  [6]]]

Example 03:

# welcome to softhunt.net
# Python program explaining
# numpy.atleast_3d() function
# when inputs are in high dimension

import numpy as np

in_arr = np.arange(16).reshape(1, 4, 4)
print ("Input array :\n ", in_arr)

out_arr = np.atleast_3d(in_arr)
print ("output array :\n ", out_arr)
print(in_arr is out_arr)

Output:

Input array :
  [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]]]
output array :
  [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]]]
True

FAQs

Difference between numpy.atleast_1d(), numpy.atleast_2d(), and numpy.atleast_3d()

numpy.atleast_1d(): When we wish to convert inputs to arrays with at least one dimension. We utilize the numpy.atleast_1d() function. Higher-dimensional inputs are maintain while scalar inputs are transform into 1-dimensional arrays.

numpy.atleast_2d(): When we wish to convert inputs to arrays with at least two dimensions, we utilize the numpy.atleast_2d() function. Higher-dimensional inputs are maintain while scalar and 1-dimensional inputs are transform into 2-dimensional arrays.

numpy.atleast_3d(): When we wish to convert inputs to arrays with at least three dimensions, we utilize the numpy.atleast_3d() function. Higher-dimensional inputs are maintain while scalar, 1, and 2-dimensional inputs are transform to 3-dimensional arrays.

What is NumPy?

NumPy is the most important Python module for scientific computing. It’s a Python library that includes a multidimensional array object, derived objects (such as masked arrays and matrices), and a variety of routines for performing fast array operations, such as mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation, and more.

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. numpy.atleast_1d() – The NumPy atleast_1d Python Function
  2. numpy.atleast_2d() – The NumPy atleast_2d Python Function

Leave a Comment