Wednesday, November 19, 2025

✅ NumPy in Python – From Beginner to Hard Level (With Programs)

 

BEGINNER LEVEL NumPy Projects

1. Introduction to NumPy

NumPy (Numerical Python) is a Python library used for:

  • Fast numerical computation

  • Working with arrays (faster than Python lists)

  • Scientific computing

  • Machine learning & data analysis

Installation

pip install numpy

Import

import numpy as np

2. NumPy Array Basics

2.1 Creating Arrays

From Python list

import numpy as np arr = np.array([1, 2, 3, 4]) print(arr)

2D Array

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

Check Data Type

print(arr.dtype)

2.2 Array Built-in Functions

Zeros, Ones, Full

np.zeros(5) np.ones((2,3)) np.full((3,3), 7)

Arange & Linspace

np.arange(1, 10, 2) # step size np.linspace(1, 5, 10) # 10 equal points

3. Array Indexing & Slicing

Indexing

arr = np.array([10, 20, 30, 40]) print(arr[2]) # 30

Slicing

print(arr[1:3]) print(arr[:3]) print(arr[::-1]) # reverse

2D Array Indexing

arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr[1, 2]) # 6 print(arr[:, 1]) # column 1

4. Array Math Operations

Element-wise operations

arr = np.array([1, 2, 3]) print(arr + 5) print(arr * 2) print(arr ** 2)

Array to array

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

5. Aggregate Functions

arr = np.array([2, 5, 8, 10]) arr.sum() arr.max() arr.min() arr.mean() arr.std() # standard deviation arr.var() # variance

6. Reshaping Arrays

Reshape

arr = np.arange(12) new_arr = arr.reshape((3,4)) print(new_arr)

Flatten

arr.flatten()

7. Stacking Arrays

Vertical Stack

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

Horizontal Stack

np.hstack((a, b))

8. NumPy Broadcasting

Broadcasting allows operations on arrays of different shapes.

Example

arr = np.array([1,2,3]) print(arr + 10)

2D + 1D example

a = np.array([[1,2,3], [4,5,6]]) b = np.array([10, 20, 30]) print(a + b)

9. Boolean Indexing

Filter

arr = np.array([10, 21, 32, 43, 54]) print(arr[arr > 30])

10. NumPy Random Module

Random number

np.random.rand(3) # 1D np.random.rand(3, 3) # 2D

Random integers

np.random.randint(1, 10, 5)

Random normal distribution

np.random.randn(3, 3)

11. Advanced NumPy

11.1 Vectorization (Speed Improvement)

Without NumPy (slow)

lst = [1, 2, 3, 4] result = [x * 2 for x in lst]

With NumPy (fast)

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

11.2 Matrix Operations

Matrix multiplication

a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) print(a.dot(b))

11.3 Statistical Operations

Correlation

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

Percentile

np.percentile([10,20,30,40], 50)

11.4 NumPy with Files

Save array to file

np.save("data.npy", arr)

Load array

arr = np.load("data.npy")

12. Hard Level – Practical Programs

12.1 Matrix Inverse, Determinant

arr = np.array([[2, 1], [5, 3]]) print(np.linalg.det(arr)) print(np.linalg.inv(arr))

12.2 Eigenvalues & Eigenvectors

A = np.array([[4, 2], [1, 3]]) values, vectors = np.linalg.eig(A) print(values) print(vectors)

12.3 Solve Linear Equation

Solve:
2x + 3y = 8
3x + 4y = 11

A = np.array([[2,3],[3,4]]) B = np.array([8,11]) solution = np.linalg.solve(A, B) print(solution)

12.4 Convolution (Image processing concept)

signal = np.array([1,2,3]) kernel = np.array([0,1]) print(np.convolve(signal, kernel))

12.5 NumPy + Matplotlib Plot

(asked earlier also)

import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title("Sine Wave") plt.show()

No comments:

Post a Comment

Python Viva Questions

  Basic Python Viva Questions 1. What is Python? Python is a high-level, interpreted, and object-oriented programming language used for w...