- LPU MCA Semester 1
- CAB107
- Unit 2
CAB107 · Unit 2: Python for AI and data science
NumPy for numerical computing
10 practice questions · in the MTE and ETE syllabus
Key points
- ndarray is fixed-type and vectorised, so it beats Python loops
- axis=0 gives column results, axis=1 gives row results
- Broadcasting stretches size-1 dimensions without copying
- Basic slices are views, not copies
NumPy provides the ndarray, a fixed-type, contiguous n-dimensional array. Vectorised operations run in compiled C over contiguous memory, which is why they are usually much faster than equivalent Python loops over lists.
Useful functions: np.arange(n) for integers 0 to n-1, np.linspace(a, b, k) for k evenly spaced values from a to b, reshape to change shape, np.eye(n) for an identity matrix, np.argsort for sorting indices.
Broadcasting lets arithmetic work on arrays of different shapes by stretching dimensions of size 1 without copying data.
import numpy as np
a = np.arange(12).reshape(3, 4)
print(a.sum(axis=0))
print(a.sum(axis=1))
x = np.arange(5)
s = x[1:4]
s[0] = 99
print(x.tolist())
Output: [12 15 18 21], then [ 6 22 38], then [0, 99, 2, 3, 4]. The axis argument names the dimension that is collapsed: axis=0 sums down the rows giving one value per column, axis=1 sums across columns giving one value per row.
Basic slices are views, so writing into s changes x; use .copy() for an independent array.
Exam trap: confusing axis=0 with row sums, and assuming a slice is a copy.
Try two questions
Why is a vectorised NumPy expression usually faster than the equivalent Python loop?
- NumPy skips the calculation for elements it considers unimportant to the final result
- The loop runs in compiled C code over contiguous memory instead of interpreted Python bytecode
- NumPy stores every number as a text string, which makes comparing the values much quicker for the CPU
- NumPy runs each iteration of the loop on a separate computer connected to the main machine
Show the answer
B. Compiled loops over typed contiguous memory remove interpreter overhead.
Why not A: NumPy computes every element; it does not skip any.
Why not C: Arrays use fixed numeric types, not text.
Why not D: Vectorised code runs on one machine in compiled loops.
What does broadcasting allow in NumPy?
- Arithmetic between arrays of different shapes by stretching dimensions of size 1 without copying data
- Sending an array over the network to several machines at the same time to share the workload
- Adding arrays only when both of them have exactly the same number of elements and the same shape as each other
- Converting every array to a Python list before computing so that shapes can be matched
Show the answer
A. Broadcasting aligns shapes from the right and virtually repeats size-1 dimensions.
Why not B: Broadcasting is a shape rule and has nothing to do with networking.
Why not C: Equal shapes are the simple case; broadcasting is what lets different shapes work.
Why not D: NumPy stays in array form for speed rather than converting to lists.