Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python3
numpy

How to use the numpy.fabs() method for a 2-D array

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The numpy.fabs() method

The numpy.fabs() method computes the absolute values of an array input. This is done element by element.

The method fabs() is similar to absolute(). However, it cannot handle complex values. To find the absolute values of complex data, we can use the absolute() method instead.

Note: To create a two-dimensional (2-D) array in Python, use a list of lists.

Syntax

Let’s view the syntax of this method:

numpy.fabs(arr,out=None, where=True)

Parameters

  • arr: This represents the input array.
  • out: This specifies the location of the result. This is an optional parameter.
  • where: This represents the condition under which the input gets broadcasted. This is an optional parameter.

Return value

The numpy.fabs() method returns absolute values of an array. The type of the return value is float.

Example

The following code shows how to use the numpy.fabs() method for python two-dimensional (2-D) arrays:

# import numpy
import numpy as np
# create 2D array using np.array
arr = np.array([[-7,-3.4,-1.2], [-2,3,-9.5]])
# Compute the absolute values
# using np.fabs()
result = np.fabs(arr)
print(result)

Explanation

  • Line 2: We import the numpy library.
  • Lines 4: We create a 2-D array called arr.
  • Line 7: We use the np.fabs() method to calculate the absolute values of the array arr.
  • Line 9: Finally, we display the result.

RELATED TAGS

python3
numpy

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring