- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Construct an array by executing a function over each coordinate in Numpy
To construct an array by executing a function over each coordinate, use the numpy.fromfunction() method in Python Numpy. The function is called with N parameters, where N is the rank of shape. Each parameter represents the coordinates of the array varying along a specific axis. For example, if shape were (2, 2), then the parameters would be array([[0, 0], [1, 1]]) and array([[0, 1], [0, 1]])
The fromfunction() returns the result of the call to function is passed back directly. Therefore, the shape of fromfunction is completely determined by function. If function returns a scalar value, the shape of fromfunction would not match the shape parameter.
Steps
At first, import the required library −
import numpy as np
To construct an array by executing a function over each coordinate, use the numpy.fromfunction() method in Python Numpy −
print("Result
",np.fromfunction(lambda a, b: a, (3, 3), dtype=float)) print("
Result
",np.fromfunction(lambda a, b: b, (3, 3), dtype=float))
Checking for array elements equality −
print("
Result
",np.fromfunction(lambda a, b: a == b, (4, 4), dtype=int))
Adding elements −
print("
Result
",np.fromfunction(lambda a, b: a + b, (5, 5), dtype=int))
Checking with greater than −
print("
Result
",np.fromfunction(lambda a, b: a > b, (5, 5), dtype=int))
Checking with less than −
print("
Result
",np.fromfunction(lambda a, b: a < b, (5, 5), dtype=int))
Example
import numpy as np # To construct an array by executing a function over each coordinate, use the numpy.fromfunction() method in Python Numpy print("Result
",np.fromfunction(lambda a, b: a, (3, 3), dtype=float)) print("
Result
",np.fromfunction(lambda a, b: b, (3, 3), dtype=float)) # Checking for array elements equality print("
Result
",np.fromfunction(lambda a, b: a == b, (4, 4), dtype=int)) # Adding elements print("
Result
",np.fromfunction(lambda a, b: a + b, (5, 5), dtype=int)) # Checking with greater than print("
Result
",np.fromfunction(lambda a, b: a > b, (5, 5), dtype=int)) # Checking with less than print("
Result
",np.fromfunction(lambda a, b: a < b, (5, 5), dtype=int))
Output
Result [[0. 0. 0.] [1. 1. 1.] [2. 2. 2.]] Result [[0. 1. 2.] [0. 1. 2.] [0. 1. 2.]] Result [[ True False False False] [False True False False] [False False True False] [False False False True]] Result [[0 1 2 3 4] [1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7] [4 5 6 7 8]] Result [[False False False False False] [ True False False False False] [ True True False False False] [ True True True False False] [ True True True True False]] Result [[False True True True True] [False False True True True] [False False False True True] [False False False False True] [False False False False False]]
- Related Articles
- Use an index array to construct a new array from a set of choices in Numpy
- Numpy Array advantage over a Nested List
- Expand the shape of an array over axis 1 in Numpy
- Expand the shape of an array over specific axis in Numpy
- Expand the shape of an array over axis 0 in Numpy
- Executing an inner join over RFC on database tables in SAP system
- Expand the shape of an array over tuple of axis in Numpy
- Set a 1-D iterator over a Numpy array
- Cube each element in a Numpy array
- Use an index array to construct a new array from a set of choices with wrap mode in Numpy
- Use an index array to construct a new array from a set of choices with clip mode in Numpy
- Construct a record array from a wide-variety of objects in Numpy
- Remove axes of length one from an array over specific axis in Numpy
- Remove axes of length one from an array over axis 0 in Numpy
- divisibleBy() function over array in JavaScript
