numpy.fabs() function is used to compute the absolute values element-wise. This function returns the absolute values (positive magnitude) of the data in arr. It always return absolute values in floats.
Syntax : numpy.fabs(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘fabs’)
Parameters :
arr : [array_like] The array of numbers for which the absolute values are required.
out : [ndarray, optional] A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
**kwargs : Allows to pass keyword variable length of argument to a function. It is used when we want to handle named argument in a function.
where : [array_like, optional] True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.Return : [ndarray or scalar] The absolute values of arr, the returned values are always floats.
Code #1 : Working
# Python program explaining # fabs() function import numpy as geek in_num = 10print ("Input number : ", in_num) out_num = geek.fabs(in_num) print ("Absolute value of positive input number : ", out_num) |
Output :
Input number : 10 Absolute value of positive input number : 10.0
Code #2 :
# Python program explaining # fabs() function import numpy as geek in_num = -9.0print ("Input number : ", in_num) out_num = geek.fabs(in_num) print ("Absolute value of negative input number : ", out_num) |
Output :
Input number : -9.0 Absolute value of negative input number : 9.0
Code #3 :
# Python program explaining # fabs() function import numpy as geek in_arr = [2, 0, -2, -5] print ("Input array : ", in_arr) out_arr = geek.fabs(in_arr) print ("Output absolute array : ", out_arr) |
Output :
Input array : [2, 0, -2, -5] Output absolute array : [ 2. 0. 2. 5.]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

