# What does it mean to apply functions element wise?

In this screen, the phrase “Applies Functions Element-wise” was used quite a number of times. What does it mean to apply functions element wise and how is it different from applying the function to accept a series along columns and/or rows.

Thank you

1 Like

Element-wise operations use the concept of `vectorization`. You carry out an operation at once without using loops. A very good example of this is your `np.arrays`. The below operations give the same results but an array should process it faster. This kind of operations are done on dataframe using the `.apply()`

The time difference may not be too significant for this example. But it increases by several order of magnitude for large data sets.

``````import numpy as np
import time

a_list = [2, 4, 6, 8, 10]

# Using for loop/ list comprehension
start = time.time()
mult_1 =[i ** 2 for i in a_list]
print(time.time() - start)
print(mult_1)
print()

# Using arrays/ vectorization
a_arr = np.array(a_list)
start = time.time()
mult_2 = a_arr ** 2
print(time.time() - start)
print(mult_2)

Ouputs
0.00013780593872070312
[4, 16, 36, 64, 100]

0.00011801719665527344
[  4  16  36  64 100]
``````
3 Likes

Hi @toluthapalowo, as @monorienaghogho illustrated in his example, you can perform operations on every single element of the list one by one. In his second example, the individual elements are squared (i.e. 2**2 = 4, 4**2 = 16 and so on…) So you will get a resultant `a_list` of `[4, 16, 36, 64, 100]`.

@monorienaghogho also points out that using element wise multiplication (in the second example) may take more time as compared to using a list-comprehension as in the first example. Note that the first output he is displaying to the console is the time taken for the code to run in seconds and therefore the first method is much more efficient.

2 Likes

Thank you for the explanation @monorienaghogho and @masterryan.prof

1 Like