Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
python
communitycreator

# What is the numpy.nan_to_num() function? Onyejiaku Theophilus Chidalu

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.

### Overview

The nan_to_num() function in NumPy replaces a NaN value with 0, and infinity with large infinite numbers or with numbers that are defined by the user using the nan, posinf, and/or neginf keywords.

### Syntax

numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)
Syntax for the nan_to_num() function in NumPy

### Parameter values

The nan_to_num() function takes the following parameter values:

• x: This is the input value to be replaced. It is a required value.
• copy: This takes a Boolean value that indicates whether to create a copy of the input value or to replace the values in place. It is an optional value.
• nan: This is the value that replaces the NaN values. When this parameter value is not passed, the NaN values are replaced by 0. This is an optional value.
• posinf: This is the value that replaces the positive infinity values. When this parameter value is not passed, the positive infinity values are replaced by a very large number. This is an optional value.
• neginf: This is the value that replaces the negative infinity values. When this parameter value is not passed, the negative infinity values are replaced by a very small number. This is an optional value.

### Return value

The nan_to_num() function returns the input data with the non-finite values replaced.

### Code example

import numpy as np
# Creating the input data
mydata = np.array([np.inf, -np.inf, np.nan, -165, 165])

# Implementing the nan_to_num() function
myarray1 = np.nan_to_num(mydata)
myarray2 = np.nan_to_num(mydata, nan=-9999, posinf=33333333, neginf=33333333)

print(myarray1)
print(myarray2)
Implementing the nan_to_num() function

### Code explanation

• Line 1: We import the numpy module.
• Line 3: We use the array() function to create the input array mydata. This array contains positive and negative infinity values, as well as NaN values.
• Line 6: We implement the nan_to_num() function on mydata by using the default values of each parameter of the function. We assign the result to a variable myarray1.
• Line 7: We implement the nan_to_num() function on the mydata. We assign the result to a variable myarray2.
• Line 9: We print the variable myarray1.
• Line 10: We print the variable myarray2.

RELATED TAGS

function
numpy
python
communitycreator

CONTRIBUTOR Onyejiaku Theophilus Chidalu

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

Learn in-demand tech skills in half the time 