Professional Documents
Culture Documents
Lab 2 DIP
Lab 2 DIP
NAME::Irfan Haider
CLASS: BCE-7
ENROLLMENT NO: 01-132182-035
DEPARTMENT OF COMPUTER ENGINEERING
BAHRIA UNIVERSITY | ISLAMABAD CAMPUS
Lab Journal - Lab 2
Enrollment #: 01_132182_035______________________________
Objective
This introductory lab is intended to familiarize students with the basics of Python, a high level
computing language for algorithmic development, data visualization and data analysis. The
students will also be introduced to manipulation of matrices and arrays in Python.
Task 1:
1 Given a matrix A, write statement(s) to find the sum of the diagonal elements of A.
2 Using the colon operator, create a vector V with values [0,1,2,…, 100].
3 Extract the second column of a matrix and store it in a vector.
4 Extract the 1st, 3rd and 5th row of matrix into another matrix.
2 A1=np.array([[2,4,6],[8,10,8],[2,10,4]])
ss=A1[range(0,2),:] print(np.transpose(ss))
3 A=np.array([[1,2],[3,4]])
B=np.array([[5,6],[7,8]])
A*B
A.dot(B)
Task 1
Write a Python program which create 2x2 matrix A. Find the determinant and inverse of this matrix.
Multiply the matrix with its inverse and display the result.
Code
import numpy as np
Task 2
Write a program that loads iris data set (that comes with Python(sklearn)): from sklearn import
datasets iris = datasets.load_iris() The iris data set comprises data of 3 different flower species of the
Iris plant. The first four columns of the data set represent sepal length, sepal width, petal length and
petal width respectively. The last column is a label which tells the type of flower (1,2 or 3). The first
50 rows correspond to type 1, the next 50 rows to type 2 and the last 50 rows to type 3. Find the
mean and standard deviation of the sepal length, sepal width, petal length and petal width for each
category of iris
Code
import pandas as pd
ds = pd.read_csv(r'E:\ mutlub\bin\iris_csv.csv')
ds.head()
print(ds[ds['class'] == 'Irissetosa']['sepallength'].mean())
print(ds[ds['class'] == 'Irissetosa']['sepalwidth'].std())
print(ds[ds['class'] == 'Irissetosa']['petallength'].mean())
print(ds[ds['class'] == 'Irissetosa']['petalwidth'].std())
print(ds[ds['class'] == 'Irisversicolor']['sepallength'].mean())
print(ds[ds['class'] == 'Irisversicolor']['sepalwidth'].std())
print(ds[ds['class'] == 'Irisversicolor']['petallength'].mean())
print(ds[ds['class'] == 'Irisversicolor']['petalwidth'].std())
print(ds[ds['class'] == 'Irisvirginica']['sepallength'].mean())
print(ds[ds['class'] == 'Irisvirginica']['sepallength'].std())
print(ds[ds['class'] == 'Irisvirginica']['petallength'].mean())
print(ds[ds['class'] == 'Irisvirginica']['petalwidth'].std())
Conclusion
In this lab I have learnt how to import the dataset and find their mean