Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Final NumPy Assessment

Welcome to the final NumPy assessment at CIIT TRANING INSTITUTE. This assessment evaluates your understanding of NumPy arrays, array creation, indexing, slicing, data types, reshaping, broadcasting, mathematical functions, filtering, sorting, random number generation, linear algebra, and file operations. Attempt the questions independently before checking the answers and solutions.

Assessment Instructions

  • Read each question carefully before answering.
  • Use Python and NumPy to test your answers where appropriate.
  • For output-based questions, predict the result before running the code.
  • For coding questions, write a complete and readable solution.
  • Try to complete the assessment before reviewing the answer key.

Suggested duration: 60–90 minutes.

Total marks: 100.

Section A: Multiple-Choice Questions

10 questions × 2 marks = 20 marks

Question 1

Which function creates an array containing a sequence of integers?

  • A. np.array_range()
  • B. np.arange()
  • C. np.sequence()
  • D. np.integer_array()

Question 2

What is the shape of the following array?

data = np.array([[1, 2, 3], [4, 5, 6]])
  • A. (6,)
  • B. (3, 2)
  • C. (2, 3)
  • D. (2, 2)

Question 3

Which expression selects the second column of a two-dimensional array named data?

  • A. data[1, :]
  • B. data[:, 1]
  • C. data[2, :]
  • D. data[1, 1]

Question 4

Which attribute returns the total number of elements in an array?

  • A. shape
  • B. ndim
  • C. size
  • D. dtype

Question 5

Which function returns unique values from an array?

  • A. np.distinct()
  • B. np.unique()
  • C. np.remove_duplicates()
  • D. np.different()

Question 6

What does axis=0 do when used with sum() on a two-dimensional array?

  • A. Calculates a sum for each row.
  • B. Calculates a sum for each column.
  • C. Returns only the first element.
  • D. Reshapes the array.

Question 7

Which operator performs element-wise multiplication of two compatible arrays?

  • A. *
  • B. @
  • C. **
  • D. //

Question 8

Which function is commonly used to solve a suitable square system of linear equations?

  • A. np.linalg.sort()
  • B. np.linalg.solve()
  • C. np.linalg.reshape()
  • D. np.linalg.random()

Question 9

Which method is recommended for creating a modern NumPy random generator?

  • A. np.random.default_rng()
  • B. np.random.create_array()
  • C. np.generator.seed_array()
  • D. np.random.make_random()

Question 10

Which NumPy file format is intended for saving a single array in binary format?

  • A. .txt
  • B. .csv
  • C. .npy
  • D. .html

Section B: Predict the Output

5 questions × 4 marks = 20 marks

Question 11: Array Slicing

import numpy as np numbers = np.array([10, 20, 30, 40, 50, 60]) print(numbers[1:5:2])

Your task: Predict the output.

Answer:

[20 40]

Question 12: Reshaping

numbers = np.arange(1, 7) matrix = numbers.reshape(2, 3) print(matrix) print(matrix.shape)

Answer:

[[1 2 3] [4 5 6]] (2, 3)

Question 13: Boolean Filtering

values = np.array([5, 12, 18, 25, 30]) print(values[values >= 18])

Answer:

[18 25 30]

Question 14: Broadcasting

a = np.array([1, 2, 3]) print(a * 4)

Answer:

[ 4 8 12]

Question 15: Aggregation

data = np.array([ [2, 4, 6], [1, 3, 5] ]) print(data.sum(axis=0)) print(data.sum(axis=1))

Answer:

[ 3 7 11] [12 9]

Section C: Practical Coding Questions

5 questions × 6 marks = 30 marks

Question 16: Create and Inspect an Array

Create a 3 × 3 array containing integers from 1 to 9. Display its shape, dimensions, size, and data type.

Solution:

import numpy as np data = np.arange(1, 10).reshape(3, 3) print(data) print("Shape:", data.shape) print("Dimensions:", data.ndim) print("Size:", data.size) print("Data type:", data.dtype)

Expected output:

[[1 2 3] [4 5 6] [7 8 9]] Shape: (3, 3) Dimensions: 2 Size: 9 Data type: int64

The displayed integer data type may differ depending on the platform and NumPy's type selection.

Question 17: Analyze Student Marks

Create a marks array for Samadhan, Pradnya, and Samiksha. Calculate each student's total and average, and identify students whose average is at least 80.

Solution:

students = np.array(["Samadhan", "Pradnya", "Samiksha"]) marks = np.array([ [75, 85, 90], [90, 95, 88], [65, 70, 80] ]) totals = marks.sum(axis=1) averages = marks.mean(axis=1) print("Totals:", totals) print("Averages:", averages) print("Qualified:", students[averages >= 80])

Expected output:

Totals: [250 273 215] Averages: [83.33333333 91. 71.66666667] Qualified: ['Samadhan' 'Pradnya']

Question 18: Filter and Replace Values

Replace negative values with zero and select all values greater than 10.

Solution:

values = np.array([-5, 8, 12, -2, 20, 7]) cleaned = np.where(values < 0, 0, values) selected = cleaned[cleaned > 10] print("Cleaned:", cleaned) print("Selected:", selected)

Expected output:

Cleaned: [ 0 8 12 0 20 7] Selected: [12 20]

Question 19: Sort Sales Values

Sort sales in descending order and identify the maximum sale.

Solution:

sales = np.array([450, 200, 700, 350, 600]) descending = np.sort(sales)[::-1] maximum = np.max(sales) print("Descending:", descending) print("Maximum:", maximum)

Expected output:

Descending: [700 600 450 350 200] Maximum: 700

Question 20: Perform Matrix Multiplication

Multiply the two matrices and display the result.

Solution:

A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) result = A @ B print(result)

Expected output:

[[19 22] [43 50]]

Section D: Advanced Application Questions

3 questions × 10 marks = 30 marks

Question 21: Sales Report Analysis

Given a matrix of monthly sales for three products:

  • Calculate the total sales for each product.
  • Calculate the total sales for each month.
  • Identify the product with the highest total sales.
  • Identify the month with the highest total sales.

Solution:

products = np.array(["Product A", "Product B", "Product C"]) sales = np.array([ [100, 150, 200], [120, 130, 180], [90, 160, 210] ]) product_totals = sales.sum(axis=1) monthly_totals = sales.sum(axis=0) best_product = products[np.argmax(product_totals)] best_month = np.argmax(monthly_totals) print("Product totals:", product_totals) print("Monthly totals:", monthly_totals) print("Best product:", best_product) print("Best month index:", best_month)

Expected output:

Product totals: [450 430 460] Monthly totals: [310 440 590] Best product: Product C Best month index: 2

Question 22: Normalize a Dataset

Normalize the following values using min-max normalization:

values = np.array([10.0, 20.0, 30.0, 40.0, 50.0])

Use the formula:
(value - minimum) / (maximum - minimum)

Solution:

minimum = values.min() maximum = values.max() normalized = (values - minimum) / (maximum - minimum) print(normalized)

Expected output:

[0. 0.25 0.5 0.75 1. ]

In reusable code, handle the case where the minimum and maximum are equal to avoid division by zero.

Question 23: Solve a Linear System

Solve the equations:
2x + y = 8
x + 3y = 9

Solution:

A = np.array([ [2.0, 1.0], [1.0, 3.0] ]) b = np.array([8.0, 9.0]) solution = np.linalg.solve(A, b) print(np.round(solution, 2))

Expected output:

[3. 2.]

Answer Key: Section A

Question Correct answer Explanation
1 B — np.arange() Creates values in a specified numerical range.
2 C — (2, 3) The array has two rows and three columns.
3 B — data[:, 1] Selects all rows from the second column.
4 C — size Returns the total number of elements.
5 B — np.unique() Returns the distinct values.
6 B — Each column axis=0 reduces across rows.
7 A — * Performs element-wise multiplication.
8 B — np.linalg.solve() Solves suitable linear systems.
9 A — np.random.default_rng() Creates a modern random generator.
10 C — .npy Stores a single NumPy array in binary format.

Suggested Evaluation Rubric

Section Marks Skills evaluated
A. Multiple choice 20 Core NumPy concepts and terminology.
B. Predict the output 20 Indexing, slicing, broadcasting, and aggregation.
C. Practical coding 30 Correct implementation and output.
D. Advanced applications 30 Problem-solving, numerical reasoning, and analysis.
Total 100 Overall NumPy proficiency.

Suggested Performance Bands

  • 90–100: Excellent — strong command of NumPy fundamentals and applications.
  • 75–89: Very good — capable of solving most practical NumPy problems.
  • 60–74: Good — review advanced indexing, axes, and broadcasting.
  • 40–59: Developing — revisit array creation, indexing, and core operations.
  • Below 40: More practice recommended before moving to advanced projects.

These performance bands are suggested learning guidelines. The institute can adjust them to match its own assessment policy.

Summary

This final assessment reviewed NumPy array creation, indexing, slicing, attributes, Boolean filtering, broadcasting, aggregation, sorting, vectorization, normalization, matrix multiplication, linear algebra, and practical data analysis.

Review any incorrect answers, rerun the coding exercises, and practice applying NumPy to real datasets. Completing this assessment provides a foundation for using NumPy in data analysis, scientific computing, machine learning preparation, and numerical programming projects.