Introduction
The numpy.fromfunction()
function is a powerful and flexible tool in Python’s NumPy library. It allows you to create arrays of any shape by generating elements using a function that you define, where the function is called with each index in the array as arguments. This tutorial aims to explain the usage of numpy.fromfunction()
through illustrative examples, ranging from basic to more advanced applications.
The Fundamentals
Before diving into examples, let’s briefly cover what the np.fromfunction()
function is and how it works. The syntax of the function is:
numpy.fromfunction(function, shape, **kwargs)
Where:
function
is the callable that will be executed on each index. The callable must accept as many arguments as there are dimensions inshape
, and return one number.shape
is a tuple defining the shape of the resulting array.**kwargs
are any additional arguments to pass to the function.
Example 1: Creating a 2D Coordinate Grid
Let’s start with a simple example of creating a 2D coordinate grid.
import numpy as np
def function(i, j):
return i, j
grid = np.fromfunction(function, (5, 5), dtype=int)
print(grid)
Output:
[[[0 0]
[1 1]
[2 2]
[3 3]
[4 4]]
...
]
Example 2: Generating a Multiplication Table
A more practical example might be generating a multiplication table.
import numpy as np
def multiply(i, j):
return (i + 1) * (j + 1)
multiplication_table = np.fromfunction(multiply, (10, 10), dtype=int)
print(multiplication_table)
Output:
[[ 1 2 3 4 5 6 7 8 9 10]
[ 2 4 6 8 10 12 14 16 18 20]
...
]
Example 3: Creating a 3D Function Mapping
Building on our knowledge, let’s create a function mapping in 3D.
import numpy as np
def function3D(x, y, z):
return x**2 + y**2 + z**2
threeD_map = np.fromfunction(function3D, (5, 5, 5), dtype=int)
print(threeD_map)
Output:
[[[ 0 1 4 9 16]
[ 1 2 5 10 17]
[ 4 5 8 13 20]
[ 9 10 13 18 25]
[16 17 20 25 32]]
[[ 1 2 5 10 17]
[ 2 3 6 11 18]
[ 5 6 9 14 21]
[10 11 14 19 26]
[17 18 21 26 33]]
[[ 4 5 8 13 20]
[ 5 6 9 14 21]
[ 8 9 12 17 24]
[13 14 17 22 29]
[20 21 24 29 36]]
[[ 9 10 13 18 25]
[10 11 14 19 26]
[13 14 17 22 29]
[18 19 22 27 34]
[25 26 29 34 41]]
[[16 17 20 25 32]
[17 18 21 26 33]
[20 21 24 29 36]
[25 26 29 34 41]
[32 33 36 41 48]]]
Example 4: Applying Conditionals within a fromfunction() Call
In our final example, we’ll introduce conditionals into the function used in np.fromfunction()
, showcasing the flexibility of this method.
import numpy as np
def conditional(i, j):
return np.where(i > j, 1, 0)
conditional_array = np.fromfunction(conditional, (5, 5), dtype=int)
print(conditional_array)
Output:
[[0 0 0 0 0]
[1 0 0 0 0]
[1 1 0 0 0]
[1 1 1 0 0]
[1 1 1 1 0]]
Conclusion
We’ve explored four different applications of the numpy.fromfunction()
function, demonstrating its versatility in generating arrays based on a function of their indices. Whether creating simple coordinate grids or applying complex conditional logic, numpy.fromfunction()
proves to be an invaluable tool in array creation and manipulation within NumPy.