ML Lab
ML Lab
ML Lab
Learning
Lab
Record
Done By:
Name:Nuzhath Tahseen
Roll No:21135A4202
WEEK-1
AIM:
1. Write a Program to perform the following operations on matrices
a) Matrix addition
b) Matrix Subtraction
c) Matrix Multiplication
d) Matrix Inversion
e) Transpose of a Matrix
DESCRIPTION:
Matrix Addition:
It is the operation of adding two matrices by adding the corresponding
entries together.
Matrix Subtraction:
Subtraction of two matrices with the same order is done, by subtracting
the elements of the same positions in the matrices. If matrix A = aij and
matrix B = bij, where i and j can be any value, then, A – B = aij – bij.
Matrix Multiplication:
Multiplication of an integer with a matrix is simply a scalar multiplication.
We may define multiplication of a matrix by a scalar mathematically as:
If A = [aij]m × n is a matrix and k is a scalar, then kA is another matrix
obtained by multiplying each element of A by the scalar k.
Matrix Inversion :
If A is a non-singular square matrix, then there exists an inverse matrix
A-1, which satisfies the following condition:
AA-1 = A-1A = I, where I is the Identity matrix.
Transpose of a Matrix:
In Linear algebra, the transpose of a matrix is one of the most commonly
used methods in matrix transformation.
For a given matrix, the transpose of a matrix is obtained by
interchanging rows into columns or columns to rows
PROGRAM:
import numpy as np
row1=int(input("Enter the row value of 1st Matrix : ",))
col1=int(input("Enter the col value of 1st Matrix : ",))
row2=int(input("Enter the row value of 2nd Matrix : ",))
col2=int(input("Enter the col value of 2nd Matrix : ",))
a=np.zeros([row1,col1])
b=np.zeros([row2,col2])
for i in range(row1):
for j in range(col1):
a[i][j]=int(input(f'Enter value for matrix 1 index[{i},{j}] : '))
for i in range(row2):
for j in range(col2):
b[i][j]=int(input(f'Enter value for matrix 2 index[{i},{j}] : '))
if(row1==row2 and col1==col2):
print("Matrix Addition")
print(a+b)
print("Matrix Subtraction")
print(a-b)
else: print("Addition not possible.")
print("Subtraction not possible.")
if(col1==row2): print("Matrix Multiplication")
print(np.matmul(a,b))
else: print("Multiplication not possible ")
if(row1==col1): print("Inverse of a")
print(np.linalg.inv(a))
else: print("Inverse of matrix a is not possible ")
if(row1==col1): print("Inverse of b")
print(np.linalg.inv(b))
else: print("Inverse of matrix b is not possible ")
print("Transpose of matrix 1 ")
print(a.transpose())
print("Transpose of matrix 2 ")
print(b.transpose())
print(type(a))
OUTPUT:
Enter the row value of 1st Matrix : 3
Enter the col value of 1st Matrix : 3
Enter the row value of 2nd Matrix : 3
Enter the col value of 2nd Matrix : 3
Enter value for matrix 1 index[0,0] : 1
Enter value for matrix 1 index[0,1] : 0
Enter value for matrix 1 index[0,2] : 4
Enter value for matrix 1 index[1,0] : 3
Enter value for matrix 1 index[1,1] : 8
Enter value for matrix 1 index[1,2] : 2
Enter value for matrix 1 index[2,0] : -2
Enter value for matrix 1 index[2,1] : -3
Enter value for matrix 1 index[2,2] : 0
Enter value for matrix 2 index[0,0] : 2
Enter value for matrix 2 index[0,1] : 4
Enter value for matrix 2 index[0,2] : 4
Enter value for matrix 2 index[1,0] : -3
Enter value for matrix 2 index[1,1] : 2
Enter value for matrix 2 index[1,2] : 3
Enter value for matrix 2 index[2,0] : -5
Enter value for matrix 2 index[2,1] : 0
Enter value for matrix 2 index[2,2] : 5
Matrix Addition : [[ 3. 4. 8.]
[ 0. 10. 5.]
[-7. -3. 5.]]
Matrix Subtraction: [[-1. -4. 0.]
[ 6. 6. -1.]
[ 3. -3. -5.]]
Matrix Multiplication :[[-18. 4. 24.]
[-28. 28. 46.]
[ 5. -14. -17.]]
Inverse of a :[[ 0.17647059 -0.35294118 -0.94117647]
[-0.11764706 0.23529412 0.29411765]
[ 0.20588235 0.08823529 0.23529412]]
Inverse of b: [[ 0.16666667 -0.33333333 0.06666667]
[ 0. 0.5 -0.3 ]
[ 0.16666667 -0.33333333 0.26666667]]
Transpose of matrix 1: [[ 1. 3. -2.]
[ 0. 8. -3.]
[ 4. 2. 0.]]
Transpose of matrix 2 :[[ 2. -3. -5.]
[ 4. 2. 0.]
[ 4. 3. 5.]]
WEEK-2
AIM:
Write a Program to perform the following operations
a) Find the minimum and maximum element of the matrix
b) Find the minimum and maximum element of each row in the
matrix
c) Find the minimum and maximum element of each column in the
matrix
d) Find trace of the given matrix
e) Find rank of the given matrix
f) Find eigenvalues and eigenvectors of the given matrix
DESCRIPTION:
MAX:
The max() function returns the item with the highest value, or the
item with the highest value in an iterable.If the values are strings, an
alphabetically comparison is done.
MIN:
The min() function returns the item with the lowest value, or the
item with the lowest value in an iterable.If the values are strings, an
alphabetically comparison is done.
RANK:
The rank of an array is the number of dimensions it has.
Syntax: numpy.linalg.matrix_rank
TRACE:
Return the sum along diagonals of the array. If a is 2-D, the sum
along its diagonal with the given offset is returned, i.e., the sum of
elements a[i,i+offset] for all i.
Eigenvalues and Eigenvectors :
Parameter: An square array.Return: It will return two values first is
eigenvalues and second is eigenvectors.
Syntax: numpy.linalg.eig().
PROGRAM:
import numpy as np
row1=int(input("Enter the row value of 1st Matrix :",))
col1=int(input("Enter the col value of 1st Matrix :",))
a=np.zeros([row1,col1])
for i in range(row1):
for j in range(col1):
a[i][j]=int(input(f'Enter value for matrix 1 index[{i},{j}] : '))
print("Minimum Element of Given Matrix :")
print(np.min(a))
print("Minimum Element of Given Matrix :")
print(np.max(a))
print("Minimum and Maximum Element corresponding to each row:")
print("Min : ",np.amin(a,axis=1))
print("Max : ",np.amax(a,axis=1))
print("Minimum and Maximum Element corresponding to each column:")
print("Min : ",np.amin(a,axis=0))
print("Max : ",np.amax(a,axis=0))
rank = np.linalg.matrix_rank(a)
print ("Rank of Matrix=",rank)
print("Trace of the matrix is : ",np.trace(a))
if(row1==col1):
x,y=np.linalg.eig(a)
print("Eigenvalues = ",x)
print("EigenVectors= ",y)
else:
print("EigenVectors and Eigenvalues can't be found.")
OUTPUT:
Enter the row value of 1st Matrix :
2
Enter the col value of 1st Matrix :
2
Enter value for matrix 1 index[0,0] :
-6
Enter value for matrix 1 index[0,1] :
3
Enter value for matrix 1 index[1,0] :
4
Enter value for matrix 1 index[1,1] :
5
Minimum Element of Given Matrix :
-6.0
Minimum Element of Given Matrix :
5.0
Minimum and Maximum Element corresponding to each row:
Min : [-6. 4.]
Max : [3. 5.]
Minimum and Maximum Element corresponding to each
column:
Min : [-6. 3.]
Max : [4. 5.]
Rank of Matrix= 2
Trace of the matrix is : -1.0
Eigenvalues :
[-7. 6.]
EigenVectors:
[[-0.9486833 -0.24253563]
[ 0.31622777 -0.9701425 ]]
WEEK-3
AIM:
Write a Program to find the mean, median, standard deviation
and mode using user defined functions
DESCRIPTION:
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear algebra, fourier
transform, and matrices.
Mean:
The mean (also know as average), is obtained by dividing the sum of
observed values by the number of observations, n.
Although data points fall above, below, or on the mean, it can be
considered a good estimate for predicting subsequent data points.
Median:
The median is the middle value of a set of data containing an odd
number of values, or the average of the two middle values of a set of
data with an even number of values.
The median is especially helpful when separating data into two equal
sized bins.
Mode:
The mode of a set of data is the value which occurs most frequently. The
excel syntax for the mode is MODE
Standard Deviation:
The standard deviation gives an idea of how close the entire set of data
is to the average value.
Data sets with a small standard deviation have tightly grouped, precise
data.
Data sets with large standard deviations have data spread out over a
wide range of values.
PROGRAM:
import numpy as np
def mean(a,n):
sum=0
for i in range(0,n):
sum=sum+a[i]
return sum/n
def median(a,n):
s=np.sort(a)
if n%2 == 0:
return (s[n//2]+s[(n-1)//2])/2
else:
return s[n//2]
def standard_deviation(a,n,m):
sq=0
for i in range(0,n):
sq=sq+(a[i]*a[i])
v=(sq/n)-(m*m)
return v**0.5
def mode(a,n): cou=0
for i in range(0,n):
c=a.count(a[i])
if c>cou:
x=a[i]
cou=c
return x
def read_obs(n): array=[]
for i in range(0,n):
ele=int(input())
array.append(ele)
return array
print("Enter the no.of elements:")
n=int(input())
a=read_obs(n)
na=np.array(a)
x=mean(na,n)
print("The mean of n observations is:",x)
m=median(na,n)
print("The median of n observations is:",m)
sd=standard_deviation(na,n,x)
print("The standard deviation of n observations is:",sd)
z=mode(a,n)
print("The mode of n observations is:",z)
OUTPUT:
Enter the no.of elements:
12
The Elements are:
15
10
19
19
7
11
15
19
20
12
17
18
The mean of n observations is: 15.166666666666666
The median of n observations is: 16.0
The standard deviation of n observations is:
4.0790794168401385
The mode of n observations is: 19
WEEK-4
AIM:
Create a data frame with columns at least 5 observations
a) Retrieve a particular column from the DataFrame
b) Summarize the data frame and observe the statistics of the
DataFrame created
c) Observe the mean and standard deviation of the data frame and print
the values.
DESCRIPTION:
a) Retrieve a particular column from the DataFrame :
info() :The info() function is used to print a concise summary of a
DataFrame.
This method prints information about a DataFrame including the index
dtype and column dtype
b)Summarize the data frame and observe the statistics of the
DataFrame created :
Describe() :
The describe() method is used for calculating some statistical data like
percentile, mean and std etc
c) Observe the mean and standard deviation of the data frame and print
the values.
Mean: Return the mean of the values over the requested axis.
DataFrame.mean(axis=NoDefault.no_default, skipna=True, level=None,
numeric_only=Non
Parameters: axis{index (0), columns (1)}
Axis for the function to be applied on.
Standard Deviation:
Return sample standard deviation over requested axis.
DataFrame.std(axis=None, skipna=True, level=None, ddof=1, numeric_
only=None)[
Parameters: axis{index (0), columns (1)}
PROGRAM:
import pandas as pd
dic={'asri':[98,99,100,100,95],
'meghu':[98,100,95,99,100],
'mahathi':[100,100,100,100,99],
'sreeja':[90,100,95,99,99],
'sravya':[99,98,97,100,98]}
df=pd.DataFrame(dic)
print(df["sreeja"])
print(df.info())
print(df.describe())
print(df.mean())
print(df.std())
OUTPUT:
0 90
1 100
2 95
3 99
4 99
Name: sreeja, dtype: int64
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 asri 5 non-null int64
1 meghu 5 non-null int64
2 mahathi 5 non-null int64
3 sreeja 5 non-null int64
4 sravya 5 non-null int64
dtypes: int64(5)
memory usage: 328.0 bytes
None
asri meghu mahathi sreeja sravya
count 5.000000 5.000000 5.000000 5.000000 5.000000
mean 98.400000 98.400000 99.800000 96.600000 98.400000
std 2.073644 2.073644 0.447214 4.159327 1.140175
min 95.000000 95.000000 99.000000 90.000000 97.000000
25% 98.000000 98.000000 100.000000 95.000000 98.000000
50% 99.000000 99.000000 100.000000 99.000000 98.000000
75% 100.000000 100.000000 100.000000 99.000000 99.000000
max 100.000000 100.000000 100.000000 100.000000 100.000000
asri 98.4
meghu 98.4
mahathi 99.8
sreeja 96.6
sravya 98.4
dtype: float64
asri 2.073644
meghu 2.073644
mahathi 0.447214
sreeja 4.159327
sravya 1.140175
dtype: float64
WEEK-5
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-6
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-7
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-8
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-9
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-10
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-11
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT:
WEEK-12
AIM:
DESCRIPTION:
PROGRAM:
OUTPUT: