numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
fromfunction(function, shape, *, dtype=<class 'float'>, like=None, **kwargs)

The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z) .

Notes

Keywords other than dtype are passed to :None:None:`function`.

Parameters

function : callable

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]])

shape : (N,) tuple of ints

Shape of the output array, which also determines the shape of the coordinate arrays passed to :None:None:`function`.

dtype : data-type, optional

Data-type of the coordinate arrays passed to :None:None:`function`. By default, dtype is float.

like : array_like

Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports 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.

versionadded

Returns

fromfunction : any

The result of the call to :None:None:`function` is passed back directly. Therefore the shape of fromfunction is completely determined by :None:None:`function`. If :None:None:`function` returns a scalar value, the shape of fromfunction would not match the shape parameter.

Construct an array by executing a function over each coordinate.

See Also

indices
meshgrid

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]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.asarray numpy.asarray_chkfinite dask.array.creation.fromfunction numpy.fromfunction numpy.asanyarray

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/core/numeric.py#1787
type: <class 'function'>
Commit: