Dip Lab 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Digital Image Processing

Introduction to Python for image processing

Lab -01

Student Name:________________________________

ID:__________________________________________

Date of Submission:____________________________

Objective

This introductory lab is intended to familiarized students with the basics of


Python, a high level computing language for arithmetic development, data
analysis and data visualization. The students will also be introduce to
manipulation if matrices and arrays.

Task 1: Solve the following problem

1. Given a Matrix A, write statement(S) to find the sum diagonal elements


of matrix A?

Import numpy as np

a=np.array([[1,2,3],[2,3,4],[5,6,7]])

Print (a)

Print(“\n”)

A=a[0,0]+a[1,1]+a[2,2]

M=[a[0,0],a[1,1],a[2,2]]

N=np.sum(m)

Print(N)

2. Using the colon operator, create a vector V with values [0,1,2,3,…..,100].


Import numpy as np

Arr=np.array(range(0,100))

A=Arr[:]

Print(A)

3. Extract the second column of the matrix and store in a vector.

Import numpy as np

a=np.array([[1,2,3],[2,3,4],[5,6,7]])

A=a[:,1]

Print(A)

4. Extract the 1st, 3rd, and 5th row of matrix into another matrix.

Import numpy as np

a=np.array([[1,2,3,4,5],[2,3,4,5,6],[5,6,7,9,10]])

A=np.array([a[:,0], a[:,2],a[:,4]])

Print(A)

5. Multiply each element of a matrix by 2 and store in matrix B.

Import numpy as np

a=np.array([[1,2,3,4,5],[2,3,4,5,6],[5,6,7,9,10]])

print(a,”\n”)

A=a*2

Print(A)

Task 2: Execute and analysis the python code


1. A1 = np.array([[1,2,3],[2,4,5],[6,7,8]])

Print(np.sum(A1, axis=0))

Print(np.sum(A1, axis=1))

Print(np.sum(A1))

Note: axis 0 represent row and axis 1 represent column.

2. A1 = np.array([[1,2,3],[2,4,5],[6,7,8]])

Ss=A1[range(0,2),:]

Print(np.transpose(ss))

Exercise1: Write a python program create 2X2 matrix A. Find the


determinant and inverse of this matrix. Multiply the matrix with its inverse
and display the result.

Import numpy as np

A=np.array([[1,2],[3,4]])

Print(“ Actual Matrix”)

Print(A)

Print(“ \nDeterminant of the matrix”)

Print(np.linalg.det(A))

Print(“\n Inverse of the matrix”)

Print(np.linalg.inv(A))

Print(“\n Element wise multiplication”)

Print(np.linalg.inv(A) * A)

Pirnt (“\n Multiplication”)


Print (A.dot(np.linalg.inv(A)))

Exercise 2:

Write a program that load iris dataset ( that comes with python(sklearn)):

From sklearn import datasets

Iris=datasets.load_iris()

The iris datasets comprises data of 3 different flower species of their iris
plant. The first four columns of the dataset represent sepal length , sepal
width, petal length and petal width respectively. The last column is a label
which tells the type of flowers(1,2, or 3). The first 50 rows define the type
1, the next 50 rows defines the type 2 and the last 50 rows define the type
5. Find the mean and standard deviation of the sepal length , sepal width,
petal length and petal width for each type of iris flowers.

Print(__docs__)

Import matplotlib.pyplot as plt

Import matplotlib.pyplot as plt1

Import numpy as n

From mpl toolkits.mplot3d import Axes3D

From sklearn import datasets

#import some data to play with

Iris= datasets.load_iris()

X=iris.data[:,:4] ## we only take the first 4 features

Y=iris.target
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
plt.gray()

plt.figure(figsize=(8,6))

plt.subplot(121), plt.scatter(x[:,0], x[:,1],c=y,


cmap=plt.cm.set1,edgecolor=’k’)

plt.xlabel(‘ Sepal length’)

plt.ylabel(‘sepal width’)

plt.xlim(x_min, x_max)

plt.ylim(y_min, y_max)

plt.xticks(())

plt.yticks(())
x_min, x_max = X[:, 2].min() - 0.5, X[:, 2].max() + 0.5
y_min, y_max = X[:, 3].min() - 0.5, X[:, 3].max() + 0.5
plt.gray()

plt.figure(figsize=(8,6))

plt.subplot(121), plt.scatter(x[:,2], x[:,2],c=y,


cmap=plt.cm.set1,edgecolor=’k’)

plt.xlabel(‘ Petal length’)

plt.ylabel(‘Petal width’)

plt.xlim(x_min, x_max)

plt.ylim(y_min, y_max)

plt.xticks(())

plt.yticks(())

plt.show()
The first 50 datasets for the type1 flowers

Dd=(type1)

Q0=np.mean(type1[:,0])

Q1= np.mean(type1[:,1])

Q2= np.mean(type1[:,2])

Q3= np.mean(type1[:,3])

M=np.mean([Q0,Q1,Q2,Q3])

Print(“Type 1 flower 1 :50”)

Print(“ mean of the sample of type1 %s”, %(np.means(type1, axis=0)))

Print(“ SD of the sample of type1 %s”,%(np.std(type1)))

Do the same code for type 2 and type 3.

You might also like