Construct a single-precision scalar
Creating a NumPy floating-point scalar from an integer, string, or Python numeric value.
NumPy · Type Alias
np.float32 is an alias for NumPy’s single scalar type. It represents a 32-bit single-precision floating-point value compatible with C float.
np.float32 is an alias for NumPy’s single scalar type. It represents a 32-bit floating-point value and can be used as a dtype when constructing arrays or as a callable to create a scalar.
numpy.float32[source]np.float32 is an alias of the NumPy single scalar type.float.numpy.float32; its C-like NumPy name is numpy.single, corresponding to C float.np.float32.import numpy as np
values = np.array([1, 2, 3], dtype=np.float32)
scalar = np.float32(0.1)
print(values.dtype) # float32
print(type(scalar).__name__) # float32Creates a float32 array and a single-precision NumPy scalar.
Creating a NumPy floating-point scalar from an integer, string, or Python numeric value.
Specifying the element type during array creation or conversion.
Interfacing with code or storage formats that require C-compatible single-precision floats.
np.float32?np.float32 is an alias of np.single. The type is a single-precision floating-point type compatible with C float, using 32 bits: one sign bit, eight exponent bits, and 23 mantissa bits.
Follow-up: How would you force an array to use it? Pass dtype=np.float32 to an array-creation function or convert an existing array with .astype(np.float32).
np.float32?Use np.float32(value) to create a NumPy scalar, or provide dtype=np.float32 when creating an array. The scalar type is available from NumPy’s top-level API.
Follow-up: What should you inspect to verify an array’s representation? Check its .dtype, which reports the array’s NumPy data type.
np.float32 relate to other names and C types?np.float32 is the canonical NumPy API name, while np.single is its Python API C-like name. Its corresponding C type is float, and its character code is f.
Follow-up: Why might this type be chosen over the default Python float? It uses a fixed 32-bit representation, which can reduce storage compared with typical 64-bit Python floating-point values.
numpy.singlenumpy.float16numpy.float64numpy.dtype