Find the minimal data type of an array-like in Python


The numpy.min_scalar() method finds the minimal data type. The 1st parameter is the value whose minimal data type is to be found. For scalar, returns the data type with the smallest size and smallest scalar kind which can hold its value. For non-scalar array, returns the vector’s dtype unmodified. Floating point values are not demoted to integers, and complex values are not demoted to floats.

Steps

At first, import the required library −

import numpy as np

The numpy.min_scalar() method finds the minimal data type. The 1st parameter is the value whose minimal data type is to be found −

print("Using the min_scalar() method in Numpy\n")
print("Result...",np.min_scalar_type(np.arange(4,dtype='f8')))
print("Result...",np.min_scalar_type(np.arange(38.9, dtype = 'f8')))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))
print("Result...",np.min_scalar_type(np.array(280, 'i1')))
print("Result...",np.min_scalar_type(np.array(80, 'u1')))
print("Result...",np.min_scalar_type(np.array(300.7, np.float32)))
print("Result...",np.min_scalar_type(np.array(120.6, np.float64)))
print("Result...",np.min_scalar_type(np.array(7.2e100, np.float32)))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))

Example

import numpy as np

# The numpy.min_scalar() method finds the minimal data type.
# The 1st parameter is the value whose minimal data type is to be found.
print("Using the min_scalar() method in Numpy\n")

print("Result...",np.min_scalar_type(np.arange(4,dtype='f8')))
print("Result...",np.min_scalar_type(np.arange(38.9, dtype = 'f8')))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))
print("Result...",np.min_scalar_type(np.array(280, 'i1')))
print("Result...",np.min_scalar_type(np.array(80, 'u1')))
print("Result...",np.min_scalar_type(np.array(300.7, np.float32)))
print("Result...",np.min_scalar_type(np.array(120.6, np.float64)))
print("Result...",np.min_scalar_type(np.array(7.2e100, np.float32)))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))

Output

Using the min_scalar() method in Numpy

Result... float64
Result... float64
Result... float64
Result... uint8
Result... uint8
Result... float16
Result... float16
Result... float16
Result... float64

Updated on: 24-Feb-2022

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements