25/08/2025, 10:58 Introduction to NumPy.
ipynb - Colab
Double-click (or enter) to edit
#Creating NumPy array
import numpy
arr=[Link]([1,2,3,4,5])
print(arr)
[1 2 3 4 5]
#Creating NumPy ndarray object
import numpy as np
arr=[Link]([1,2,3,4,5])
print(arr)
print(type(arr))
[1 2 3 4 5]
<class '[Link]'>
import numpy as np
# Creating a rank 1 Array
arr = [Link]([1, 2, 3])
print(arr)
# Creating a rank 2 Array
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
print(arr)
# Creating an array from tuple
arr = [Link]((1, 3, 2))
print(arr)
[1 2 3]
[[1 2 3]
[4 5 6]]
[1 3 2]
#Arithmetic Operations
import numpy as np
# Defining Array 1
a = [Link]([[1, 2],
[3, 4]])
# Defining Array 2
b = [Link]([[4, 3],
[2, 1]])
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
# sum of array elements
# Performing Unary operations
print ("\nSum of all array elements: ", [Link]())
# Adding two arrays
# Performing Binary operations
print ("\nArray sum:\n", a + b)
Adding 1 to every element: [[2 3]
[4 5]]
Subtracting 2 from each element: [[ 2 1]
[ 0 -1]]
Sum of all array elements: 10
Array sum:
[[5 5]
[5 5]]
# Python code to demonstrate matrix operations add()
import numpy
# initializing matrices
[Link] 1/4
25/08/2025, 10:58 Introduction to [Link] - Colab
x = [Link]([[1, 2], [4, 5]])
y = [Link]([[7, 8], [9, 10]])
# using add() to add matrices
print ("The element wise addition of matrix is : ")
print ([Link](x,y))
The element wise addition of matrix is :
[[ 8 10]
[13 15]]
# Python code to demonstrate matrix operations subtract()
# importing numpy for matrix operations
import numpy
# initializing matrices
x = [Link]([[1, 2], [4, 5]])
y = [Link]([[7, 8], [9, 10]])
# using subtract() to subtract matrices
print ("The element wise subtraction of matrix is : ")
print ([Link](x,y))
The element wise subtraction of matrix is :
[[-6 -6]
[-5 -5]]
# Python code to demonstrate matrix operations divide()
# importing numpy for matrix operations
import numpy
# initializing matrices
x = [Link]([[1, 2], [4, 5]])
y = [Link]([[7, 8], [9, 10]])
# using divide() to divide matrices
print ("The element wise division of matrix is : ")
print ([Link](x,y))
The element wise division of matrix is :
[[0.14285714 0.25 ]
[0.44444444 0.5 ]]
# Python code to demonstrate matrix operations
# multiply() and dot()
# importing numpy for matrix operations
import numpy
# initializing matrices
x = [Link]([[1, 2], [4, 5]])
y = [Link]([[7, 8], [9, 10]])
# using multiply() to multiply matrices element wise
print ("The element wise multiplication of matrix is : ")
print ([Link](x,y))
# using dot() to multiply matrices
print ("The product of matrices is : ")
print ([Link](x,y))
The element wise multiplication of matrix is :
[[ 7 16]
[36 50]]
The product of matrices is :
[[25 28]
[73 82]]
# Aggregate Functions
# NumPy provides statistical and mathematical functions
arr = [Link]([1, 2, 3, 4, 5])
print([Link](arr)) # 15 (sum of all elements)
print([Link](arr)) # 1 (minimum value)
print([Link](arr)) # 5 (maximum value)
print([Link](arr)) # 3.0 (average)
print([Link](arr)) # 1.414 (standard deviation)
print([Link](arr)) # 2.0 (variance)
[Link] 2/4
25/08/2025, 10:58 Introduction to [Link] - Colab
15
1
5
3.0
1.4142135623730951
2.0
#1D array
import numpy as np
arr = [Link]([10, 20, 30, 40, 50])
print(arr[0])
10
#Multidimentional Array
import numpy as np
matrix = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[1, 2])
#Slicing array
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
[2 3 4 5]
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
[5 6 7]
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
[1 2 3 4]
#Negative indexing
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
[5 6]
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr[[0, 2, 4]])
[1 3 5]
#iteration on 1-D Array
import numpy as np
arr = [Link]([1, 2, 3])
for x in arr:
print(x)
1
2
3
#Iterate on each scalar element of the 2-D array:
import numpy as np
arr = [Link]([[1, 2, 3], [4, 5, 6]])
for x in arr:
[Link] 3/4
25/08/2025, 10:58 Introduction to [Link] - Colab
for y in x:
print(y)
1
2
3
4
5
6
#Boolean values
import numpy as np
a = [Link]([1, 0, 1, 0, 0, 1, 0])
b = [Link](bool)
print(b)
[ True False True False False True False]
#Shape maniplation
#shape of a 2-D array
import numpy as np
arr = [Link]([[1, 2, 3, 4], [5, 6, 7, 8]])
print([Link])
(2, 4)
# Shape defines the dimensions of an array
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print([Link]) # (2, 3) → 2 rows, 3 columns
reshaped = [Link](3, 2)
print(reshaped)
(2, 3)
[[1 2]
[3 4]
[5 6]]
[Link] 4/4