DS SEM-3
Working with Tabular Numeric Data (NumPy with Python
Getting started
· Install
and import: pip install numpy, then use import numpy as np in scripts for
consistency and shorter code in answers.
· NumPy
arrays are homogeneous, n-dimensional arrays optimized for vectorized
operations and broadcasting, making them faster than Python lists for numeric
work.
NumPy arrays with array()
· Create
from Python lists or nested lists: a = np.array(), b = np.array([,]).
· dtype is
inferred; can be set: np.array(, dtype=np.float64).
· Exam tip:
mention “ndarray is homogeneous and supports vectorized ops.”
Example:
import numpy as np
a = np.array([10, 20, 30])
b = np.array([[1, 2], [3, 4]])
print(a.dtype, a.shape) # int64 (3,)
print(b.ndim, b.size) # 2 4
Array attribute
· shape:
tuple of dimensions, e.g., (2,3).
· ndim:
number of axes, e.g., 1D/2D.
· itemsize
and nbytes: bytes per element and total bytes.
Example:
x = np.array([[1,2,3],[4,5,6]], dtype=np.int32)
print(x.shape, x.ndim, x.size, x.dtype, x.itemsize, x.nbytes)
Arrays with initial placeholder content
· Zeros: np.zeros((2,3))
· ones:
np.ones((3,))
· arange:
np.arange(0,10,2),
· linspace :
np.linspace(0,1,5).
· eye/identity:
np.eye(3) for identity matrices.
· random
examples (if allowed in syllabus): np.random.rand(2,3).
Example:
z = np.zeros((2,3))
o = np.ones(4)
f = np.full((2,2), 5)
r = np.arange(1, 10, 2)
Integer indexing
· 1D: a[i]
returns element at index i.[3]
· 2D: A[i,
j] accesses row i, column j; A[i] returns row i.[3]
· Negative
indices count from end: a[-1].[3]
· Fancy
indexing: use integer arrays/lists to pick positions: a[].[2][3][1]
Example:
A =
np.array([[10,11,12],[20,21,22]])
print(A[0,2], A[^1], A[-1,-1]) # 12 [20
21 22] 22
Array indexing (fancy indexing)
· Select
specific rows/cols by index arrays: A[,] picks (0,1) and (1,2).[4][1][3]
· Row/column
selection: A[, :] or A[:, ].[4][1][3]
· Useful
for reordering or sampling without loops.[3]
Example:
A =
np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A[[0,2], :]) # rows 0 and 2
print(A[:, [0,2]]) # columns 0 and 2
Boolean array indexing
· Create a
boolean mask from a condition: mask = A > 5; A[mask] returns matching
elements.[3]
· Combine
conditions with & and |, use parentheses: A[(A%2==0) & (A>3)]. [3]
· Good for
filtering without loops.[2]
Example:
A = np.array([1,4,6,7,10])
print(A[A > 5]) # [ 6 7 10]
print(A[(A % 2 == 0)]) # [ 4 6 10]
Slicing and iterating
· Slicing:
a[start:stop:step]; defaults allowed, negative step reverses: a[::-1].[3]
· 2D
slices: A[r0:r1, c0:c1] gives a view (no copy); modifying the slice changes
original; use .copy() if needed.[3]
· Iteration
is possible but vectorization is preferred for performance.[2]
Example:
A =
np.array([[1,2,3],[4,5,6],[7,8,9]])
sub = A[0:2, 1:3] # [[2,3],[5,6]]
sub[0,0] = 99 # changes A unless
copy() used
Basic arithmetic operations
· Elementwise:
+, -, *, /, **, np.add, np.subtract, np.multiply, np.divide, np.sqrt.[3]
· Works
shape-wise; if shapes match or broadcast-compatible, operations apply per
element.[3]
· Matrix
multiplication (if needed): A @ B or np.matmul(A,B) distinct from elementwise
*.[3]
Example:
x = np.array([1,2,3],
dtype=float)
y = np.array([4,5,6], dtype=float)
print(x + y, x * y, x / y, x ** 2)
Mathematical functions in NumPy
· Elementwise
ufuncs: np.sqrt, np.exp, np.log, np.sin, np.cos.
· Aggregations:
a.sum(), a.mean(), a.min(), a.max(), axis parameter for row/col operations.[2]
· Example
with axis:
M =
np.array([[1,2,3],[4,5,6]])
print(M.sum()) # 21
print(M.sum(axis=0)) # column-wise: [5
7 9]
print(M.mean(axis=1)) # row-wise: [2.
5.]
Changing the shape of an array
· reshape(new_shape)
returns a view when possible; size must match.
· ravel()
flattens to 1D view; flatten() creates a copy.
· Transpose:
A.T swaps axes for 2D; np.transpose for general axes.
Example:
a = np.arange(6) # [0 1 2 3 4 5]
b = a.reshape((2,3)) # [[0 1
2],[3 4 5]]
c = b.T # [[0 3],[1
4],[2 5]]
Stacking and splitting arrays
· Vertical
and horizontal stacking: np.vstack([a,b]), np.hstack([a,b]).
· Column_stack
for 1D to 2D columns: np.column_stack([a,b]).
· Splitting:
np.vsplit(A, n), np.hsplit(A, n) or np.split(A, indices, axis).
Example:
a = np.array([1,2,3])
b = np.array([4,5,6])
print(np.vstack([a,b])) # [[1 2 3],[4 5
6]]
print(np.hstack([a,b])) # [1 2 3 4 5 6]
Broadcasting
· Rule:
trailing dimensions must be equal or one of them is 1; arrays expand logically
to match without copying.
· Common
cases: add scalar to array; add 1D row to 2D rows; add column vector (n,1) to
2D columns.
· Avoid
shape mismatches; reshape with np.newaxis or None.
Example:
M =
np.array([[1,2,3],[4,5,6]])
row = np.array([10,20,30])
col = np.array([[^100],[^200]])
print(M + row) # row-wise add
print(M + col) # column-wise
add
Quick exam pointers
· Define
ndarray, homogeneity, and vectorization; give one-line examples.
· Distinguish
elementwise * vs matrix @.
· Explain
view vs copy in slicing; mention .copy() to avoid side-effects.
· State
broadcasting rule and give a shape example, e.g., (2,3) + (3,) works, (2,3) +
(2,) doesn’t.
⁂Read More from other sources :
![]()
1. https://numpy.org/doc/stable/user/absolute_beginners.html
2. https://realpython.com/numpy-tutorial/
3. https://cs231n.github.io/python-numpy-tutorial/
4. https://www.w3schools.com/python/numpy/default.asp
5. https://www.geeksforgeeks.org/python/numpy-tutorial/
6. https://www.youtube.com/watch?v=QUT1VHiLmmI
7. https://www.kaggle.com/code/iamramzanai/numpy-tutorial-beginner-to-advanced
Comments
Post a Comment