def my_multiplication(a, b):
#your code that works on multiplying 2 numbers
return c
v_my_multiplication = np.vectorize(my_multiplication)
v_my_multiplication([1, 2, 3], [1, 6])
#Will now work for np.array instead of just 2 numbers and utilize the broadcasting and vectorized implementation benefits that numpy has to offer.
2条答案
按热度按时间62o28rlo1#
你可以在你的函数上使用np.vectorise来获得你的自定义乘法函数,使用所有常见的numpy特性,比如广播。
5tmbdcev2#
尝试
numpy.dot
或x.dot(y)
。请参阅文档here