📖 Scenario: You work as a data analyst for a small company. You have collected some data points representing the heights (in cm) of a group of people. You want to analyze this data to find the average height and understand how spread out the heights are. You will use NumPy to handle the data and SciPy to calculate the standard deviation.
🎯 Goal: Build a simple Python program that uses NumPy to store height data and SciPy to calculate the standard deviation. You will then print the average height and the standard deviation.
📋 What You'll Learn
Create a NumPy array with the exact height values given.
Create a variable to hold the average height using NumPy.
Use SciPy's stats module to calculate the standard deviation of the heights.
Print the average height and the standard deviation.
💡 Why This Matters
🌍 Real World
Data scientists often need to summarize and understand data using statistics like mean and standard deviation to make decisions.
💼 Career
Knowing how to use NumPy and SciPy for basic statistics is essential for data analysis roles in many industries.
Progress0 / 4 steps
1
Create a NumPy array with height data
Import NumPy as np and create a NumPy array called heights with these exact values: 160, 165, 170, 175, 180.
NumPy
Hint
Use np.array([...]) to create the array with the exact numbers.
2
Calculate the average height using NumPy
Create a variable called average_height and set it to the mean of the heights array using NumPy's mean function.
NumPy
Hint
Use np.mean(heights) to get the average.
3
Calculate the standard deviation using SciPy
Import stats from scipy. Then create a variable called std_dev and set it to the standard deviation of heights using stats.tstd.
NumPy
Hint
Use from scipy import stats and then stats.tstd(heights) to get the standard deviation.
4
Print the average height and standard deviation
Write two print statements: one to print average_height with the text "Average height:" and another to print std_dev with the text "Standard deviation:".
NumPy
Hint
Use print("Average height:", average_height) and print("Standard deviation:", std_dev).
Practice
(1/5)
1. What is the main benefit of using SciPy together with NumPy?
easy
A. SciPy replaces NumPy for basic array operations.
B. SciPy provides extra math functions that NumPy does not have.
C. SciPy is used only for plotting graphs.
D. SciPy is a database for storing NumPy arrays.
Solution
Step 1: Understand NumPy's role
NumPy is mainly for fast array operations and basic math.
Step 2: Understand SciPy's role
SciPy adds many advanced math and scientific functions on top of NumPy.
Final Answer:
SciPy provides extra math functions that NumPy does not have. -> Option B
Quick Check:
Extra math tools = SciPy [OK]
Hint: SciPy adds advanced math tools beyond NumPy basics [OK]
Common Mistakes:
Thinking SciPy replaces NumPy
Confusing SciPy with plotting libraries
Believing SciPy stores data like a database
2. Which of the following is the correct way to import the SciPy stats module alongside NumPy?
easy
A. import numpy as np; import scipy.stats as stats
B. import numpy; import scipy.stats
C. from numpy import stats; import scipy
D. import numpy.stats; import scipy as sp
Solution
Step 1: Check standard import conventions
NumPy is usually imported as np, SciPy submodules like stats as stats or sp.stats.
Step 2: Validate each option
import numpy as np; import scipy.stats as stats uses correct aliases and imports the stats submodule properly.
Final Answer:
import numpy as np; import scipy.stats as stats -> Option A
Quick Check:
Standard imports = import numpy as np; import scipy.stats as stats [OK]
Hint: Use 'import numpy as np' and 'import scipy.stats as stats' [OK]
Common Mistakes:
Importing non-existent numpy.stats
Using wrong aliases or missing 'as'
Importing whole scipy without submodule
3. What is the output of this code?
import numpy as np
from scipy import stats
arr = np.array([1, 2, 2, 3, 4])
mode_result = stats.mode(arr)
print(mode_result.mode[0])
medium
A. 1
B. 4
C. 3
D. 2
Solution
Step 1: Understand the array values
The array is [1, 2, 2, 3, 4], where 2 appears twice, others once.
Step 2: Understand stats.mode function
stats.mode returns the most frequent value; here it is 2.
Final Answer:
2 -> Option D
Quick Check:
Most frequent value = 2 [OK]
Hint: Mode is the most frequent number in the array [OK]
Common Mistakes:
Confusing mode with mean or median
Not accessing mode_result.mode[0]
Assuming first element is mode
4. Find the error in this code snippet:
import numpy as np
from scipy import stats
arr = np.array([1, 2, 3, 4])
mode_result = stats.mode(arr)
print(mode_result[0])
medium
A. mode_result is an object; correct access is mode_result.mode, not mode_result[0].
B. stats.mode returns a tuple, so indexing with [0] is correct.
C. The array must be a list, not a NumPy array.
D. Missing import for numpy as np.
Solution
Step 1: Check stats.mode return type
stats.mode returns an object with attributes like mode and count, not a tuple.
Step 2: Identify correct attribute access
Access the mode value with mode_result.mode, not by indexing mode_result[0].
Final Answer:
mode_result is an object; correct access is mode_result.mode, not mode_result[0]. -> Option A
Quick Check:
Use .mode attribute, not [0] index [OK]
Hint: Use .mode attribute to get mode, not indexing [OK]
Common Mistakes:
Using indexing instead of attribute access
Confusing return type as tuple
Thinking array must be list
5. You have two NumPy arrays:
import numpy as np
from scipy import stats
arr1 = np.array([1, 2, 2, 3])
arr2 = np.array([2, 2, 3, 4])
How can you find the combined mode of all elements from both arrays using SciPy?
hard
A. Use stats.mode(arr1 + arr2)
B. Use stats.mode(arr1 * arr2)
C. Use stats.mode(np.concatenate((arr1, arr2)))
D. Use stats.mode(arr1) and stats.mode(arr2) separately and pick the higher
Solution
Step 1: Combine both arrays
Use np.concatenate to join arr1 and arr2 into one array.
Step 2: Apply stats.mode on combined array
Passing the combined array to stats.mode finds the overall mode.
Final Answer:
Use stats.mode(np.concatenate((arr1, arr2))) -> Option C
Quick Check:
Combine arrays first, then mode [OK]
Hint: Join arrays with concatenate before finding mode [OK]
Common Mistakes:
Adding or multiplying arrays instead of concatenating
Finding modes separately and comparing manually
Not combining arrays before mode
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.