NumPy is widely used in Data Science because it provides an extensive set of built-in mathematical and statistical functions. These functions make it incredibly simple to analyze datasets and calculate key metrics such as averages, totals, variances, and more.

Common Statistical Functions Overview

Function Description
np.max() / np.amax()Returns the maximum value in the array.
np.min() / np.amin()Returns the minimum value in the array.
np.mean()Calculates the arithmetic mean (average) of the array.
np.median()Finds the median value (middle element) of the data.
np.average()Computes the weighted average (defaults to mean if no weights given).
np.sum()Calculates the total sum of all elements in the array.
np.prod()Calculates the product of all elements in the array.
np.std()Calculates the standard deviation (spread of data).
np.var()Calculates the variance (squared standard deviation).
np.argmax()Returns the index of the maximum value.
np.argmin()Returns the index of the minimum value.
np.percentile()Computes the nth percentile of the data.

Analyzing Data: A Practical Example

Let's use a real-world scenario. Imagine we have a list of marks scored by a student in different subjects. We can convert this list into a NumPy array and use statistical functions to analyze the student's performance.

import numpy as np arr = [85, 75, 96, 52, 14, 86, 32] marks = np.array(arr) print("Student Marks Array:", marks)

1. Finding Maximum and Minimum Marks

# Find out max marks print("Maximum Marks:", np.max(marks)) # Find out min marks print("Minimum Marks:", np.min(marks))

Output:

Maximum Marks: 96 Minimum Marks: 14

2. Finding Mean, Median, and Average

# Find out mean marks (Total / Number of elements) print("Mean Marks:", np.mean(marks)) # Find out median marks (Middle value when sorted) print("Median Marks:", np.median(marks)) # Find out average marks # (Can take an optional 'weights' parameter, otherwise acts like mean) print("Average Marks:", np.average(marks))

Output:

Mean Marks: 62.857142857142854 Median Marks: 75.0 Average Marks: 62.857142857142854

3. Finding Total and Product

# Find the total sum of all marks print("Total Marks:", np.sum(marks)) # Find the product of all marks (multiplying all elements together) print("Product of Marks:", np.prod(marks))

Output:

Total Marks: 440 Product of Marks: 14757273600

Advanced Statistical Operations

Beyond the basics, NumPy allows you to dig deeper into the distribution and spread of your data.

Standard Deviation & Variance

The standard deviation (np.std) tells you how spread out the numbers are from the mean. The variance (np.var) is simply the standard deviation squared.

# Calculate Standard Deviation print("Standard Deviation:", np.std(marks)) # Calculate Variance print("Variance:", np.var(marks))

Output:

Standard Deviation: 29.897785052069634 Variance: 893.8775510204081

Finding Indices of Max/Min Values

Sometimes you don't just want to know what the highest mark was, but where it is located in the array. argmax() and argmin() return the index positions.

# Find the index of the highest mark print("Index of Max Mark:", np.argmax(marks)) # 96 is at index 2 # Find the index of the lowest mark print("Index of Min Mark:", np.argmin(marks)) # 14 is at index 4

Percentiles

Percentiles are used in statistics to give you a number that describes the value that a given percentage of the values are lower than.

# Calculate the 50th percentile (which is the same as the median) print("50th Percentile:", np.percentile(marks, 50)) # Calculate the 75th percentile print("75th Percentile:", np.percentile(marks, 75))

Output:

50th Percentile: 75.0 75th Percentile: 85.5