Introduction to NumPy:
NumPy (Numerical Python) is a powerful open-source Python library used for performing
numerical and scientific computations. It supports multi-dimensional arrays (matrices) and
provides mathematical functions to operate on these arrays efficiently.
Importance of NumPy:
1. Foundation for Data Science and scientific computing.
2. Enables fast operations on large datasets.
3. Essential for AI/ML, Data Analysis, Image Processing.
4. Provides powerful broadcasting and vectorization.
5. Acts as the base for other libraries like Pandas, SciPy, Scikit-learn, TensorFlow, etc.
Merits / Advantages of NumPy:
No. Advantage / Merit
1 Fast execution using optimized C code (better than lists).
2 Supports multi-dimensional arrays and matrices.
3 Has a wide range of mathematical, statistical, and linear algebra functions.
4 Allows vectorized operations (apply operations without loops).
5 Saves memory due to compact array storage.
6 Easy to integrate with C/C++ and Fortran code.
7 Can handle very large datasets easily.
Demerits / Disadvantages of NumPy:
No. Disadvantage / Demerit
1 Cannot store elements of different data types in the same array easily.
2 Steep learning curve for complete beginners.
3 Debugging vectorized code can be challenging.
4 Less flexible than Python lists for non-numeric data.
5 Some advanced operations are hard to understand for students.
6 Large array operations may consume more RAM.
7 It is not ideal for small datasets or simple tasks.
Example Code:
import numpy as np
# Create a 1D NumPy array
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)
# Perform operations
print("Mean:", np.mean(arr))
print("Sum:", np.sum(arr))
print("Square:", arr ** 2)
Installation of NumPy
NumPy is not included with standard Python, so you need to install it separately using the Python
package manager pip.
✅ Installation using pip:
Open your command prompt (Windows) or terminal (Linux/Mac) and type:
pip install numpy
🧪 To check if NumPy is installed:
python
>>> import numpy
>>> print(numpy.__version__)
If no error occurs, NumPy is successfully installed.
✅ Installation using Anaconda (Optional method):
If you're using Anaconda (a data science platform):
conda install numpy
Sample Code After Installation:
import numpy as np
# Create array
arr = np.array([10, 20, 30, 40])
print("Array:", arr)
# Array operations
print("Mean:", np.mean(arr))
print("Square Root:", np.sqrt(arr))
NumPy Array
A NumPy array is a powerful data structure that stores elements (usually numbers) in a grid format
—1D, 2D, or multi-dimensional—and allows fast mathematical and logical operations on large
datasets.
It's the core data structure of the NumPy library, and much faster than Python lists.
Understanding the NumPy Array
Homogeneous: All elements in an array are of the same data type.
Efficient: Operates faster than lists due to compact memory layout.
Multidimensional: Can be 1D, 2D (matrices), or n-D.
The Fundamental Data Structure in NumPy
Python Data Type NumPy Equivalent
List 1D Array
List of Lists 2D Array (Matrix)
Table/Matrix Multi-dimensional Array (ndarray)
Creation of Arrays using Different Methods
1. Using np.array()
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
2. Using np.zeros() — Creates an array filled with 0s
zeros_arr = np.zeros((2, 3)) # 2 rows, 3 columns
3. Using np.ones() — Creates an array filled with 1s
ones_arr = np.ones((3, 2))
4. Using np.arange() — Creates range-based array
range_arr = np.arange(1, 10, 2) # [1, 3, 5, 7, 9]
5. Using np.linspace() — Linearly spaced numbers
lin_arr = np.linspace(0, 1, 5) # [0. , 0.25, 0.5 , 0.75, 1.]
Exploring Array Attributes
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:\n", arr)
print("Shape:", arr.shape) # (2, 3)
print("Size:", arr.size) # 6
print("Dimensions:", arr.ndim) # 2
print("Data Type:", arr.dtype) # int64
Attribute Description
Shape Rows × Columns (tuple)
size Total number of elements
ndim Number of dimensions
dtype Data type of elements
Importance of NumPy Arrays
No. Reason
1 Fast data processing in Python.
2 Supports mathematical and logical operations.
3 Essential for Machine Learning and AI.
4 Simplifies matrix operations.
5 Saves memory and improves speed.
6 Used in computer graphics and simulations.
Array Indexing and Slicing?
✅ Indexing
Accessing individual elements of an array using their position (index).
✅ Slicing
Accessing a range of elements (sub-array) using start:stop:step format.
NumPy indexing and slicing are more powerful than standard Python lists because they work on
multi-dimensional arrays.
Accessing Elements and Subarrays in NumPy
👉 One-Dimensional Array Indexing:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[0]) # 10
print(arr[-1]) # 50 (last element)
👉 One-Dimensional Slicing:
print(arr[1:4]) # [20 30 40]
print(arr[:3]) # [10 20 30]
print(arr[::2]) # [10 30 50]
👉 Multi-Dimensional Array Indexing:
arr2 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(arr2[0, 1]) # 2 (row 0, column 1)
print(arr2[2][2]) # 9 (row 2, column 2)
👉 Multi-Dimensional Slicing:
print(arr2[0:2, 1:]) # [[2 3], [5 6]]
print(arr2[:, 1]) # [2 5 8] – all rows, column 1
Difference: 1D vs Multi-Dimensional Indexing
Feature 1D Array 2D/3D Array
Syntax arr[i] arr[i, j] or arr[i][j]
Shape [n] [m, n] or higher
Access Single number index Tuple of indexes
Use Case Linear data Tables, matrices, images
Importance of Indexing and Slicing
No. Reason
1 Essential for data access and manipulation
2 Allows for partial data processing (subarrays)
3 Avoids use of loops (faster operations)
4 Enables complex mathematical computation on subsets
5 Used in image processing, AI, ML, etc.
Array Operations in NumPy
Array Operations involve performing mathematical and logical operations on NumPy arrays. This
includes:
Element-wise arithmetic
Comparison operations
Universal functions (ufuncs) for math operations
These operations are vectorized, meaning they are faster and more efficient than Python loops.
Performing Element-wise Operations
NumPy allows direct element-by-element operations:
✅ Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [4 10 18]
print(a ** 2) # [1 4 9]
✅ Arrays must be of the same shape or broadcastable to each other.
Exploring Universal Functions (ufuncs)
Universal Functions (ufuncs) are built-in vectorized functions in NumPy that operate element-wise
on arrays.
✅ Example:
x = np.array([1, 2, 3, 4])
print(np.sqrt(x)) # [1. 1.41 1.73 2.]
print(np.exp(x)) # [2.71 7.38 20.08 54.59]
print(np.sin(x)) # [0.84 0.91 0.14 -0.76]
print(np.log(x)) # [0. 0.69 1.09 1.38]
ufuncs operate faster than standard Python functions and support broadcasting.
Difference: Basic Arithmetic vs NumPy Array Ops
Feature Python Arithmetic Ops NumPy Array Operations
Operation type Scalar operations Element-wise operations
Speed Slower (loop-based) Faster (vectorized)
Syntax Verbose Clean, simple
Data type Python types (int, float) NumPy arrays
Broadcasting Not supported Supported
Works on arrays? No Yes
Mathematical functions Need math module Built-in ufuncs
Loops required? Yes No
Importance of Array Operations
Reason Description
✅ Speeds up computation without loops
✅ Required for data manipulation
Reason Description
✅ Used in all numerical tasks
✅ Simplifies mathematical expressions
✅ Crucial for ML, AI, data analysis
Linear Algebra with NumPy
Linear Algebra is a branch of mathematics that deals with vectors, matrices, and linear equations.
In Python, the NumPy library provides powerful tools for linear algebra operations.
Introduction to Linear Algebra in NumPy
NumPy provides the numpy.linalg module which includes:
Matrix multiplication
Determinant
Inverse of a matrix
Solving systems of linear equations
Key Operations in Linear Algebra with NumPy
1️⃣ Matrix Multiplication
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = np.dot(A, B) # OR A @ B
print(result)
2️⃣ Determinant of a Matrix
det = np.linalg.det(A)
print(det)
3️⃣ Inverse of a Matrix
inv = np.linalg.inv(A)
print(inv)
4️⃣ Solving Linear Equations (Ax = b)
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x)
Difference between Basic and NumPy Linear Algebra
Feature Without NumPy With NumPy
Code complexity High (manual implementation) Low (built-in functions)
Performance Slow for large data Fast and efficient
Matrix support Limited Strong multi-dimensional support
Operations Manual calculation Vectorized with linalg module
Learning Difficult Easier with NumPy
File Input and Output with NumPy
File I/O (Input/Output) in NumPy refers to saving data to and loading data from files (like .txt,
.csv, .npy, .npz) using NumPy’s built-in functions. This is essential for working with large
datasets, intermediate results, or preprocessed data.
Basic Functions in NumPy File I/O
Operation Function Used Description
Save to text file np.savetxt() Saves array to .txt or .csv
Load from text file np.loadtxt() Loads array from .txt or .csv
Save in binary Saves in .npy format (efficient
np.save()
format binary)
Load from binary np.load() Loads from .npy file
np.savez() /
Save multiple arrays Saves multiple arrays to .npz
savez_compressed()
Example Programs
1️⃣ Save and Load using .txt file
import numpy as np
data = np.array([[1, 2], [3, 4]])
np.savetxt("data.txt", data)
loaded_data = np.loadtxt("data.txt")
print(loaded_data)
2️⃣ Save and Load using .npy file (binary)
np.save("data.npy", data)
loaded_data = np.load("data.npy")
print(loaded_data)
3️⃣ Save multiple arrays using .npz
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.savez("multi_data.npz", arr1=a, arr2=b)
data = np.load("multi_data.npz")
print(data['arr1'])
print(data['arr2'])
Difference between File I/O in NumPy vs Regular Python
Feature Regular Python File I/O NumPy File I/O
Format Text or binary .txt, .csv, .npy, .npz
Data Types Manual (need str() or int()) Auto-detects arrays
Speed Slower for large numeric data Optimized for numeric arrays
Read/Write Ease Manual parsing required One-line read/write with NumPy
File Size Larger for text files Smaller with .npy (binary format)
Compatibility General purpose Best for NumPy-based systems
Diagram Suggestion for Notes
+---------------------+
| NumPy Array |
+---------------------+
↓
Save to File
↓
-------------------------------
| .txt | .csv | .npy/.npz |
-------------------------------
↓
Load from File
↓
+---------------------+
| NumPy Array Restored |
+---------------------+
Broadcasting in NumPy
Broadcasting is a powerful feature in NumPy that allows arithmetic operations between arrays of
different shapes without explicitly replicating data.
Broadcasting automatically expands the smaller array across the larger one so operations can be
performed element-wise.
✅ Example:
import numpy as np
a = np.array([1, 2, 3])
b = 2
result = a + b
print(result) # Output: [3 4 5]
Here, b is broadcasted to match the shape of a.
Broadcasting Rules (Simplified)
1. If arrays differ in dimensions, the smaller one is padded with 1s on the left.
2. Dimensions are compatible if:
o They are equal, or
o One of them is 1
3. Arrays are broadcasted to match each other in shape.
Importance in Diploma-Level Python Subject
Use in Python Subject Why Broadcasting Matters
Data Science / ML Perform vectorized operations quickly
Image Processing Add or scale pixel values easily
Mathematics / Statistics Efficient matrix operations
IoT / Sensor Analysis Apply functions across sensor data arrays
Projects & Assignments Reduces loops and manual dimension matching
Real-Time Data Apply same value to entire datasets
Use Case Examples
1️⃣ Adding scalar to array
a = np.array([10, 20, 30])
b = 5
print(a + b) # Output: [15 25 35]
2️⃣ Adding 2D array with 1D array (row-wise)
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
print(a + b)
# Output:
# [[11 22 33]
# [14 25 36]]
Difference Between Broadcasting vs Normal Operations
Feature Normal Array Operations Broadcasting
Shapes Must be exactly the same Can be different (if broadcastable)
Speed Often slower Faster and optimized
Memory Usage May create copies Saves memory
Flexibility Rigid Flexible across shapes
Manual Work Requires reshaping Automatic shape matching
Diagram: Broadcasting Example
Array A shape = (3, 3) Array B shape = (1, 3)
A = [[1 2 3] + B = [10 20 30]
[4 5 6]
[7 8 9]]
Result:
[[11 22 33]
[14 25 36]
[17 28 39]]
Here, B is broadcasted vertically to match A.