import numpy as np
arr = np.array([1,2,3,4,5])
print(arr)
[1 2 3 4 5]
#2-D Arrays
arr2 = np.array([
[11,22,33],
[7,8,9]
])
print(arr2)
print(arr2.ndim) # ndim is used to get the dimension of the array
[[11 22 33]
[ 7 8 9]]
2
# numpy can handle multidimensional arrays (n-dimensions)
#3-D Arrays
arr3 = np.array([
[
[111,112,113],
[211,212,213]
],
[
[222,223,224],
[313,312,311]
]
])
print(arr3)
print(arr3.ndim)
[[[111 112 113]
[211 212 213]]
[[222 223 224]
[313 312 311]]]
3
print(type(arr3))
NumPy Array Shape
print(arr.shape)
print(arr2.shape)
print(arr3.shape)
(5,)
(2, 3)
(2, 2, 3)
NumPy Array Reshape
arr = np.array([1,2,3,4,5,6])
print(arr)
# Reshaping the array to 2x3 matrix
# array must be evenly distributable
reshaped_arr = arr.reshape(2,3)
print(reshaped_arr)
[1 2 3 4 5 6]
[[1 2 3]
[4 5 6]]
NumPy Array Indexing
arr1 = np.array([1,2,3,4,5])
print(arr[0])
print(arr[1]+arr[3])
1
6
#Access 2D arrays
arr2 = np.array([
[1,2,3],
[4,5,6]
])
print(arr2[0,1])
#Access 3D arrays
arr3 = np.array([
[
[0,1,2],
[10,11,12]
],
[
[100,101,102],
[110,111,112]
]
])
print(arr3[0,1,2])
print(arr3[1,0,1])
12
101
# Negative Indexing
arr = np.array([
[1,2,3,4,5],
[6,7,8,9,10]
])
print(arr[0,-2])
print(arr[1,-3])
4
8
NumPy Array Iterating
# For Loop
# Iterate through a 2D array using a nested loop
for row in arr:
for item in row:
print(item)
1
2
3
4
5
6
7
8
9
10
#3D iteration
for x in arr3:
for y in x:
for z in y:
print(z)
0
1
2
10
11
12
100
101
102
110
111
112
NumPy Array Slicing
[:end] : 0 to end
[start:end] : start to end
[start:] : start to last index
[::step] : start to end increment of step
[start:end:step] : start to end follows given steps
#Creating a NumPy array
arr = np.array([0,1,2,3,4,5,6,7,8,9])
#Slicing from beginning to index 6
print(arr[:6])
#Slicing from index 2 to index 5
print(arr[2:5])
#Slicing form index 3 to end
print(arr[3:])
#Slicing with a step of 2
print(arr[::2])
#Slicing with start:end:step
sliced_arr = arr[2:7:2]
print(sliced_arr)
[0 1 2 3 4 5]
[2 3 4]
[3 4 5 6 7 8 9]
[0 2 4 6 8]
[2 4 6]
# Creating a 2D Numpy Array and Apply Slicing
arr_2d = np.array([
[1,2,3,10],
[4,5,6,11],
[7,8,9,12]
])
# array_name[(start:end:step for row) , (start:end:step for col)]
#Slicing rows
row = arr_2d[0]
print(row)
#Slicing col
print(arr_2d[:,2])
#Slicing a sub-matrix
# I want 1st 2 rows and col 2 and 3
matrix = arr_2d[:2, 1:3]
print(matrix)
[ 1 2 3 10]
[3 6 9]
[[2 3]
[5 6]]
Numpy DataTypes
a = np.array([1,2,3])
print(a.dtype)
b = np.array([1.2,9.4])
print(b.dtype)
int32
float64
# Specifying data types in Numpy arrays
arr_int32 = np.array([1,2,3], dtype=np.int32)
arr_float32 = np.array([1.2,3.9, 2.7], dtype=np.float32)
print(arr_int32, arr_int32.dtype)
print(arr_float32, arr_float32.dtype)
[1 2 3] int32
[1.2 3.9 2.7] float32
#Convertion between data types
# Converting int32 array to float64
arr_float64 = arr_int32.astype(np.float64)
print(arr_float64.dtype)
float64
Joining Numpy Arrays
# Concatenation using np.concatenate
arr1 = np.array([1,2,3])
arr2 = np.array([4,5])
result = np.concatenate((arr1,arr2))
result
array([1, 2, 3, 4, 5])
Splitting Numpy Arrays
# Creating data using np.arange
arr = np.arange(1,10)
arr
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Spliting the array into 3 equal parts
split_arr = np.split(arr,3)
split_arr
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
# Unequal splitting with np.array_split
unequal_split = np.array_split(arr,4)
unequal_split
[array([1, 2, 3]), array([4, 5]), array([6, 7]), array([8, 9])]
unequal_split = np.array_split(arr,6)
unequal_split
[array([1, 2]),
array([3, 4]),
array([5, 6]),
array([7]),
array([8]),
array([9])]
Numpy Searching Arrays
#np.where
# Finding indices where the value is greater than 3
index = np.where(arr>3)
index
(array([3, 4, 5, 6, 7, 8], dtype=int64),)
for value in index:
print(arr[value])
[4 5 6 7 8 9]
Array Masking
# Create a boolean mask
mask = arr>5
filtered_values = arr[mask]
filtered_values
array([6, 7, 8, 9])
mask
array([False, False, False, False, False, True, True, True, True])
Numpy Sorting Arrays
arr = np.array([3,4,1,2,5])
sorted_arr = np.sort(arr)
ulta_sort = np.sort(arr)[::-1] #sorting then reversing
print(sorted_arr)
print(ulta_sort)
[1 2 3 4 5]
[5 4 3 2 1]
# Sorting 2D arrays
arr_2d = np.array([
[3,1,6],
[2,5,4]
])
# Sort along rows (axis = 1 by default)
sorted_rows = np.sort(arr_2d)
print(sorted_rows)
# Sort along col (axis = 0)
sorted_cols = np.sort(arr_2d, axis=0)
print(sorted_cols)
[[1 3 6]
[2 4 5]]
[[2 1 4]
[3 5 6]]
Array Operations
arr1 = np.array([1,2,3])
arr2 = np.array([1,2,3])
add = arr1+arr2
add
array([2, 4, 6])
mul = arr1*arr2
mul
array([1, 4, 9])
sqrt = np.sqrt(arr1)
sqrt
array([1. , 1.41421356, 1.73205081])
# Adding a value to 2D array
arr = np.array([
[1,2,3],
[4,5,6]
])
scalar = 10
result = arr+scalar
result
array([[11, 12, 13],
[14, 15, 16]])
# Adding different values to each value in the row
row_to_add = np.array([10,20,30]) #list size must be equal to row size
res = arr+row_to_add
res
array([[11, 22, 33],
[14, 25, 36]])
Descriptive Statistics
Mean, Median, Variance and Standard Deviation
data = np.array([10,20,30,40,50])
mean = np.mean(data)
median = np.median(data)
variance = np.var(data)
std_dev = np.std(data)
print(mean)
print(median)
print(variance)
print(std_dev)
30.0
30.0
200.0
14.142135623730951
Handling Missing Values
data = np.array([1,2,np.nan,np.nan,33,7,np.nan])
# Checking for NAN values
nan_count = np.sum(np.isnan(data))
nan_count
s= np.isnan(data)
s
array([False, False, True, True, False, False, True])
# Removing Nan values
data_cleaned = data[~np.isnan(data)]
data_cleaned
array([ 1., 2., 33., 7.])
# Handling missing values
#Calculate mean values excluding all nan
mean = np.nanmean(data)
# replacing nan values with mean value
data_filled = np.where(np.isnan(data), mean, data)
data_filled
array([ 1. , 2. , 10.75, 10.75, 33. , 7. , 10.75])