NumPy – Single Detailed Notes
1. Introduction to NumPy
NumPy (Numerical Python) is a core Python library used for numerical and scientific computing. It
provides fast, memory‑efficient array operations and mathematical functions.
Why NumPy? - Faster than Python lists - Less memory usage - Built‑in mathematical operations -
Foundation for libraries like Pandas, SciPy, TensorFlow
import numpy as np
2. NumPy Array (ndarray)
The main object in NumPy is the ndarray (n‑dimensional array).
Creating an Array
a = [Link]([1, 2, 3])
- Converts a list into a NumPy array - All elements must be of the same data type
Types of Arrays
• 1D Array: [1, 2, 3]
• 2D Array (Matrix): [[1, 2], [3, 4]]
• 3D Array: Multiple matrices
b = [Link]([[1,2],[3,4]])
3. Difference: Python List vs NumPy Array
Feature Python List NumPy Array
Speed Slow Fast
Data Type Mixed Same
Math Ops Manual Automatic
Memory More Less
1
4. Array Attributes (Detailed Notes)
a = [Link]([[1,2,3],[4,5,6]])
[Link] → Number of Dimensions
• ndim stands for number of dimensions of the array
• It tells whether the array is 1D, 2D, 3D, etc.
• For the given array, there are rows and columns, so it is 2D
[Link] # 2
[Link] → Structure of the Array
• shape shows how the data is arranged
• It is written as (rows, columns) for 2D arrays
[Link] # (2, 3)
Meaning: - 2 → number of rows - 3 → number of columns
[Link] → Total Number of Elements
• size gives the total count of elements in the array
• Formula:
size = rows × columns
[Link] # 6
Because:
2 × 3 = 6
[Link] → Data Type of Elements
• dtype tells the type of data stored in the array
• NumPy stores all elements using the same data type
• This makes NumPy fast and memory efficient
2
[Link] # int32 or int64
Examples:
[Link]([1,2,3]).dtype # int
[Link]([1.5,2.5]).dtype # float
[Link]([1,2.5]).dtype # float (automatic conversion)
Quick Summary Table
Attribute Meaning Example Output
ndim Number of dimensions 2
shape Rows, Columns (2, 3)
size Total elements 6
dtype Data type int32 / int64
5. Creating Special Arrays (Detailed Notes)
NumPy provides built-in functions to quickly create arrays with specific values. These are called special
arrays.
1. [Link]() → Array Filled with Zeros
[Link]((2,3))
Output:
[[0. 0. 0.]
[0. 0. 0.]]
Explanation: - Creates an array where all elements are 0 - (2,3) means 2 rows and 3 columns -
Default data type is float
Use: - Initializing arrays - Mathematical calculations
2. [Link]() → Array Filled with Ones
3
[Link]((3,3))
Output:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Explanation: - Creates an array where all elements are 1 - (3,3) means 3 rows and 3 columns
Use: - Creating test data - Mathematical operations
3. [Link]() → Identity Matrix
[Link](3)
Output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Explanation: - Creates a square matrix - Diagonal elements are 1 - All other elements are 0
Use: - Linear algebra - Matrix calculations
4. [Link]() → Sequence of Numbers
[Link](1, 10, 2)
Output:
[1 3 5 7 9]
Explanation: - Works like Python range() - Syntax:
[Link](start, stop, step)
- start = 1 - stop = 10 (not included) - step = 2
Use: - Creating number sequences
4
5. [Link]() → Evenly Spaced Numbers
[Link](1, 10, 5)
Output:
[ 1. 3.25 5.5 7.75 10. ]
Explanation: - Generates 5 evenly spaced numbers between 1 and 10 - Both start and end values are
included
Use: - Graph plotting - Scientific calculations
Summary Table
Function Purpose Example Output
zeros() All zeros 0 values
ones() All ones 1 values
eye() Identity matrix Diagonal = 1
arange() Number sequence Step-based
linspace() Even spacing Fixed count
6. Indexing and Slicing (Detailed Notes)
Indexing and slicing are used to access elements or parts of an array.
1D Array Indexing and Slicing
a = [Link]([10, 20, 30, 40])
• a[0] → First element → 10
• a[-1] → Last element → 40
• a[1:3] → Elements from index 1 to 2 → [20 30]
Note: - Indexing starts from 0 - Last index is excluded in slicing
5
2D Array Indexing
b = [Link]([[1,2,3],[4,5,6]])
• b[1,2] → Element in 2nd row, 3rd column → 6
Syntax:
array[row_index, column_index]
7. Mathematical Operations (Element-wise)
NumPy performs element-wise operations by default.
a = [Link]([1,2,3])
b = [Link]([4,5,6])
• a + b → [5 7 9] (Addition)
• a - b → [-3 -3 -3] (Subtraction)
• a * b → [4 10 18] (Multiplication)
• a / b → [0.25 0.4 0.5] (Division)
Scalar Operation
a * 2
Output:
[2 4 6]
8. Universal Functions (ufuncs)
Universal functions perform fast mathematical operations on arrays.
[Link](a) # Square root
[Link](a) # Natural logarithm
[Link](a) # Exponential
[Link](a) # Sine
[Link](a) # Cosine
6
9. Aggregation Functions
Aggregation functions return a single value from an array.
a = [Link]([1,2,3,4,5])
• [Link](a) → Sum of elements
• [Link](a) → Average
• [Link](a) → Maximum value
• [Link](a) → Minimum value
• [Link](a) → Standard deviation
10. Reshaping Arrays
Reshaping changes the shape of an array without changing data.
a = [Link]([1,2,3,4,5,6])
[Link](2,3)
Output:
[[1 2 3]
[4 5 6]]
11. Matrix Operations
A = [Link]([[1,2],[3,4]])
B = [Link]([[5,6],[7,8]])
Matrix Multiplication
A @ B
Transpose of a Matrix
A.T
7
12. Copy vs View
a = [Link]([1,2,3])
b = [Link]() # Independent copy
c = [Link]() # Shared memory
Explanation: - copy() → Changes do not affect original array - view() → Changes affect original
array
End of Notes