NumPy Statistical Functions Practice Questions with Solutions

Introduction

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

11. Python Program to Calculate Mean, Median, and Mode of a NumPy Array

Problem Statement

Write a Python program to calculate the mean, median, and mode of a NumPy array.

Note: NumPy does not provide a built-in mode() function, so this example uses Python’s collections.Counter.

Python Solution

import numpy as np
from collections import Counter

array = np.array([12, 15, 18, 20, 15, 25, 30, 15, 40])

mean = np.mean(array)
median = np.median(array)

counter = Counter(array)
mode = counter.most_common(1)[0][0]

print("Array:")
print(array)

print("\nMean:", mean)
print("Median:", median)
print("Mode:", mode)

Sample Output

Array:
[12 15 18 20 15 25 30 15 40]

Mean: 21.11111111111111
Median: 18.0
Mode: 15

Explanation

  • np.mean() calculates the average.
  • np.median() returns the middle value.
  • Counter().most_common() finds the most frequently occurring value.

Concepts Covered

  • Mean
  • Median
  • Mode
  • Counter

12. Python Program to Calculate Variance and Standard Deviation

Problem Statement

Write a Python program to calculate the variance and standard deviation of a NumPy array.

Python Solution

import numpy as np

marks = np.array([65, 70, 75, 80, 85, 90, 95])

print("Marks:")
print(marks)

print("\nVariance:", np.var(marks))
print("Standard Deviation:", np.std(marks))

Sample Output

Marks:
[65 70 75 80 85 90 95]

Variance: 100.0
Standard Deviation: 10.0

Explanation

  • Variance measures how spread out the values are.
  • Standard deviation is the square root of variance.

Concepts Covered

  • np.var()
  • np.std()
  • Data Dispersion

13. Python Program to Find Minimum, Maximum, and Range

Problem Statement

Write a Python program to calculate the minimum value, maximum value, and range of a NumPy array.

Python Solution

import numpy as np

array = np.array([28, 45, 67, 12, 89, 34, 55])

minimum = np.min(array)
maximum = np.max(array)
data_range = maximum - minimum

print("Array:")
print(array)

print("\nMinimum:", minimum)
print("Maximum:", maximum)
print("Range:", data_range)

Sample Output

Array:
[28 45 67 12 89 34 55]

Minimum: 12
Maximum: 89
Range: 77

Explanation

The range is calculated by subtracting the minimum value from the maximum value.

Concepts Covered

  • np.min()
  • np.max()
  • Range

14. Python Program to Calculate Percentiles

Problem Statement

Write a Python program to calculate the 25th, 50th, and 75th percentiles of a NumPy array.

Python Solution

import numpy as np

scores = np.array([55, 60, 65, 70, 75, 80, 85, 90, 95])

print("Scores:")
print(scores)

print("\n25th Percentile:", np.percentile(scores, 25))
print("50th Percentile:", np.percentile(scores, 50))
print("75th Percentile:", np.percentile(scores, 75))

Sample Output

25th Percentile: 65.0
50th Percentile: 75.0
75th Percentile: 85.0

Explanation

Percentiles divide the dataset into 100 equal parts and are widely used in statistics and data analysis.

Concepts Covered

  • np.percentile()
  • Quartiles
  • Data Distribution

15. Python Program to Calculate Correlation Between Two Arrays

Problem Statement

Write a Python program to calculate the Pearson correlation coefficient between two datasets.

Python Solution

import numpy as np

math_marks = np.array([60, 70, 80, 90, 100])

science_marks = np.array([58, 68, 79, 88, 98])

correlation_matrix = np.corrcoef(math_marks, science_marks)

print("Correlation Matrix:")
print(correlation_matrix)

print("\nCorrelation Coefficient:")
print(correlation_matrix[0, 1])

Sample Output

Correlation Matrix:
[[1.         0.9989]
 [0.9989     1.      ]]

Correlation Coefficient:
0.9989

Explanation

The correlation coefficient measures the strength of the relationship between two variables.

  • +1 → Perfect positive correlation
  • 0 → No correlation
  • -1 → Perfect negative correlation

Concepts Covered

  • np.corrcoef()
  • Correlation
  • Statistical Analysis

16. Python Program to Calculate Covariance Between Two Datasets

Problem Statement

Write a Python program to calculate the covariance between two NumPy arrays.

Python Solution

import numpy as np

x = np.array([2, 4, 6, 8, 10])

y = np.array([1, 3, 5, 7, 9])

covariance = np.cov(x, y)

print("Covariance Matrix:")
print(covariance)

Sample Output

Covariance Matrix:
[[10. 10.]
 [10. 10.]]

Explanation

Covariance measures how two variables change together.

  • Positive covariance → Variables increase together.
  • Negative covariance → One increases while the other decreases.

Concepts Covered

  • np.cov()
  • Covariance Matrix
  • Relationship Between Variables

17. Python Program to Calculate Row-wise and Column-wise Mean

Problem Statement

Write a Python program to calculate the row-wise and column-wise mean of a 3×4 NumPy matrix.

Python Solution

import numpy as np

matrix = np.array([
    [10, 20, 30, 40],
    [50, 60, 70, 80],
    [90, 100, 110, 120]
])

print("Matrix:")
print(matrix)

print("\nRow-wise Mean:")
print(np.mean(matrix, axis=1))

print("\nColumn-wise Mean:")
print(np.mean(matrix, axis=0))

Sample Output

Row-wise Mean:
[25. 65. 105.]

Column-wise Mean:
[50. 60. 70. 80.]

Explanation

  • axis=1 calculates the mean of each row.
  • axis=0 calculates the mean of each column.

Concepts Covered

  • axis Parameter
  • Row-wise Statistics
  • Column-wise Statistics

18. Python Program to Analyze Student Marks Using Statistical Functions

Problem Statement

Write a Python program to analyze student marks by calculating:

  • Mean
  • Median
  • Maximum
  • Minimum
  • Standard Deviation
  • Variance

Python Solution

import numpy as np

marks = np.array([72, 85, 91, 68, 77, 94, 88, 81, 73, 90])

print("Student Marks:")
print(marks)

print("\nMean:", np.mean(marks))
print("Median:", np.median(marks))
print("Maximum:", np.max(marks))
print("Minimum:", np.min(marks))
print("Standard Deviation:", np.std(marks))
print("Variance:", np.var(marks))

Sample Output

Student Marks:
[72 85 91 68 77 94 88 81 73 90]

Mean: 81.9
Median: 83.0
Maximum: 94
Minimum: 68
Standard Deviation: 8.39
Variance: 70.49

Explanation

This example demonstrates how multiple statistical functions can be used together to summarize a dataset, making it useful for educational reports, business analytics, and machine learning preprocessing.

Concepts Covered

  • Mean
  • Median
  • Maximum
  • Minimum
  • Variance
  • Standard Deviation
  • Statistical Analysis

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() and np.max() find minimum and maximum values.
  • np.percentile() calculates percentile values.
  • np.sum() and np.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.

Scroll to Top