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

Shallow and Deep Copy Concepts

Shallow and deep copies are important concepts when working with NumPy arrays and Python objects. They help you understand whether data is shared between objects or duplicated independently. In this lesson, you will learn how to create copies, inspect their behavior, and choose the appropriate approach for your data.

1. What Is a Shallow Copy?

A shallow copy creates a new outer object but does not recursively copy every object contained inside it. The copied container can still refer to the same nested objects as the original.

For a regular NumPy array containing numeric values, array.copy() creates independent element data. However, for an array with dtype=object, the array's references are copied while the referenced Python objects can remain shared.

import numpy as np

original = np.array([10, 20, 30, 40])

shallow_copy = original.copy()

shallow_copy[0] = 999

print("Original:", original)
print("Copy:", shallow_copy)

Output

Original: [10 20 30 40]
Copy: [999  20  30  40]

For this numeric array, the copy has its own element storage. Changing an element in the copied array does not affect the original.

Remember: The phrase "shallow copy" describes copying an outer object without recursively copying nested objects. NumPy's ndarray.copy() copies the array's element data, but object arrays need special care.

2. What Is a Deep Copy?

A deep copy recursively copies nested objects where supported, producing an independent object structure. Python provides the copy.deepcopy() function for this purpose.

Deep copying is particularly useful when working with nested Python lists, dictionaries, or NumPy object arrays that contain mutable objects.

import copy

original = [
    [10, 20],
    [30, 40]
]

deep_copy = copy.deepcopy(original)

deep_copy[0][0] = 999

print("Original:", original)
print("Deep copy:", deep_copy)

Output

Original: [[10, 20], [30, 40]]
Deep copy: [[999, 20], [30, 40]]

The nested list inside deep_copy is independent from the corresponding nested list in original.

Tip: Use copy.deepcopy() when you need an independent nested Python object structure and shallow copying is not sufficient.

3. Shallow Copy vs Deep Copy

Feature Shallow Copy Deep Copy
Outer object Creates a new outer object. Creates a new outer object.
Nested objects May continue to be shared. Recursively copies nested objects where supported.
Memory usage Usually lower for nested structures. Can require more memory.
Processing time Usually faster for nested structures. Can take longer for complex structures.
Python function copy.copy() copy.deepcopy()
Numeric NumPy array arr.copy() copies its element data. Usually unnecessary for a plain numeric array.

4. Understanding Shallow Copies with Nested Lists

A Python list can contain other lists. A shallow copy duplicates the outer list but keeps references to the same inner lists.

import copy

original = [
    [1, 2],
    [3, 4]
]

shallow_copy = copy.copy(original)

shallow_copy[0][0] = 100

print("Original:", original)
print("Shallow copy:", shallow_copy)

Output

Original: [[100, 2], [3, 4]]
Shallow copy: [[100, 2], [3, 4]]

Both lists show the updated nested value because the inner list is shared.

Changing Only the Outer List

original = [[1, 2], [3, 4]]
shallow_copy = copy.copy(original)

shallow_copy.append([5, 6])

print("Original:", original)
print("Shallow copy:", shallow_copy)

Output

Original: [[1, 2], [3, 4]]
Shallow copy: [[1, 2], [3, 4], [5, 6]]

Appending a new element changes only the outer copied list. It does not append the new nested list to the original outer list.

5. Deep Copy with Nested Lists

A deep copy recursively copies the nested list structure. Modifying a nested list in the copied structure does not modify the corresponding nested list in the original.

import copy

original = [
    [10, 20],
    [30, 40]
]

deep_copy = copy.deepcopy(original)

deep_copy[1][0] = 777

print("Original:", original)
print("Deep copy:", deep_copy)

Output

Original: [[10, 20], [30, 40]]
Deep copy: [[10, 20], [777, 40]]

Only the copied nested list changes. The original retains its values.

6. Shallow Copy and Deep Copy with NumPy Object Arrays

NumPy arrays with dtype=object store references to Python objects. The array's element references and the objects they point to should be considered separately.

Using ndarray.copy()

import numpy as np

original = np.empty(2, dtype=object)
original[0] = [10, 20]
original[1] = [30, 40]

copied = original.copy()

copied[0][0] = 999

print("Original first item:", original[0])
print("Copied first item:", copied[0])

Output

Original first item: [999, 20]
Copied first item: [999, 20]

The array objects are separate, but their elements refer to the same nested lists. Modifying a nested list therefore affects what is seen through both arrays.

Using copy.deepcopy()

import copy
import numpy as np

original = np.empty(2, dtype=object)
original[0] = [10, 20]
original[1] = [30, 40]

deep_copy = copy.deepcopy(original)

deep_copy[0][0] = 999

print("Original first item:", original[0])
print("Deep copy first item:", deep_copy[0])

Output

Original first item: [10, 20]
Deep copy first item: [999, 20]

In this example, copy.deepcopy() recursively copies the nested lists referenced by the object array.

Important: Deep-copy behavior can depend on the objects stored in an object array. Some custom objects may define special copying behavior. Test the result when nested mutable objects matter.

7. Choosing the Right Copying Method

Requirement Recommended Approach
Independent numeric NumPy array arr.copy()
Share array data intentionally arr.view(), where appropriate
Copy a nested Python list without recursively copying its contents copy.copy()
Independently copy nested mutable Python objects copy.deepcopy()
Check whether NumPy arrays share element memory np.shares_memory(a, b)
Best practice: For ordinary numeric NumPy arrays, arr.copy() is generally the straightforward way to create independent element data. Reserve deep copying for nested Python objects when recursive independence is required.

8. Practical Example: Student Records

Imagine that CIIT TRANING INSTITUTE maintains student marks and wants to create a separate practice dataset for Samadhan, Pradnya, and Samiksha.

import copy

student_records = {
    "Samadhan": {"marks": [78, 82]},
    "Pradnya": {"marks": [85, 90]},
    "Samiksha": {"marks": [92, 95]}
}

# Create a fully independent nested structure
practice_records = copy.deepcopy(student_records)

practice_records["Samadhan"]["marks"][0] = 100

print("Original Samadhan marks:",
      student_records["Samadhan"]["marks"])

print("Practice Samadhan marks:",
      practice_records["Samadhan"]["marks"])

Output

Original Samadhan marks: [78, 82]
Practice Samadhan marks: [100, 82]

The original student records remain unchanged because the nested dictionary and list structures were deep-copied.

9. Common Mistakes

Mistake 1: Assuming a Shallow Copy Duplicates Nested Objects

A shallow copy creates a new outer container, but nested mutable objects can remain shared. Use copy.deepcopy() when recursive independence is required.

Mistake 2: Using Deep Copy for Every Numeric Array

A regular numeric NumPy array usually needs only arr.copy() to obtain independent element data. Deep copying can add unnecessary overhead.

Mistake 3: Confusing a View with a Copy

A view can share the original array's element data. Use np.shares_memory() when you need to check whether arrays share memory.

10. Practice Exercises

Exercise 1: Shallow Copy

Create a nested list, make a shallow copy using copy.copy(), and change an element inside one of its nested lists. Observe the original list.

Exercise 2: Deep Copy

Create a nested list and use copy.deepcopy(). Modify a nested element in the copy and confirm that the original stays unchanged.

Exercise 3: NumPy Numeric Array

Create a NumPy array of marks, make a copy using arr.copy(), and update the copied array.

Exercise 4: Object Array

Create a NumPy object array containing nested lists. Use arr.copy() and inspect what happens when a nested list is modified.

Exercise 5: Student Records

Create nested records for Samadhan, Pradnya, and Samiksha. Make a deep copy and modify one student's practice marks without changing the original records.

Summary

A shallow copy creates a new outer object but may continue sharing nested objects. A deep copy recursively copies nested objects where supported, making it useful for independent nested data structures.

For ordinary numeric NumPy arrays, array.copy() creates independent element data. For object arrays containing mutable Python objects, nested references may still be shared, so copy.deepcopy() may be appropriate.

Choose the copying method according to your data structure and requirements. Use np.shares_memory() to investigate whether NumPy arrays share element memory, and test nested objects carefully when independent changes are important.