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

ML Lab Manual Final

Ml lab

Uploaded by

h8792670
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

ML Lab Manual Final

Ml lab

Uploaded by

h8792670
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

MACHINE LEARNING LAB MANUAL

NAME OF THE FACULTY : SMD SHAFIULLA


DEPARTMENT : COMPUTER SCIENCE & ENGINEERING
YEAR & SEM : III YEAR II SEM
COURSE CODE : CS604PC
REGULATION : R18
BRANCH & SECTION : COMPUTER SCIENCE & ENGINEERING
ACADEMIC YEAR : 2023-24
NO. OF CREDITS : 1.5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

VISION

The Vision of the department is to produce competent graduates suitable for


industries and organizations at global level including research and development with
Social responsibility.

MISSION

a) To provide an open environment to foster professional and personal growth with


a strong theoretical and practical background.
b) Emphasis on hardware and software development making the graduates industry
ready with social ethics.
c) Further the Department is to provide training and to partner with Global entities
in education and research.
PROGRAM EDUCATIONAL OBJECTIVES(PEOs)
Students will establish themselves as effective professionals by solving real
problems through the use of computer science knowledge and with attention to
PEO1
team work, effective communication, critical thinking and problem solving
skills.
Students will develop professional skills that prepare them for immediate
PEO2 employment and for life-long learning in advanced areas of computer science
and related fields.

Students will demonstrate their ability to adapt to a rapidly changing


PEO3
environment by having learned and applied new skills and new technologies

Students will be provided with an educational foundation that prepares them for
PEO4 excellence, leadership roles along diverse career paths with encouragement to
professional ethics and active participation needed for a successful career.
PROGRAM OUTCOMES(POs)
Engineering knowledge: Apply the knowledge of Mathematics, Science,
PO1 Engineering fundamentals, and an Engineering specialization to the solution
of complex Engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze
PO2 complex engineering problems reaching substantiated conclusions using first
principles of Mathematics, Natural sciences, and Engineering sciences
Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified
PO3
needs with appropriate consideration for the public health and safety, and the
cultural, societal, and environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge
and research methods including design of experiments, analysis and
PO4
interpretation of data, and synthesis of the information to provide valid
conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources,
PO5 and modern engineering and IT tools including prediction and modeling to
complex engineering activities with an understanding of the limitations
The engineer and society: Apply reasoning informed by the contextual
PO6 knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering practice.
Environment and sustainability: Understand the impact of the professional
PO7 engineering solutions in societal and environmental contexts, and demonstrate
the knowledge of, and need for sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and
PO8
responsibilities and norms of the engineering practice.
Individual and team work: Function effectively as an individual, and as a
PO9
member or leader in diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities
with the engineering community and with society at large, such as, being able
PO10
to comprehend and write effective reports and design documentation, make
effective presentations, and give and receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding
of the engineering and management principles and apply these to one’s own
PO11
work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
Life-long learning: Recognize the need for, and have the preparation and
PO12 ability to engage in independent and life-long learning in the broadest context
of technological change.
PROGRAM SPECIFIC OUTCOMES(PSOs)
Understand, design and analyze computer programs in the areas related to
PSO1 Algorithms, System Software, Web design, Big data, Artificial Intelligence,
Machine Learning and Networking.
Focus on improving software reliability, network security or information
PSO2
retrieval systems.
Make use of modern computer tools for creating innovative career paths, to be
PSO3
an entrepreneur and desire for higher studies
Experiment 1
The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school days
in a week, the probability that it is Friday is 20 %. What is the probability that a student is absent
given that today is Friday? Apply Baye’s rule in python to get the result. (Ans: 15%)
Aim : The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school
days in a week, the probability that it is Friday is 20 %. What is the probability that a student is
absent given that today is Friday? Apply Baye’s rule in python to get the result.
Environment : Python IDLE/ Python 3.12
Code snippet :
# The probability that it is Friday and that a student is absent is 3%
pAF=0.03
print("The probability that it is Friday and that a student is absent :",pAF)
# The probability that it is Friday is 20%
pF=0.2
print("The probability that it is Friday : ",pF)
# The probability that a student is absent given that today is Friday
pResult=(pAF/pF)
# Display the Result
print("The probability that a student is absent given that today is Friday : ",pResult * 100,"%")
Output :
Experiment 2
Extract the data from database using python
Aim : To Extract the data from database using python
Environment : Python IDLE/ Python 3.12
Code snippet :
import mysql.connector
# Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root", passwd =
"",database="SampleDB")
# Creating the cursor object
cur = myconn.cursor()
# Executing the query
cur.execute("select * from students")
# Fetching the rows from the cursor object
result = cur.fetchall()
print("Student Details are :")
# Printing the result
for x in result:
print(x);
# Commit the transaction
myconn.commit()
# Close the connection
myconn.close()
Output :
Experiment 3
Implement k-nearest neighbours classification using python
Aim : To Implement k-nearest neighbours classification using python
Environment : Python IDLE/ Python 3.12
Code snippet :
# Import necessary modules
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import random
# Loading data
data_iris = load_iris()
# To get list of target names
label_target = data_iris.target_names
print()
print("Sample Data from Iris Dataset")
print("*"*30)
# to display the sample data from the iris dataset
for i in range(10):
rn = random.randint(0,120)
print(data_iris.data[rn],"===>",label_target[data_iris.target[rn]])

# Create feature and target arrays


X = data_iris.data
y = data_iris.target

# Split into training and test set


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.3, random_state=1)
print("The Training dataset length: ",len(X_train))
print("The Testing dataset length: ",len(X_test))
try:
nn = int(input("Enter number of neighbors :"))
knn = KNeighborsClassifier(nn)

knn.fit(X_train, y_train)
# to display the score
print("The Score is :",knn.score(X_test, y_test))
# To get test data from the user
test_data = input("Enter Test Data :").split(",")
for i in range(len(test_data)):
test_data[i] = float(test_data[i])
print()

v = knn.predict([test_data])
print("Predicted output is :",label_target[v])
except:
print("Please supply valid input......")
Output :
Experiment 4
Given the following data, which specify classifications for nine combinations of VAR1 and VAR2
predict a classification for a case where VAR1=0.906 and VAR2=0.606, using the result of k
means clustering with 3 means (i.e., 3 centroids)
VAR1 VAR2 CLASS
1.713 1.586 0
0.180 1.786 1
0.353 1.240 1
0.940 1.566 0
1.486 0.759 1
1.266 1.106 0
1.540 0.419 1
0.459 1.799 1
0.773 0.186 1
Aim : Given the following data, which specify classifications for nine combinations of VAR1 and
VAR2 predict a classification for a case where VAR1=0.906and VAR2=0.606, using the result of
k-means clustering with 3 means (i.e., 3centroids)
Environment : Python IDLE/ Python 3.12
Code snippet :
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1.713,1.586], [0.180,1.786], [0.353,1.240],
[0.940,1.566], [1.486,0.759], [1.266,1.106],[1.540,0.419],[0.459,1.799],[0.773,0.186]])
y=np.array([0,1,1,0,1,0,1,1,1])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X,y)
print("The input data is ")
print("VAR1 \t VAR2 \t CLASS")
i=0
for val in X:
print(val[0],"\t",val[1],"\t",y[i])
i+=1
print("="*20)
# To get test data from the user
print("The Test data to predict ")
test_data = []
VAR1 = float(input("Enter Value for VAR1 :"))
VAR2 = float(input("Enter Value for VAR2 :"))
test_data.append(VAR1)
test_data.append(VAR2)
print("="*20)
print("The predicted Class is : ",kmeans.predict([test_data]))
Output :
Experiment 5
The following training examples map descriptions of individuals onto high, medium and low
credit-worthiness.
medium skiing design single twenties no -> highRisk
high golf trading married forties yes -> lowRisk
low speedway transport married thirties yes -> medRisk
medium football banking single thirties yes -> lowRisk
high flying media married fifties yes -> highRisk
low football security single twenties no -> medRisk
medium golf media single thirties yes -> medRisk
medium golf transport married forties yes -> lowRisk
high skiing banking single thirties yes -> highRisk
low golf unemployed married forties yes -> highRisk
Input attributes are (from left to right) income, recreation, job, status, age-group, home-owner.
Find the unconditional probability of `golf' and the conditional probability of `single' given
`medRisk' in the dataset?
Aim :To determine unconditional probability of golf and the conditional probability of single
given medRisk in the dataset.
Environment : Python IDLE/ Python 3.12
Code snippet :
total_Records=10
numGolfRecords=4
unConditionalprobGolf=numGolfRecords / total_Records
print("Unconditional probability of golf: ={}".format(unConditionalprobGolf))
#conditional probability of 'single' given 'medRisk'
numMedRiskSingle=2
numMedRisk=3
probMedRiskSingle=numMedRiskSingle/total_Records
probMedRisk=numMedRisk/total_Records
conditionalProb=(probMedRiskSingle/probMedRisk)
print("Conditional probability of single given medRisk: = {}".format(conditionalProb))
Output :
Experiment 6
Implement linear regression using python.
Aim : To implement linear regression using python
Environment : Python IDLE/ Python 3.12
Code snippet :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# To read data from Age_Income.csv file
dataFrame = pd.read_csv('Age_Income.csv')
# To place data in to age and income vectors
age = dataFrame['Age']
income = dataFrame['Income']
# number of points
num = np.size(age)
# To find the mean of age and income vector
mean_age = np.mean(age)
mean_income = np.mean(income)
# calculating cross-deviation and deviation about age
CD_ageincome = np.sum(income*age) - num*mean_income*mean_age
CD_ageage = np.sum(age*age) - num*mean_age*mean_age
# calculating regression coefficients
b1 = CD_ageincome / CD_ageage
b0 = mean_income - b1*mean_age
# to display coefficients
print("Estimated Coefficients :")
print("b0 = ",b0,"\nb1 = ",b1)
# To plot the actual points as scatter plot
plt.scatter(age, income, color = "b",marker = "o")
# TO predict response vector
response_Vec = b0 + b1*age
# To plot the regression line
plt.plot(age, response_Vec, color = "r")
# Placing labels
plt.xlabel('Age')
plt.ylabel('Income')
# To display plot
plt.show()

Age_Income.csv
Age,Income
25,25000
23,22000
24,26000
28,29000
34,38600
32,36500
42,41000
55,81000
45,47500
Output :
Experiment 7
Implement Naïve Bayes theorem to classify the English text
Aim : To implement Naïve Bayes theorem to classify the English text
Environment : Python IDLE/ Python 3.12
Code snippet :
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])
print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)
count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv
This is very good place,pos
I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg
Output :
Experiment 8
Implement an algorithm to demonstrate the significance of genetic algorithm
Aim : To implement an algorithm to demonstrate the significance of genetic algorithm
Environment : Python IDLE/ Python 3.12
Code snippet :
import numpy
# Parameter initialization
genes = 2
chromosomes = 10
mattingPoolSize = 6
offspringSize = chromosomes - mattingPoolSize
lb = -5
ub = 5
populationSize = (chromosomes, genes)
generations = 3
#Population initialization
population = numpy.random.uniform(lb, ub, populationSize)
for generation in range(generations):
print(("Generation:", generation+1))
fitness = numpy.sum(population*population, axis=1)
print("\npopulation")
print(population)
print("\nfitness calcuation")
print(fitness)
# Following statement will create an empty two dimensional array to store parents
parents = numpy.empty((mattingPoolSize, population.shape[1]))
# A loop to extract one parent in each iteration
for p in range(mattingPoolSize):
# Finding index of fittest chromosome in the population
fittestIndex = numpy.where(fitness == numpy.max(fitness))
# Extracting index of fittest chromosome
fittestIndex = fittestIndex[0][0]
# Copying fittest chromosome into parents array
parents[p, :] = population[fittestIndex, :]
# Changing fitness of fittest chromosome to avoid reselection of that chromosome
fitness[fittestIndex] = -1
print("\nParents:")
print(parents)
# Following statement will create an empty two dimensional array to store offspring
offspring = numpy.empty((offspringSize, population.shape[1]))
for k in range(offspringSize):
#Determining the crossover point
crossoverPoint = numpy.random.randint(0,genes)
# Index of the first parent.
parent1Index = k%parents.shape[0]
# Index of the second.
parent2Index = (k+1)%parents.shape[0]
# Extracting second half of the offspring
offspring[k, crossoverPoint:] = parents[parent2Index, crossoverPoint:]
print("\nOffspring after crossover:")
print(offspring)
# Implementation of random initialization mutation.
for index in range(offspring.shape[0]):
randomIndex = numpy.random.randint(1,genes)
randomValue = numpy.random.uniform(lb, ub, 1)
offspring [index, randomIndex] = offspring [index, randomIndex] +randomValue
print("\n Offspring after Mutation")
print(offspring)
population[0:parents.shape[0], :] = parents
population[parents.shape[0]:, :] = offspring
print("\nNew Population for next generation:")
print(population)
fitness = numpy.sum(population*population, axis=1)
fittestIndex = numpy.where(fitness == numpy.max(fitness))
# Extracting index of fittest chromosome
fittestIndex = fittestIndex[0][0]
# Getting Best chromosome
fittestInd = population[fittestIndex, :]
bestFitness = fitness[fittestIndex]
print("\nBest Individual:")
print(fittestInd)
print("\nBest Individual's Fitness:")
print(bestFitness)

Output :
Experiment 9
Implement the finite words classification system using Back-propagation algorithm
Aim : To implement the finite words classification system using Back-propagation algorithm
Environment : Python IDLE/ Python 3.12
Code snippet :
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
msglbl_data = pd.read_csv('scient.csv', names=['Message', 'Label'])
print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)
count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data
prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Output :

You might also like