Contents

# Course Contents

1. Introduction
2. NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
3. Intermediate Array Stuff
3.2 newaxis
3.3 reshape
3.4 boolean indexing
3.5 nan
3.6 infinity
3.7 random
4. Common Operations
4.1 where
4.2 Math Funcs
4.3 all and any
4.4 concatenate
4.5 Stacking
4.6 Sorting
4.7 unique
5. Challenges

This video covers numpy’s special floating point constant, infinity.

## Code

``````import numpy as np

# np.inf and np.NINF
np.array([np.inf, np.NINF])  # [ inf, -inf]

# more commonly, these values occur when you divide by 0
np.array([-1, 1])/0  # [-inf,  inf]

# behaviors
np.inf * 22  # inf
np.inf + np.inf # inf
np.inf - np.inf  # nan
np.inf / np.inf    # nan

# positive infinity equals positive infinity and negative infinity equals negative infinity
np.inf == np.inf   # True
np.NINF == np.NINF # True

# isolate infinite values by checking == positive infinity or == negative infinity
foo = np.array([4.4, np.inf, 1.0, np.NINF, 3.1, np.inf])
foo == np.inf  # [False,  True, False, False, False,  True]
foo == np.NINF # [False, False, False,  True, False, False]

# Alternatively,
np.isposinf(foo)  # [False,  True, False, False, False,  True]
np.isneginf(foo)  # [False, False, False,  True, False, False]
np.isinf(foo)     # [False,  True, False,  True, False,  True]
``````

## Transcript

Like nan, numpy reserves floating point constants for infinity and negative infinity that behave specially. If you want to insert these values directly, you can use np.inf and np.NINF.
More commonly, these values occur when you divide by 0.
All the special behaviors you might expect for these values exist such as

• multiplying infinity by a positive constant equals infinity
• adding infinity to infinity equals infinity
• subtracting infinity from infinity has undefined behavior and produces nan - and dividing infinity by infinity has undefined behavior and produces nan

Unlike nan, positive infinity equals positive infinity and negative infinity equals negative infinity. So, if you have an array, you can isolate infinite values just by checking == positive infinity or == negative infinity.
Alternatively, you can also use the functions isposinf(), isneginf() and isinf().