python numpy 函数矢量化
"""
函数矢量化 矢量化后 可以对数组数据批量操作
减少for循环的使用
"""
import numpy as np
import math as m
def foo(x, y):
return m.sqrt(x ** 2 + y ** 2)
x, y = 3, 4
print(foo(x, y))
# numpy数组数据
x, y = np.array([3, 4, 5]), np.array([4, 5, 6])
# 报错
# print(foo(x, y))
# 方法一:矢量化函数
foo_vec = np.vectorize(foo)
print(foo_vec(x, y))
print(foo_vec(x, y).dtype)
# 方法二:frompyfunc
foo_func = np.frompyfunc(foo, 2, 1)
print(foo_func(x, y))
print(foo_func(x, y).dtype)