Recall & Review
beginner
What does the
astype() method do in NumPy?The
astype() method converts the data type of a NumPy array to another specified type, creating a new array with the new type.Click to reveal answer
beginner
How do you convert a NumPy array of floats to integers using
astype()?Use
array.astype(int) to convert all elements from float to integer type.Click to reveal answer
beginner
Does
astype() modify the original array or create a new one?astype() creates a new array with the new type and does not change the original array.Click to reveal answer
intermediate
What happens if you convert a float array with decimal values to integers using
astype()?The decimal part is truncated (cut off), so values are converted to their integer part only.
Click to reveal answer
intermediate
Can you use
astype() to convert a NumPy array to a string type?Yes, you can convert a NumPy array to string type by using
astype(str).Click to reveal answer
What does
array.astype(float) do?✗ Incorrect
astype(float) creates a new array with elements converted to float type without changing the original array.
If you convert
np.array([1.9, 2.5, 3.7]) to int using astype(int), what is the result?✗ Incorrect
Converting floats to int truncates decimals, so values become [1, 2, 3].
Does
astype() modify the original NumPy array?✗ Incorrect
astype() returns a new array and leaves the original unchanged.
Which of these is a valid way to convert a NumPy array to string type?
✗ Incorrect
astype(str) converts array elements to string type.
What happens if you try to convert a string array with non-numeric text to int using
astype(int)?✗ Incorrect
Trying to convert non-numeric strings to int raises a ValueError because conversion is not possible.
Explain how to use
astype() to change the data type of a NumPy array and what happens to the original array.Think about whether the original data changes or not.
You got /3 concepts.
Describe what happens when converting a float array with decimal values to integers using
astype().Consider how decimals are handled when changing to int.
You got /3 concepts.