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

NumPy Unique Values and Set Operations

NumPy provides functions for identifying unique values and performing mathematical set operations on arrays. These operations help remove duplicates, compare datasets, find common elements, and identify values that occur in only one dataset. In this lesson, you will learn how to use unique values, intersections, unions, differences, and membership checks for practical data analysis.

1. Introduction to Unique Values

A unique value is a value that appears only once in the resulting collection of distinct values. The np.unique() function returns the distinct values from an array in sorted order by default.

import numpy as np values = np.array([10, 20, 10, 30, 20, 40, 30, 50]) result = np.unique(values) print("Original array:", values) print("Unique values:", result)

Output:

Original array: [10 20 10 30 20 40 30 50] Unique values: [10 20 30 40 50]

Repeated values are included only once in the unique result. The original array is not modified.

Why Use Unique Values?

  • Remove duplicate values from a dataset.
  • Identify distinct categories or numerical values.
  • Count the number of different values.
  • Prepare data for grouping and further analysis.

2. Counting Unique Values

You can determine how many distinct values occur in an array by checking the size of the result returned by np.unique().

import numpy as np scores = np.array([75, 80, 75, 90, 85, 80, 95, 90]) unique_scores = np.unique(scores) print("Unique scores:", unique_scores) print("Number of unique scores:", unique_scores.size)

Output:

Unique scores: [75 80 85 90 95] Number of unique scores: 5

Count the Frequency of Each Unique Value

Set return_counts=True to obtain the frequency of each unique value.

import numpy as np values = np.array([10, 20, 10, 30, 20, 10, 40]) unique_values, counts = np.unique( values, return_counts=True ) print("Unique values:", unique_values) print("Counts:", counts)

Output:

Unique values: [10 20 30 40] Counts: [3 2 1 1]
Value Frequency
103
202
301
401

3. Finding Unique Values and Their Indices

NumPy can also return the indices of the first occurrences of unique values and the inverse mapping that reconstructs the original array from its unique values.

import numpy as np values = np.array([30, 10, 30, 20, 10]) unique_values, first_indices, inverse = np.unique( values, return_index=True, return_inverse=True ) print("Unique values:", unique_values) print("First occurrence indices:", first_indices) print("Inverse mapping:", inverse) print("Reconstructed array:", unique_values[inverse])

Output:

Unique values: [10 20 30] First occurrence indices: [1 3 0] Inverse mapping: [2 0 2 1 0] Reconstructed array: [30 10 30 20 10]
Remember: The unique values are sorted by default, but return_index=True reports the original index of the first occurrence of each returned value.

4. Unique Values in Two-Dimensional Arrays

When an array contains multiple dimensions, np.unique() can return unique elements across the entire array or unique rows when axis=0 is specified.

Unique Elements Across the Entire Array

import numpy as np data = np.array([ [10, 20, 10], [30, 20, 40] ]) print(np.unique(data))

Output:

[10 20 30 40]

Unique Rows

import numpy as np data = np.array([ [10, 20], [30, 40], [10, 20], [50, 60] ]) print(np.unique(data, axis=0))

Output:

[[10 20] [30 40] [50 60]]

With axis=0, NumPy compares complete rows and removes duplicate rows. The remaining rows are returned in sorted order.

5. Introduction to Set Operations

Set operations compare two collections of values. They are useful when you need to find common values, combine distinct values, or identify values that exist in one array but not another.

Set Operations Union All distinct values from both arrays A ∪ B Intersection Values common to both arrays A ∩ B Difference Values in A but not in B A − B

Set operations compare the distinct values present in two arrays.

NumPy provides dedicated functions for these operations. Their results are generally sorted and contain distinct values.

6. Union of Two Arrays

The np.union1d() function returns the unique values that occur in either of two arrays.

import numpy as np a = np.array([10, 20, 30, 40]) b = np.array([30, 40, 50, 60]) result = np.union1d(a, b) print("Union:", result)

Output:

Union: [10 20 30 40 50 60]

Values that occur in both arrays appear only once in the union.

Example Use Cases

  • Combining distinct product IDs from two lists.
  • Creating a combined list of categories.
  • Finding every distinct value across two datasets.

7. Intersection of Two Arrays

The np.intersect1d() function returns values that are present in both arrays. Duplicate values are removed from the result.

import numpy as np a = np.array([10, 20, 30, 40, 50]) b = np.array([30, 40, 50, 60, 70]) common_values = np.intersect1d(a, b) print("Common values:", common_values)

Output:

Common values: [30 40 50]

Return the Indices of Common Values

import numpy as np a = np.array([10, 20, 30, 40]) b = np.array([30, 40, 50]) common, index_a, index_b = np.intersect1d( a, b, return_indices=True ) print("Common values:", common) print("Indices in A:", index_a) print("Indices in B:", index_b)

Output:

Common values: [30 40] Indices in A: [2 3] Indices in B: [0 1]

The returned index arrays identify the first matching occurrences of the common values in each input array.

8. Difference Between Two Arrays

The np.setdiff1d() function returns the unique values that occur in the first array but not in the second array.

import numpy as np a = np.array([10, 20, 30, 40, 50]) b = np.array([30, 50, 60]) result = np.setdiff1d(a, b) print("A minus B:", result) print("B minus A:", np.setdiff1d(b, a))

Output:

A minus B: [10 20 40] B minus A: [60]
Important: Set difference is directional. np.setdiff1d(a, b) is not necessarily equal to np.setdiff1d(b, a).

9. Symmetric Difference

Symmetric difference returns values that occur in either array but not in both. NumPy provides np.setxor1d() for this operation.

import numpy as np a = np.array([10, 20, 30, 40]) b = np.array([30, 40, 50, 60]) result = np.setxor1d(a, b) print("Symmetric difference:", result)

Output:

Symmetric difference: [10 20 50 60]
Operation Function Result for A = [1, 2, 3], B = [2, 3, 4]
Union np.union1d() [1, 2, 3, 4]
Intersection np.intersect1d() [2, 3]
Difference A − B np.setdiff1d() [1]
Symmetric difference np.setxor1d() [1, 4]

10. Checking Membership with np.isin()

The np.isin() function checks whether each element of one array appears in a collection of test values. It returns a Boolean array with the same shape as the input array.

import numpy as np values = np.array([10, 20, 30, 40, 50, 60]) targets = np.array([20, 40, 60]) mask = np.isin(values, targets) print("Membership mask:", mask) print("Matching values:", values[mask])

Output:

Membership mask: [False True False True False True] Matching values: [20 40 60]

Find Values Not in a Given Collection

import numpy as np values = np.array([10, 20, 30, 40, 50]) excluded = np.array([20, 40]) mask = ~np.isin(values, excluded) print(values[mask])

Output:

[10 30 50]
Tip: Use np.isin() to create a membership mask, then use that mask to select matching or non-matching elements.

11. Practical Project: Comparing Student Enrollments

Imagine two courses with different student enrollment lists. We can use NumPy set operations to find all enrolled students, students attending both courses, and students enrolled in only one course.

import numpy as np course_a = np.array([101, 102, 103, 105, 107]) course_b = np.array([103, 104, 105, 108]) all_students = np.union1d(course_a, course_b) both_courses = np.intersect1d(course_a, course_b) only_a = np.setdiff1d(course_a, course_b) only_b = np.setdiff1d(course_b, course_a) one_course_only = np.setxor1d(course_a, course_b) print("All enrolled:", all_students) print("Enrolled in both:", both_courses) print("Only in Course A:", only_a) print("Only in Course B:", only_b) print("Enrolled in exactly one:", one_course_only)

Output:

All enrolled: [101 102 103 104 105 107 108] Enrolled in both: [103 105] Only in Course A: [101 102 107] Only in Course B: [104 108] Enrolled in exactly one: [101 102 104 107 108]

Project Learning Outcomes

  • Combine distinct values using union.
  • Identify shared values using intersection.
  • Find values exclusive to each dataset using set difference.
  • Find values appearing in exactly one collection using symmetric difference.

12. Common Mistakes and Practice Exercises

Common Mistakes

  • Expecting unique values in original order: np.unique() sorts its output by default.
  • Expecting duplicates in set operations: set operation results contain distinct values.
  • Confusing set difference direction: A minus B is different from B minus A.
  • Using membership checks incorrectly: np.isin(a, b) checks each element of a against the values in b.

Exercise 1: Unique Values

Create an array containing repeated values. Use np.unique() to find the distinct values.

Exercise 2: Frequency Counting

Use return_counts=True to count how many times each unique value appears.

Exercise 3: Union and Intersection

Given two arrays, find all distinct values and the values shared by both arrays.

Exercise 4: Difference

Use np.setdiff1d() to find values that occur in the first array but not in the second.

Exercise 5: Symmetric Difference

Use np.setxor1d() to find values present in exactly one of two arrays.

Exercise 6: Membership

Use np.isin() to select values that match a specified list of target values.

Summary

In this lesson, you learned to identify distinct array values with np.unique(), count their frequencies, retrieve first occurrence indices, and identify unique rows in multidimensional arrays.

You also practiced set operations using np.union1d(), np.intersect1d(), np.setdiff1d(), and np.setxor1d(). Finally, you learned to check membership with np.isin() and apply these operations to compare datasets.