ML Lab Manual
ML Lab Manual
ML Lab Manual
Lab Manual
by Harivinod N
Prepared by
Mr. Harivinod N
Dept. of Computer Science and Engineering,
VCET Puttur
website:
www.techjourney.in
Vivekananda College of Engineering & Technology TCP03
[A Unit of Vivekananda Vidyavardhaka Sangha Puttur ®] Rev 1.2
Affiliated to Visvesvaraya Technological University CS
Approved by AICTE New Delhi & Recognised by Govt of Karnataka 30/06/2018
COURSE LABORATORY MANUAL
A. LABORATORY OVERVIEW
Degree: BE Programme: CS
Semester: 7 Academic Year: 2018-19
Laboratory Title: Machine Learning Laboratary Laboratory Code: 15CSL76
L-T-P-S: 1-0-2-0 Duration of SEE: 3 Hrs
Total Contact Hours: 40 SEE Marks: 80
Credits: 2 CIE Marks: 20
Lab Manual Author: Harivinod N Sign
Date 30/06/2018
Checked By: Pramod Kumar P M Sign Dt :
B. DESCRIPTION
1. PREREQUISITES:
• Creative thinking, sound mathematical insight and programming skills.
• Design and Analysis of Algorithms (15CS43)
• Design and Analysis of Algorithms Laboratory (15CSL47)
• Fundamentals of Data Structures (15CS33)
• Data Structures Laboratary (15CSL37)
• Computer Programming Laboratory (15CPL16/26)
2. BASE COURSE:
• Machine Learning (15CS73)
3. COURSE OUTCOMES:
At the end of the course, the student will be able to;
1. Understand the implementation procedures for the machine learning algorithms.
2. Design python programs for various learning algorithms.
3. Apply appropriate data sets to the machine learning algorithms.
4. Identify and apply machine learning algorithms to solve real world problems.
4. RESOURSES REQUIRED:
• Hardware resources
◦ Desktop PC
◦ Windows / Linux operating system
• Software resources
◦ Python
◦ Anaconda IDE with Spider
• Datasets from standard reporsitaries (Ex: https://archive.ics.uci.edu/ml/datasets.html)
6. GENERAL INSTRUCTIONS:
• Implement the program in Python editor like Spider and demosnstrate the same.
• External practical examination.
◦ All laboratory experiments are to be included
◦ Students are allowed to pick one experiment from the lot.
◦ Marks distribution: Procedure + Conduction + Viva: 20 + 50 +10 (80)
◦ Change of experiment is allowed only once and marks allotted to the procedure part to
be made zero.
7. CONTENTS:
Expt
Title of the Experiments RBT CO
No.
1 Implement and demonstratethe FIND-S algorithm for finding the most
CO
specific hypothesis based on a given set of training data samples. Read the L3
1,2,3,4
training data from a .CSV file.
2 For a given set of training data examples stored in a .CSV file, implement
CO
and demonstrate the Candidate-Elimination algorithm to output a L3
1,2,3,4
description of the set of all hypotheses consistent with the training examples.
3 Write a program to demonstrate the working of the decision tree based ID3
CO
algorithm. Use an appropriate data set for building the decision tree and L3
1,2,3,4
apply this knowledge toclassify a new sample.
4 Build an Artificial Neural Network by implementing the Backpropagation CO
L3
algorithm and test the same using appropriate data sets. 1,2,3,4
5 Write a program to implement the naïve Bayesian classifier for a sample
CO
training data set stored as a .CSV file. Compute the accuracy of the classifier, L3
1,2,3,4
considering few test data sets.
6 Assuming a set of documents that need to be classified, use the naïve
Bayesian Classifier model to perform this task. Built-in Java classes/API CO
L3
can be used to write the program. Calculate the accuracy, precision, and 1,2,3,4
recall for your data set.
7 Write a program to construct a Bayesian network considering medical data.
CO
Use this model to demonstrate the diagnosis of heart patients using standard L3
1,2,3,4
Heart Disease Data Set. You can use Java/Python ML library classes/API.
8 Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the
same data set for clustering using k-Means algorithm. Compare the results CO
L3
of these two algorithms and comment on the quality of clustering. You can 1,2,3,4
add Java/Python ML library classes/API in the program.
9 Write a program to implement k-Nearest Neighbour algorithm to classify
CO
the iris data set. Print both correct and wrong predictions. Java/Python ML L3
1,2,3,4
library classes can be used for this problem.
10 Implement the non-parametric Locally Weighted Regression algorithm in
CO
order to fit data points. Select appropriate data set for your experiment and L3
1,2,3,4
draw graphs.
11 Open ended experiment - 1
12 Open ended experiment - 2
8. REFERENCE:
1. Tom M. Mitchell, Machine Learning, India Edition 2013, McGraw Hill Education.
2. Trevor Hastie, Robert Tibshirani, Jerome Friedman, The Elements of Statistical
Learning, 2nd edition, Springer series in statistics.
3. Ethem Alpaydın, Introduction to machine learning, second edition, MIT press.
-
C. EVALUATION SCHEME
For CBCS 2015 scheme:
1. Laboratory Components : 12 Marks
(Record writing, Laboratory performance and Viva-voce)
2. Laboratory IA tests: 8 Marks
(Minimum 2 IAs are mandatory. For the final IA test marks, average of the 2 IA test
marks shall be considered and converted to maximum of 8)
3. Continuous Internal Evaluation (CIE) = 12+ 8 = 20 Marks
4. SEE : 80 Marks
-
D1. ARTICULATION MATRIX
Mapping of CO to PO
POs
COs 1 2 3 4 5 6 7 8 9 10 11 12
1. Understand the implementation
3 3 3 3 2 1 1 - 3 3 2 1
procedures for the ML algorithms.
2. Design python programs for various
3 3 3 3 3 1 - - 3 2 1 1
learning algorithms.
3. Apply appropriate data sets to the
3 3 3 3 2 2 - - 3 1 - -
machine learning algorithms.
4. Identify and apply machine learning
3 3 3 3 3 3 1 2 3 3 2 1
algorithms to solve real world problems.
Note: Mappings in the Tables D1 (above) and D2 (below) are done by entering in the corresponding cell the
Correllation Levels in terms of numbers. For Slight (Low): 1, Moderate (Medium): 2, Substantial (High): 3 and for no
correllation: “ - ”.
Find-S Algorithm
1. Initialize h to the most specific hypothesis in H
2. For each positive training instance x
For each attribute constraint a i in h :
If the constraint a i in h is satisfied by x then do nothing
Else replace a i in h by the next more general constraint that is satisfied by x
3. Output hypothesis h
• It is Guaranteed to output the most specific hypothesis within H that is consistent with the
positive training examples.
• Also Notice that negative examples are ignored.
Limitations of the Find-S algorithm:
• No way to determine if the only final hypothesis (found by Find-S) is consistent with data or
there are more hypothesis that is consistent with data.
• Inconsistent sets of training data can mislead the finds algorithm as it ignores negative data
samples.
• A good concept learning algorithm should be able to backtrack the choice of hypothesis
found so that the resulting hypothesis can be improved over time. Unfortunately, Find-S
provide no such method.
6. PROCEDURE / PROGRAMME :
FindS.py
import csv
def loadCsv(filename):
lines = csv.reader(open(filename, "r"));
dataset = list(lines)
headers = dataset.pop(0)
return dataset, headers
def print_hypothesis(h):
print('<',end=' ')
for i in range(0,len(h)-1):
print(h[i],end=',')
print('>')
def findS():
dataset,features=loadCsv('data11_sports6.csv')
rows=len(dataset);
cols=len(dataset[0]);
flag = 0
for x in range(0,rows):
t=dataset[x]
findS()
Result-1
Dataset: data11_tennis6.csv
Sky,AirTemp,Humidity,Wind,EnjoySport
sunny,warm,normal,strong,warm,same,1
sunny,warm,high,strong,warm,same,1
rainy,cold,high,strong,warm,change,0
sunny,warm,high,strong,cool,change,1
Output:
The Maximally Specific Hypothesis for a given Training Examples
< sunny,warm,?,strong,?,?,>
Result-2
Dataset: data12_tennis4.csv
Sky,AirTemp,Humidity,Wind,EnjoySport
sunny,hot,high,weak,1
sunny,hot,high,strong,1
overcast,hot,high,weak,1
rain,mild,high,weak,0
rain,cool,normal,weak,1
rain,cool,normal,strong,0
overcast,cool,normal,strong,1
sunny,cool,normal,weak,1
rain,mild,normal,weak,1
Output
The Maximally Specific Hypothesis for a given Training Examples
< ?,?,?,?,>
8. LEARNING OUTCOMES :
• Students will be able to apply Find-S algorithm to the real world problem and find the most
specific hyposis from the training data.
9. APPLICATION AREAS:
• Classification based problems.
10. REMARKS:
1. EXPERIMENT NO: 2
2. TITLE: CANDIDATE-ELIMINATION ALGORITHM
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• For a given set of training data examples stored in a .CSV file, implement and demonstrate
the Candidate-Elimination algorithm to output a description of the set of all hypotheses
consistent with the training examples.
5. THEORY:
• The key idea in the Candidate-Elimination algorithm is to output a description of the set of
all hypotheses consistent with the training examples.
• It computes the description of this set without explicitly enumerating all of its members.
• This is accomplished by using the more-general-than partial ordering and maintaining a
compact representation of the set of consistent hypotheses.
• The algorithm represents the set of all hypotheses consistent with the observed training
examples. This subset of all hypotheses is called the version space with respect to the
hypothesis space H and the training examples D, because it contains all plausible versions of
the target concept.
• A version space can be represented with its general and specific boundary sets.
• The Candidate-Elimination algorithm represents the version space by storing only its most
general members G and its most specific members S.
• Given only these two sets S and G, it is possible to enumerate all members of a version
space by generating hypotheses that lie between these two sets in general-to-specific partial
ordering over hypotheses. Every member of the version space lies between these boundaries
Algorithm
1. Initialize G to the set of maximally general hypotheses in H
2. Initialize S to the set of maximally specific hypotheses in H
3. For each training example d, do
3.1. If d is a positive example
Remove from G any hypothesis inconsistent with d ,
For each hypothesis s in S that is not consistent with d ,
Remove s from S
Add to S all minimal generalizations h of s such that h is consistent with d,
and some member of G is more general than h
Remove from S, hypothesis that is more general than another hypothesis in S
3.2. If d is a negative example
Remove from S any hypothesis inconsistent with d
For each hypothesis g in G that is not consistent with d
Remove g from G
Add to G all minimal specializations h of g such that h is consistent with d,
and some member of S is more specific than h
Remove from G any hypothesis that is less general than another hypothesis in G
def get_domains(examples):
d = [set() for i in examples[0]]
for x in examples:
for i, xi in enumerate(x):
d[i].add(xi)
return [list(sorted(x)) for x in d]
def candidate_elimination(examples):
domains = get_domains(examples)[:-1]
n = len(domains)
G = set([("?",)*n])
S = set([("0",)*n])
i=0
print("\nS[0]:",str(S),"\nG[0]:",str(G))
for xcx in examples:
i=i+1
x, cx = xcx[:-1], xcx[-1] # Splitting data into attributes and decisions
if cx=='Y': # x is positive example
G = {g for g in G if fulfills(x, g)}
S = generalize_S(x, G, S)
else: # x is negative example
S = {s for s in S if not fulfills(x, s)}
G = specialize_G(x, domains, G, S)
print("\nS[{0}]:".format(i),S)
print("G[{0}]:".format(i),G)
return
candidate_elimination(examples)
Result-1
Data: data21_sports.csv ( Sky,AirTemp,Humidity,Wind,Water,Forecast,EnjoySport)
sunny,warm,normal,strong,warm,same,Y
sunny,warm,high,strong,warm,same,Y
rainy,cold,high,strong,warm,change,N
sunny,warm,high,strong,cool,change,Y
Output
Maximally specific hypotheses - S
Maximally general hypotheses - G
Result-2
Data: data22_shape.csv ( Size,Color,Shape,Label)
big,red,circle,N
small,red,triangle,N
small,red,circle,Y
big,blue,circle,N
small,blue,circle,Y
Output
Maximally specific hypotheses - S
Maximally general hypotheses - G
8. LEARNING OUTCOMES :
• The students will be able to apply candidate elimination algorithm and output a description
of the set of all hypotheses consistent with the training examples
9. APPLICATION AREAS:
• Classification based problems.
10. REMARKS:
5. THEORY:
• ID3 algorithm is a basic algorithm that learns decision trees by constructing them topdown,
beginning with the question "which attribute should be tested at the root of the tree?".
• To answer this question, each instance attribute is evaluated using a statistical test to
determine how well it alone classifies the training examples. The best attribute is selected
and used as the test at the root node of the tree.
• A descendant of the root node is then created for each possible value of this attribute, and
the training examples are sorted to the appropriate descendant node (i.e., down the branch
corresponding to the example's value for this attribute).
• The entire process is then repeated using the training examples associated with each
descendant node to select the best attribute to test at that point in the tree.
• A simplified version of the algorithm, specialized to learning boolean-valued functions (i.e.,
concept learning), is described below.
def load_csv(filename):
lines = csv.reader(open(filename, "r"));
dataset = list(lines)
headers = dataset.pop(0)
return dataset, headers
class Node:
def __init__(self, attribute):
self.attribute = attribute
self.children = []
self.answer = "" # NULL indicates children exists.
# Not Null indicates this is a Leaf Node
for k in attr:
dic[k] = []
for y in range(len(data)):
key = data[y][col]
if delete:
del data[y][col]
dic[key].append(data[y])
def entropy(S):
attr = list(set(S))
if len(attr) == 1: #if all are +ve/-ve then entropy = 0
return 0
sums = 0
for cnt in counts:
sums += -1 * cnt * math.log(cnt, 2)
return sums
return total_entropy
n = len(data[0])-1
gains = [compute_gain(data, col) for col in range(n) ]
return node
def classify(node,x_test,features):
if node.answer != "":
print(node.answer)
return
pos = features.index(node.attribute)
for value, n in node.children:
if x_test[pos]==value:
classify(n,x_test,features)
print("The decision tree for the dataset using ID3 algorithm is ")
print_tree(node, 0)
Output
The decision tree for the dataset using ID3 algorithm is
Outlook
overcast
yes
rain
Wind
weak
yes
strong
no
sunny
Humidity
normal
yes
high
no
The test instance : ['rain', 'cool', 'normal', 'strong']
The predicted label : no
The test instance : ['sunny', 'mild', 'normal', 'strong']
The predicted label : yes
8. LEARNING OUTCOMES :
• The student will be able to demonstrate the working of the decision tree based ID3
algorithm, use an appropriate data set for building the decision tree and apply this
knowledge toclassify a new sample.
9. APPLICATION AREAS:
• Classification related prblem areas
10. REMARKS:
1. EXPERIMENT NO: 4
2. TITLE: BACKPROPAGATION ALGORITHM
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Build an Artificial Neural Network by implementing the Backpropagation algorithm and test
the same using appropriate data sets.
5. THEORY:
• Artificial neural networks (ANNs) provide a general, practical method for learning real-
valued, discrete-valued, and vector-valued functions from examples.
• Algorithms such as BACKPROPAGATION gradient descent to tune network parameters to
best fit a training set of input-output pairs.
• ANN learning is robust to errors in the training data and has been successfully applied to
problems such as interpreting visual scenes, speech recognition, and learning robot control
strategies.
Backpropogation algorithm
1. Create a feed-forward network with ni inputs, nhidden hidden units, and nout output units.
2. Initialize each wi to some small random value (e.g., between -.05 and .05).
3. Until the termination condition is met, do
For each training example <(x1,…xn),t>, do
// Propagate the input forward through the network:
a. Input the instance (x1, ..,xn) to the n/w & compute the n/w outputs ok for every unit
// Propagate the errors backward through the network:
b. For each output unit k, calculate its error term k ; k = ok(1-ok)(tk-ok)
c. For each hidden unit h, calculate its error term h; h=oh(1-oh) k wh,k k
d. For each network weight wi,j do; wi,j=wi,j+wi,j where wi,j= j xi,j
6. PROCEDURE / PROGRAMME :
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float) # Features ( Hrs Slept, Hrs Studied)
y = np.array(([92], [86], [89]), dtype=float) # Labels(Marks obtained)
X = X/np.amax(X,axis=0) # Normalize
y = y/100
def sigmoid(x):
return 1/(1 + np.exp(-x))
def sigmoid_grad(x):
return x * (1 - x)
# Variable initialization
epoch=1000 #Setting training iterations
eta =0.2 #Setting learning rate (eta)
input_neurons = 2 #number of features in data set
hidden_neurons = 3 #number of hidden layers neurons
output_neurons = 1 #number of neurons at output layer
for i in range(epoch):
#Forward Propogation
h_ip=np.dot(X,wh) + bh # Dot product + bias
h_act = sigmoid(h_ip) # Activation function
o_ip=np.dot(h_act,wout) + bout
output = sigmoid(o_ip)
#Backpropagation
# Error at Output layer
Eo = y-output # Error at o/p
outgrad = sigmoid_grad(output)
d_output = Eo* outgrad # Errj=Oj(1-Oj)(Tj-Oj)
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.89427812]
[0.88503667]
[0.89099058]]
8. LEARNING OUTCOMES :
• The student will be able to build an Artificial Neural Network by implementing the
Backpropagation algorithm and test the same using appropriate data sets.
9. APPLICATION AREAS:
• Speech recognition, Character recognition, Human Face recognition
10. REMARKS:
1. EXPERIMENT NO: 5
2. TITLE: NAÏVE BAYESIAN CLASSIFIER
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Write a program to implement the naïve Bayesian classifier for a sample training data set
stored as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.
5. THEORY:
Naive Bayes algorithm : Naive Bayes algorithm is a classification technique based on Bayes’
Theorem with an assumption of independence among predictors. In simple terms, a Naive Bayes
classifier assumes that the presence of a particular feature in a class is unrelated to the presence of
any other feature. For example, a fruit may be considered to be an apple if it is red, round, and
about 3 inches in diameter. Even if these features depend on each other or upon the existence of the
other features, all of these properties independently contribute to the probability that this fruit is an
apple and that is why it is known as ‘Naive’.
Naive Bayes model is easy to build and particularly useful for very large data sets. Along with
simplicity, Naive Bayes is known to outperform even highly sophisticated classification methods.
Bayes theorem provides a way of calculating posterior probability P(c|x) from P(c), P(x) and P(x|c).
Look at the equation below:
where
P(c|x) is the posterior probability of class (c, target) given predictor (x, attributes).
P(c) is the prior probability of class.
P(x|c) is the likelihood which is the probability of predictor given class.
P(x) is the prior probability of predictor.
The naive Bayes classifier applies to learning tasks where each instance x is described by a
conjunction of attribute values and where the target function f (x) can take on any value from some
finite set V. A set of training examples of the target function is provided, and a new instance is
presented, described by the tuple of attribute values (a1, a2, ... ,an). The learner is asked to predict
the target value, or classification, for this new instance.
The Bayesian approach to classifying the new instance is to assign the most probable target value,
Now we could attempt to estimate the two terms in Equation (19) based on the training data. It is
easy to estimate each of the P(vj) simply by counting the frequency with which each target value vj
occurs in the training data.
The naive Bayes classifier is based on the simplifying assumption that the attribute values are
conditionally independent given the target value. In other words, the assumption is that given the
target value of the instance, the probability of observing the conjunction a l, a2, … , an, is just the
product of the probabilities for the individual attributes: P(a l, a2, … , an | vj) = Πi P(ai | vj).
Substituting this, we have the approach used by the naive Bayes classifier.
where vNB denotes the target value output by the naive Bayes classifier.
When dealing with continuous data, a typical assumption is that the continuous values associated
with each class are distributed according to a Gaussian distribution. For example, suppose the
training data contains a continuous attribute, x. We first segment the data by the class, and then
compute the mean and variance of x in each class.
Let μ be the mean of the values in x associated with class Ck, and let σ2k be the variance of the
values in x associated with class Ck. Suppose we have collected some observation value v. Then,
the probability distribution of v given a class Ck, p(x=v|Ck) can be computed by plugging v into the
equation for a Normal distribution parameterized by μ and σ2k . That is
def loadCsv(filename):
lines = csv.reader(open(filename, "r"));
dataset = list(lines)
for i in range(len(dataset)):
dataset[i] = [float(x) for x in dataset[i]]
return dataset
def separateByClass(dataset):
separated = {}
for i in range(len(dataset)):
x = dataset[i]
if (x[-1] not in separated):
separated[x[-1]] = []
separated[x[-1]].append(x)
return separated
def compute_mean_std(dataset):
mean_std = [ (st.mean(attribute), st.stdev(attribute))
for attribute in zip(*dataset)]; #zip(*res) transposes a matrix (2-d array/list)
del mean_std[-1] # Exclude label
return mean_std
def summarizeByClass(dataset):
separated = separateByClass(dataset);
summary = {} # to store mean and std of +ve and -ve instances
for classValue, instances in separated.items():
#summaries is a dictionary of tuples(mean,std) for each class value
summary[classValue] = compute_mean_std(instances)
return summary
dataset = loadCsv('data51.csv');
print('Pima Indian Diabetes Dataset loaded...')
print('Total instances available :',len(dataset))
print('Total attributes present :',len(dataset[0])-1)
splitRatio = 0.2
trainingSet, testSet = splitDataset(dataset, splitRatio)
print('\nDataset is split into training and testing set.')
print('Training examples = {0} \nTesting examples = {1}'.format(len(trainingSet),
len(testSet)))
summaries = summarizeByClass(trainingSet);
predictions = perform_classification(summaries, testSet)
Sample Result
Pima Indian Diabetes Dataset loaded...
Total instances available : 768
Total attributes present : 8
First Five instances of dataset:
1 : [6.0, 148.0, 72.0, 35.0, 0.0, 33.6, 0.627, 50.0, 1.0]
2 : [1.0, 85.0, 66.0, 29.0, 0.0, 26.6, 0.351, 31.0, 0.0]
3 : [8.0, 183.0, 64.0, 0.0, 0.0, 23.3, 0.672, 32.0, 1.0]
4 : [1.0, 89.0, 66.0, 23.0, 94.0, 28.1, 0.167, 21.0, 0.0]
5 : [0.0, 137.0, 40.0, 35.0, 168.0, 43.1, 2.288, 33.0, 1.0]
8. LEARNING OUTCOMES :
• The student will be able to apply naive baysian classifier for the relevent problem and
analyse the results.
9. APPLICATION AREAS:
• Real time Prediction: Naive Bayes is an eager learning classifier and it is sure fast. Thus, it
could be used for making predictions in real time.
• Multi class Prediction: This algorithm is also well known for multi class prediction feature.
Here we can predict the probability of multiple classes of target variable.
• Text classification/ Spam Filtering/ Sentiment Analysis: Naive Bayes classifiers mostly used
in text classification (due to better result in multi class problems and independence rule)
have higher success rate as compared to other algorithms. As a result, it is widely used in
Spam filtering (identify spam e-mail) and Sentiment Analysis (in social media analysis, to
identify positive and negative customer sentiments)
• Recommendation System: Naive Bayes Classifier and Collaborative Filtering together
builds a Recommendation System that uses machine learning and data mining techniques to
filter unseen information and predict whether a user would like a given resource or not
10. REMARKS:
1. EXPERIMENT NO: 6
2. TITLE: DOCUMENT CLASSIFICATION USING NAÏVE BAYESIAN CLASSIFIER
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier
model to perform this task. Built-in Java classes/API can be used to write the program.
Calculate the accuracy, precision, and recall for your data set.
5. THEORY:
For the theoey of the naive bayesian classifier refer Experiment No. 5. Theory of performance
anaysis analysis is ellaborated here.
• For classification tasks, the terms true positives, true negatives, false positives, and false
negatives compare the results of the classifier under test with trusted external judgments.
The terms positive and negative refer to the classifier's prediction (sometimes known as the
expectation), and the terms true and false refer to whether that prediction corresponds to the
external judgment (sometimes known as the observation).
• Precision - Precision is the ratio of correctly predicted positive documents to the total
predicted positive documents. High precision relates to the low false positive rate.
Precision = (Σ True positive ) / ( Σ True positive + Σ False positive)
• Recall (Sensitivity) - Recall is the ratio of correctly predicted positive documents to the all
observations in actual class.
Recall = (Σ True positive ) / ( Σ True positive + Σ False negative)
• Accuracy - Accuracy is the most intuitive performance measure and it is simply a ratio of
correctly predicted observation to the total observations. One may think that, if we have
high accuracy then our model is best. Yes, accuracy is a great measure but only when you
have symmetric datasets where values of false positive and false negatives are almost same.
Therefore, you have to look at other parameters to evaluate the performance of your model.
For our model, we have got 0.803 which means our model is approx. 80% accurate.
Accuracy = (Σ True positive + Σ True negative) / Σ Total population
import pandas as pd
msg=pd.read_csv('data6.csv',names=['message','label']) #Tabular form data
print('Total instances in the dataset:',msg.shape[0])
msg['labelnum']=msg.label.map({'pos':1,'neg':0})
X=msg.message
Y=msg.labelnum
print('\nThe message and its label of first 5 instances are listed below')
X5, Y5 = X[0:5], msg.label[0:5]
for x, y in zip(X5,Y5):
print(x,',',y)
print('Recall :',metrics.recall_score(ytest,predicted),
'\nPrecison :',metrics.precision_score(ytest,predicted))
print('Confusion matrix')
print(metrics.confusion_matrix(ytest,predicted))
Data set
I love this sandwich,pos
This is an amazing place,pos
I feel very good about these beers,pos
This is my best work,pos
What an awesome view,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
He is my sworn enemy,neg
My boss is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to dance,pos
I am sick and tired of this place,neg
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I went to my enemy's house today,neg
Output
Total instances in the dataset: 18
The message and its label of first 5 instances are listed below
I love this sandwich , pos
This is an amazing place , pos
I feel very good about these beers , pos
This is my best work , pos
What an awesome view , pos
Accuracy metrics
Accuracy of the classifer is 0.4
Recall : 0.4
Precison : 1.0
Confusion matrix
8. LEARNING OUTCOMES :
• The student will be able to apply naive baysian classifier for document classification and
analyse the results.
9. APPLICATION AREAS:
• Applicable in document classification
10. REMARKS:
1. EXPERIMENT NO: 7
2. TITLE: BAYESIAN NETWORK
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Write a program to construct a Bayesian network considering medical data. Use this model
to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set. You
can use Java/Python ML library classes/API.
5. THEORY:
• Bayesian networks are very convenient for representing similar
probabilistic relationships between multiple events.
• Bayesian networks as graphs - People usually represent Bayesian
networks as directed graphs in which each node is a hypothesis or a
random process. In other words, something that takes at least 2
possible values you can assign probabilities to. For example, there can
be a node that represents the state of the dog (barking or not barking at
the window), the weather (raining or not raining), etc.
• The arrows between nodes represent the conditional probabilities between them — how
information about the state of one node changes the probability distribution of another node
it’s connected to.
6. PROCEDURE / PROGRAMME :
Program for the Illustration of Baysian Belief networks using 5 nodes using Lung cancer data.
(The Conditional probabilities are given)
import numpy as np
import pandas as pd
import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianModel
from adpgmpy.inference import VariableElimination
[5 rows x 14 columns]
8. LEARNING OUTCOMES :
• The student will be able to apply baysian network for the medical data and demonstrate the
diagnosis of heart patients using standard Heart Disease Data Set.
9. APPLICATION AREAS:
• Applicable in prediction and classification • Document Classification
• Gene Regulatory Networks • Information Retrieval
• Medicine • Semantic Search
• Biomonitoring
10. REMARKS:
1. EXPERIMENT NO: 8
2. TITLE: CLUSTERING BASED ON EM ALGORITHM AND K-MEANS
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM: Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for
clustering using k-Means algorithm. Compare the results of these two algorithms and comment on
the quality of clustering. You can add Java/Python ML library classes/API in the program.
5. THEORY:
Expectation Maximization algorithm
• The basic approach and logic of this clustering method is as follows.
• Suppose we measure a single continuous variable in a large sample of observations. Further,
suppose that the sample consists of two clusters of observations with different means (and
perhaps different standard deviations); within each sample, the distribution of values for the
continuous variable follows the normal distribution.
• The goal of EM clustering is to estimate the means and standard deviations for each cluster
so as to maximize the likelihood of the observed data (distribution).
• Put another way, the EM algorithm attempts to approximate the observed distributions of
values based on mixtures of different distributions in different clusters. The results of EM
clustering are different from those computed by k-means clustering.
• The latter will assign observations to clusters to maximize the distances between clusters.
The EM algorithm does not compute actual assignments of observations to clusters, but
classification probabilities.
• In other words, each observation belongs to each cluster with a certain probability. Of
course, as a final result we can usually review an actual assignment of observations to
clusters, based on the (largest) classification probability.
K means Clustering
• The algorithm will categorize the items into k groups of similarity. To calculate that
similarity, we will use the euclidean distance as measurement.
• The algorithm works as follows:
1. First we initialize k points, called means, randomly.
2. We categorize each item to its closest mean and we update the mean’s coordinates,
which are the averages of the items categorized in that mean so far.
3. We repeat the process for a given number of iterations and at the end, we have our
clusters.
• The “points” mentioned above are called means, because they hold the mean values of the
items categorized in it. To initialize these means, we have a lot of options. An intuitive
method is to initialize the means at random items in the data set. Another method is to
initialize the means at random values between the boundaries of the data set (if for a feature
x the items have values in [0,3], we will initialize the means with values for x at [0,3]).
6. PROCEDURE / PROGRAMME :
plt.subplot(2, 2, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[gmm_y], s=40)
plt.title('GMM Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
print('Observation: The GMM using EM algorithm based clustering matched the true labels
more closely than the Kmeans.')
Observation: The GMM using EM algorithm based clustering matched the true labels more
closely than the Kmeans.
8. LEARNING OUTCOMES :
• The students will be apble to apply EM algorithm and k-Means algorithm for clustering and
anayse the results.
9. APPLICATION AREAS:
• Text mining • Image analysis
• Pattern recognition • Web cluster engines
10. REMARKS:
1. EXPERIMENT NO: 9
2. TITLE: K-NEAREST NEIGHBOUR
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set.
Print both correct and wrong predictions. Java/Python ML library classes can be used for
this problem.
5. THEORY:
• K-Nearest Neighbors is one of the most basic yet essential classification algorithms in
Machine Learning. It belongs to the supervised learning domain and finds intense
application in pattern recognition, data mining and intrusion detection.
• It is widely disposable in real-life scenarios since it is non-parametric, meaning, it does not
make any underlying assumptions about the distribution of data.
• Algorithm
Input: Let m be the number of training data samples. Let p be an unknown point.
Method:
1. Store the training samples in an array of data points arr[]. This means each element
of this array represents a tuple (x, y).
2. for i=0 to m
Calculate Euclidean distance d(arr[i], p).
3. Make set S of K smallest distances obtained. Each of these distances correspond to
an already classified data point.
4. Return the majority label among S.
6. PROCEDURE / PROGRAMME :
# Load dataset
iris=datasets.load_iris()
print("Iris Data set loaded...")
# Perform Training
classifier.fit(x_train, y_train)
# Perform testing
y_pred=classifier.predict(x_test)
Result-1
Iris Data set loaded...
Dataset is split into training and testing samples...
Size of trainng data and its label (135, 4) (135,)
Size of trainng data and its label (15, 4) (15,)
Label 0 - setosa
Label 1 - versicolor
Label 2 - virginica
Results of Classification using K-nn with K=1
Sample: [4.4 3. 1.3 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 2.5 3. 1.1] Actual-label: 1 Predicted-label: 1
Sample: [6.1 2.8 4. 1.3] Actual-label: 1 Predicted-label: 1
Sample: [6. 2.7 5.1 1.6] Actual-label: 1 Predicted-label: 2
Sample: [6.7 2.5 5.8 1.8] Actual-label: 2 Predicted-label: 2
Sample: [5.1 3.8 1.5 0.3] Actual-label: 0 Predicted-label: 0
Sample: [6.7 3.1 4.4 1.4] Actual-label: 1 Predicted-label: 1
Sample: [4.8 3.4 1.6 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 3.5 1.4 0.3] Actual-label: 0 Predicted-label: 0
Sample: [5.4 3.7 1.5 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.7 2.8 4.1 1.3] Actual-label: 1 Predicted-label: 1
Sample: [4.5 2.3 1.3 0.3] Actual-label: 0 Predicted-label: 0
Sample: [4.4 2.9 1.4 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.1 3.5 1.4 0.2] Actual-label: 0 Predicted-label: 0
Sample: [6.2 3.4 5.4 2.3] Actual-label: 2 Predicted-label: 2
Result-2
Iris Data set loaded...
Dataset is split into training and testing samples...
Size of trainng data and its label (135, 4) (135,)
Size of trainng data and its label (15, 4) (15,)
Label 0 - setosa
Label 1 - versicolor
Label 2 - virginica
Results of Classification using K-nn with K=1
Sample: [6.5 3. 5.5 1.8] Actual-label: 2 Predicted-label: 2
Sample: [5.7 2.8 4.1 1.3] Actual-label: 1 Predicted-label: 1
Sample: [6.6 3. 4.4 1.4] Actual-label: 1 Predicted-label: 1
Sample: [6.9 3.1 5.1 2.3] Actual-label: 2 Predicted-label: 2
Sample: [5.1 3.8 1.9 0.4] Actual-label: 0 Predicted-label: 0
Sample: [7.2 3.2 6. 1.8] Actual-label: 2 Predicted-label: 2
Sample: [5.5 2.6 4.4 1.2] Actual-label: 1 Predicted-label: 1
Sample: [6. 2.9 4.5 1.5] Actual-label: 1 Predicted-label: 1
Sample: [5.1 3.7 1.5 0.4] Actual-label: 0 Predicted-label: 0
Sample: [5.2 3.4 1.4 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5. 3.5 1.6 0.6] Actual-label: 0 Predicted-label: 0
Sample: [4.9 3.1 1.5 0.1] Actual-label: 0 Predicted-label: 0
Sample: [5. 3. 1.6 0.2] Actual-label: 0 Predicted-label: 0
Sample: [5.7 3. 4.2 1.2] Actual-label: 1 Predicted-label: 1
Sample: [5.8 2.7 5.1 1.9] Actual-label: 2 Predicted-label: 2
Classification Accuracy : 1.0
8. LEARNING OUTCOMES :
• The student will be able to implement k-Nearest Neighbour algorithm to classify the iris
data set and Print both correct and wrong predictions.
9. APPLICATION AREAS:
• Recommender systems
• Classification problems
10. REMARKS:
1. EXPERIMENT NO: 10
2. TITLE: LOCALLY WEIGHTED REGRESSION ALGORITHM
3. LEARNING OBJECTIVES:
• Make use of Data sets in implementing the machine learning algorithms.
• Implement ML concepts and algorithms in Python
4. AIM:
• Implement the non-parametric Locally Weighted Regression algorithm in order to fit data
points. Select appropriate data set for your experiment and draw graphs.
5. THEORY:
• Given a dataset X, y, we attempt to find a linear model h(x) that minimizes residual sum of
squared errors. The solution is given by Normal equations.
• Linear model can only fit a straight line, however, it can be empowered by polynomial
features to get more powerful models. Still, we have to decide and fix the number and types
of features ahead.
• Alternate approach is given by locally weighted regression.
• Given a dataset X, y, we attempt to find a model h(x) that minimizes residual sum of
weighted squared errors.
• The weights are given by a kernel function which can be chosen arbitrarily and in my case I
chose a Gaussian kernel.
• The solution is very similar to Normal equations, we only need to insert diagonal weight
matrix W.
Algorithm
def local_regression(x0, X, Y, tau):
# add bias term
x0 = np.r_[1, x0]
X = np.c_[np.ones(len(X)), X]
# predict value
return x0 @ beta
def radial_kernel(x0, X, tau):
return np.exp(np.sum((X - x0) ** 2, axis=1) / (-2 * tau * tau))
6. PROCEDURE / PROGRAMME :
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def localWeight(point,xmat,ymat,k):
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
return W
def localWeightRegression(xmat,ymat,k):
m,n = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
def graphPlot(X,ypred):
sortindex = X[:,1].argsort(0) #argsort - index of the smallest
xsort = X[sortindex][:,0]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(bill,tip, color='green')
ax.plot(xsort[:,1],ypred[sortindex], color = 'red', linewidth=5)
plt.xlabel('Total bill')
plt.ylabel('Tip')
plt.show();
8. LEARNING OUTCOMES :
• To understand and implement linear regression and analyse the results with change in the
parameters
9. APPLICATION AREAS:
• Demand anaysis in business
• Optimization of business processes
• Forecasting
10. REMARKS: