numpy.frompyfunc() in Python (original) (raw)
Last Updated : 29 Jun, 2021
numpy.frompyfunc(func, nin, nout) function allows to create an arbitrary Python function as Numpy ufunc (universal function).
Parameters:
func: [A python function object ] An arbitrary python function
nin: [int] Number of input arguments to that function.
nout: [int] Number of objects returned by that function.
Return: A Numpy universal function object.
For example, abs_value = numpy.frompyfunc(abs, 1, 1) will create a ufunc that will return the absolute values of array elements.
Code #1:
Python3
import
numpy as np
a
=
np.array([
34
,
67
,
89
,
15
,
33
,
27
])
string_generator
=
np.frompyfunc(
str
,
1
,
1
)
print
(
"Original array-"
, a)
print
(
"After conversion to string-"
, string_generator(a))
Output:
Original array- [34 67 89 15 33 27] After conversion to string- ['34' '67' '89' '15' '33' '27']
Code #2:
Python3
import
numpy as np
a
=
np.array([
345
,
122
,
454
,
232
,
334
,
56
,
66
])
def
fun(x):
`` s
=
str
(x)
`` return
s[::
-
1
]
=
=
s
check_palindrome
=
np.frompyfunc(fun,
1
,
1
)
print
(
"Original array-"
, a)
print
(
"Checking of number as palindrome-"
,
`` check_palindrome(a))
Output:
Original array- [345 122 454 232 334 56 66] Checking of number as palindrome- [False False True True False False True]
Note: This custom ufunc created using frompyfunc always accept a ndarray as an input argument and also return a ndarray object as output.
Similar Reads
- numpy.fromiter() function – Python NumPy's fromiter() function is a handy tool for creating a NumPy array from an iterable object. This iterable can be any Python object that provides elements one at a time. The function is especially useful when you need to convert data from a custom data source, like a file or generator, into a Num 2 min read
- numpy.fromfunction() function – Python numpy.fromfunction() function construct an array by executing a function over each coordinate and the resulting array, therefore, has a value fn(x, y, z) at coordinate (x, y, z). Syntax : numpy.fromfunction(function, shape, dtype) Parameters : function : [callable] The function is called with N para 2 min read
- numpy.fromstring() function – Python numpy.fromstring() function create a new one-dimensional array initialized from text data in a string. Syntax : numpy.fromstring(string, dtype = float, count = -1, sep = ' ') Parameters : string : [str] A string that contained the data. dtype : [data-type, optional] Data-type of the array. Default d 1 min read
- numpy.frombuffer() function – Python numpy.frombuffer() function interpret a buffer as a 1-dimensional array. Syntax : numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0) Parameters : buffer : [buffer_like] An object that exposes the buffer interface. dtype : [data-type, optional] Data-type of the returned array, default da 1 min read
- NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read
- numpy.i0() function | Python numpy.i0() function is the modified Bessel function of the first kind, order 0. it's usually denoted by I0. Syntax : numpy.i0(x) Parameters : x : [array_like, dtype float or complex] Argument of the Bessel function. Return : [ndarray, shape = x.shape, dtype = x.dtype] The modified Bessel function ev 1 min read
- numpy.exp2() in Python numpy.exp2(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate 2**x for all x being the array elements. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray, optional 2 min read
- numpy.info() function in Python In Numpy we can get all the information about the function, class, or module like what will the parameter and what will be the type of the return value with the help of numpy.info() function. This function returns the help information for a function, class, or module. Syntax: numpy.info(numpy.info(o 1 min read
- numpy.asarray() in Python numpy.asarray()function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays. Syntax : numpy.asarray(arr, dtype=None, order=None) Parameters : arr : [array_like] Input data, in any form that can be converted to a 2 min read
- numpy.roots() function - Python numpy.roots() function return the roots of a polynomial with coefficients given in p. The values in the rank-1 array p are coefficients of a polynomial. If the length of p is n+1 then the polynomial is described by: p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] Syntax : numpy.roots(p) Parame 1 min read