NumPy Functional programming: frompyfunc() function
numpy.frompyfunc() function
The frompyfunc() function is used to take an arbitrary Python function and returns a NumPy ufunc.
Can be used to add broadcasting to a built-in Python function.
Syntax:
numpy.frompyfunc(func, nin, nout)
Version: 1.15.0
Parameter:
Name | Description | Required / Optional |
---|---|---|
func | An arbitrary Python function. Python function object |
Required |
nin | The number of input arguments. int |
Required |
nout | The number of objects returned by func. int |
Required |
Returns: out : ufunc
Returns a NumPy universal function (ufunc) object.
Notes:
The returned ufunc always returns PyObject arrays.
NumPy.frompyfunc() method Example-1:
Use frompyfunc to add broadcasting to the Python function oct:
>>> import numpy as np
>>> array = np.frompyfunc(oct, 1, 1)
>>> array(np.array((25, 50, 75)))
Output:
array(['0o31', '0o62', '0o113'], dtype=object)
NumPy.frompyfunc() method Example-2:
Use frompyfunc to add broadcasting to the Python function oct:
>>> import numpy as np
>>> array = np.frompyfunc(oct, 1, 1)
>>> np.array((oct(25), oct(50), oct(75))) # for comparison
Output:
array(['0o31', '0o62', '0o113'], dtype='<U5')
Python - NumPy Code Editor:
Previous: vectorize() function
Next: piecewise() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/functional-programming/frompyfunc.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics