Programming Articles

Page 227 of 2547

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 260 Views

The numpy.min_scalar_type() method finds the minimal data type that can hold a given value. For scalars, it returns the data type with the smallest size that can store the value. For arrays, it returns the array's dtype unmodified. Floating point values are not demoted to integers, and complex values are not demoted to floats. Syntax numpy.min_scalar_type(a) Parameters a − The value whose minimal data type is to be found. Can be a scalar or array-like. Basic Examples Let's start with simple scalar values to understand how the function determines minimal data types ...

Read More

Get the approximate number of decimal digits to which this kind of float is precise in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 718 Views

To get the approximate number of decimal digits to which a specific float type is precise, use the precision attribute of the numpy.finfo() method in Python NumPy. The finfo() function provides machine limits for floating point types. Basic Usage Import NumPy and use finfo() with a float type ? import numpy as np # Get float info for different types info16 = np.finfo(np.float16) info32 = np.finfo(np.float32) info64 = np.finfo(np.float64) print("Float16 precision:", info16.precision) print("Float32 precision:", info32.precision) print("Float64 precision:", info64.precision) Float16 precision: 3 Float32 precision: 6 Float64 precision: 15 Detailed ...

Read More

Get the number of bits in the exponent portion of the floating point representation in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 683 Views

To get the number of bits in the exponent portion of the floating point representation, use the iexp attribute of the numpy.finfo() method in Python NumPy. The first parameter is the float data type to get information about. What is numpy.finfo()? The numpy.finfo() function provides machine limits for floating-point types. The iexp attribute specifically returns the number of bits used for the exponent in the IEEE 754 floating-point representation ? Syntax numpy.finfo(dtype).iexp Float16 Type Checking for float16 type. The iexp gets the number of bits in the exponent portion ? ...

Read More

Get the Machine limits information for float types in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 801 Views

To get the machine limits information for float types, use the numpy.finfo() method in Python NumPy. The first parameter is the floating type i.e. the kind of float data type to get information about. Syntax numpy.finfo(dtype) Where dtype is the floating-point data type such as float16, float32, or float64. Getting Float16 Limits Check the machine limits for 16-bit floating-point numbers ? import numpy as np # Get machine limits for float16 a = np.finfo(np.float16) print("Minimum of float16 type...") print(a.min) print("Maximum of float16 type...") print(a.max) Minimum of float16 ...

Read More

Get the Machine limits information for integer types in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 353 Views

To get machine limits information for integer types in Python, use the numpy.iinfo() method. This function returns an object containing the minimum and maximum values for a specified integer data type, helping you understand the range of values that can be stored. Syntax numpy.iinfo(int_type) Parameters: int_type − The integer data type to get information about (e.g., np.int16, np.int32, np.int64) Basic Example Let's check the limits for different integer types ? import numpy as np # Get machine limits for int16 info_16 = np.iinfo(np.int16) print("int16 minimum:", info_16.min) print("int16 maximum:", ...

Read More

Return a scalar type which is common to the input arrays in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 510 Views

To return a scalar type which is common to the input arrays, use the numpy.common_type() method in Python NumPy. This method finds the most appropriate data type that can represent all input arrays without losing precision. The return type will always be an inexact (i.e. floating point) scalar type, even if all the arrays are integer arrays. All input arrays except int64 and uint64 can be safely cast to the returned dtype without loss of information. If one of the inputs is an integer array, the minimum precision type returned is a 64-bit floating point dtype. Syntax ...

Read More

Return the base 2 logarithm for complex value input in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 379 Views

The numpy.log2() function computes the base-2 logarithm of array elements. When working with complex numbers, it returns complex logarithmic values using the formula log₂(z) = ln(z) / ln(2). Syntax numpy.log2(x, out=None, where=True) Parameters The function accepts the following parameters − x − Input array or scalar value out − Optional output array to store results where − Condition to broadcast over input Example with Complex Numbers Here's how to calculate base-2 logarithm for complex values − import numpy as np # Create an array with complex ...

Read More

Determine common type following standard coercion rules in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 233 Views

In NumPy, find_common_type() determines the common data type following standard coercion rules. This function helps when working with mixed data types in arrays and scalars, returning the most appropriate common type. Syntax numpy.find_common_type(array_types, scalar_types) Parameters The function takes two parameters: array_types − A list of dtypes or dtype convertible objects representing arrays scalar_types − A list of dtypes or dtype convertible objects representing scalars How It Works The method returns the common data type, which is the maximum of array_types ignoring scalar_types, unless the maximum of scalar_types is of ...

Read More

Return the length of a string array element-wise in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To return the length of a string array element-wise, use the numpy.char.str_len() method in Python NumPy. The method returns an output array of integers representing the length of each string element. Syntax numpy.char.str_len(a) Parameters: a − Array-like of str or unicode Returns: Array of integers representing the length of each string element. Basic Example Let's create a simple string array and find the length of each element ? import numpy as np # Create array of strings names = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom']) # Get ...

Read More

Test whether similar int type of different sizes are subdtypes of integer class in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 133 Views

To test whether similar int type of different sizes are subdtypes of integer class, use the numpy.issubdtype() method in Python NumPy. The parameters are the dtype or object coercible to one. Syntax numpy.issubdtype(arg1, arg2) Parameters: arg1: dtype or object coercible to one arg2: dtype or object coercible to one Returns: Boolean value indicating whether arg1 is a subtype of arg2. Testing Signed Integer Subtypes First, let's check if different sized integer types are subtypes of np.signedinteger − import numpy as np # Testing different signed integer sizes ...

Read More
Showing 2261–2270 of 25,466 articles
« Prev 1 225 226 227 228 229 2547 Next »
Advertisements