numpy.atleast_2d() – The NumPy atleast_2d Python Function

Introduction to NumPy atleast_2d Python Function

Guide to NumPy atleast_2d Python Function – When we wish to convert inputs to arrays with at least two dimensions, we utilize the numpy.atleast_2d() function. Higher-dimensional inputs are maintained while scalar and 1-dimensional inputs are transformed into 2-dimensional arrays.

Syntax:

numpy.atleast_2d(*arrays)

Parameters:

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

Return: An array, or list of arrays, each with a.ndim >= 2. Copies are avoided where possible, and views with two or more dimensions are returned.

Code Examples of NumPy atleast_2d Python Function

Example 01:

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

import numpy as np
num = 15

print ("Input number : ", num)

	
out_arr = np.atleast_2d(num)
print ("output 2d array from input number : ", out_arr)

Output:

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

Example 02:

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

import numpy as np

my_list = [1, 2, 3],

print ("Input list : ", my_list)
	
out_arr = np.atleast_2d(my_list)
print ("output 2d array : ", out_arr)

Output:

Input list :  ([1, 2, 3],)
output 2d array :  [[1 2 3]]

Example 03:

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

import numpy as np

in_arr = np.arange(9).reshape(3, 3)
print ("Input array :\n ", in_arr)

out_arr = np.atleast_2d(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]]
output array :
  [[0 1 2]
 [3 4 5]
 [6 7 8]]
True

FAQs

What is Difference between numpy.atleast_1d() and numpy.atleast_2d()

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.

How do you reshape an array in Numpy?

NumPy is a Python module for array processing. It includes a high-performance multidimensional array object as well as utilities for manipulating them. It is the most important Python module for scientific computing. Numpy is mostly use to create n-dimensional arrays.

Changing the form of a NumPy array simply means changing the number of elements and dimensions of the array. By reshaping an array, we may add or remove dimensions and modify the number of elements in each dimension.

We use the reshape function with the provided array to reshape a NumPy array.

Syntax:

array.reshape(shape)

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

Leave a Comment