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

Overview

The atleast_2d() function in Python is used to convert inputs to a given array(s) which has at least two dimensions.

Syntax

Let's view the syntax of the method.

numpy.atleast_2d(*arys)
Syntax for the atleast_2d() function

Parameter value

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

Return value

The atleast_2d() function returns an array with a.ndim >= 2.

Code example

Let's view the code for this method.

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

Code explanation

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

Free Resources