NumPy Creating Arrays
Concept
Arrays are the foundation of NumPy. They store homogeneous data efficiently.
Examples
import numpy as np
arr = [Link]([1, 2, 3])
zeros = [Link]((2, 3))
ones = [Link]((3, 2))
arange_arr = [Link](0, 10, 2)
linspace_arr = [Link](0, 1, 5)
random_arr = [Link](1, 100, (2, 3))
Student Tasks
1. Create a 1D array of numbers from 10 to 50 with a step of 5.
2. Create a 4×4 array filled with random integers between 1 and 10.
3. Create an array of 10 evenly spaced numbers between 0 and 5.
4. Create a 3×3 identity matrix.
NumPy Array Indexing
Concept
Indexing helps access individual elements or specific parts of an array.
Examples
arr = [Link]([10, 20, 30, 40])
print(arr[2]) # 30
mat = [Link]([[1,2,3],[4,5,6],[7,8,9]])
print(mat[1,2]) #6
print(mat[:,1]) # [2 5 8]
Student Tasks
1. Extract the element at 3rd row and 2nd column from a 3×3 array.
2. Access all elements in the second column of a 2D array.
3. Print the last element of a 1D array using negative indexing.
NumPy Array Slicing
Concept
Slicing extracts portions of arrays using the format [start:end:step].
Examples
arr = [Link]([10,20,30,40,50,60])
print(arr[1:4]) # [20 30 40]
print(arr[:3]) # [10 20 30]
print(arr[::2]) # [10 30 50]
Student Tasks
1. Slice the first three elements of an array.
2. Print every alternate element in an array.
3. Slice the middle row from a 3×3 matrix.
NumPy Data Types
Concept
Data types define the nature of elements stored (int, float, bool, etc.).
Examples
arr = [Link]([1, 2, 3], dtype='float32')
print([Link])
print([Link](int))
Student Tasks
1. Create an array of integers and convert it into floats.
2. Verify the data type of a boolean array.
3. Convert a float array to integer type.
NumPy Copy vs View
Concept
• Copy: Creates a new array, independent of the original.
• View: References the original array (changes reflect).
Examples
a = [Link]([1,2,3])
b = [Link]()
c = [Link]()
a[0] = 100
print(b) # [1 2 3]
print(c) # [100 2 3]
Student Tasks
1. Create a view of an array and change one element.
2. Create a copy of an array and verify independence.
NumPy Array Shape
Concept
Shape tells you the array’s dimensionality — (rows, columns).
Examples
arr = [Link]([[1,2,3],[4,5,6]])
print([Link])
print([Link])
print([Link])
Student Tasks
1. Find the number of elements in a 3×3 array.
2. Print the shape, size, and dimensions of any array.
NumPy Array Reshape
Concept
reshape() changes the shape of arrays without changing the data.
Examples
arr = [Link](6)
print([Link]((2, 3)))
print([Link]((3, 2)))
Student Tasks
1. Create a 1D array of 12 elements and reshape it into 3×4.
2. Reshape a 2D array into 1D.
3. Flatten a 3D array.
NumPy Array Iterating
Concept
Iterating lets you loop through each element or dimension.
Examples
arr = [Link]([[1,2,3],[4,5,6]])
for x in [Link](arr):
print(x)
Student Tasks
1. Iterate over each element in a 2×3 array and print it.
2. Print the square of every element using iteration.
NumPy Array Join
Concept
Joining merges arrays horizontally or vertically.
Examples
a = [Link]([[1, 2], [3, 4]])
b = [Link]([[5, 6], [7, 8]])
print([Link]((a, b))) # Horizontal join
print([Link]((a, b))) # Vertical join
Student Tasks
1. Join two 1D arrays [1,2,3] and [4,5,6].
2. Vertically join two 2×2 matrices.
3. Combine arrays using [Link].
NumPy Array Split
Concept
Splitting divides one array into multiple subarrays.
Examples
arr = [Link]([10,20,30,40,50,60])
print(np.array_split(arr, 3))
Student Tasks
1. Split a 1D array of 12 elements into 4 equal parts.
2. Horizontally split a 4×4 matrix into two parts.
NumPy Array Search
Concept
Find indices or positions that satisfy certain conditions.
Examples
arr = [Link]([10, 20, 30, 40, 50])
print([Link](arr == 30))
print([Link](arr > 25))
Student Tasks
1. Find indices of all even numbers in an array.
2. Locate where values are greater than 10.
3. Use nonzero() to find non-zero elements.
NumPy Array Sort
Concept
Sort elements in ascending order.
Examples
arr = [Link]([5, 3, 8, 1])
print([Link](arr))
Student Tasks
1. Sort a 1D array in ascending and descending order.
2. Sort each row in a 3×3 matrix.
3. Use argsort() to display sorted indices.
NumPy Array Filter
Concept
Filtering extracts elements based on conditions (Boolean masks).
Examples
arr = [Link]([10, 15, 20, 25])
filter_arr = arr > 15
print(arr[filter_arr])
Student Tasks
1. Extract all numbers divisible by 3 from an array.
2. Filter out values greater than 50 from a random array.
3. Apply multiple conditions (&, |) in filtering.
Final Challenge Tasks (Expert Level)
1. Create a 5×5 matrix with random integers and:
o Replace all even numbers with 0
o Replace all odd numbers with -1
2. Generate a 3×3 matrix and compute:
o Mean, median, variance, and standard deviation
3. Create two arrays of shape (2,3) and:
o Concatenate horizontally
o Split them vertically
4. Build a 1D array of 20 random integers, and:
o Filter values between 10 and 50
o Sort the filtered result in descending order