numpy.atleast_1d() – The NumPy atleast_1d Python Function

Introduction to NumPy atleast_1d Python Function

Guide to NumPy atleast_1d Python Function – When we wish to convert inputs to arrays with at least one dimension. We utilize the numpy.atleast_1d() function. Higher-dimensional inputs are maintained while scalar inputs are transformed into 1-dimensional arrays.

Syntax:

numpy.atleast_1d(*arrays)

Parameters:

  • arrays1, arrays2, … : [array_like] One or more input arrays.

Return: [ndarray] An array, or list of arrays, each with a.ndim >= 1. Copies are made only if necessary.

Code Examples of NumPy atleast_1d Python Function

Example 01:

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

import numpy as np
num = 15

print ("Input number : ", num)

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

Output:

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

Example 02:

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

import numpy as np

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

print ("Input list : \n", my_list)
	
out_arr = np.atleast_1d(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_1d() 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_1d(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 Python NumPy package?

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.

Why NumPy is so fast?

NumPy is a Python core module for manipulating and operating on high-level mathematical functions, multi-dimensional arrays, linear algebra, Fourier transformations, and random number capabilities, among other things. It offers Python with tools for integrating C, C++, and Fortran programs. NumPy is mostly used for scientific computing in Python.

NumPy Arrays are faster than Python Lists because of the following reasons:

  • A collection of homogenous data types store in contiguous memory spaces is refer to as an array. A list, on the other hand, is a collection of disparate data types store in non-contiguous memory regions in Python.
  • The NumPy package divides a job into many pieces, which are subsequently process in parallel.
  • NumPy is a Python library that combines C, C++, and Fortran code. In comparison to Python, these programming languages have an extremely short execution time.

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.transpose() – The NumPy transpose Python Function

Leave a Comment