This is a detailed tutorial of NumPy Universal Functions. Learn about the different functions that you can apply to the `ndArray`

Objects.

Table of Contents

## Ufuncs

Ufuncs stands for universal functions. These are those function which can be helpful in operating NumPy Arrays. They help in operating the NumPy array, which we refer to as `ndarray`

objects. They operate in element by element sequence, and this function supports various standard features. This takes in a fixed number of inputs and in return, gives us a fixed number of outputs. So this function is “vectorized” wrapper.

These are mostly used in the implementation of vectorization in Numpy. This is a very fast way if iterating every element. These are also very helpful in the computation, and they also help in broadcasting.

These also take in a various additional parameter which is really helpful in the data manipulation. Some of them are:

`where`

– This is a conditional argument that will define where the operation will take place.`dtype`

– This will help in defining the return type of the elements.`out`

– It is the output array where record values will be put in.

As a result, we hugely depend on vectorization, so we need to know what vectorization is?

So Vectorization is the process of converting iterative value into vector-based operations. It is a really quick process because of the latest CPUs. As a result, the conversion rate is pretty high. And it takes very little time in completing the process.

### Examples

Now let we will try to add two lists of arrays, so one way of doing that is by iterating over both lists and then add them up. let us take an example without using Ufuncs:

#First we will take the lists we want to add up a=[9, 8, 7, 6] b=[3, 4, 5, 6] # now we will take an empty list where all the new values will appear. c=[] #we will use the zip() method here which is a python built-in method for i, j in zip(a, b): #now we will append the two lists c.append(i + j) #now we will print the new list print(c)

**Output.**

[12, 12, 12, 12]

So here we add up the two lists by using the zip() method. Let us take another example of the same.

#First we will take the lists we want to add up a=[9, 4, 2, 1] b=[6, 3, 8, 7] # now we will take an empty list where all the new values will appear. c=[] #we will use the zip() method here which is a python built-in method for i, j in zip(a, b): #now we will append the two lists c.append(i + j) #now we will print the new list print(c)

**Output.**

[15, 7, 10, 8]

So here we took a different set of values in the list. As a result, we see that add up the two and display the result in the third one.

Now in NumPy, we have a separate function for thin in Ufuncs which is `add(x,y)`

. This will also give us the same results.

Now let us take an example to understand it better:

#now we will import the numpy package and then make an alias as np import numpy as np # here we will take the two set of values which we are going to add up a=[9, 4, 2, 1] b=[6, 3, 8, 7] #we will use the add() function here c=np.add(a, b) #now we will print the added list print(c)

**Output.**

[15 7 10 8]

Here in this example, we are adding the two sets of values using the add() function, which is part of the Ufuncs library.

Let us take another example:

#now we will import the numpy package and then make an alias as np import numpy as np # here we will take the two set of values which we are going to add up a=[3, 6, 3, 2] b=[5, 7, 9, 8] #we will use the add() function here c=np.add(a, b) #now we will print the added list print(c)

**Output.**

[ 8 13 12 10]

Similarly, we will take another example in which we will be using the subtract():

#now we will import the numpy package and then make an alias as np import numpy as np # here we will take the two set of values which we are going to add up a=[9, 4, 2, 1] b=[6, 3, 8, 7] #we will use the add() function here c=np.subtract(a, b) #now we will print the added list print(c)

**Output.**

[ 3 1 -6 -6]

Here in this example, we are subtracting the two lists. As a result, we see how the two lists are being subtracted and then giving us value in negative as well.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!