Gen AI Developer Week 1 — Day 2

Sai Chinmay Tripurari
3 min readDec 28, 2024

--

NumPy is the foundation of scientific computing in Python, offering powerful tools for numerical data manipulation and high-performance multidimensional arrays. From efficient data processing to forming the backbone of advanced libraries, NumPy is essential for AI and machine learning applications. This article, as part of my Generative AI Developer Week, explores its key features and uses.

Let’s get started by installing NumPy…

# Install numpy by using the below command
pip install numpy

Importing NumPy

import numpy as np

Key Concepts — Creating Arrays

# 1D Numpy Array - Creation
arr = np.array([ 1,2,3,4,5 ])
print(arr)

# 2D Numpy Array - Creation
arr2 = np.array([[1,2,3], [4,5,6]])
print(arr2)

Array — Properties

# Array Properties
print("Shape:",arr2.shape) #(rows, columns)
print("Size:",arr2.size) #Total Elements
print("Data Type:",arr2.dtype) #Type of Elements

Array — Operations

Element-wise addition and multiplication

# Array Operations
ar1 = np.array([1, 2, 3])
ar2 = np.array([4, 5, 6])
print("Addition:", ar1 + ar2)
print("Multiplication", ar1 * ar2) #General Multiplication Note: Not Dot Product

Matrix multiplication

# Dot Matrix - Matrix multiplication
mat1 = np.array([[1,2], [3,4]])
mat2 = np.array([[5,6], [7,8]])
print(np.dot(mat1, mat2))

Transpose

print("Transpose:\n", mat1.T)

Arrays — Indexing and Slicing

# Indexing and Slicing the Arrays
ar = np.array([10, 20, 30, 40, 50])
print(ar[1]) # Access the second element
print(ar[1:4]) # Slice from index 1 to 3

Useful Functions — Create arrays with specific values

zeros = np.zeros((3, 3)) # Creates Null Matrix
ones = np.ones((2, 2)) # Creates Unit Matrix
random_vals = np.random.rand(3, 3) # Creates 3x3 Matrix with random values

print(zeros)
print(ones)
print(random_vals)

Useful Functions — Summing values

arr = np.array([10, 20, 30, 40, 50])
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(arr)) # Sum of all elements
print(np.sum(matrix, axis=0)) # Sum by columns
print(np.sum(matrix, axis=1)) # Sum by rows

Practice Task — 1

# TASK 1
# Create a 3x3 array with values ranging from 1 to 9.
# Create a 2D Matrix and Multiply by 2

mat = np.array([[1,2,3], [4,5,6], [7,8,9]]) * 2
print(mat)

Practice Task — 2

# Task 2
# Create two 1D arrays: [1, 2, 3] and [4, 5, 6].
# Perform element-wise addition and multiplication.

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
print("Sum: ", arr1 + arr2)
print("Multiply: ",arr1 * arr2)

Practice Task — 3

# Task 3
# Create two matrices: mat1 = [[1, 2], [3, 4]], mat2 = [[5, 6], [7, 8]]
# Dot Product Matrix

ar1 = np.array([[1,2], [3,4]])
ar2 = np.array([[5,6], [7,8]])
print(np.dot(ar1, ar2))

Practice Task — 4

# Task 4
# Take a 3x2 matrix and compute its transpose.
# Transpose Matrix

matT = np.array([[1,2], [3,4], [5,6]])
print(matT)
print("\n")
print(matT.T)

Practice Task — 5

# Task 5
# Create a matrix of Random Values 4x4
# Compute the sum of all elements, the sum of each row, and the sum of each column.

rmat = np.random.randint(1, 10, (4 , 4))
print(np.sum(rmat)) #Sum of all elements
print(np.sum(rmat, axis = 0)) #Column Sum
print(np.sum(rmat, axis = 1)) #Row Sum

--

--

Sai Chinmay Tripurari
Sai Chinmay Tripurari

Written by Sai Chinmay Tripurari

Software Developer | ReactJS & React Native Expert | AI & Cloud Enthusiast | Building intuitive apps, scalable APIs, and exploring AI-driven solutions.

No responses yet