Complete the code to create a NumPy array with integer data type.
import numpy as np arr = np.array([1, 2, 3, 4], dtype=[1]) print(arr.dtype)
Using int32 sets the array data type to 32-bit integers, which is efficient for integer data.
Complete the code to create a NumPy array of floats for better precision.
import numpy as np arr = np.array([1.0, 2.5, 3.1], dtype=[1]) print(arr.dtype)
float64 is used for floating-point numbers with double precision, suitable for decimal values.
Fix the error in the code by choosing the correct dtype for a boolean array.
import numpy as np arr = np.array([True, False, True], dtype=[1]) print(arr.dtype)
The bool dtype is used for arrays of True/False values, which is memory efficient.
Fill both blanks to create a NumPy array of integers and convert it to float for better precision.
import numpy as np arr = np.array([1, 2, 3], dtype=[1]) arr_float = arr.astype([2]) print(arr_float.dtype)
Start with an integer array (int32) and convert it to floating-point (float64) for decimal precision.
Fill both blanks to create a float array, check its size, and convert it to int to save memory.
import numpy as np arr = np.array([1.5, 2.3, 3.7], dtype=[1]) print(arr.nbytes) arr_int = arr.astype([2]) print(arr_int.nbytes) print(arr_int)
Start with float64 for precision, then convert to int32 to reduce memory size. The printed sizes show memory usage before and after conversion.