Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Mean Explained – Python np.mean() Function with Examples

NumPy – Mean Function

The mean is one of the most important concepts in statistics and data analysis.

In NumPy, the np.mean() function is used to calculate the average of array elements quickly and efficiently.

It is widely used in:

  • Data science
  • Machine learning
  • Statistics
  • Finance
  • Scientific computing

What is Mean?

The mean (average) is calculated by:

Sum of all values ÷ Number of values


Import NumPy

import numpy as np

1. Mean of a Simple Array

import numpy as np

A = np.array([10, 20, 30, 40, 50])

result = np.mean(A)

print(result)

Output:

30.0

2. Mean of 2D Array

import numpy as np

A = np.array([[1, 2],
[3, 4]])

print(np.mean(A))

Output:

2.5

3. Mean Along Axis (Rows vs Columns)

Mean of Columns (axis=0)

import numpy as np

A = np.array([[1, 2],
[3, 4]])

print(np.mean(A, axis=0))

Output:

[2. 3.]

Mean of Rows (axis=1)

import numpy as np

A = np.array([[1, 2],
[3, 4]])

print(np.mean(A, axis=1))

Output:

[1.5 3.5]

4. Mean of Floating Numbers

import numpy as np

A = np.array([2.5, 3.5, 4.0])

print(np.mean(A))

Output:

3.3333333333333335

5. Weighted Understanding (Concept)

Mean is often used as a base for:

  • Standard deviation
  • Variance
  • Normalization
  • Data scaling

6. Mean with Large Dataset

import numpy as np

A = np.arange(1, 101)

print(np.mean(A))

Output:

50.5

7. Handling Missing Values (NaN)

import numpy as np

A = np.array([1, 2, np.nan, 4, 5])

print(np.nanmean(A))

Output:

3.0

Real-World Applications

1. Data Science

  • Average sales
  • Performance analysis

2. Machine Learning

  • Feature normalization
  • Loss calculation

3. Finance

  • Average profit/loss
  • Market analysis

4. Statistics

  • Central tendency measurement

Why Use NumPy Mean?

Using NumPy provides:

  • Fast computation
  • Easy axis-based calculations
  • Handles large datasets
  • Supports NaN-safe operations

Combined with Python, it becomes essential for modern data analysis.


Mean vs Sum

OperationMeaning
Sum                  Total of values
Mean                  Average of values

Summary

NumPy provides a simple and powerful way to calculate averages using:

np.mean(array, axis=...)

It supports multi-dimensional arrays, axis operations, and missing data handling.


Conclusion

The mean function is a core statistical tool used in almost every data science and machine learning workflow. NumPy makes calculating it fast, simple, and reliable.




Post a Comment

0 Comments