0% found this document useful (0 votes)
11 views7 pages

CE 712 Tutorial2 Part2

This tutorial introduces numerical computing with Python using the NumPy library, focusing on vector operations. It covers defining vectors, accessing elements, performing basic operations, and introduces matrices and their manipulations. The tutorial also provides practical examples and shortcuts for using Jupyter Notebook effectively.

Uploaded by

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

CE 712 Tutorial2 Part2

This tutorial introduces numerical computing with Python using the NumPy library, focusing on vector operations. It covers defining vectors, accessing elements, performing basic operations, and introduces matrices and their manipulations. The tutorial also provides practical examples and shortcuts for using Jupyter Notebook effectively.

Uploaded by

thelancehk6105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

07 August 2025

CE 712: Digital Image Processing for Remotely Sensed Data

Tutorial 2- Part 2

Vector Operations

This tutorial gives a basic introduction to numerical computing using Python. Here, we will be using
the NumPy library which is the most widely used fundamental Python library for numerical computing.
NumPy makes it easier for you to enter matrices and vectors and manipulate them.

The topics covered in this section are listed below:


1. Defining a Vector
2. Accessing elements within a vector
3. Basic operations on vectors
Throughout this tutorial, we will be using the Jupyter Notebook for writing and running Python codes.
Keep a note of the following shortcuts which are used for performing some of the frequently used
operations in Jupyter Notebook (shortcuts work only after selecting a cell):
➢ Ctrl + Enter → Runs current cell.
➢ Shift + Enter → Runs current cell and creates a new cell.

➢ Tap ‘a’ or ‘A’ → Insert a new cell above the selected cell.
➢ Tap ‘b’ or ‘B’ → Insert a new cell below the selected cell.

➢ Double tap ‘d’ or ‘D’ → Deletes current cell.


In the text that follows, any line that starts with two greater than signs (>>) is used to denote the python
code and the text following immediately (in green colour) is the output.

Defining a Vector
• To start using NumPy in your script, it should be first imported into your script:
>> import numpy as np
Here, the entire NumPy module is imported under an alternate name ‘np’. Almost all of Python’s basic
commands revolve around the use of multi-dimensional arrays.
• 1D NumPy arrays don’t follow the concept of a row vector or column vector representation. It can
be defined as:
>> v = [Link]([3, 1])
This creates a 1D NumPy array which has the label “v”. The first entry in the vector is 3 and the second
entry is 1. If you want to view the vector just type its label and run the cell:
>> v
array([3, 1])
Note that if you run the above line with a semicolon at the end (v;), then it will not print its value as
the statement is terminated.
• All NumPy arrays are set to the “[Link]” data type. This can be checked using the type()
function:
>> type(v)
[Link]
• For using a 1D NumPy array as a row or column vector which can be used in matrix operations like
transpose, multiplication, etc., then it has to be defined in a 2D system. This can be achieved with
the use of additional square brackets:
>> v
array([[3, 1]])
Here, v is defined as a row vector.
• You can define a row vector of any size in this manner:
>> p = [Link]([[3, 1, 7, -21, 5, 6]])
>> p
array([[ 3, 1, 7, -21, 5, 6]])
• The transpose of a vector p is defined by p.T or [Link](p).
>> p.T
array([[ 3],
[ 1],
[ 7],
[-21],
[ 5],
[ 6]])
• Similarly, a column vector can be created as follows:
>> [Link]([[3],[1]])
array([[3],[1]])
Note that if the created vector is not assigned to a variable, running the cell will automatically print the
result unless the statement is terminated by a semicolon (;).
• A common task is to create a large vector with numbers that fit a repetitive pattern. NumPy can
automatically generate a 1D array containing a set of numbers with a common increment using the
arange() function. For example, to define a vector whose first entry is 1, the second entry is 2, the
third is 3, and sequentially through 8, you enter the following:
>> [Link](1,9)
array([1, 2, 3, 4, 5, 6, 7, 8])
• If you wish to use an increment other than one, then you have to additionally define the value of the
increment. For example, to define a 1D array that starts with 2 and ends with a number less than 4
with steps of 0.25, you enter the following:
>> q = [Link](2,4,0.25)
>> q
array([2. , 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75])
• However, in order to implement matrix operations in such 1D array, it has to be converted to a 2D
system by entering the following:
>> u = [Link]([q])
>> u
array([[2. , 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75]])
• Negative increments can also be used to create a 1D array.
>> [Link](4, -5, -1)
array([ 4, 3, 2, 1, 0, -1, -2, -3, -4])
• To open row or column vector image and extract the values into an array.
>> from PIL import Image
>> row_img = [Link](r"** give your filepath\row_vector.png")
>> row_img #to display image
>> row_img_array=[Link](row_img) #to extract values into an array
>> row_img_array #to print array
------------------------------------------------------------------
QUESTION 1:
CAN YOU READ THE PROVIDED COLUMN VECTOR IMAGE AND COMPUTE ITS
TRANSPOSE?
------------------------------------------------------------------
• In NumPy, the elements are accessed based on their index position starting from zero.
For example, consider the following 1D array.
>> s = [Link](0,10,2)
>> s
array([0, 2, 4, 6, 8])
Index 0 1 2 3 4

• To view the first entry in this 1D array, just type in the following:
>> s[0]
0
For the easiness in reading elements from the end of an array, NumPy supports negative indexing.
For example, see the following cases:
>> s[-1]
8
>> s[-2]
6
• NumPy also allows you to look at specific parts of the array or vector. For example, consider
the following array:
To extract all the elements from index position 2 onwards:
s[2:]
array([4, 6, 8])
Using only a colon as index will extract all the elements.
s[:]
array([0, 2, 4, 6, 8])
If you want to only look at the 1st three entries (i.e., index = 0,1,2) in an array you can use the following
method:
>> s[:3]
array([0, 2, 4])
• For accessing the elements of a row or column vector (say, v), the general format is v[row, col].
For example, consider the following row vector.
>> v = [Link]([[3, 1]])
• The second element of v can be accessed by:
>> v[0,1]
1
Here, 0 and 1 means 0th row and 1st column, respectively.
------------------------------------------------------------------
QUESTION 2:
PRINT THE SECOND DIGITAL NUMBER OF THE ROW AND COLUMN VECTOR IMAGES
GIVEN TO YOU?
------------------------------------------------------------------

Basic operations on vectors


Once you master the notation you are free to perform other operations. Consider the following vector
v and u.
• Consider the addition of two row vectors u and v:
>> v = [Link]([[1, 2, 5, 9]])
>> u = [Link]([[0, 3, 4, 1]])
>> u+v
array([[ 1, 5, 9, 10]])
• Additionally, scalar multiplication is defined in the standard way. Also note that scalar division is
defined in a way that is consistent with scalar multiplication:
>> -2*u
array([[ 0, -6, -8, -2]])
>> v/3
array([[0.33333333, 0.66666667, 1.66666667, 3. ]])
The above steps can also be performed in a combined manner like a linear equation:
>> -2*u+v/3
array([[ 0.33333333, -5.33333333, -6.33333333, 1. ]])

Introduction to Matrices in Python


This section gives a basic introduction to defining matrices using NumPy.
Defining a matrix is similar to a series of row vectors separated by a comma.
>> A = [Link]([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
>> A
array([[1, 2, 3],
[3, 4, 5],
[6, 7, 8]])
• Elements of a matrix can be accessed similar to that of a row or column vector.
>> A[2,1]
7
• The transpose of a matrix can be taken in a similar way to that of vectors.
>> A.T
array([[1, 3, 6],
[2, 4, 7],
[3, 5, 8]])
• We can also use concatenate() function to build matrices. It concatenates a list of matrices or
vectors along a specified axis.
>> A = [Link]([[1, 2], [3, 4]])
>> B = [Link]([[5, 6]])
>> [Link]((A,B), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>> [Link]((A,B.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
• Matrices can also be built by reshaping a 1D array or a row or column vector. This is done using
the reshape() function.
>> A = [Link]([1,2,3,4])
>> [Link]((2,2))
array([[1, 2],
[3, 4]])
• The [Link]() function under NumPy creates a matrix of user-defined size containing random
numbers on (0,1). For example, to create a matrix of size 3×5:
>> [Link](3,5)
array([[0.4711654, 0.43820924, 0.27811151, 0.75529723, 0.0543609],
[0.27594576,0.00670618,0.25241212, 0.09938344, 0.22554914],
[0.21134293, 0.3018054,0.48821577, 0.95241624, 0.69080116]])
Matrix Functions and Operations
Once you are able to create and manipulate a matrix, you can perform many standard operations on it.
For example, you can find the determinant and inverse of a matrix using the linear algebra library under
NumPy (linalg).
>> b = [Link]([[2,1],[4,6]])
>> [Link](b)
7.999999999999998
• If you want to round all the elements in a matrix, then use the round().
>> round([Link](b), 5)
8.0
• To calculate the inverse of b:
>> [Link](b)
array([[ 0.75 , -0.125],
[-0.5 , 0.25 ]])
• Matrix multiplications can be easily done using NumPy. The only thing to be taken care of is that
the dimensions should satisfy the matrix multiplication rules.
>> v = [Link]([[1, 2, 1]])
>> [Link](v.T)
array([[6]])
>> A = [Link]([[1, 2, 3], [0, 1, 2], [4, 2, 0]])
>> [Link](A)
array([[5, 6, 7]])

Refer
For more detail on NumPy library, refer to its official documentation ([Link]

No submission required for this tutorial.

You might also like