ML LAB MANUAL 1.Implement and demonstrate the use of set of training data samples. Read the training data from a .CSV file. import csv with open('tennis.csv', 'r') as f: reader = csv.reader(f) your_list = list(reader) h = [['0', '0', '0', '0', '0', '0']] for i in your_list: print(i) if i[ - 1] == "True": j = 0 for x in i: if x != "True": if x != h[0][j] and h[0][j] == '0': h[0][j] = x elif x != h[0][j] and h[0][j] != '0': h[0][j] = '?' else: pass j = j + 1 print("Most specific hypothesis is") print(h) DATA tennis .csv outlook,temperature,humidity,wind, answer sunny,hot,high,weak,no sunny,hot,high,strong,no overcast,hot,high,weak,yes rain,mild,high,weak,yes rain,cool,normal,weak,yes rain,cool,normal,strong,no overcast,cool,normal,strong,yes sunny,mild,high,weak,no sunny,cool,normal,weak,yes rain,mild,normal,weak,yes sunny,mild,normal,strong,yes overcast,mild,high,strong,yes overcast,hot,normal,weak,yes rain,mild,high,strong,no OUTPUT ['outlook', 'temperature', 'humidity', 'wind', ' '] ['answer sunny', 'hot', 'high', 'weak', 'no '] ['sunny', 'hot', 'high', 'strong', 'no '] ['overcast', 'hot', 'high', 'weak', 'yes '] ['rain', 'mild', 'high', 'weak', 'yes '] ['rain', 'cool', 'normal', 'weak', 'yes '] ['rain', 'cool', 'normal', 'strong', 'no '] ['overcast', 'cool', 'normal', 'strong', 'yes '] ['sunny', 'mild', 'high', 'weak', 'no '] ['sunny', 'cool', 'normal', 'weak', 'yes '] ['rain', 'mild', 'normal', 'weak', 'yes '] ['sunny', 'mild', 'normal', 'strong', 'yes '] ['overcast', 'mild', 'high', 'strong', 'yes '] ['overcast', 'hot', 'normal', 'weak', 'yes '] ['rain', 'mild', 'high', 'strong', 'no '] Most specific hypothesis is [['0', '0', '0', '0', '0', '0']] 2. Write a program 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 to classify a new sample. import numpy as np import math from data_loader import read_data class Node: def __init__(self, attribute): self.attribute = attribute self.children = [] self.answer = "" def __str__(self): return self.attribute def subtables(data, col, delete): dict = {} items = np.unique(data[:, col]) count = np.zeros((items.shape[0], 1), dtype=np.int32) for x in range(items.shape[0]): for y in range(data.shape[0]): if data[y, col] == items[x]: count[x] += 1 for x in range(items.shape[0]): dict[items[x]] = np.empty((int(count[x]), data.shape[1]), dtype="|S32") count = np.zeros((items.shape[0], 1), dtype=np.int32) for y in range(data.shape[0]): for x in range(items.shape[0]): if data[y, col] == items[x]: dict[items[x]][count[x][0]] = data[y] count[x][0] += 1 if delete: for x in items: dict[x] = np.delete(dict[x], col, 1) return items, dict def entropy(S): items = np.unique(S) if items.size == 1: return 0 counts = np.zeros((items.shape[0], 1)) for x in range(items.shape[0]): counts[x] = sum(S == items[x]) / (S.size * 1.0) sums = 0 for count in counts: if count > 0: sums += - 1 * count * math.log2(count) return sums def information_gain(data, col): total_entropy = entropy(data[:, - 1]) items, dict = subtables(data, col, delete=False) weighted_entropy = 0 total_size = data.shape[0] for x in items: subset = dict[x] weighted_entropy += (subset.shape[0] / total_size) * entropy(subset[:, - 1]) return total_entropy - weighted_entropy def create_node(data, metadata): if (np.unique(data[:, - 1])).shape[0] == 1: node = Node("") node.answer = np.unique(data[:, - 1])[0].decode() return node gains = np.zeros((data.shape[1] - 1, 1)) for col in range(data.shape[1] - 1): gains[col] = information_gain(data, col) split = np.argmax(gains) node = Node(metadata[split]) metadata_new = metadata[:split] + metadata[split + 1:] items, dict = subtables(data, split, delete=True) for x in items: child = create_node(dict[x], metadata_new) node.children.append((x.decode(), child)) return node def print_tree(node, level): indent = " " * level if node.answer != "": print(indent + " - > " + node.answer) return print(indent + node.attribute) for value, child in node.children: print(indent + f"[{value}]") print_tree(child, level + 1) def classify(sample, tree, metadata): while tree.answer == "": attr_index = metadata.index(tree.attribute) value = sample[attr_index] found = False for val, child in tree.children: if val == value: tree = child found = True break if not found: return "Unknown" return tree.answer # --- Main Execution --- metadata, traindata = read_data("tennis.csv") data = np.array(traindata, dtype="|S32") tree = create_node(data, metadata) print("Decision Tree:") print_tree(tree, 0) # Classify a new sample new_sample = ["sunny", "cool", "normal", "strong"] result = classify(new_sample, tree, metadata) print(" \ nNew Sample:", new_sample) print("Predicted Class:", result) Dataloader.py import csv def read_data(filename): with open(filename, 'r') as csvfile: datareader = csv.reader(csvfile, delimiter=',') headers = next(datareader) metadata = headers traindata = [row for row in datareader] return metadata, traindata DATA T ennis.cs v outlook,temperature,humidity,wind, answer sunny,hot,high,weak,no sunny,hot,high,strong,no overcast,hot,high,weak,yes rain,mild,high,weak,yes rain,cool,normal,weak,yes rain,cool,normal,strong,no overcast,cool,normal,strong,yes sunny,mild,high,weak,no sunny,cool,normal,weak,yes rain,mild,normal,weak,yes sunny,mild,normal,strong,yes overcast,mild,high,strong,yes overcast,hot,normal,weak,yes rain,mild,high,strong,no OUTPUT Decision Tree: outlook [answer sunny] - > no [overcast] - > yes [rain] wind [strong] - > no [weak] - > yes [sunny] humidity [high] - > no [normal] - > yes New Sample: ['sunny', 'cool', 'normal', 'strong'] Predicted Class: yes 3. Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the same using appropriate data sets. import numpy as np X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float) y = np.array(([92], [86], [89]), dtype=float) 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)) #Derivative of Sigmoid Function def derivatives_sigmoid(x): return x * (1 - x) #Variable initialization epoch=7000 #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 #weight and bias initialization wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons)) bh=np.random.uniform(size=(1,hiddenlayer_neurons)) wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons)) bout=np.random.uniform(size=(1,output_neurons)) #draws a random range of numbers uniformly of dim x*y for i in range(epoch): #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) hiddengrad = derivatives_sigmoid(hlayer_act)#how much hidden layer wts #contributed to error d_hiddenlayer = EH * hiddengrad wout += hlayer_act.T.dot(d_output) *lr# dotproduct of nextlayererror and #currentlayerop # bout += np.sum(d_output, axis=0,keepdims=True) *lr wh += X.T.dot(d_hiddenlayer) *lr #bh += np.sum(d_hiddenlayer, axis=0,keepdims=True) *lr print("Input: \ n" + str(X)) print("Actual Output: \ n" + str(y)) print("Predicted Output: \ n" ,output) OUTPUT Input: [[0.66666667 1. ] [0.33333333 0.55555556] [1. 0.66666667]] Actual Output: [[0.92] [0.86] [0.89]] Predicted Output: [[0.89500216] [0.882244 ] [0.89245635]] 4. 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. import csv import random import math def loadCsv(filename): lines = csv.reader(open(filename, "r")) dataset = [] for row in lines: if len(row) == 0: continue # Skip empty rows dataset.append([float(x) for x in row]) print(f"Loaded dataset with {len(dataset)} rows, each with {len(dataset[0])} columns") return dataset def splitDataset(dataset, splitRatio): trainSize = int(len(dataset) * splitRatio) trainSet = [] copy = list(dataset) while len(trainSet) < trainSize: index = random.randrange(len(copy)) trainSet.append(copy.pop(index)) return trainSet, copy def separateByClass(dataset): 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): return sum(numbers) / float(len(numbers)) def stdev(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): summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)] del summaries[ - 1] # remove summary of class label return summaries def summarizeByClass(dataset): separated = separateByClass(dataset) summaries = {} for classValue, instances in separated.items(): summaries[classValue] = summarize(instances) return summaries def calculateProbability(x, mean, stdev): if stdev == 0: # Avoid division by zero by adding a very small number stdev = 1e - 9 exponent = math.exp( - (math.pow(x - mean, 2) / (2 * math.pow(stdev, 2)))) return (1 / (math.sqrt(2 * math.pi) * stdev)) * exponent def calculateClassProbabilities(summaries, inputVector): probabilities = {} for classValue, classSummaries in summaries.items(): probabilities[classValue] = 1 for i in range(len(classSummaries)): mean_, stdev_ = classSummaries[i] x = inputVector[i] probabilities[classValue] *= calculateProbability(x, mean_, stdev_) return probabilities def predict(summaries, inputVector): probabilities = calculateClassProbabilities(summaries, inputVector) bestLabel, bestProb = None, - 1 for classValue, probability in probabilities.items(): if bestLabel is None or probability > bestProb: bestProb = probability bestLabel = classValue return bestLabel def getPredictions(summaries, testSet): predictions = [] for i in range(len(testSet)): result = predict(summaries, testSet[i]) predictions.append(result) return predictions def getAccuracy(testSet, predictions): correct = 0 for i in range(len(testSet)): if testSet[i][ - 1] == predictions[i]: correct += 1 return (correct / float(len(testSet))) * 100.0 def main(): filename = '5data.csv' # make sure this file exists and has no empty lines splitRatio = 0.67 dataset = loadCsv(filename) trainingSet, testSet = splitDataset(dataset, splitRatio) print(f"Split {len(dataset)} rows into train={len(trainingSet)} and test={len(testSet)} rows") # Prepare model summaries = summarizeByClass(trainingSet) # Test model predictions = getPredictions(summaries, testSet) accuracy = getAccuracy(testSet, predictions) print(f'Accuracy of the classifier is : {accuracy:.2f}%') if __name__ == "__main__": main() DATA 5 data.csv 5.1,3.5,1.4,0.2,0 4.9,3.0,1.4,0.2,0 6.2,3.4,5.4,2.3,1 5.9,3.0,5.1,1.8,1 5.5,2.3,4.0,1.3,1 5.0,3.6,1.4,0.2,0 6.7,3.1,4.7,1.5,1 5.6,2.9,3.6,1.3,1 4.8,3.4,1.6,0.2,0 7.2,3.6,6.1,2.5,1 5.4,3.7,1.5,0.2,0 6.1,2.8,4.7,1.2,1 6.3,3.3,6.0,2.5,1 4.6,3.1,1.5,0.2,0 5.7,2.8,4.1,1.3,1 OUTPUT Loaded dataset with 15 rows, each with 5 columns Split 15 rows into train=10 and test=5 rows Accuracy of the classifier is : 100.00% 5. Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier model to perform this task. Built - in Python librari es /API can be used to write the program. Calculate the accuracy, precision, and recall for your data set. #importing libraries 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, precision_score, recall_score # Sample data: list of documents and their labels (binary classification here) documents = [ "I love this product, it's amazing!", "This is the worst thing I've ever bought.", "Absolutely fantastic! Highly recommend.", "Do not waste your money on this.", "Great value for the price.", "Terrible, broke after one use.", "Excellent quality and fast shipping.", "Very disappointed, not as described.", "Works perfectly, very happy with it.", "Horrible customer service and bad quality." ] labels = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] # 1 = positive, 0 = negative # Step 1: Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(documents, labels, test_size=0.3, random_state=42) # Step 2: Convert text documents into feature vectors vectorizer = CountVectorizer() X_train_vec = vectorizer.fit_transform(X_train) X_test_vec = vectorizer.transform(X_test) # Step 3: Train a Naïve Bayes classifier nb_classifier = MultinomialNB() nb_classifier.fit(X_train_vec, y_train) # Step 4: Make predictions on the test set y_pred = nb_classifier.predict(X_test_vec) # Step 5: Calculate accuracy, precision, and recall accuracy = accuracy_score(y_test, y_pred) precision = precision_score(y_test, y_pred) recall = recall_score(y_test, y_pred) print(f"Accuracy: {accuracy:.2f}") print(f"Precision: {precision:.2f}") print(f"Recall: {recall:.2f}") OUTPUT Accuracy: 0.33 Precision: 0.33 Recall: 1.00 6. Apply Support Vector Machine to classify the given data set. Algorithm: Applying Support Vector Machine (SVM) for Classification Input: • Dataset with features XXX and labels yyy • (Optional) For text data: raw text features Output: • Trained SVM model • Predicted labels on test data • Performance metrics (accuracy, confusion matrix, etc.) Steps: 1. Load Dataset - Read the dataset into memory (e.g., using pandas if CSV). 2. Preprocess the Data • If data is textual: convert text to numerical features (e.g., using CountVectorizer or TF IDF). • If data is numerical, check for missing values, normalize or scale if needed. 3. Split Dataset - Divide the data into training and testing sets (e.g., 70% train, 30% test) using random sampling. 4. Initialize the SVM Classifier • Choose kernel type (linear, RBF, polynomial, etc.) based on data characteristics. • Set other hyperparameters like regularization parameter CCC, gamma, etc. 5. Train the Model o Fit the SVM model on the training data (features and labels). 6. Predict on Test Data - Use the trained model to predict labels on the test set. 7. Evaluate the Model • Calculate accuracy score. • Generate confusion matrix. • (Optional) Calculate precision, recall, F1 - score for detailed analysis. 8. (Optional) Tune Hyperparameters o Use grid search or other methods to optimize model performance. Code: #import libraries import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import classification_report, confusion_matrix # Load Iris dataset iris = datasets.load_iris() X = iris.data y = iris.target # Split the dataset into training and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Create SVM classifier with RBF kernel model = SVC(kernel='rbf', C=1.0, gamma='scale') model.fit(X_train, y_train) # Predict on test data y_pred = model.predict(X_test) # Evaluation print("Confusion Matrix:") print(confusion_matrix(y_test, y_pred))