Hi Enthusiastic Learners!!
In this Article we will discuss that Why you SHOULD use Universal Functions or Built-in Functions in NumPy? While answering this question we will show you that how much efficient NumPy built-in functions are over standard loops and other approaches.
Let’s begin with an example, imagine we are given an array and we want to multiply each value of array by scalar value
4. In this case standard approach will be to traverse each element of array via loop and then multiply each value by
Creating a 1-Dimensional array and performing desired operation.
import numpy as np # 1-D array array_1d = np.arange(4,14) array_1d
array([ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
We have created an array
array_1d having values between 4 to 14, we will now create a function which will help us to traverse through our 1-D array.
Let name of our function be
def travArray1D(array_1d): # new array for storing our result # an empty array of same lenght as of original array result = np.empty(len(array_1d)) # creating a for loop for i in range(len(array_1d)): result[i] = array_1d[i] * 4 return result
Now, let’s call our function
travArray1D() while passing it values of
# Calling function and printing output result = travArray1D(array_1d) print("\n Final array after multiplication:") print(result)
Final array after multiplication: [16. 20. 24. 28. 32. 36. 40. 44. 48. 52.]
Now to get better judgement of our code we will calculate Time Taken by our function in to complete its operations.
For doing so, we will be using a functionality of IPython —
And to get correct result lets create a huge array having
10000000 elements in it.
huge_array_1d = np.random.randint(1, 100, size=10000000) print("Number of elements in huge array = " + str(len(huge_array_1d)) ) print("\n#### Best time for Huge Array ####\n") %timeit travArray1D(huge_array_1d)
Number of elements in huge array = 10000000 #### Best time for Huge Array #### 1 loop, best of 3: 4.65 s per loop
It took many seconds to complete its operations on the few Million operations. Which is a very high number because now a days amount of data has increased a lot and each device end up facing Billions of operations per second. Thus this approach can’t be used as it is very slow. And if we even talk about a standard computer system even it is doing Billions of Numerical Operations per Sec.
Thus, we need a better approach to deal with issue.
Luckily! Python provides us with Universal Functions (UFuncs)
UFuncs are built-in functions which improves performance of each operation drastically. Especially while working on Arrays, it is wise to use UFuncs.
And one more profit here is Reduced Line of Code, thus saving memory.
Let’s perform earlier operations again, this time, using UFuncs.
# Multiply array_1d with '4' result = array_1d * 4 result
array([16, 20, 24, 28, 32, 36, 40, 44, 48, 52])
Yes, you saw that correct, just a Single Line of Code did the job.
Now, let’s time-it using
%timeit, and see do we get improved performance or not.
print("\n#### Best time for Huge Array ####\n") %timeit (huge_array_1d * 4)
#### Best time for Huge Array #### 100 loops, best of 3: 15.1 ms per loop
Well, that’s the beauty of Universal Functions (built-in functions).
- Total processing time reduced from
Seconds to few Milli-seconds
- Total line of code reduced from
Multiple lines to 1 line
It is enough to justify that, UFuncs provide us not only better performance, also better cod-ability.
You can even use it on multi-dimension arrays. Let’s see an example for that as well.
# Create a multi-dimensional array array_multi = np.array([[2, 3, 5, 6], [7, 8, 9, 4]]) array_multi
array([[2, 3, 5, 6], [7, 8, 9, 4]])
# Multiply multi-dimensional array with '4' array_multi * 4
array([[ 8, 12, 20, 24], [28, 32, 36, 16]])
To learn more about Universal Functions and to know what are such other operations or advanced operations that can be performed on NumPy arrays, please read our next article “Universal Functions in NumPy”
Stay Tuned!! Keep Learning & to get latest updates from
ML for Analytics please Subscribe to our blog via your E-mail. Also, visit our YouTube channel for learning this via videos.