Bird
0
0

Why might converting a NumPy array of floats to integers using astype(int) lead to unexpected results?

hard📝 Conceptual Q10 of 15
NumPy - Array Data Types
Why might converting a NumPy array of floats to integers using astype(int) lead to unexpected results?
ABecause <code>astype(int)</code> truncates decimals instead of rounding
BBecause <code>astype(int)</code> rounds decimals automatically
CBecause <code>astype(int)</code> converts floats to strings first
DBecause <code>astype(int)</code> changes the array shape
Step-by-Step Solution
Solution:
  1. Step 1: Understand float to int conversion behavior

    NumPy's astype(int) truncates the decimal part, it does not round.
  2. Step 2: Recognize why this may cause surprises

    Values like 2.9 become 2, which may be unexpected if rounding was assumed.
  3. Final Answer:

    Because astype(int) truncates decimals instead of rounding -> Option A
  4. Quick Check:

    astype(int) truncates, no rounding [OK]
Quick Trick: astype(int) truncates decimals, watch for rounding needs [OK]
Common Mistakes:
  • Assuming astype(int) rounds values
  • Expecting string conversion
  • Thinking array shape changes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes