Skip to content

Statistical functions

NumPy provides a wide range of array methods and functions that can be used to perform various operations on arrays. These methods and functions are optimized for performance and can be used to simplify code and improve readability.

  • Statistical functions: mean, median, mode, std
  • Mathematical functions: sin, cos, tan, exp
  • Linear algebra functions: dot, inv, det

Mean

The mean is the average value of a dataset. It is calculated by summing all the values and dividing by the number of values.

import numpy as np

# Create a NumPy array with exam scores
scores = np.array([85, 90, 78, 92, 88, 76, 95, 89, 91, 82])

# Calculate the mean score
mean_score = np.mean(scores)

print("Mean Score: ", mean_score)

Median

The median is the middle value of a dataset when it is sorted in ascending order. If the dataset has an even number of values, the median is the average of the two middle values.

import numpy as np

# Create a NumPy array with exam scores
scores = np.array([85, 90, 78, 92, 88, 76, 95, 89, 91, 82])

# Calculate the median score
median_score = np.median(scores)

print("Median Score: ", median_score)

Mode

The mode is the value that appears most frequently in a dataset. If there are multiple values that appear with the same frequency, the mode is not unique.

import numpy as np
from scipy import stats

# Create a NumPy array with exam scores
scores = np.array([85, 90, 78, 92, 88, 76, 95, 89, 91, 82])

# Calculate the mode score
mode_score = stats.mode(scores)

print("Mode Score: ", mode_score.mode[0])

Standard Deviation (Std)

The standard deviation is a measure of the spread or dispersion of a dataset. It is calculated as the square root of the variance.

import numpy as np

# Create a NumPy array with exam scores
scores = np.array([85, 90, 78, 92, 88, 76, 95, 89, 91, 82])

# Calculate the standard deviation of the scores
std_dev = np.std(scores)

print("Standard Deviation: ", std_dev)