Challenge - 5 Problems
Vectorization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of vectorized addition vs loop addition
What is the output of the following code comparing vectorized addition and loop addition in pandas?
Pandas
import pandas as pd import numpy as np s = pd.Series(np.arange(5)) # Vectorized addition vec_result = s + 10 # Loop addition loop_result = pd.Series([x + 10 for x in s]) print(vec_result.equals(loop_result))
Attempts:
2 left
💡 Hint
Think about whether vectorized and loop operations produce the same results in pandas Series.
✗ Incorrect
Both vectorized addition and loop addition produce the same Series values, so the equality check returns True.
❓ data_output
intermediate2:00remaining
Resulting DataFrame after vectorized operation
Given this DataFrame, what is the output after applying a vectorized operation to double the 'value' column?
Pandas
import pandas as pd df = pd.DataFrame({'value': [1, 2, 3, 4]}) df['double'] = df['value'] * 2 print(df)
Attempts:
2 left
💡 Hint
Multiplying a column by 2 doubles each value in that column.
✗ Incorrect
The vectorized multiplication doubles each element in the 'value' column and stores it in 'double'.
🔧 Debug
advanced2:00remaining
Why does this loop code run slower than vectorized code?
Consider these two snippets. Why is the loop version slower than the vectorized version?
Pandas
import pandas as pd import numpy as np import time s = pd.Series(np.arange(1000000)) start = time.time() result_loop = pd.Series([x * 2 for x in s]) loop_time = time.time() - start start = time.time() result_vec = s * 2 vec_time = time.time() - start print(loop_time > vec_time)
Attempts:
2 left
💡 Hint
Think about how pandas and numpy optimize vectorized operations internally.
✗ Incorrect
Vectorized operations run faster because they use optimized C code internally, avoiding slow Python loops.
🧠 Conceptual
advanced2:00remaining
Why vectorized operations reduce code complexity
Which statement best explains why vectorized operations reduce code complexity in data science?
Attempts:
2 left
💡 Hint
Think about how vectorized code looks compared to loop-based code.
✗ Incorrect
Vectorized operations allow applying an operation to whole arrays or columns at once, reducing the need for explicit loops and making code cleaner.
🚀 Application
expert2:00remaining
Identify the output of a mixed vectorized and conditional operation
What is the output of this code that applies a vectorized operation with a condition on a pandas Series?
Pandas
import pandas as pd s = pd.Series([1, 2, 3, 4, 5]) result = s.where(s > 3, s * 10) print(result.tolist())
Attempts:
2 left
💡 Hint
The where method keeps values where condition is True, else replaces with second argument.
✗ Incorrect
Values greater than 3 remain unchanged; others are multiplied by 10.