B. The operation arr = arr.add(10) creates a new array, not in-place.
C. The correct in-place addition uses arr += 10.
D. The method add does not exist for numpy arrays.
Solution
Step 1: Check if numpy arrays have add() method
NumPy arrays do not have an add method; this causes an AttributeError.
Step 2: Understand in-place addition syntax
Using arr = arr.add(10) is invalid and does not perform in-place addition; correct syntax is arr += 10.
Step 3: Combine observations
All points are true: method doesn't exist, code creates new array if it did, and correct syntax is arr += 10.
Final Answer:
All of the above. -> Option A
Quick Check:
Use arr += 10; no arr.add() method [OK]
Hint: No arr.add(); use arr += 10 for in-place add [OK]
Common Mistakes:
Trying to use arr.add() method
Assigning arr = arr + 10 expecting in-place
Ignoring AttributeError from invalid method
5. You have a large numpy array data and want to normalize it by subtracting the mean and dividing by the standard deviation in-place to save memory. Which code snippet correctly does this?
hard
A. mean = data.mean()
data = (data - mean) / data.std()
B. data = data - data.mean()
data = data / data.std()
C. data.subtract(data.mean())
data.divide(data.std())
D. mean = data.mean()
data -= mean
data /= data.std()
Solution
Step 1: Understand in-place normalization steps
Subtract mean and divide by std in-place to avoid new arrays.
Step 2: Analyze options for in-place operations
mean = data.mean()
data -= mean
data /= data.std() uses data -= mean and data /= data.std(), which modify data directly.
Step 3: Identify why other options are incorrect
Options A and B assign new arrays to data, not in-place. data.subtract(data.mean())
data.divide(data.std()) uses invalid methods.
Final Answer:
mean = data.mean()
data -= mean
data /= data.std() -> Option D
Quick Check:
Use -= and /= for in-place normalization [OK]
Hint: Use -= and /= to normalize array in-place [OK]
Common Mistakes:
Assigning new arrays instead of in-place changes
Using non-existent methods like subtract() on numpy arrays
Forgetting to compute mean and std once before operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.