Challenge - 5 Problems
NumPy Statistics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Calculate mean and median with NumPy
What is the output of this code that calculates the mean and median of a dataset using NumPy?
NumPy
import numpy as np data = np.array([10, 20, 30, 40, 50]) mean_val = np.mean(data) median_val = np.median(data) print(f"Mean: {mean_val}, Median: {median_val}")
Attempts:
2 left
💡 Hint
Remember that mean is the average and median is the middle value when data is sorted.
✗ Incorrect
The mean is the sum of all values divided by the number of values: (10+20+30+40+50)/5 = 30. The median is the middle value in the sorted list, which is 30.
❓ data_output
intermediate2:00remaining
Variance and Standard Deviation Calculation
Given this NumPy code, what is the output of variance and standard deviation?
NumPy
import numpy as np values = np.array([2, 4, 4, 4, 5, 5, 7, 9]) variance = np.var(values) std_dev = np.std(values) print(f"Variance: {variance:.2f}, Standard Deviation: {std_dev:.2f}")
Attempts:
2 left
💡 Hint
Variance is the average of squared differences from the mean. Standard deviation is the square root of variance.
✗ Incorrect
The variance of the data is 4.0 and the standard deviation is the square root of 4.0, which is 2.0.
❓ visualization
advanced2:30remaining
Histogram Visualization of Data Distribution
Which option shows the correct code to create a histogram of the data array using NumPy and Matplotlib?
NumPy
import numpy as np import matplotlib.pyplot as plt data = np.random.normal(loc=0, scale=1, size=1000) # Choose the correct code to plot histogram
Attempts:
2 left
💡 Hint
Histograms use plt.hist() to show frequency distribution.
✗ Incorrect
plt.hist() creates a histogram. Options A, B, and D use incorrect plotting functions for histograms.
🧠 Conceptual
advanced2:00remaining
Why Use NumPy for Statistics?
Which statement best explains why NumPy is preferred for statistical calculations on large datasets?
Attempts:
2 left
💡 Hint
Think about speed and efficiency when working with numbers in Python.
✗ Incorrect
NumPy is built on fast C code and supports vectorized operations, which speeds up calculations on large arrays compared to plain Python loops.
🔧 Debug
expert2:00remaining
Identify the Error in Statistical Calculation
What error does this code raise when calculating the mean of a list with a string element?
NumPy
import numpy as np values = [10, 20, '30', 40] mean_val = np.mean(values) print(mean_val)
Attempts:
2 left
💡 Hint
Check what happens when NumPy tries to add numbers and strings.
✗ Incorrect
NumPy tries to convert all elements to floats to compute the mean. The string '30' cannot be converted to float automatically, causing a ValueError.