0
0
NumPydata~5 mins

Type casting with astype() in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AChanges the original array in place
BConverts array elements to float type
CDeletes the array
DConverts array elements to string type
If you convert np.array([1.9, 2.5, 3.7]) to int using astype(int), what is the result?
A[1, 2, 3]
B[1.9, 2.5, 3.7]
C[1, 2, 3] but as floats
D[2, 3, 4]
Does astype() modify the original NumPy array?
AYes, it changes the original array
BIt deletes the original array
CNo, it returns a new array with the new type
DIt only works on lists
Which of these is a valid way to convert a NumPy array to string type?
Aarray.convert(str)
Barray.astype(int)
Carray.to_string()
Darray.astype(str)
What happens if you try to convert a string array with non-numeric text to int using astype(int)?
AIt raises a ValueError
BConversion succeeds with no errors
CIt converts all to zero
DIt converts to float instead
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.