CBSE AI 417 Practical: NumPy Arrays, Shape & Vector Math

⚡ Direct Answer & Key Takeaway

In CBSE Class 10 AI (Code 417), the 50-mark Practical assessment tests NumPy under Unit 4 (Data Science) using `np.array()`, `.shape`, `.ndim`, `.reshape()`, and element-wise arithmetic. Unlike Python lists, NumPy `ndarray` stores homogeneous numeric types in contiguous memory, enabling vectorized operations like `scores+5` or `arr.mean(axis=0)` without loops. Remember that `reshape(r,c)` requires `r*c==arr.size`, and `arange(start,stop,step)` excludes `stop`. Both Part B theory (40 marks) and viva questions regularly test 2D indexing and axis aggregations.

1. Creating NumPy ndarrays and Inspecting Dimensions

A NumPy `ndarray` forms the numerical backbone of CBSE Class 9 and 10 Data Science practicals because Python lists cannot perform direct matrix math or fast columnar statistics. You initialize a 1D array using `np.array([12, 18, 24, 30])` or generate sequences with `np.arange(1, 10, 2)`, `np.zeros((2, 3))`, and `np.ones((3, 2))`. Every array exposes structural attributes that examiners ask during live coding checks: `.ndim` returns the number of axes, `.shape` returns a tuple of `(rows, columns)`, `.size` counts total elements, and `.dtype` reveals the uniform storage type such as `int64` or `float64`. Mixing a single float into an integer list automatically upcasts the entire array to floating-point values, ensuring consistent precision across downstream machine learning calculations in both Code 417 and Class 11–12 Code 843 labs.

  • Use `np.array()` for fixed values and `np.arange(start, stop, step)` for evenly spaced ranges.
  • Check `.shape` and `.ndim` before feeding arrays into Data Science or OpenCV operations.
  • Pass tuples like `(2, 3)`—not two separate integers—into `np.zeros((2, 3))` and `np.ones((2, 3))`.

2. Reshaping Arrays and 2D Slicing Rules

Transforming tabular features before feeding them to a model requires mastery of `.reshape()` and zero-based 2D slicing. When converting a 1D vector of six marks into a two-row table, `arr.reshape(2, 3)` succeeds because `2 * 3` equals `.size`, whereas `arr.reshape(2, 4)` triggers a `ValueError`. You can also pass `-1` for one unknown dimension, such as `arr.reshape(2, -1)`, letting NumPy infer three columns automatically. For extraction, `matrix[row_slice, col_slice]` follows half-open intervals `[start:stop]` where `stop` is never included. Using `matrix[:, 1]` isolates the entire second column as a 1D view, while `matrix[0:2, 1:3]` carves out a submatrix. Because slicing produces a view rather than an independent copy, modifying a slice alters the original array unless `.copy()` is explicitly called. Always verify `.shape` before matrix operations during practical evaluations.

  • Maintain exact element conservation: `new_rows * new_cols == arr.size` is mandatory for `.reshape()`.
  • Slice rows first and columns second using `arr[r1:r2, c1:c2]`, remembering `r2` and `c2` are excluded.
  • Call `.copy()` on sliced subsets whenever original dataset values must remain untouched.

3. Vectorized Arithmetic, Broadcasting and Axis Statistics

Vectorized math eliminates slow `for` loops by applying operators element by element across aligned arrays. Adding two `(2, 3)` matrices computes cell-by-cell sums, while broadcasting automatically stretches a scalar like `+ 5` or a compatible `(3,)` row vector across every row of a `(2, 3)` dataset. Note carefully that `*` performs element-wise multiplication, not matrix multiplication; for dot products in AI regression tasks, use `np.dot(A, B)` or the `@` operator. Statistical reductions—`np.sum()`, `np.mean()`, `np.min()`, and `np.max()`—accept an `axis` parameter that students frequently confuse in viva exams. Setting `axis=0` collapses rows to compute one value per column, whereas `axis=1` collapses columns to produce one summary metric per row. This distinction lets you quickly calculate subject averages (`axis=0`) versus individual student totals (`axis=1`) in one line.

  • Scalar broadcasting applies operations across every element without extra memory allocation.
  • Use `axis=0` for column summaries (down rows) and `axis=1` for row summaries (across columns).
  • Reserve `@` or `np.dot()` for true linear algebra matrix products.

4. NumPy Array Reshape, Broadcasting & Axis Stats (CBSE AI 417)

import numpy as np

# Marks of 3 students across 4 AI unit tests
raw_scores = np.arange(70, 94, 2)  # 12 values: 70, 72, ..., 92
marks = raw_scores.reshape(3, 4)

print("Shape:", marks.shape, "| Dims:", marks.ndim)
bonus_marks = marks + 3  # Broadcasting +3 to every cell
print("With +3 bonus:\n", bonus_marks)
print("Subject averages (axis=0):", np.mean(marks, axis=0))
print("Student totals (axis=1):", np.sum(marks, axis=1))

Verified Console / Dry-Run Output:

Shape: (3, 4) | Dims: 2
With +3 bonus:
 [[73 75 77 79]
 [81 83 85 87]
 [89 91 93 95]]
Subject averages (axis=0): [78. 80. 82. 84.]
Student totals (axis=1): [292 324 356]

5. Worked NumPy Attributes, Reshaping & Axis Reference Table

NumPy Operation / ExpressionSample Input ArrayReturned ResultPractical / Viva Note
`arr.shape` & `arr.ndim``np.array([[2, 4, 6], [8, 10, 12]])``(2, 3)` and `2`2 rows, 3 columns, 2 axes
`np.arange(1, 10, 3)``start=1, stop=10, step=3``array([1, 4, 7])`Stops strictly before `10`
`arr.reshape(3, -1)``np.arange(6)` (`size=6`)`[[0, 1], [2, 3], [4, 5]]``-1` auto-infers `2` columns
`arr.mean(axis=0)``[[10, 20], [30, 40]]``array([20., 30.])`Averages down each column
`arr[0:2, 1]``[[10, 20], [30, 40]]``array([20, 40])`Extracts column index `1` as 1D view
🎯 1-on-1 Live Mentorship & Free Study Kit

Book a Free 1:1 CBSE AI 417 Coding Session with 4Achievers Junior

Practice live NumPy reshaping, 2D slicing, and CBSE AI 417 practical questions in personalized 1-on-1 or 1:4 Micro-Pod coding sessions for Grades 3–12.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Calling `arr.reshape(2, 5)` on a 12-element array, which raises a `ValueError` because `2 * 5 != 12`.
  • Avoid Mistake: Confusing `axis=0` (column-wise vertical reduction) with `axis=1` (row-wise horizontal reduction) during `np.mean()` or `np.sum()`.
  • Avoid Mistake: Expecting `*` to compute matrix multiplication instead of element-wise multiplication; use `@` or `np.dot()` for dot products.
  • Avoid Mistake: Assuming `np.arange(1, 5)` includes `5`; it stops at `4` just like Python's built-in `range()`.
  • Examiner / Mentor Tip: Draw a small 2x3 grid on scratch paper before answering any `axis=0` vs `axis=1` question in Part B or Viva.
  • Examiner / Mentor Tip: Double-check whether `np.zeros()` was given a single tuple `np.zeros((2, 3))`—two separate arguments trigger a `TypeError`.
  • Examiner / Mentor Tip: If asked why NumPy beats lists in AI models, cite contiguous homogeneous storage and vectorized C-level execution.

7. Interactive Concept Check Quiz

Question: What is the output of `np.sum(np.arange(2, 14, 2).reshape(2, 3), axis=0)`?

8. Frequently Asked Questions

Why does CBSE AI 417 prefer NumPy arrays over standard Python lists?
A Python list can store mixed data types and concatenates with `+`, whereas a NumPy `ndarray` enforces one uniform data type and performs fast element-wise math (`arr + 5`) without writing loops.
What does `arr.reshape(3, -1)` do in NumPy?
Passing `-1` tells NumPy to calculate that dimension automatically from `.size`. For a 12-element array, `arr.reshape(3, -1)` automatically produces a `(3, 4)` matrix.
What is the default data type of `np.zeros()` and `np.ones()`?
`np.zeros((2, 3))` creates a 2x3 array filled with `0.0` (`float64` by default), whereas `np.ones((2, 3))` fills every cell with `1.0`. Pass `dtype=int` if integer zeros or ones are required.
Is NumPy still tested in CBSE Class 11 and 12 AI (Code 843)?
Yes. NumPy indexing, `.reshape()`, broadcasting, and matrix dot products form the foundation for Class 11–12 AI (Code 843) Data Handling and Regression workflows.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class