numpy.ma.fromfunction
-
ma.fromfunction(function, shape, **dtype) = <numpy.ma.core._convert2ma object> -
Construct an array by executing a function over each coordinate.
The resulting array therefore has a value
fn(x, y, z)at coordinate(x, y, z).- Parameters
-
-
functioncallable -
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, ifshapewere(2, 2), then the parameters would bearray([[0, 0], [1, 1]])andarray([[0, 1], [0, 1]]) -
shape(N,) tuple of ints -
Shape of the output array, which also determines the shape of the coordinate arrays passed to
function. -
dtypedata-type, optional -
Data-type of the coordinate arrays passed to
function. By default,dtypeis float. -
likearray_like -
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.Note
The
likekeyword is an experimental feature pending on acceptance of NEP 35.New in version 1.20.0.
-
- Returns
-
-
fromfunctionany -
The result of the call to
functionis passed back directly. Therefore the shape offromfunctionis completely determined byfunction. Iffunctionreturns a scalar value, the shape offromfunctionwould not match theshapeparameter.
-
See also
-
indices,meshgrid
Notes
Keywords other than
dtypeare passed tofunction.Examples
>>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) array([[ True, False, False], [False, True, False], [False, False, True]])>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) array([[0, 1, 2], [1, 2, 3], [2, 3, 4]])
© 2005–2021 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.20/reference/generated/numpy.ma.fromfunction.html