Multiply the fractional part of two Numpy arrays with a scalar value


To return the fractional and integral parts of array values, use the numpy.modf() method in Python Numpy. Multiply the fractional values using the index 0 values. The fractional and integral parts are negative if the given number is negative.

The out is 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. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

Creating two 2D numpy arrays using the array() method −

arr1 = np.array([1.7, 20.4, 100.8, 45.8, -22.7, np.nan, np.inf])
arr2 = np.array([5.1, 41.2, 120.4, 30.4, -69.6, np.nan, np.inf])

Display the arrays −

print("Array 1...
", arr1) print("
Array 2...
", arr2)

Get the type of the arrays −

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

Get the dimensions of the Arrays −

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

To return the fractional and integral parts of array values, use the numpy.modf() method in Python Numpy. Multiply the fractional values using the index 0 values −

print("
Result...
",np.modf(arr1[0]*arr2[0]))

Example

import numpy as np

# Creating two 2D numpy arrays using the array() method
arr1 = np.array([1.7, 20.4, 100.8, 45.8, -22.7, np.nan, np.inf])
arr2 = np.array([5.1, 41.2, 120.4, 30.4, -69.6, np.nan, np.inf])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # To return the fractional and integral parts of array values, use the numpy.modf() method in Python Numpy # Multiply the fractional values using the index 0 values print("
Result...
",np.modf(arr1[0]*arr2[0]))

Output

Array 1...
[ 1.7 20.4 100.8 45.8 -22.7 nan inf]

Array 2...
[ 5.1 41.2 120.4 30.4 -69.6 nan inf]

Our Array 1 type...
float64

Our Array 2 type...
float64

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
1

Result...
(0.6699999999999999, 8.0)

Updated on: 07-Feb-2022

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements