Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
python
communitycreator

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

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Implementing the atleast_2d() function

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.

RELATED TAGS

function
numpy
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring