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 valuesa = np.arange(8).reshape(2,4)print("Original:",a)# converting the input values to an arraymyarray = np.atleast_3d(a)print("Converted",myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 3: We assign numerical values for the variable,
x, starting from0to7using thearange()function with a shape of2rows and4columns. - Line 6: We call the
atleast_3d()function and pass thexvariable as its argument. The result is assigned to a variable,myarray. - Line 8: We print the
myarrayarray.