NumPy provides powerful statistical functions to analyze and summarize data efficiently. These functions are widely used in data science, machine learning, business analytics, finance, and scientific research. In this chapter, you’ll practice beginner-friendly NumPy statistical function questions with complete solutions. NumPy Statistical Functions practice questions with solutions help in better understand of concepts.
1. Python Program to Find the Mean of an Array
Problem Statement
Write a Python program to calculate the mean (average) of all elements in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
result = np.mean(numbers)
print(result)
Sample Output
30.0
Explanation
The np.mean() function returns the average value of all array elements.
Concepts Covered
np.mean()- Average
2. Python Program to Find the Median of an Array
Problem Statement
Write a Python program to calculate the median of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([15, 10, 25, 20, 30])
result = np.median(numbers)
print(result)
Sample Output
20.0
Explanation
The np.median() function returns the middle value after sorting the array.
Concepts Covered
np.median()- Median
3. Python Program to Find the Standard Deviation
Problem Statement
Write a Python program to calculate the standard deviation of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([2, 4, 6, 8, 10])
result = np.std(numbers)
print(result)
Sample Output
2.8284271247461903
Explanation
The np.std() function measures how far values are spread from the mean.
Concepts Covered
np.std()- Standard Deviation
4. Python Program to Find the Variance
Problem Statement
Write a Python program to calculate the variance of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([2, 4, 6, 8, 10])
result = np.var(numbers)
print(result)
Sample Output
8.0
Explanation
The np.var() function calculates the variance of array elements.
Concepts Covered
np.var()- Variance
5. Python Program to Find the Minimum Value
Problem Statement
Write a Python program to find the smallest value in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([45, 12, 89, 30, 56])
print(np.min(numbers))
Sample Output
12
Explanation
The np.min() function returns the smallest value in the array.
Concepts Covered
np.min()- Minimum Value
6. Python Program to Find the Maximum Value
Problem Statement
Write a Python program to find the largest value in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([45, 12, 89, 30, 56])
print(np.max(numbers))
Sample Output
89
Explanation
The np.max() function returns the largest value in the array.
Concepts Covered
np.max()- Maximum Value
7. Python Program to Calculate Percentile
Problem Statement
Write a Python program to calculate the 75th percentile of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
result = np.percentile(numbers, 75)
print(result)
Sample Output
40.0
Explanation
The np.percentile() function returns the specified percentile value.
Concepts Covered
np.percentile()- Percentile
8. Python Program to Calculate the Sum of an Array
Problem Statement
Write a Python program to calculate the sum of all array elements.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40])
print(np.sum(numbers))
Sample Output
100
Explanation
The np.sum() function adds all elements of the array.
Concepts Covered
np.sum()- Sum
9. Python Program to Calculate the Product of Array Elements
Problem Statement
Write a Python program to calculate the product of all array elements.
Python Solution
import numpy as np
numbers = np.array([2, 3, 4])
print(np.prod(numbers))
Sample Output
24
Explanation
The np.prod() function multiplies all elements of the array.
Concepts Covered
np.prod()- Product
10. Python Program to Calculate the Cumulative Sum
Problem Statement
Write a Python program to calculate the cumulative sum of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([5, 10, 15, 20])
print(np.cumsum(numbers))
Sample Output
[ 5 15 30 50]
Explanation
The np.cumsum() function returns the cumulative sum of array elements.
Concepts Covered
np.cumsum()- Cumulative Sum
Chapter Summary
In this chapter, you learned how to use NumPy statistical functions such as np.mean(), np.median(), np.std(), np.var(), np.min(), np.max(), np.percentile(), np.sum(), np.prod(), and np.cumsum(). These functions are essential for analyzing datasets and are widely used in data science, machine learning, business intelligence, and research.
Key Takeaways
np.mean()calculates the average.np.median()returns the middle value.np.std()measures data spread.np.var()calculates variance.np.min()andnp.max()find minimum and maximum values.np.percentile()calculates percentile values.np.sum()andnp.prod()perform aggregation.np.cumsum()returns cumulative totals.
Frequently Asked Questions (FAQs)
1. What is the difference between mean and median?
Mean is the average of all values, while the median is the middle value after sorting the data.
2. What does standard deviation measure?
It measures how much the values vary from the average.
3. Which NumPy function calculates variance?
Use np.var() to calculate the variance of an array.
4. How do I calculate the 90th percentile?
Use np.percentile(array, 90).
5. Which function returns the cumulative sum?
Use np.cumsum().
6. What is the purpose of np.prod()?
It multiplies all array elements and returns the final product.
7. Why are NumPy statistical functions important?
They help summarize, analyze, and interpret data efficiently, making them essential for data analysis, machine learning, finance, and scientific computing.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

