📜  Python中的 numpy.frompyfunc()

📅  最后修改于: 2022-05-13 01:54:21.986000             🧑  作者: Mango

Python中的 numpy.frompyfunc()

numpy.frompyfunc(func, nin, nout)函数允许创建任意Python函数作为 Numpy ufunc(通用函数)。

例如, abs_value = numpy.frompyfunc(abs, 1, 1) 将创建一个ufunc ,它将返回数组元素的绝对值。

代码#1:

Python3
# Python code to demonstrate the
# use of numpy.frompyfunc
import numpy as np
 
# create an array of numbers
a = np.array([34, 67, 89, 15, 33, 27])
 
# python str function as ufunc
string_generator = np.frompyfunc(str, 1, 1)
 
print("Original array-", a)
print("After conversion to string-", string_generator(a))


Python3
# Python code to demonstrate
# user-defined function as ufunc
import numpy as np
 
# create an array of numbers
a = np.array([345, 122, 454, 232, 334, 56, 66])
 
# user-defined function to check
# whether a no. is palindrome or not
def fun(x):
    s = str(x)
    return s[::-1]== s
     
# 'check_palindrome' as universal function
check_palindrome = np.frompyfunc(fun, 1, 1)
print("Original array-", a)
print("Checking of number as palindrome-",
                      check_palindrome(a))


输出:
Original array- [34 67 89 15 33 27]
After conversion to string- ['34' '67' '89' '15' '33' '27']

代码#2:

Python3

# Python code to demonstrate
# user-defined function as ufunc
import numpy as np
 
# create an array of numbers
a = np.array([345, 122, 454, 232, 334, 56, 66])
 
# user-defined function to check
# whether a no. is palindrome or not
def fun(x):
    s = str(x)
    return s[::-1]== s
     
# 'check_palindrome' as universal function
check_palindrome = np.frompyfunc(fun, 1, 1)
print("Original array-", a)
print("Checking of number as palindrome-",
                      check_palindrome(a))
输出:
Original array- [345 122 454 232 334  56  66]
Checking of number as palindrome- [False False True True False False True]

注意:这个使用frompyfunc创建的自定义 ufunc 总是接受一个 ndarray 作为输入参数,并返回一个 ndarray 对象作为输出。