- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract the fractional and integral parts of a specific array value in Numpy
To extract the fractional and integral parts of a specific array value, use the index value inside the numpy.modf() method. 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
Create an array −
arr = np.array([1.7, 20.4, 100.8, 50, -10.5, np.nan, np.inf])
Display the arrays −
print("Array...
", arr)
Get the type of the arrays −
print("
Our Array type...
", arr.dtype)
Get the dimensions of the Arrays −
print("
Our Array Dimensions...
", arr.ndim)
Get the number of elements in the Array −
print("
Number of elements...
", arr.size)
Return the fractional and integral parts of array value −
print("
The fractional and integral parts of array values...
",np.modf(arr))
To extract the fractional and integral parts of a specific array value, use the index value inside the modf() method −
print("
Result (specific array values)...
",np.modf(arr[0]))
Example
import numpy as np # Create an array arr = np.array([1.7, 20.4, 100.8, 50, -10.5, np.nan, np.inf]) # Display the array print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimensions...
", arr.ndim) # Get the number of elements in the Array print("
Number of elements...
", arr.size) # Return the fractional and integral parts of array value print("
The fractional and integral parts of array values...
",np.modf(arr)) # To extract the fractional and integral parts of a specific array value, use the index value inside the modf() method print("
Result (specific array values)...
",np.modf(arr[0]))
Output
Array... [ 1.7 20.4 100.8 50. -10.5 nan inf] Our Array type... float64 Our Array Dimensions... 1 Number of elements... 7 The fractional and integral parts of array values... (array([ 0.7, 0.4, 0.8, 0. , -0.5, nan, 0. ]), array([ 1., 20., 100., 50., -10., nan, inf])) Result (specific array values)... (0.7, 1.0)
- Related Articles
- Return the fractional and integral parts of a value in Numpy
- Return the fractional and integral parts of array values in Numpy
- Return the truncated value of a specific array element in Numpy
- Multiply the fractional part of two Numpy arrays with a scalar value
- Return the ceil of a specific array element in Numpy
- Return the floor of a specific array element in Numpy
- Return the absolute value of a masked Array in NumPy
- Expand the shape of an array over specific axis in Numpy
- How to find the fractional and integer parts of x in a two-item tuple in Python?
- Reduce a multi-dimensional array and multiply elements along specific axis in Numpy
- Reduce a multi-dimensional array and add elements along specific axis in Numpy
- Divide a scalar value into every element of a masked Array and return the floor value in NumPy
- Divide every element of a masked Array into a scalar value and return the floor value in NumPy
- Return mantissa and exponent as a pair for a specific array element in Numpy
- Create a record array from a (flat) list of array and fetch specific values based on index in Numpy
