Find the minimal data type of a scalar value 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 −

print("Using the min_scalar() method in Numpy\n")
print("Result...",np.min_scalar_type(55))
print("Result...",np.min_scalar_type(38.9))
print("Result...",np.min_scalar_type(-78))
print("Result...",np.min_scalar_type(479))
print("Result...",np.min_scalar_type(2e100))
print("Result...",np.min_scalar_type(-45.8))
print("Result...",np.min_scalar_type(6.5e100))

Example

# 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.

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(55))
print("Result...",np.min_scalar_type(38.9))
print("Result...",np.min_scalar_type(-78))
print("Result...",np.min_scalar_type(479))
print("Result...",np.min_scalar_type(2e100))
print("Result...",np.min_scalar_type(-45.8))
print("Result...",np.min_scalar_type(6.5e100))

Output

Using the min_scalar() method in Numpy

Result... uint8
Result... float16
Result... int8
Result... uint16
Result... float64
Result... float16
Result... float64

Updated on: 28-Feb-2022

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements