0% found this document useful (0 votes)
4 views

PCC-CSM691 Experiment 1

The document provides a comprehensive guide on using Python's NumPy module to create and manipulate vectors and matrices. It covers the creation of horizontal and vertical vectors, basic operations such as addition, subtraction, multiplication, division, and dot product, as well as matrix creation, multiplication, transposition, and flattening. Each operation is illustrated with code examples and their corresponding outputs.

Uploaded by

subhooo224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PCC-CSM691 Experiment 1

The document provides a comprehensive guide on using Python's NumPy module to create and manipulate vectors and matrices. It covers the creation of horizontal and vertical vectors, basic operations such as addition, subtraction, multiplication, division, and dot product, as well as matrix creation, multiplication, transposition, and flattening. Each operation is illustrated with code examples and their corresponding outputs.

Uploaded by

subhooo224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 1

Aim: Understanding Vectors, Matrices, and solving problems in Python. Creation of a Vector in
Python
Python NumPy module is used to create a vector. We use numpy.array() method to create a one-
dimensional array i.e. a vector.
Syntax:
numpy.array(list)
Example 1: Horizontal Vector
import numpy as np
lst = [10,20,30,40,50]
vctr = np.array(lst)
vctr = np.array(lst)
print("Vector created from a list:")
print(vctr)
Output:
Vector created from a list:
[10 20 30 40 50]
Example 2: Vertical Vector
import numpy as np
lst = [[2],
[4],
[6],
[10]]
vctr = np.array(lst)
vctr = np.array(lst)
print("Vector created from a list:")
print(vctr)
Output:
Vector created from a list:
[[ 2]
[ 4]
[ 6]
[10]]
Basic Operations on a Python Vector
Having created a Vector, now let us perform some basic operations on these Vectors now!
Here is a list of the basic operations that can be performed on a Vector–
● Addition
● Subtraction
● Multiplication
● Division
● Dot Product, etc.
1. Performing addition operation on a Python Vector
import numpy as np
lst1 = [10,20,30,40,50]
lst2 = [1,2,3,4,5]
vctr1 = np.array(lst1)
vctr2= np.array(lst2)
print("Vector created from a list 1:")
print(vctr1)
print("Vector created from a list 2:")
print(vctr2)
vctr_add = vctr1+vctr2
print("Addition of two vectors: ",vctr_add)
Output:
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Addition of two vectors: [11 22 33 44 55]
2. Performing Subtraction of two vectors
import numpy as np
lst1 = [10,20,30,40,50]
lst2 = [1,2,3,4,5]
vctr1 = np.array(lst1)
vctr2= np.array(lst2)
print("Vector created from a list 1:")
print(vctr1)
print("Vector created from a list 2:")
print(vctr2)
vctr_sub = vctr1-vctr2
print("Subtraction of two vectors: ",vctr_sub)
Output:
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Subtraction of two vectors: [ 9 18 27 36 45]
3. Performing multiplication of two vectors
import numpy as np
lst1 = [10,20,30,40,50]
lst2 = [1,2,3,4,5]
vctr1 = np.array(lst1)
vctr2= np.array(lst2)
print("Vector created from a list 1:")
print(vctr1)
print("Vector created from a list 2:")
print(vctr2)
vctr_mul = vctr1*vctr2
print("Multiplication of two vectors: ",vctr_mul)
Output:
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Multiplication of two vectors: [ 10 40 90 160 250]
4. Performing Vector division operation
import numpy as np
lst1 = [10,20,30,40,50]
lst2 = [10,20,30,40,50]
vctr1 = np.array(lst1)
vctr2= np.array(lst2)
print("Vector created from a list 1:")
print(vctr1)
print("Vector created from a list 2:")
print(vctr2)
vctr_div = vctr1/vctr2
print("Division of two vectors: ",vctr_div)
Output:
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[10 20 30 40 50]
Multiplication of two vectors: [ 1 1 1 1 1 ]
5. Vector Dot Product
In a vector dot product, we perform the summation of the product of the two vectors in an element-
wise fashion.
Let us have a look at the below.
vector c = x . y = (x1 * y1 + x2 * y2)
Example:
import numpy as np
lst1 = [10,20,30,40,50]
lst2 = [1,1,1,1,1]
vctr1 = np.array(lst1)
vctr2= np.array(lst2)
print("Vector created from a list 1:")
print(vctr1)
print("Vector created from a list 2:")
print(vctr2)
vctr_dot = vctr1.dot(vctr2)
print("Dot product of two vectors: ",vctr_dot)
Output:
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 1 1 1 1]
Dot product of two vectors: 150
Matrix Operations in Python
Create Matrix in NumPy
In NumPy, we use the np.array() function to create a matrix. For example,
import numpy as np
# create a 2x2 matrix
matrix1 = np.array([[1, 3],
[5, 7]])

print("2x2 Matrix:\n",matrix1)
# create a 3x3 matrix
matrix2 = np.array([[2, 3, 5],
[7, 14, 21],
[1, 3, 5]])

print("\n3x3 Matrix:\n",matrix2)
Output
2x2 Matrix:
[[1 3]
[5 7]]
3x3 Matrix:
[[ 2 3 5]
[ 7 14 21]
[ 1 3 5]]
Perform Matrix Multiplication in NumPy
We use the np.dot() function to perform multiplication between two matrices.
Let's see an example.
import numpy as np
# create two matrices
matrix1 = np.array([[1, 3],
[5, 7]])
matrix2 = np.array([[2, 6],
[4, 8]])
# calculate the dot product of the two matrices
result = np.dot(matrix1, matrix2)
print("matrix1 x matrix2: \n",result)
Output
matrix1 x matrix2:
[[14 30]
[38 86]]
Transpose NumPy Matrix
The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns. For
2x2 matrix,
Matrix:
a11 a12
a21 a22
Transposed Matrix:
a11 a21
a12 a22
In NumPy, we can obtain the transpose of a matrix using the np.transpose() function. For example,
import numpy as np
# create a matrix
matrix1 = np.array([[1, 3],
[5, 7]])
# get transpose of matrix1
result = np.transpose(matrix1)
print(result)
Output
[[1 5]
[3 7]]
Flatten Matrix in NumPy
Flattening a matrix simply means converting a matrix into a 1D array.
To flatten a matrix into a 1-D array we use the array.flatten() function. Let's see an example.
import numpy as np
# create a 2x3 matrix
matrix1 = np.array([[1, 2, 3],
[4, 5, 7]])
result = matrix1.flatten()
print("Flattened 2x3 matrix:", result)
Output
Flattened 2x3 matrix: [1 2 3 4 5 7]

You might also like