3/31/25, 11:56 AM numpy.
ipynb - Colab
1) Import Numpy
import numpy as np
2) Creating Numpy Arrays
# 1D array
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
[1 2 3 4 5]
# 2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
[[1 2 3]
[4 5 6]]
# 3D array
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3)
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
3) Array Initialization
# Array of zeros
zeros_array = np.zeros((3, 3))
print(zeros_array)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
# Array of ones
ones_array = np.ones((2, 4))
print(ones_array)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
https://colab.research.google.com/drive/1eMlXb5p4LNiBLpLgNycHN9Hg-93QqM-R#scrollTo=8HdXIZGs4GZj 1/5
3/31/25, 11:56 AM numpy.ipynb - Colab
# Array of ones
ones_array = np.ones((2, 4))
print(ones_array)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
4) Generate Sequences
# Evenly spaced numbers (from 0 to 10 with step 2)
seq_array = np.arange(0, 10, 2)
print(seq_array)
[0 2 4 6 8]
# Linearly spaced numbers (10 numbers between 1 and 5)
lin_space = np.linspace(1, 5, 10)
print(lin_space)
[1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222
3.66666667 4.11111111 4.55555556 5. ]
5) Random Numbers
# Random integers (low, high, size)
rand_ints = np.random.randint(1, 10, (3, 3))
print(rand_ints)
[[1 8 6]
[3 7 1]
[8 4 7]]
# Random numbers from normal distribution (mean=0, std=1)
rand_norm = np.random.randn(3, 3)
print(rand_norm)
[[-1.04383977 0.21497311 0.53101952]
[ 1.32831324 0.01348164 -0.94785281]
[-0.2488779 1.3122035 0.84281779]]
6) Array Properties
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (rows, columns)
print(arr.size) # Total elements
print(arr.ndim) # Number of dimensions
print(arr.dtype) # Data type of elements
(2, 3)
6
https://colab.research.google.com/drive/1eMlXb5p4LNiBLpLgNycHN9Hg-93QqM-R#scrollTo=8HdXIZGs4GZj 2/5
3/31/25, 11:56 AM numpy.ipynb - Colab
2
int64
7) Reshape & Flatten
# Reshape (2x3 to 3x2)
reshaped_arr = arr.reshape(3, 2)
print(reshaped_arr)
[[1 2]
[3 4]
[5 6]]
# Flatten (convert multi-dimensional array to 1D)
flattened_arr = arr.flatten()
print(flattened_arr)
[1 2 3 4 5 6]
8) Mathematical Operations
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Element-wise addition
print(a - b) # Element-wise subtraction
print(a * b) # Element-wise multiplication
print(a / b) # Element-wise division
print(a ** 2) # Element-wise power
[5 7 9]
[-3 -3 -3]
[ 4 10 18]
[0.25 0.4 0.5 ]
[1 4 9]
mat1 = np.array([[1, 2], [3, 4]])
mat2 = np.array([[5, 6], [7, 8]])
dot_product = np.dot(mat1, mat2)
print(dot_product)
[[19 22]
[43 50]]
9) Aggregate Functions
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr)) # Sum of elements
print(np.mean(arr)) # Mean
print(np.median(arr)) # Median
https://colab.research.google.com/drive/1eMlXb5p4LNiBLpLgNycHN9Hg-93QqM-R#scrollTo=8HdXIZGs4GZj 3/5
3/31/25, 11:56 AM numpy.ipynb - Colab
print(np.std(arr)) # Standard deviation
print(np.var(arr)) # Variance
print(np.min(arr)) # Minimum value
print(np.max(arr)) # Maximum value
15
3.0
3.0
1.4142135623730951
2.0
1
5
10) Indexing & Slicing
arr = np.array([10, 20, 30, 40, 50])
print(arr[1]) # Access single element (index 1)
print(arr[1:4]) # Slice elements from index 1 to 3
print(arr[:3]) # First 3 elements
print(arr[-2:]) # Last 2 elements
20
[20 30 40]
[10 20 30]
[40 50]
11) Filtering Data
arr = np.array([10, 20, 30, 40, 50])
# Condition-based filtering
filtered_arr = arr[arr > 20]
print(filtered_arr)
[30 40 50]
12) Stacking Arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Vertical stack
v_stack = np.vstack((a, b))
print(v_stack)
# Horizontal stack
h_stack = np.hstack((a, b))
print(h_stack)
[[1 2]
[3 4]
[5 6]
[7 8]]
[[1 2 5 6]
[3 4 7 8]]
https://colab.research.google.com/drive/1eMlXb5p4LNiBLpLgNycHN9Hg-93QqM-R#scrollTo=8HdXIZGs4GZj 4/5
3/31/25, 11:56 AM numpy.ipynb - Colab
13) Sorting Arrays
arr = np.array([3, 1, 4, 1, 5, 9])
sorted_arr = np.sort(arr)
print(sorted_arr)
[1 1 3 4 5 9]
14) Splitting Arrays
arr = np.array([1, 2, 3, 4, 5, 6])
# Split into 3 parts
split_arr = np.array_split(arr, 3)
print(split_arr)
[array([1, 2]), array([3, 4]), array([5, 6])]
https://colab.research.google.com/drive/1eMlXb5p4LNiBLpLgNycHN9Hg-93QqM-R#scrollTo=8HdXIZGs4GZj 5/5