Friday, May 30, 2025

🧮 Essential Functions of NumPy for Numerical Computation in Python

Introduction 


NumPy (Numerical Python) is the foundation of many data science and scientific computing tasks in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions.


In this post, we’ll explore essential NumPy functions, grouped into five key categories, with examples and expected outputs.


📦 1. Array Creation Functions


NumPy provides multiple ways to create arrays:


numpy.array()


Converts lists or tuples into NumPy arrays.


import numpy as np


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

print(arr)

# Output: [1 2 3]


numpy.zeros()


Creates an array filled with zeros.


zeros_arr = np.zeros((2, 3))

print(zeros_arr)

# Output:

# [[0. 0. 0.]

# [0. 0. 0.]]


numpy.ones()


Creates an array filled with ones.


ones_arr = np.ones((3, 2))

print(ones_arr)

# Output:

# [[1. 1.]

# [1. 1.]

# [1. 1.]]



🔁 2. Array Manipulation Functions


numpy.reshape()


Changes the shape of an array without changing the data.


a = np.array([1, 2, 3, 4, 5, 6])

reshaped = a.reshape((2, 3))

print(reshaped)

# Output:

# [[1 2 3]

# [4 5 6]]


numpy.transpose()


Transposes the dimensions of an array.


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

transposed = np.transpose(b)

print(transposed)

# Output:

# [[1 3]

# [2 4]]


numpy.concatenate()


Joins two or more arrays along an existing axis.


x = np.array([[1, 2]])

y = np.array([[3, 4]])

concat = np.concatenate((x, y), axis=0)

print(concat)

# Output:

# [[1 2]

# [3 4]]


➕ 3. Mathematical Functions


numpy.sum()


Computes the sum of array elements.


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

print(np.sum(arr))

# Output: 10


numpy.mean()


Calculates the mean (average) of array elements.


print(np.mean(arr))

# Output: 2.5


numpy.median()


Returns the median of the array elements.


print(np.median(arr))

# Output: 2.5


📊 4. Statistical Functions


numpy.std()


Computes the standard deviation.


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

print(np.std(arr))

# Output: 1.4142135623730951


numpy.var()


Computes the variance.


print(np.var(arr))

# Output: 2.0


numpy.corrcoef()


Returns the Pearson correlation coefficients.


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

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

corr_matrix = np.corrcoef(x, y)

print(corr_matrix)

# Output:

# [[1. 1.]

# [1. 1.]]


📐 5. Linear Algebra Functions


numpy.dot()


Performs dot product of two arrays.


a = np.array([1, 2])

b = np.array([3, 4])

print(np.dot(a, b))

# Output: 11

# (1*3 + 2*4)


numpy.linalg.inv()


Computes the inverse of a square matrix.


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

inverse = np.linalg.inv(matrix)

print(inverse)

# Output:

# [[-2. 1. ]

# [ 1.5 -0.5]]


numpy.linalg.det()


Computes the determinant of a square matrix.


det = np.linalg.det(matrix)

print(det)

# Output: -2.0000000000000004


✅ Conclusion


NumPy's core functions allow you to efficiently create, manipulate, and analyze numerical data in Python. Whether you're performing simple calculations or complex linear algebra operat

ions, these tools are essential for any data professional.

No comments:

Post a Comment

🔁 Understanding Loops in Python – A Complete Guide

Introduction  Loops are fundamental in any programming language, and Python is no exception. They allow us to execute a block of code repeat...