ML Lab Draft Manual
ML Lab Draft Manual
on
MACHINE LEARNING LAB
(U21CD6L2)
By
Mr. Syed Juber
Assistant Professor
Dept. of Information Technology
DEPARTMENT OF
INFORMATION TECHNOLOGY
LAB MANUAL
MACHINE LEARNING LAB (U21CD6L2)
Experiment 1:
Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis
based on a given set of training data samples. Read the training data from a CSV file.
FIND-S Algorithm
1. Initialize h to the most specific hypothesis in H
2. For each positive training instance x
For each attribute constraint ai in h
If the constraint ai is satisfied by x
Then do nothing
Else replace ai in h by the next more general constraint that is satisfied by x
3. Output hypothesis h
Program:
# Example usage:
total_instances = total_training_instances(df)
print("The total number of training instances are:", total_instances)
Output:
# Example usage:
init_hypothesis = initial_hypothesis(df)
print("The initial hypothesis are:", init_hypothesis)
Output:
# Example usage:
hypotheses = hypothesis_for_instances(df)
for idx, h in enumerate(hypotheses, start=1):
print(f"The hypothesis for the training instance {idx} is:")
print(h)
Output:
# Example usage:
max_specific_hypothesis = maximally_specific_hypothesis(df)
print("The Maximally specific hypothesis for the training instance is:\n",
max_specific_hypothesis)
Output:
Viva Questions:
1. What is the main objective of the FIND-S algorithm in machine learning?
2. Can you explain the concept of a hypothesis space and how it relates to the FIND-S
algorithm?
3. How does the FIND-S algorithm initialize the most specific hypothesis?
4. What is the significance of the training data samples in the context of the FIND-S
algorithm?
5. Could you describe the process of reading training data from a CSV file for
implementing the FIND-S algorithm?
6. How does the FIND-S algorithm update the most specific hypothesis based on the
training data samples?
7. Can you discuss any limitations or assumptions of the FIND-S algorithm?
8. How does the FIND-S algorithm handle noisy or inconsistent training data?
9. What are some practical applications where the FIND-S algorithm could be used
effectively?
10. Can you demonstrate the implementation of the FIND-S algorithm on a sample dataset,
showing the progression of the most specific hypothesis with each training example?
• 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
Program:
Output:
Output:
Output:
# Remove fully generalized rows from general_h and return final specific_h and general_h
indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
Output:
Viva Questions:
1. What is the Candidate-Elimination algorithm primarily used for in machine learning?
2. How does the Candidate-Elimination algorithm handle hypothesis generation and
elimination?
3. Can you explain the concept of a version space and its role in the Candidate-Elimination
algorithm?
4. How would you represent the training data stored in a .CSV file for use with the
Candidate-Elimination algorithm?
5. Describe the process of initializing the version space and hypotheses in the Candidate-
Elimination algorithm.
6. What are some advantages of using the Candidate-Elimination algorithm over other
learning methods?
7. How does the Candidate-Elimination algorithm update the version space after
processing each training example?
8. What are some potential challenges or limitations of the Candidate-Elimination
algorithm?
9. Can you give an example of a real-world scenario where the Candidate-Elimination
algorithm could be applied?
10. Can you walk me through a simple implementation of the Candidate-Elimination
algorithm using a small dataset?
ID3 Algorithm
ID3(Examples, Target_attribute, Attributes)
Examples are the training examples. Target_attribute is the attribute whose value is to
bepredicted by the tree. Attributes is a list of other attributes that may be tested by the
learned decision tree. Returns a decision tree that correctly classifies the given
Examples.
Otherwise Begin
A ← the attribute from Attributes that best* classifies Examples
The decision attribute for Root ← A
For each possible value, vi, of A,
Add a new tree branch below Root, corresponding to the test A = vi
Let Examples vi, be the subset of Examples that have value vi for A
If Examples vi , is empty
Then below this new branch add a leaf node with label = most
commonvalue of Target_attribute in Examples
Else below this new branch add the subtree
ID3(Examples vi, Targe_tattribute, Attributes –
{A}))
End
Return Root
Program:
counts = [0, 0]
for i in range(2):
counts[i] = sum([1 for x in S if attr[i] == x]) / (len(S) * 1.0)
sums = 0
for cnt in counts:
sums += -1 * cnt * math.log(cnt, 2)
return sums
total_size = len(data)
entropies = [0] * len(attr)
ratio = [0] * len(attr)
n = len(data[0]) - 1
gains = [0] * n
for col in range(n):
gains[col] = compute_gain(data, col)
split = gains.index(max(gains))
node = Node(features[split])
fea = features[:split] + features[split + 1:]
for x in range(len(attr)):
child = build_tree(dic[attr[x]], fea)
node.children.append((attr[x], child))
return node
# Main program
dataset, features = load_csv("id3.csv")
node1 = build_tree(dataset, features)
print("The decision tree for the dataset using ID3 algorithm is")
print_tree(node1, 0)
Output:
Backpropagation Algorithm
BACKPROPAGATION (training_example, ƞ, nin, nout, nhidden)
Each training example is a pair of the form (𝑥, 𝑡), where (𝑥) is the vector of network input
values, and (𝑡) is the vector of target network output values. ƞ is the learning rate (e.g., 0.05).
ni, is the number of network inputs, nhidden the number of units in the hidden layer, and nout the
number of output units.The input from unit i into unit j is denoted xji, and the weight from unit
i to unit j is denoted wji
1 2 9 92
2 1 5 86
3 3 6 89
Program:
import numpy as np
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float) # two inputs [sleep,study]
y = np.array(([92], [86], [89]), dtype=float) # one output [Expected % in Exams]
X = X/np.amax(X,axis=0) # maximum of X array longitudinally
y = y/100
#Sigmoid Function
def sigmoid (x):
return 1/(1 + np.exp(-x))
#Variable initialization
epoch=5000 #Setting training iterations
lr=0.1 #Setting learning rate
inputlayer_neurons = 2 #number of features in data set
hiddenlayer_neurons = 3 #number of hidden layers neurons
output_neurons = 1 #number of neurons at output layer
#Forward Propogation
hinp1=np.dot(X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp= outinp1+ bout
output = sigmoid(outinp)
#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO* outgrad
EH = d_output.dot(wout.T)
Output:
import csv
import random
import math
def loadcsv(filename):
# Loads a CSV file and converts its data into a list of lists
lines = csv.reader(open(filename, "r"))
dataset = list(lines)
for i in range(len(dataset)):
# Converts strings into numbers for processing
dataset[i] = [float(x) for x in dataset[i]]
return dataset
def separatebyclass(dataset):
# Separates the dataset by class values (assuming the last column contains the class labels)
separated = {}
for i in range(len(dataset)):
vector = dataset[i]
if vector[-1] not in separated:
separated[vector[-1]] = []
separated[vector[-1]].append(vector)
return separated
def mean(numbers):
# Calculates the mean of a list of numbers
return sum(numbers) / float(len(numbers))
def stdev(numbers):
# Calculates the standard deviation of a list of numbers
avg = mean(numbers)
variance = sum([pow(x - avg, 2) for x in numbers]) / float(len(numbers) - 1)
return math.sqrt(variance)
def summarize(dataset):
# Summarizes the mean and standard deviation of each attribute in the dataset
summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
del summaries[-1] # Excludes labels (assumes they are the last column)
return summaries
def summarizebyclass(dataset):
# Summarizes dataset statistics by class
separated = separatebyclass(dataset)
summaries = {}
for classvalue, instances in separated.items():
summaries[classvalue] = summarize(instances)
return summaries
def main():
filename = 'naivedata.csv'
splitratio = 0.67
dataset = loadcsv(filename)
# Prepare model
main()
Output:
Viva Questions:
1. What is the primary objective of using a Naïve Bayesian classifier in machine learning?
2. Can you explain the concept of Bayes' theorem and how it is applied in the Naïve
Bayesian classifier?
3. How does the Naïve Bayesian classifier handle feature independence assumption?
4. Describe the process of reading and preprocessing training data from a .CSV file for
implementing the Naïve Bayesian classifier.
5. What role do prior probabilities play in the Naïve Bayesian classifier?
6. How do you compute the likelihood probabilities in the Naïve Bayesian classifier?
7. Can you explain the concept of Laplace smoothing and its importance in the Naïve
Bayesian classifier?
8. How would you evaluate the accuracy of the Naïve Bayesian classifier using test
datasets?
9. Can you discuss any limitations or assumptions of the Naïve Bayesian classifier?
10. Can you walk me through a simple implementation of the Naïve Bayesian classifier
using a .CSV file for training data and demonstrate how it computes the accuracy using
test datasets?
Program:
import pandas as pd
msg=pd.read_csv('naivetext.csv',names=['message','label'])
msg['labelnum']=msg.label.map({'pos':1,'neg':0})
X=msg.message
y=msg.labelnum
print(X)
print(y)
df = pd.DataFrame(xtrain_dtm.toarray(), columns=count_vect.get_feature_names_out())
Output:
Program:
# Load the heart disease dataset from a CSV file named 'heart.csv'
heartDisease = pd.read_csv('heart.csv')
Output:
Program:
iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
model = KMeans(n_clusters=3)
model.fit(X)
y_gmm = gmm.predict(xs)
#y_cluster_gmm
plt.subplot(2, 2, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_gmm], s=40)
plt.title('GMM Classification')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
Output:
Program:
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn import datasets
iris=datasets.load_iris()
x = iris.data
y = iris.target
print('Confusion Matrix')
print(confusion_matrix(y_test,y_pred))
print('Accuracy Metrics')
print(classification_report(y_test,y_pred))
. . . . . . . .
. . . . . . . .
Viva Questions:
1. What is the Iris dataset?
2. What is the k-Nearest Neighbor algorithm, and how does it work?
3. How is the distance between data points calculated in k-Nearest Neighbor?
4. Why is it important to split the dataset into training and testing sets?
5. How do you select the value of k in k-Nearest Neighbor?
Program (a):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Normalize X
X_normalized = (X - np.mean(X)) / np.std(X)
plt.scatter(X_normalized, y, label='Data')
plt.xlabel('Normalized Total Bill')
plt.ylabel('Tip Amount')
plt.title('Tips Dataset')
plt.legend()
plt.show()
Program (b):
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.nonparametric.smoothers_lowess import lowess
# Normalize X
Output (a):
Output (b):