Bird
Raised Fist0
NumPydata~20 mins

Why statistics with NumPy matters - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy Statistics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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}")
AMean: 30.0, Median: 30.0
BMean: 25.0, Median: 30.0
CMean: 30.0, Median: 25.0
DMean: 20.0, Median: 40.0
Attempts:
2 left
💡 Hint
Remember that mean is the average and median is the middle value when data is sorted.
❓ data_output
intermediate
2: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}")
AVariance: 3.50, Standard Deviation: 1.87
BVariance: 2.00, Standard Deviation: 1.41
CVariance: 4.00, Standard Deviation: 2.00
DVariance: 5.00, Standard Deviation: 2.24
Attempts:
2 left
💡 Hint
Variance is the average of squared differences from the mean. Standard deviation is the square root of variance.
❓ visualization
advanced
2: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
A
plt.scatter(data)
plt.title('Data Distribution')
plt.show()
B
plt.hist(data, bins=30, color='blue', edgecolor='black')
plt.title('Data Distribution')
plt.show()
C
plt.bar(data, bins=30)
plt.title('Data Distribution')
plt.show()
D
plt.plot(data, bins=30)
plt.title('Data Distribution')
plt.show()
Attempts:
2 left
💡 Hint
Histograms use plt.hist() to show frequency distribution.
🧠 Conceptual
advanced
2:00remaining
Why Use NumPy for Statistics?
Which statement best explains why NumPy is preferred for statistical calculations on large datasets?
ANumPy automatically creates visualizations for statistical data without extra code.
BNumPy replaces the need for any other Python libraries for data analysis.
CNumPy stores data in text files which makes it easier to read and write statistical results.
DNumPy uses optimized C code for fast computation and supports vectorized operations, making statistics faster and efficient.
Attempts:
2 left
💡 Hint
Think about speed and efficiency when working with numbers in Python.
🔧 Debug
expert
2: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)
AValueError: could not convert string to float: '30'
BSyntaxError: invalid syntax
CNo error, output is 25.0
DTypeError: unsupported operand type(s) for +: 'int' and 'str'
Attempts:
2 left
💡 Hint
Check what happens when NumPy tries to add numbers and strings.

Practice

(1/5)
1. Why is it useful to calculate statistics like mean and median using NumPy?
easy
A. Because it deletes unnecessary data points
B. Because it helps summarize large data sets quickly and clearly
C. Because it makes data harder to understand
D. Because it changes the original data values

Solution

  1. Step 1: Understand the purpose of statistics

    Statistics like mean and median give a simple summary of data, making it easier to understand large amounts of numbers.
  2. Step 2: Recognize NumPy's role

    NumPy provides fast and easy functions to calculate these statistics, saving time and effort.
  3. Final Answer:

    Because it helps summarize large data sets quickly and clearly -> Option B
  4. Quick Check:

    Statistics summarize data = Because it helps summarize large data sets quickly and clearly [OK]
Hint: Statistics simplify data understanding fast with NumPy [OK]
Common Mistakes:
  • Thinking statistics change original data
  • Believing statistics delete data points
  • Confusing summary with data removal
2. Which of the following is the correct way to calculate the mean of a NumPy array named data?
easy
A. np.mean(data)
B. np.mean[data]
C. data.mean()()
D. mean(data)

Solution

  1. Step 1: Recall NumPy mean syntax

    The correct function to calculate mean in NumPy is np.mean() with the array passed inside parentheses.
  2. Step 2: Check each option

    mean(data) misses the np. prefix, data.mean()() has extra parentheses, np.mean[data] uses square brackets incorrectly.
  3. Final Answer:

    np.mean(data) -> Option A
  4. Quick Check:

    Correct NumPy mean syntax = np.mean(data) [OK]
Hint: Use np.mean(array) to get mean quickly [OK]
Common Mistakes:
  • Forgetting to use np. prefix
  • Using square brackets instead of parentheses
  • Adding extra parentheses after method
3. What is the output of this code?
import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(np.median(data))
medium
A. 2
B. 2.5
C. 3
D. 3.0

Solution

  1. Step 1: Understand the median calculation

    The median is the middle value when data is sorted. For [1,2,3,4,5], the middle is 3.
  2. Step 2: Check NumPy median output type

    NumPy returns median as a float, so output is 3.0, not integer 3.
  3. Final Answer:

    3.0 -> Option D
  4. Quick Check:

    Median of odd-length array = middle value as float [OK]
Hint: Median of odd list is middle number as float [OK]
Common Mistakes:
  • Confusing median with mean
  • Expecting integer instead of float output
  • Choosing average of two numbers for odd-length array
4. The following code is meant to calculate the standard deviation of data, but it gives an error. What is the problem?
import numpy as np
data = np.array([10, 20, 30])
std_dev = np.std data
print(std_dev)
medium
A. Data array is not numeric
B. np.std does not exist in NumPy
C. Missing parentheses after np.std
D. print() cannot display std_dev

Solution

  1. Step 1: Identify syntax error in function call

    The code calls np.std without parentheses, which is invalid syntax in Python.
  2. Step 2: Correct the function call

    Adding parentheses and passing data inside fixes the error: np.std(data).
  3. Final Answer:

    Missing parentheses after np.std -> Option C
  4. Quick Check:

    Function calls need parentheses [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
  • Forgetting parentheses on function calls
  • Thinking np.std is not a NumPy function
  • Assuming print cannot show numbers
5. You have a large dataset with some missing values represented as np.nan. Which NumPy function helps calculate the mean while ignoring these missing values?
hard
A. np.nanmean()
B. np.mean()
C. np.median()
D. np.std()

Solution

  1. Step 1: Understand the problem with missing values

    Missing values as np.nan cause normal mean calculations to return nan instead of a number.
  2. Step 2: Use the correct NumPy function

    np.nanmean() calculates the mean ignoring nan values, giving a valid average.
  3. Final Answer:

    np.nanmean() -> Option A
  4. Quick Check:

    Ignore NaNs with np.nanmean() [OK]
Hint: Use np.nanmean() to skip NaN values in mean [OK]
Common Mistakes:
  • Using np.mean() which returns nan if any NaN present
  • Confusing median with mean for missing data
  • Trying to use np.std() for mean calculation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard