The NumPy fabs function in Python is a helpful tool for dealing with numbers. It’s essentially the same as the modulus function in mathematics and is used to calculate the absolute value of a particular number or an array of numerical values. It’s especially useful for NumPy since it works with ndarrays.

Basically, the numpy fabs() function returns the positive magnitude of numeric data. However, it doesn’t work with complex values – for those, you can use the abs() function. All in all, it’s a great way to get a handle on your numbers!

## Syntax of numpy.fabs()

The numpy fabs function looks like the one given below:

```
numpy.fabs(a, /, out=None, *, where=True, casting='same_kind',
order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'fabs'>
```

The above function returns the positive/absolute value element-wise in an array.

**The parameters**

*a*:->The array of numerical values whose absolute magnitudes are to be calculated. If a is a scalar value then the array returned, that is ,b, is also going to be a scalar value.*array_like**out*:)->When storing the result, a location can be specified in this parameter. The shape must be the same as the input array. If it is not provided, or if we assign it to NONE, then a new array is created and returned.*ndarray*,*None,(this parameter is optional**where*->For the locations in which, where==TRUE, the out array will return the ufunc results. Anywhere else, the out array will be retaining it’s original value. If the where parameter is true, then the universal function value is changed and if it is false or not specified, then only the output retains the returned value. To know more about universal functions(ufunc) in python, click here.**array_like, (this parameter is optional**)*kwargs*:->Arguments that involve keywords are specified in this parameter.*(this parameter is also optional)*

**Return value**

The numpy.fabs() function, returns an array, say ‘b’ (for example) which is a scalar in case of a scalar input. It is an array containing the positive magnitudes of all the given numerical data in the input array. The return type is always float.

## Examples of Numpy.fabs()

Let us look at some examples on how to implement and use the numpy fabs() function.

### Example 1 – Calculating Absolute Value Using Numpy

The first one is to return the absolute value of a single element. This code uses the numpy module to calculate** **the absolute value of the number -9 and stores the result in the variable n. The fabs() function is used to calculate the absolute value of any number. Finally, the result is printed using the print() function.

```
#importing required module
import numpy as py
n=py.fabs(-9) #using the fab function and storing the result in a variable
print(n) #printing the result
```

Output:

```
9.0
```

### Example 2 – Passing an Existing Array to Numpy.fabs()

Now, let us take an array with existing values. The code uses the numpy module to import the fabs function. This function takes a list of numbers as input and returns an array of numbers with the absolute value of each number in the list. The code then prints out the list of numbers with the absolute value of each number in the list.

```
#importing required module
import numpy as py
n=[-1.3,-8.6,50.0,-4,-67.55,69.1,0] #pre-defined array
s=py.fabs(n) #using the fabs function
print(s) #printing the result
```

The output of the above code will be something like the one shown below:

```
[1.3 ,8.6 ,50. ,4. ,67.55 ,69.1 ,0.]
```

### Example 3 – Passing User Input Array

Now, we are going to look at another example where the array is going to be an user input. Hence, we need to extract input from the user and then find the absolute value of all the elements of the user defined array. Given below is how we are going to go about it.

```
import numpy as py #importing required modules
n=eval(input("enter required values separated by a comma=")) #take user input
n=py.array(n) #convert user input into an array
s=py.fabs(n) #using the fabs() function
print(s) #displaying the result
```

The output of the above code is going to be as follows:

```
enter required values separated by a comma=-1.9,-5.4,-8.0,-33.33 #enter required input
[ 1.9 5.4 8. 33.33]
```

### Example 4 – Numpy fabs function on 2D arrays

You can also use the fabs function on 2d arrays in the following way. This code imports the NumPy module, creates a 2D array of negative numbers called “n”, then uses the numpy fabs function to compute the absolute values of the elements in the array and store them in the array “s”. Finally, it prints the absolute values of the elements in the array.

```
import numpy as py #import required module
n=[[-1.2,-6.7],[-4.6,-9.1],[-6.9,-2.2]] #initializing 2D array
s=py.fabs(n) #compute absolute value
print("the absolute value of the 2D array is")
print(s) #display the output
```

The output will be as follows:

```
the absolute value of the 2D array is
[[1.2 6.7]
[4.6 9.1]
[6.9 2.2]]
```

## Conclusion:

The numpy fabs() function is an incredibly useful tool for NumPy users to quickly and easily find the absolute value of any number, array, or matrix. It’s especially helpful for dealing with large arrays of numerical values, as it can quickly calculate the absolute value of each item in the array. This makes it much easier to work with complex numbers and data sets. All in all, it’s a great way to get a handle on your numbers and keep them in check!