10-16-2022, 03:49 AM
Python – Return NumPy Array From Function
5/5 – (1 vote)
Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go!![[Image: 1f680.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png)
A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a variable, and return it to the caller of the function using the keywordoperation ““.
Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy Ways
Create and Return 1D Array
For example, the following code creates a function of numbers 0, 1, 2, …, 9 using the function and returns the array to the caller of the function:
The function creates a new NumPy array with evenly-spaced integers between (inclusive) and (exclusive).
The size defines the difference between subsequent values. For example, creates the NumPy array .
To better understand the function, have a look at this video:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVERAXaqUsjc%2Fhqdefault.jpg)
I also created this figure to demonstrate how NumPy’s function works on three examples:
![[Image: image-134.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-134.png)
In the code example, we used with default and only specifying the argument.
If you need an even deeper understanding, I’d recommend you check out our full guide on the Finxter blog.
Recommended Tutorial: NumPy Arange Function — A Helpful Illustrated Guide
Create and Return 2D NumPy Array
You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) nested list and converting the nested list to a NumPy array by passing it into the function.
The following code snippet uses nested list comprehension to create a 2D NumPy array following a more complicated creation pattern:
Output:
I definitely recommend reading the following tutorial to understand nested list comprehension in Python:
Recommended Tutorial: Nested List Comprehension in Python
More Ways
There are many other ways to return an array in Python.
For example, you can use either of those methods inside the function body to create and initialize a NumPy array:
To get a quick overview what to put into the function and how these methods work, I’d recommend you check out our full tutorial.
Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy Ways
Related Tutorials
Programmer Humor
5/5 – (1 vote)
Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go!
![[Image: 1f680.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png)
A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a variable
Code:
arrCode:
return arr
Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy WaysCreate and Return 1D Array
For example, the following code creates a function
Code:
create_array()Code:
np.arange()Code:
import numpy as np def create_array(): ''' Function to return array ''' return np.arange(10) numbers = create_array()
print(numbers)
# [0 1 2 3 4 5 6 7 8 9]The
Code:
np.arange([start,] stop[, step])Code:
startCode:
stopThe
Code:
stepCode:
np.arange(1, 6, 2)Code:
[1, 3, 5]To better understand the function, have a look at this video:
I also created this figure to demonstrate how NumPy’s
Code:
arange()![[Image: image-134.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-134.png)
In the code example, we used
Code:
np.arange(10)Code:
start=0Code:
step=1Code:
stop=10If you need an even deeper understanding, I’d recommend you check out our full guide on the Finxter blog.
Recommended Tutorial: NumPy Arange Function — A Helpful Illustrated GuideCreate and Return 2D NumPy Array
You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) nested list and converting the nested list to a NumPy array by passing it into the
Code:
np.array()The following code snippet uses nested list comprehension to create a 2D NumPy array following a more complicated creation pattern:
Code:
import numpy as np def create_array(a,b): ''' Function to return array ''' lst = [[(i+j)**2 for i in range(a)] for j in range(b)] return np.array(lst) arr = create_array(4,3)
print(arr)Output:
Code:
[[ 0 1 4 9] [ 1 4 9 16] [ 4 9 16 25]]I definitely recommend reading the following tutorial to understand nested list comprehension in Python:
Recommended Tutorial: Nested List Comprehension in PythonMore Ways
There are many other ways to return an array in Python.
For example, you can use either of those methods inside the function body to create and initialize a NumPy array:
- Method 1: Use Code:
np.array()
- Method 2: Use Code:
np.zeros()
- Method 3: Use Code:
np.ones()
- Method 4: Use Code:
np.full()
- Method 5: Use Code:
np.empty()
- Method 6: Use Code:
np.arange()
- Bonus: Initialize a NumPy array with CSV data
To get a quick overview what to put into the function and how these methods work, I’d recommend you check out our full tutorial.
Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy WaysRelated Tutorials
Programmer Humor
Code:
Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you.
