Creating Arrays with an Intentional Type
Unit ID: M08-U01 Estimated active time: 20-30 minutes
Create arrays from supplied values and state the intended type. Integers suit whole minutes. Floating-point values suit rates or means.
import numpy as np
study_minutes = np.array(
[[30, 45, 60, 45], [20, 35, 40, 55]],
dtype=np.int64,
)
print(study_minutes.dtype)
NumPy chooses a common type for the array. A text value can force an unexpected string type, so validation belongs before calculation.
Useful constructors include np.zeros, np.ones, np.arange, and np.linspace. Use them when the generated values have a clear meaning; do not replace understandable supplied data with clever construction.
Practice: create a 2 x 3 integer array of fictional quiz attempts. Check its dtype and explain why object or text dtype would be a warning for this task.
