Bird
0
0

You have a NumPy array of strings representing numbers: arr = np.array(['1', '2', '3.5']). How do you convert it to a float array?

hard📝 Application Q8 of 15
NumPy - Array Data Types
You have a NumPy array of strings representing numbers: arr = np.array(['1', '2', '3.5']). How do you convert it to a float array?
Aarr.astype(bool)
Barr.astype(int)
Carr.astype('str')
Darr.astype(float)
Step-by-Step Solution
Solution:
  1. Step 1: Identify target type for conversion

    The strings represent numbers including decimals, so float is the correct target type.
  2. Step 2: Use astype() to convert strings to float

    Calling arr.astype(float) converts string numbers to floats correctly.
  3. Final Answer:

    arr.astype(float) -> Option D
  4. Quick Check:

    Convert numeric strings to float with astype(float) [OK]
Quick Trick: Use astype(float) to convert numeric strings to floats [OK]
Common Mistakes:
  • Using astype(int) loses decimals
  • Trying to convert to bool or str unnecessarily
  • Not converting strings before numeric operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes