Untitled
unknown
plain_text
a month ago
40 kB
7
Indexable
Ex.No:1(a)
Date:
Aim:
Implementation of Uniformed Search Algorithms - Breadth First Search
The aim of implementing Breadth First Search algorithm is to traverse a graph or a tree data structure
in a systematic way, visiting all nodes and edges in the structure in a particular order, without revisiting
any node twice
Software Required:
Python IDLE 3.13.3
Algorithm:
1. most common search strategy for traversing a tree or graph.
2. searches breadthwise in a tree or graph, so it is called breadth-first search.
3. starts searching from the root node of the tree and expands all successor node at the current level
before moving to nodes of next level.
4. example of a general-graph search algorithm.
5. implemented using FIFO queue data structure.
Program:
from collections import deque
def bfs(graph,start):
visited=set()
queue=deque([start])
while queue:
vertex=queue.popleft()
if vertex not in visited:
print(vertex,end="")
visited.add(vertex)
queue.extend(neighbor for neighbor in graph[vertex]if neighbor not in visited)
graph={
'5':['3','7'],
'3':['2','4'],
'7':['8'],
'2':[],
'4':['8'],
'8':[]
}
bfs(graph,'5')
Page | 1
Ex.No:1(b)
Date:
Aim:
Implementation of Uniformed Search Algorithms - Depth first Search
The aim of implementing Depth First Search algorithm is to traverse a graph or a tree data structure in
a systematic way, visiting all nodes and edges in the structure in a particular order, without revisiting any
node twice
Software Required:
Python IDLE 3.13.3
Algorithm:
1. recursive algorithm for traversing a tree or graph data structur.
2. starts from the root node and follows each path to its greatest depth node before moving to the
next path.
3. uses a stack data structure for its implementation.
4. process of the DFS algorithm is similar to the BFS algorithm.
Program:
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=" ")
for neighbor in graph[start]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
graph = {
'5': ['3', '7'],
'3': ['2', '4'],
'7': ['8'],
'2': [],
'4': ['8'],
'8': []
}
Page | 1
dfs(graph, '5')
Ex.No:2(a)
Date:
Aim:
Implementation of Informed search algorithms - A* search algorithm
To implements the simple informed search algorithm A* search methods using python
Software Required:
Python IDLE 3.13.3
Algorithm:
1. put the start node in the open list.
2. set its cost g=0.
3. repeat until open list is empty.
3.1. select the node from open list with the lowest f,
f=g+h.
3.2. if this node is the goal stop and return the path.
3.3. move the node to the closed list.
3.4. for each neighbour of the selected node.
• compute g
• compute h
• compute f
3.5. if the neighbour is not in closed list add or update in open list.
4. if open list becomes empty report no solution.
5. end.
Program:
def a_star_algorithm(start, goal):
# OPEN set contains nodes to be evaluated
open_set = {start}
# CLOSED set contains nodes already evaluated
closed_set = set()
# g[n] = cost from start node to n
g = {}
g[start] = 0
# parent[n] = previous node of n in optimal path
parent = {}
parent[start] = start
Page | 1
while open_set:
current = None
# Select node with minimum f(n) = g(n) + h(n)
for node in open_set:
if current is None or g[node] + heuristic(node) < g[current] + heuristic(current):
current = node
# If goal node is reached, reconstruct path
if current == goal:
path = []
while parent[current] != current:
path.append(current)
current = parent[current]
path.append(start)
path.reverse()
print("Path found:", path)
return path
# Move current node from OPEN to CLOSED
open_set.remove(current)
closed_set.add(current)
# Explore neighbors
for neighbor, cost in get_neighbors(current):
if neighbor in closed_set:
continue
new_cost = g[current] + cost
if neighbor not in open_set:
open_set.add(neighbor)
parent[neighbor] = current
g[neighbor] = new_cost
elif new_cost < g[neighbor]:
g[neighbor] = new_cost
parent[neighbor] = current
print("Path does not exist")
return None
# Function to return neighbors of a node
def get_neighbors(node):
return graph[node]
# Heuristic function (estimated cost to goal)
def heuristic(node):
h = {
'A': 10,
'B': 8,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
}
return h[node]
# Graph representation (Adjacency list)
graph = {
'A': [('B', 6), ('F', 3)],
'B': [('C', 3), ('D', 2)],
'C': [('D', 1), ('E', 5)],
'D': [('C', 1), ('E', 8)],
'E': [('I', 5), ('J', 5)],
'F': [('G', 1), ('H', 7)],
'G': [('I', 3)],
'H': [('I', 2)],
'I': [('E', 5), ('J', 3)],
'J': []
}
# Function call
a_star_algorithm('A', 'J')
Ex.No:2(b)
Date:
Aim:
Implementation of Informed search algorithms
Simplified memory-bounded A* (SMA*)
To implements the simple informed search algorithm Simplified memory-bounded A* search
methods using python
Software Required:
Python IDLE 3.13.3
Algorithm:
1. create start node.
2. insert start node into open list.
3. while open list is not empty,
3.1. select node n with lowest f(n).
3.2. if n is the goal then return solution or expand n.
3.3. for each child node c compute f(c)=f(c)+h(c). then insert c into open list.
3.4. if memory limit exceeded,
• remove the worst leaf / child node.
• backup its ‘f’ value to its parent.
3.5. end while.
4. returns failure.
Program:
import heapq
class Node:
def __init__(self, state, parent=None, g=0, h=0):
self.state = state
self.parent = parent
self.g = g
self.h = h
self.f = g + h
self.children = []
def __lt__(self, other):
return self.f < other.f
Page | 1
def sma_star(start, goal, graph, heuristic, memory_limit):
open_list = []
start_node = Node(start, None, 0, heuristic[start])
heapq.heappush(open_list, start_node)
while open_list:
# Get best node
current = heapq.heappop(open_list)
if current.state == goal:
return reconstruct_path(current)
# Expand node
for neighbor, cost in graph[current.state]:
g = current.g + cost
h = heuristic[neighbor]
child = Node(neighbor, current, g, h)
current.children.append(child)
heapq.heappush(open_list, child)
# Memory check
if len(open_list) > memory_limit:
# Remove worst leaf node (highest f-value)
worst = max(open_list, key=lambda x: x.f)
open_list.remove(worst)
heapq.heapify(open_list)
# Backup value to parent
if worst.parent:
worst.parent.f = max(worst.parent.f, worst.f)
return None
def reconstruct_path(node):
path = []
while node:
path.append(node.state)
node = node.parent
return path[::-1]
graph = {
'A': [('B', 1), ('C', 3)],
'B': [('D', 3), ('E', 1)],
'C': [('F', 5)],
'D': [],
'E': [('G', 2)],
'F': [],
'G': []
}
heuristic = {
'A': 6,
'B': 4,
'C': 5,
'D': 4,
'E': 2,
'F': 6,
'G': 0
}
path = sma_star('A', 'G', graph, heuristic, memory_limit=4)
print("Solution Path:", path)
Result:
Thus, the program for Simplified memory-bounded A* (SMA*) was executed and output is
verified.
Ex.No:3
Date:
Aim:
Implementation of Navie Bayes Models
To Implement Navie Bayes Models using python.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. calculate the prior probability.
2. calculate the likelyhood probabilities.
3. apply baye’s theorem to calculate the posterior probability for each class.
4. make the prediction based on highest posterior probability.
Program:
# Step 1: Import required libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
# Step 2: Create dataset inside the program
data = {
'Outlook': ['Sunny','Sunny','Overcast','Rain','Rain','Rain','Overcast',
'Sunny','Sunny','Rain','Sunny','Overcast','Overcast','Rain'],
'Temperature': ['Hot','Hot','Hot','Mild','Cool','Cool','Cool',
'Mild','Cool','Mild','Mild','Mild','Hot','Mild'],
'Humidity': ['High','High','High','High','Normal','Normal','Normal',
'High','Normal','Normal','Normal','High','Normal','High'],
'Windy': ['Weak','Strong','Weak','Weak','Weak','Strong','Strong',
'Weak','Weak','Weak','Strong','Strong','Weak','Strong'],
'PlayTennis': ['No','No','Yes','Yes','Yes','No','Yes',
'No','Yes','Yes','Yes','Yes','Yes','No']
}
Page | 1
df = pd.DataFrame(data)
# Step 3: Separate input features and output label
X = df.iloc[:, :-1]
y = df.iloc[:, -1]
# Step 4: Encode categorical features
le_outlook = LabelEncoder()
le_temperature = LabelEncoder()
le_humidity = LabelEncoder()
le_windy = LabelEncoder()
le_play = LabelEncoder()
X['Outlook'] = le_outlook.fit_transform(X['Outlook'])
X['Temperature'] = le_temperature.fit_transform(X['Temperature'])
X['Humidity'] = le_humidity.fit_transform(X['Humidity'])
X['Windy'] = le_windy.fit_transform(X['Windy'])
y = le_play.fit_transform(y)
# Step 5: Train Naive Bayes model using full dataset
classifier = GaussianNB()
classifier.fit(X, y)
# Step 6: Provide test data manually
test_data = {
'Outlook': ['Sunny'],
'Temperature': ['Cool'],
'Humidity': ['High'],
'Windy': ['Strong']
}
test_df = pd.DataFrame(test_data)
# Encode test data using SAME encoders
test_df['Outlook'] = le_outlook.transform(test_df['Outlook'])
test_df['Temperature'] = le_temperature.transform(test_df['Temperature'])
test_df['Humidity'] = le_humidity.transform(test_df['Humidity'])
test_df['Windy'] = le_windy.transform(test_df['Windy'])
# Step 7: Predict output
prediction = classifier.predict(test_df)
result = le_play.inverse_transform(prediction)
print("\nTest Data Given:")
print(test_data)
print("\nPrediction Result:")
print("Play Tennis:", result[0])
Result:
Thus, the naive baye program was executed and output is verified.
Ex.No:4
Date:
Aim:
Implementation of Bayesian Networks
To Implement Bayesian Networks using python.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Nodes: First determine the set of variables that are required to model the domain.
2. Now order them, {X1, . . . ,Xn}. Any order will work, but the resulting network will be more
compact if the variables are ordered such that causes precede effects.
3. Links: For i = 1 to n do:
• Choose, from X1, . . . ,Xi−1, a minimal set of parents for Xi, such that Equation,
P(Xi |Xi-1,........., X1) = P(Xi|Parents(Xi)) is satisfied.
• For each parent insert a link from the parent to Xi.
• CPTs: Write down the conditional probability table, P(Xi|Parents(Xi)).
Program:
# Prior probabilities
P_B = {True: 0.001, False: 0.999}
P_E = {True: 0.002, False: 0.998}
# Conditional probability of Alarm
P_A = {
(True, True): 0.95,
(True, False): 0.94,
(False, True): 0.29,
(False, False): 0.001
}
# Conditional probability of John calling
P_J = {True: 0.90, False: 0.05}
# Conditional probability of Mary calling
P_M = {True: 0.70, False: 0.01}
Page | 1
def joint_probability(b, e, a, j, m):
prob = P_B[b]
prob *= P_E[e]
prob *= P_A[(b, e)] if a else (1 - P_A[(b, e)])
prob *= P_J[a] if j else (1 - P_J[a])
prob *= P_M[a] if m else (1 - P_M[a])
return prob
q1 = joint_probability(
b=False,
e=False,
a=True,
j=True,
m=True
)
print("P(J, M, A, ¬B, ¬E) =", q1)
Result:
Thus, the Bayesian networks program was executed and output is verified.
Ex.No:5
Date:
Aim:
Build regression model
To implement Linear Regression without using built-in machine learning libraries and to determine
the slope and intercept of the best-fit line for the given dataset, and to predict the output for a new input
value.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Linear regression shows the linear relationship between the independent variable (X-axis) and the
dependent variable (Y-axis), hence called linear regression.
2. Regression line of X on Y: The best estimate for the value of X for any specific values of Y is
X=a+bY where a = X intercept, b= Slope of the line, X=dependent variable and Y=independent
variable.
3. Regression line of Y on X: The best estimate for the value of Y for any specific values of X is
Y=a+bX
4. Mathematically, we can represent a linear regression as: y= a0+a1x+ ε Here, Y= Dependent Variable
(Target Variable) X= Independent Variable (predictor Variable) a0= intercept of the line (Gives an
additional degree of freedom) a1 = Linear regression coefficient (scale factor to each input value).
ε = random error
Program:
import numpy as np
# Sample data
X = np.array([1,2,3,4,5])
y = np.array([1.2,1.8,2.6,3.2,3.8])
# Calculate means
mean_x = np.mean(X)
mean_y = np.mean(y)
# Calculate slope (m)
numerator = np.sum((X - mean_x) * (y - mean_y))
denominator = np.sum((X - mean_x)**2)
m = numerator / denominator
Page | 1
# Calculate intercept (b)
b = mean_y - m * mean_x
print("Slope:", m)
print("Intercept:", b)
# Prediction
x_new = 7
y_pred = m * x_new + b
print("Predicted value for x=7:", y_pred)
Result:
Thus, the Linear Regression model was successfully implemented without using machine learning
libraries. The slope and intercept of the best-fit line were calculated, and the predicted value for the
given input was obtained successfully.
Ex.No:6(a)
Date:
Aim:
Build Decision Tree
To implement a Decision Tree classifier using the ID3 algorithm and to classify data based on the
given attributes using Information Gain.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Begin the tree with the root node, says S, which contains the complete dataset.
2. Find the best attribute in the dataset using Attribute Selection Measure (ASM).
3. Divide the S into subsets that contains possible values for the best attributes.
4. Generate the decision tree node, which contains the best attribute.
5. Recursively make new decision trees using the subsets of the dataset created in step -3. Continue this
process until a stage is reached where cannot further classify the nodes and called the final node as a
leaf node.
Program:
import pandas as pd
import numpy as np
# ----------------------------
# Step 1: Calculate Entropy
# ----------------------------
def entropy(target_col):
values, counts = np.unique(target_col, return_counts=True)
entropy_val = 0
for i in range(len(values)):
p = counts[i] / sum(counts)
entropy_val -= p * np.log2(p)
return entropy_val
# ----------------------------
# Step 2: Calculate Information Gain
# ----------------------------
def information_gain(data, attribute, target_name):
total_entropy = entropy(data[target_name])
Page | 1
values, counts = np.unique(data[attribute], return_counts=True)
weighted_entropy = 0
for i in range(len(values)):
subset = data[data[attribute] == values[i]]
weighted_entropy += (counts[i] / sum(counts)) * entropy(subset[target_name])
info_gain = total_entropy - weighted_entropy
return info_gain
# ----------------------------
# Step 3: Build Decision Tree (ID3)
# ----------------------------
def id3(data, original_data, features, target_name, parent_node_class=None):
# If all target values are same → return that class
if len(np.unique(data[target_name])) <= 1:
return np.unique(data[target_name])[0]
# If dataset is empty → return most common class in original dataset
elif len(data) == 0:
return np.unique(original_data[target_name])[np.argmax(
np.unique(original_data[target_name], return_counts=True)[1])]
# If no features left → return parent node class
elif len(features) == 0:
return parent_node_class
else:
# Majority class
parent_node_class = np.unique(data[target_name])[np.argmax(
np.unique(data[target_name], return_counts=True)[1])]
# Select best feature (max info gain)
item_values = [information_gain(data, feature, target_name) for feature in features]
best_feature_index = np.argmax(item_values)
best_feature = features[best_feature_index]
tree = {best_feature: {}}
# Remove selected feature
features = [i for i in features if i != best_feature]
# Grow tree
for value in np.unique(data[best_feature]):
sub_data = data[data[best_feature] == value]
subtree = id3(sub_data, data, features, target_name, parent_node_class)
tree[best_feature][value] = subtree
return tree
# ----------------------------
# Step 4: Prediction Function
# ----------------------------
def predict(query, tree, default=None):
for key in list(query.keys()):
if key in tree:
try:
result = tree[key][query[key]]
except:
return default
if isinstance(result, dict):
return predict(query, result)
else:
return result
# ----------------------------
# Step 5: Example Dataset (Categorical)
# ----------------------------
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast',
'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Mild',
'Hot', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal',
'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong',
'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'PlayTennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes',
'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
# ----------------------------
# Step 6: Train Decision Tree
# ----------------------------
tree = id3(df, df, df.columns[:-1], 'PlayTennis')
print("\nDecision Tree:\n")
print(tree)
# ----------------------------
# Step 7: Test Prediction
# ----------------------------
query = {
'Outlook': 'Sunny',
'Temperature': 'Cool',
'Humidity': 'High',
'Wind': 'Strong'
}
prediction = predict(query, tree)
print("\nPrediction for given input:", prediction)
Result:
The Decision Tree was successfully constructed using the ID3 algorithm. The model was able to
classify the given input data accurately based on the learned decision rules. The prediction for the test
query was obtained, demonstrating the effectiveness of the algorithm in decision-making.
Ex.No:6(b)
Date:
Aim:
Build Random Forest
To implement the Random Forest algorithm using scikit-learn and evaluate its performance on the Iris
dataset for classification.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. a subset of data points & a subset of features is selected for constructing each decision tree for
eg. put n random records & m features are taken from dataset of k number of records.
2. individual decision trees are constructed for each sample.
3. each decision tree will generate an output.
4. find output is considered based on majarity voting or averaging for classification& regression
respectively.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create model
model = RandomForestClassifier(n_estimators=100)
# Train model
model.fit(X_train, y_train)
# Predict
y_pred = model.predict(X_test)
Page | 1
# Accuracy
print("Random Forest Accuracy:", accuracy_score(y_test, y_pred))
# Sample Prediction
sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(sample)
print("Sample Prediction Class:", prediction)
print("Class Name:", data.target_names[prediction][0])
Result:
The Random Forest model was successfully trained and tested on the Iris dataset. The model
achieved good accuracy in classification and was able to correctly predict the class of a given sample
input, demonstrating its effectiveness.
Ex.No:7
Date:
Aim:
Build SVM model
To implement the Support Vector Machine (SVM) algorithm using scikit-learn with RBF kernel and
perform classification on the Iris dataset.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. load a dataset using the padas library.
2. split the dataset into training and testing sets using train_test_split function from scikit-learn.
3. train three SVM model with different kernels (linear, polynomial, and RBF) using SVC function
from scikit-learn.
4. predict the test set labels using the trained models.
5. evaluate the accuracy of the models using the accuracy_score function from scikit-learn
6. print the accuracy of each model
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create model
model = SVC(kernel='rbf')
# Train model
model.fit(X_train, y_train)
Page | 1
# Predict
y_pred = model.predict(X_test)
# Accuracy
print("SVM Accuracy:", accuracy_score(y_test, y_pred))
# Sample Prediction
sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(sample)
print("Sample Prediction Class:", prediction)
print("Class Name:", data.target_names[prediction][0])
Result:
The SVM model was successfully trained and tested on the Iris dataset. The model achieved good
accuracy and correctly classified the given sample input, demonstrating its effectiveness in classification
tasks.
Ex.No:8(a)
Date:
Aim:
Implement Ensembling Techniques - Bagging
To implement the Bagging Classifier using Decision Tree as the base model and evaluate its
performance on the Iris dataset.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. assume that there are n observations and m features in the training set. Select a random sample
from the training dataset without replacement.
2. a subset of m features is chosen randomly to create a model using sample observations.
3. the feature offering the best split out of the lot is used to split the nodes.
4. the tree is grown, to have the best root nodes.
5. the above steps are repeated n times. It aggregates the output of individual decision trees to give
the best prediction.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Base model
base_model = DecisionTreeClassifier()
# Bagging model
bagging = BaggingClassifier(
estimator=base_model,
n_estimators=10,
random_state=42
)
Page | 1
# Train
bagging.fit(X_train, y_train)
# Predict
y_pred = bagging.predict(X_test)
# Accuracy
print("Bagging Accuracy:", accuracy_score(y_test, y_pred))
Result:
The Bagging Classifier model was successfully trained and tested on the Iris dataset. The model
improved classification performance by combining multiple decision trees and achieved good accuracy.
Ex.No:8(b)
Date:
Aim:
Implement Ensembling Techniques - Boosting
To implement the AdaBoost (Boosting) algorithm using scikit-learn and evaluate its performance on
the Iris dataset.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. the boosting algorithm assigns equal weight to each data sample It inputs the data to the first
base algorithm and the predictions are made for each data sample.
2. the boosting algorithm assesses model predictions and increases the weight of samples with a
more significant error It also assigns a weight based on model performance .A model that outputs
excellent predictions will have a high amount of influence over the final decision.
3. the algorithm passes the weighted data to the next decision tree.
4. the algorithm repeats steps 2 and 3 until instances of training errors are within acceptable limit.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Boosting model
boost = AdaBoostClassifier(
n_estimators=50,
learning_rate=1.0,
random_state=42
)
# Train
boost.fit(X_train, y_train)
Page | 1
# Predict
y_pred = boost.predict(X_test)
# Accuracy
print("Boosting Accuracy:", accuracy_score(y_test, y_pred))
Result:
The AdaBoost model was successfully trained and tested on the Iris dataset. The model combined
multiple weak learners to improve classification performance and achieved good accuracy.
Ex.No:8(c)
Date:
Aim:
Implement Ensembling Techniques - Stacking
To implement the Stacking Classifier by combining multiple base models and a meta model, and
evaluate its performance on the Iris dataset.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Split the dataset into training and testing data.
2. Train multiple base models (such as Decision Tree, SVM) using the training data.
3. Each base model makes predictions on the dataset.
4. Combine the predictions of base models to create a new dataset.
5. Train a meta-model (such as Logistic Regression) using these predictions.
6. The meta-model produces the final prediction by combining the outputs of base models.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Base models
estimators = [
('dt', DecisionTreeClassifier()),
('svm', SVC(probability=True))
]
Page | 1
# Meta model
meta_model = LogisticRegression()
# Stacking model
stack = StackingClassifier(
estimators=estimators,
final_estimator=meta_model
)
# Train
stack.fit(X_train, y_train)
# Predict
y_pred = stack.predict(X_test)
# Accuracy
print("Stacking Accuracy:", accuracy_score(y_test, y_pred))
Result:
The Stacking Classifier was successfully implemented using multiple base models and a meta
learner. The model achieved good classification accuracy by combining the strengths of individual
models.
Ex.No:9(a)
Date:
Aim:
Implement Clustering Algorithm - K-means Clustering
To implement the K-Means clustering algorithm and group the data points into clusters based on their
features.
Software Required:
Python IDLE 3.13.3
Algorithm:
1.Specify the number of k clusters to assign.
2.Randomly initialize k centroids.
3.Repeat:
4.Expectation: Assign each point to its closest centroid.
5.Maximization: Compute the new centroid (mean) of each cluster.
6.Until the centroids do not change.
Program:
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# Generate sample data
X, y = make_blobs(n_samples=200, centers=3, random_state=42)
# Apply K-Means
kmeans = KMeans(n_clusters=3, random_state=42)
y_kmeans = kmeans.fit_predict(X)
# Plot clusters
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans)
plt.title("K-Means Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
Result:
The K-Means clustering algorithm was successfully applied to the dataset. The data points were
grouped into clusters, and the results were visualized using a scatter plot, demonstrating effective
clustering.
Page | 1
Ex.No:9(b)
Date:
Aim:
Implement Clustering Algorithm - Gaussian Mixter Model (GMM)
Clustering
To implement the Gaussian Mixture Model (GMM) clustering algorithm and group data points based
on probability distribution.
Software Required:
Python IDLE 3.13.3
Algorithm:
1.Specify the number of clusters (k components).
2.Initialize the parameters of the Gaussian distributions (mean, covariance, and weight).
3.Repeat until convergence:
4.Expectation (E-step): Calculate the probability that each data point belongs to each Gaussian
component.
5.Maximization (M-step): Update the mean, covariance, and weight of each Gaussian using the
calculated probabilities.
6.Check convergence: Stop when the parameters or likelihood values stop changing significantly.
Program:
from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
# Generate sample data
X, y = make_blobs(n_samples=300, centers=3, cluster_std=1.5, random_state=42)
# Create GMM model
gmm = GaussianMixture(n_components=3, random_state=42)
# Fit model
gmm.fit(X)
# Predict cluster labels
labels = gmm.predict(X)
# Plot clusters
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.title("GMM Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
Page | 1
Ex.No:10
Date:
Aim:
Implement EM for Bayesian Networks
To implement the Expectation-Maximization (EM) algorithm for learning parameters in a Bayesian
Network using a Disease–Symptom model.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Initialize the parameters of the Bayesian network with random probability values.
2. Provide the observed data.
3. Repeat until convergence:
4. Expectation Step (E-step): Estimate the probability of hidden variables using current parameters.
5. Maximization Step (M-step): Update the parameters using the estimated probabilities.
6. Stop when the parameter values stabilize.
Program:
import numpy as np
# ----------------------------
# Step 1: Observed Data (Symptoms)
# ----------------------------
# 1 = symptom present, 0 = no symptom
S = np.array([1, 1, 0, 1, 0, 1, 0, 0, 1, 1])
N = len(S)
# ----------------------------
# Step 2: Initialize Parameters
# ----------------------------
# P(Disease = 1)
theta_D = 0.5
# Conditional probabilities
P_S_given_D = {
0: 0.3, # P(S=1 | D=0)
1: 0.8 # P(S=1 | D=1)
}
Page | 1
iterations = 10
# ----------------------------
# Step 3: EM Algorithm
# ----------------------------
for it in range(iterations):
print("\nIteration:", it + 1)
# ------------------------
# E-STEP
# ------------------------
gamma = np.zeros(N) # P(D=1 | S)
for i in range(N):
s = S[i]
# Likelihoods
if s == 1:
p_s_d1 = P_S_given_D[1]
p_s_d0 = P_S_given_D[0]
else:
p_s_d1 = 1 - P_S_given_D[1]
p_s_d0 = 1 - P_S_given_D[0]
# Bayes rule
numerator = theta_D * p_s_d1
denominator = numerator + (1 - theta_D) * p_s_d0
gamma[i] = numerator / denominator
# ------------------------
# M-STEP
# ------------------------
# Update P(D=1)
theta_D = np.mean(gamma)
# Update P(S=1 | D=1)
P_S_given_D[1] = np.sum(gamma * S) / np.sum(gamma)
# Update P(S=1 | D=0)
P_S_given_D[0] = np.sum((1 - gamma) * S) / np.sum(1 - gamma)
# ------------------------
# Print results
# ------------------------
print("P(Disease=1):", round(theta_D, 4))
print("P(S=1 | D=1):", round(P_S_given_D[1], 4))
print("P(S=1 | D=0):", round(P_S_given_D[0], 4))
print("\nFinal Parameters:")
print("P(Disease=1):", round(theta_D, 4))
print("P(S=1 | D=1):", round(P_S_given_D[1], 4))
print("P(S=1 | D=0):", round(P_S_given_D[0], 4))
Result:
The EM algorithm was successfully implemented to estimate the hidden parameters of the
Bayesian Network. The model iteratively updated the probabilities and converged to stable values,
effectively learning the relationship between disease and symptoms.
Ex.No:11
Date:
Aim:
Simple Neural Network (NN) Algorithm
To build and train a simple Neural Network model using backpropagation to solve the XOR
problem.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Initialize the training data and network structure (input, hidden, and output layers).
2. Initialize weights and biases randomly.
3. Repeat for a fixed number of epochs:
4. Forward propagation: Compute hidden layer and output layer values.
5. Compute error: Calculate the difference between actual and predicted output.
6. Backpropagation: Update weights and biases using the learning rate.
7. After training, generate predictions and evaluate the model performance.
Program:
#XOR problem
import numpy as np
# ----------------------------
# Activation Functions
# ----------------------------
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# ----------------------------
# Training Data (XOR Problem)
# ----------------------------
X = np.array([[0,0],
[0,1],
[1,0],
[1,1]])
Page | 1
y = np.array([[0],
[1],
[1],
[0]])
# ----------------------------
# Initialize Weights
# ----------------------------
np.random.seed(1)
input_layer_neurons = 2
hidden_layer_neurons = 2
output_neurons = 1
# Weights and Biases
W1 = np.random.uniform(size=(input_layer_neurons, hidden_layer_neurons))
b1 = np.random.uniform(size=(1, hidden_layer_neurons))
W2 = np.random.uniform(size=(hidden_layer_neurons, output_neurons))
b2 = np.random.uniform(size=(1, output_neurons))
learning_rate = 0.5
# ----------------------------
# Training Process
# ----------------------------
epochs = 10000
for epoch in range(epochs):
# Forward Propagation
hidden_input = np.dot(X, W1) + b1
hidden_output = sigmoid(hidden_input)
final_input = np.dot(hidden_output, W2) + b2
predicted_output = sigmoid(final_input)
# Error Calculation
error = y - predicted_output
# Backpropagation
d_output = error * sigmoid_derivative(predicted_output)
error_hidden = d_output.dot(W2.T)
d_hidden = error_hidden * sigmoid_derivative(hidden_output)
# Update Weights and Biases
W2 += hidden_output.T.dot(d_output) * learning_rate
b2 += np.sum(d_output, axis=0, keepdims=True) * learning_rate
W1 += X.T.dot(d_hidden) * learning_rate
b1 += np.sum(d_hidden, axis=0, keepdims=True) * learning_rate
# ----------------------------
# Output Results
# ----------------------------
print("Final Predicted Output:")
print(predicted_output)
binary_output = (predicted_output > 0.5).astype(int)
accuracy = np.mean(binary_output == y)
print("Accuracy:", accuracy)
TP = np.sum((binary_output == 1) & (y == 1))
TN = np.sum((binary_output == 0) & (y == 0))
FP = np.sum((binary_output == 1) & (y == 0))
FN = np.sum((binary_output == 0) & (y == 1))
precision = TP / (TP + FP + 1e-8)
recall = TP / (TP + FN + 1e-8)
f1_score = 2 * (precision * recall) / (precision + recall + 1e-8)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1_score)
print("Confusion Matrix:")
print("TP:", TP, "FP:", FP)
print("FN:", FN, "TN:", TN)
Result:
The Neural Network model was successfully trained using the XOR dataset. The model learned
the non-linear pattern and produced accurate predictions with good performance metrics such as
accuracy, precision, recall, and F1-score.
Ex.No:12
Date:
Aim:
Deep Learning Neural Network (DNN) Algorithm
To implement a Deep Neural Network (DNN) without using TensorFlow and perform classification
on the Iris dataset using forward propagation and backpropagation.
Software Required:
Python IDLE 3.13.3
Algorithm:
1. Load the dataset and preprocess the data.
2. Split the dataset into training and testing sets.
3. Build a deep neural network with multiple hidden layers.
4. Compile the model with optimizer, loss function, and metrics.
5. Train the model using the training dataset.
6. Predict outputs using the trained model.
7. Evaluate the model using accuracy and other performance metrics.
Program:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, precision_score,
recall_score, f1_score
# ----------------------------
# Activation Functions
# ----------------------------
def relu(x):
return np.maximum(0, x)
def relu_derivative(x):
return (x > 0).astype(float)
def softmax(x):
exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))
return exp_x / np.sum(exp_x, axis=1, keepdims=True)
Page | 1
# ----------------------------
# 1. Load Dataset
# ----------------------------
data = load_iris()
X = data.data
y = data.target
# ----------------------------
# 2. Preprocessing
# ----------------------------
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# One-hot encoding
def one_hot(y, num_classes):
return np.eye(num_classes)[y]
y_train_oh = one_hot(y_train, 3)
# ----------------------------
# 3. Initialize Weights
# ----------------------------
np.random.seed(42)
W1 = np.random.randn(4, 16)
b1 = np.zeros((1, 16))
W2 = np.random.randn(16, 12)
b2 = np.zeros((1, 12))
W3 = np.random.randn(12, 8)
b3 = np.zeros((1, 8))
W4 = np.random.randn(8, 3)
b4 = np.zeros((1, 3))
# ----------------------------
# 4. Training
# ----------------------------
learning_rate = 0.01
epochs = 200
for epoch in range(epochs):
# Forward Pass
z1 = np.dot(X_train, W1) + b1
a1 = relu(z1)
z2 = np.dot(a1, W2) + b2
a2 = relu(z2)
z3 = np.dot(a2, W3) + b3
a3 = relu(z3)
z4 = np.dot(a3, W4) + b4
y_pred = softmax(z4)
# Loss (Cross Entropy)
loss = -np.mean(np.sum(y_train_oh * np.log(y_pred + 1e-8), axis=1))
# Backpropagation
dz4 = y_pred - y_train_oh
dW4 = np.dot(a3.T, dz4)
db4 = np.sum(dz4, axis=0, keepdims=True)
da3 = np.dot(dz4, W4.T)
dz3 = da3 * relu_derivative(z3)
dW3 = np.dot(a2.T, dz3)
db3 = np.sum(dz3, axis=0, keepdims=True)
da2 = np.dot(dz3, W3.T)
dz2 = da2 * relu_derivative(z2)
dW2 = np.dot(a1.T, dz2)
db2 = np.sum(dz2, axis=0, keepdims=True)
da1 = np.dot(dz2, W2.T)
dz1 = da1 * relu_derivative(z1)
dW1 = np.dot(X_train.T, dz1)
db1 = np.sum(dz1, axis=0, keepdims=True)
# Update weights
W4 -= learning_rate * dW4
b4 -= learning_rate * db4
W3 -= learning_rate * dW3
b3 -= learning_rate * db3
W2 -= learning_rate * dW2
b2 -= learning_rate * db2
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
# Print loss
if epoch % 20 == 0:
print(f"Epoch {epoch}, Loss: {loss:.4f}")
# ----------------------------
# 5. Testing
# ----------------------------
z1 = np.dot(X_test, W1) + b1
a1 = relu(z1)
z2 = np.dot(a1, W2) + b2
a2 = relu(z2)
z3 = np.dot(a2, W3) + b3
a3 = relu(z3)
z4 = np.dot(a3, W4) + b4
y_pred_test = softmax(z4)
y_pred_classes = np.argmax(y_pred_test, axis=1)
# ----------------------------
# 6. Evaluation
# ----------------------------
accuracy = accuracy_score(y_test, y_pred_classes)
print("\nAccuracy:", accuracy)
cm = confusion_matrix(y_test, y_pred_classes)
print("\nConfusion Matrix:\n", cm)
precision = precision_score(y_test, y_pred_classes, average='macro')
recall = recall_score(y_test, y_pred_classes, average='macro')
f1 = f1_score(y_test, y_pred_classes, average='macro')
print("\nPrecision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
Result:
The Deep Neural Network was successfully implemented and trained on the Iris dataset. The
model achieved good classification accuracy and performance metrics such as precision, recall, and
F1-score, demonstrating effective learning without using deep learning frameworksEditor is loading...
Leave a Comment