Is product of random values with ninf values possible in Python?

By IncludeHelp Last updated : November 25, 2023

Yes, it is possible to multiply random values with negative infinity (ninf or -np.inf) in NumPy. If you perform a mathematical operation on an array/matrix of random values and ninf (-np.inf), the result will always be an array of negative infinity (ninf) values, regardless of the other values in the array.

Example

In this example, there is a matrix with random values and performing product (multiply) operation on it with negative infinity (-np.inf). The result will always be negative infinity (-np.inf) as all values of the matrix.

# Importing numpy library
import numpy as np

# Creating a 3x3 matrix with random values
mat = np.random.rand(3, 3)

# Printing the matrix
print("Matrix is:\n", mat, "\n")

# Product of ninf and random values
result = mat * -(np.inf)

# Printing result
print("Resultant matrix is:\n", result)

Output

The output of the above example is:

RUN 1:

Matrix is:
 [[0.47503765 0.89852215 0.44089459]
 [0.99020252 0.1870008  0.45106105]
 [0.95367231 0.60086548 0.29832788]] 

Resultant matrix is:
 [[-inf -inf -inf]
 [-inf -inf -inf]
 [-inf -inf -inf]]

RUN 2:

Matrix is:
 [[0.90651728 0.54580694 0.61908765]
 [0.63088148 0.40662232 0.40957797]
 [0.85507069 0.93435799 0.68297454]] 

Resultant matrix is:
 [[-inf -inf -inf]
 [-inf -inf -inf]
 [-inf -inf -inf]]

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.