-
numpy.fromfunction(function, shape, **kwargs)
[source] -
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: 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, ifshape
were(2, 2)
, then the parameters in turn be (0, 0), (0, 1), (1, 0), (1, 1).shape : (N,) tuple of ints
Shape of the output array, which also determines the shape of the coordinate arrays passed to
function
.dtype : data-type, optional
Data-type of the coordinate arrays passed to
function
. By default,dtype
is float.Returns: fromfunction : any
The result of the call to
function
is passed back directly. Therefore the shape offromfunction
is completely determined byfunction
. Iffunction
returns a scalar value, the shape offromfunction
would match theshape
parameter.Notes
Keywords other than
dtype
are passed tofunction
.Examples
1234>>> np.fromfunction(
lambda
i, j: i
=
=
j, (
3
,
3
), dtype
=
int
)
array([[
True
,
False
,
False
],
[
False
,
True
,
False
],
[
False
,
False
,
True
]], dtype
=
bool
)
1234>>> np.fromfunction(
lambda
i, j: i
+
j, (
3
,
3
), dtype
=
int
)
array([[
0
,
1
,
2
],
[
1
,
2
,
3
],
[
2
,
3
,
4
]])
numpy.fromfunction()

2025-01-10 15:47:30
Please login to continue.