0

I am new at python and numpy. Consider that I have a function called addTwo that just returns the given value + 2. My question is what's the difference between np.vectorize(addTwo )(matrix) and addTwo (matrix). both give me the same output. Basically my question is: if I have following code:

import numpy as np
def addTwo(a):
    return a + 2

matr = np.array([[1, 2], [2, 3], [3, 4]])

I wonder difference between these two:

addTwo(matr)
np.vectorize(addTwo)(matr)
Hector Barbossa
  • 455
  • 1
  • 5
  • 10
  • add your example to the post please – pd shah Oct 22 '17 at 08:22
  • Stay away from `vectorize` for now. Beginners often misuse it, thinking it's a short cut to real 'vectorize'. The docs are long but should be read in full. – hpaulj Oct 22 '17 at 09:35
  • A recent question about this, https://stackoverflow.com/q/46593021 and https://stackoverflow.com/q/44746576 – hpaulj Oct 22 '17 at 09:47

1 Answers1

1

According to documentation ( https://docs.scipy.org/doc/numpy-1.9.1/reference/generated/numpy.vectorize.html): The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop. It means there is no reazon in vectorize of function wich could be applied directly as it is in your example. Actually this could lead to degraded performance. Main goal of the "vectorize" is to hide a for loop from you code. But it will not avoid it neither change expected results.

Timofey Chernousov
  • 1,284
  • 8
  • 12