0% found this document useful (0 votes)
7 views32 pages

Python Search and ML Algorithms Guide

The document outlines a series of experiments involving the implementation of various algorithms including uninformed search algorithms (BFS, DFS), informed search algorithms (A*, memory-bounded A*), Naive Bayes models, Bayesian Networks, regression models, decision trees, SVM models, and clustering algorithms. Each experiment includes an aim, algorithm steps, and a Python program with outputs demonstrating successful implementation. The document serves as a comprehensive guide for implementing these algorithms in Python.

Uploaded by

farjasmf2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

Python Search and ML Algorithms Guide

The document outlines a series of experiments involving the implementation of various algorithms including uninformed search algorithms (BFS, DFS), informed search algorithms (A*, memory-bounded A*), Naive Bayes models, Bayesian Networks, regression models, decision trees, SVM models, and clustering algorithms. Each experiment includes an aim, algorithm steps, and a Python program with outputs demonstrating successful implementation. The document serves as a comprehensive guide for implementing these algorithms in Python.

Uploaded by

farjasmf2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LIST OF EXPERIMENTS

1. Implementation of Uninformed search algorithms (BFS, DFS)


2. Implementation of Informed search algorithms (A*, memory- bounded A*)
3. Implement naive Bayes models
4. Implement Bayesian Networks
5. Build Regression models
6. Build decision trees and random forests
7. Build SVM models
8. Implement ensembling techniques

9. Implement clustering algorithms


10. Implement EM for Bayesian networks
11. Build simple NN models

12. [Link] deep learning NN models


[Link].1a Implementation of Uninformed search algorithms (BFS, DFS)

Aim:
To write a Python program to implement Breadth First Search (BFS).

Algorithm:

Step 1. Start
Step 2. Put any one of the graph’s vertices at the back of the queue.
Step 3. Take the front item of the queue and add it to the visited list.
Step 4. Create a list of that vertex's adjacent nodes. Add those which are not within the
visited list to the rear of the queue.
Step 5. Continue steps 3 and 4 till the queue is empty.
Step 6. Stop

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = [] # List for visited nodes.


queue = [] #Initialize a queue

def bfs(visited, graph, node): #function for BFS


[Link](node)
[Link](node)

while queue: # Creating loop to visit each node


m = [Link](0)
print (m, end = " ")

for neighbour in graph[m]:


if neighbour not in visited:
[Link](neighbour)
[Link](neighbour)

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
Output:
Following is the Breadth-First Search
537248

Result:

Thus, the Python program to implement Breadth First Search (BFS) was developed
successfully.
[Link].1b Implementation of Uninformed search algorithms (BFS, DFS)

Aim:
To write a Python program to implement Depth First Search (DFS).

Algorithm:

Step [Link]
Step [Link] any one of the graph's vertex on top of the stack.
Step [Link] that take the top item of the stack and add it to the visited list of the vertex.
Step [Link], create a list of that adjacent node of the vertex. Add the ones which aren't in the
visited list of vertexes to the top of the stack.
Step [Link] steps 3 and 4 until the stack is empty.
Step [Link]

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): #function for dfs


if node not in visited:
print (node)
[Link](node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
Output:
Following is the Depth-First Search
5
3
2
4
8
7

Result:
Thus the Python program to implement Depth First Search (DFS) was developed
successfully.
[Link].2a Implementation of Informed search algorithms (A*, memory- bounded A*)

Aim:
To write a Python program to implement A* search algorithm.

Algorithm:

Step 1: Create a priority queue and push the starting node onto the [Link]
minimum value (min_index) to location 0.
Step 2: Create a set to store the visited nodes.
Step 3: Repeat the following steps until the queue is empty:
3.1 : Pop the node with the lowest cost + heuristic from the queue.
3.2 : If the current node is the goal, return the path to the goal.
3.3 : If the current node has already been visited, skip it.
3.4 : Mark the current node as visited.
: Expand the current node and add its neighbors to the queue.
Step 4: If the queue is empty and the goal has not been found, return None (no path found).
Step 5: Stop

Program:
import heapq

class Node:

def init (self, state, parent, cost, heuristic):


[Link] = state
[Link] = parent
[Link] = cost
[Link] =
heuristic

def lt (self, other):


return ([Link] + [Link]) < ([Link] + [Link])

def astar(start, goal, graph):


heap = []
[Link](heap, (0, Node(start, None, 0, 0)))
visited = set()

while heap:
(cost, current) = [Link](heap)

if [Link] == goal:
path = []
while current is not None:
[Link]([Link])
current = [Link]
# Return reversed path
return path[::-1]

if [Link] in visited:
continue

[Link]([Link])

for state, cost in graph[[Link]].items():


if state not in visited:
heuristic = 0 # replace with your heuristic function
[Link](heap, (cost, Node(state, current, [Link] + cost, heuristic)))

return None # No path found

graph = {
'A': {'B': 1, 'D': 3},
'B': {'A': 1, 'C': 2, 'D': 4},
'C': {'B': 2, 'D': 5, 'E': 2},
'D': {'A': 3, 'B': 4, 'C': 5, 'E': 3},
'E': {'C': 2, 'D': 3}
}
start = 'A'
goal = 'E'

result = astar(start, goal, graph)


print(result)

Output:
['A', 'B', 'C', 'E']

Result:
Thus the python program for A* Search was developed and the output was verified
successfully.
[Link].2b Implementation of Informed search algorithms (A*, memory- bounded A*)

Aim:
To write a Python program to implement memory- bounded A* search algorithm.

Algorithm:

Step 1: Create a priority queue and push the starting node onto the
queue. Step 2: Create a set to store the visited nodes.
Step 3: Set a counter to keep track of the number of nodes expanded.
Step 4: Repeat the following steps until the queue is empty or the node counter exceeds the
max_nodes:
4.1 : Pop the node with the lowest cost + heuristic from the queue.
4.2 : If the current node is the goal, return the path to the goal.
4.3 : If the current node has already been visited, skip it.
4.4 : Mark the current node as visited.
: Increment the node counter.
: Expand the current node and add its neighbors to the queue.
Step 5: If the queue is empty and the goal has not been found, return None (no path found).
Step 6: Stop

Program:
import heapq

class Node:

def init (self, state, parent, cost, heuristic):


[Link] = state
[Link] = parent
[Link] = cost
[Link] =
heuristic

def lt (self, other):


return ([Link] + [Link]) < ([Link] + [Link])

def astar(start, goal, graph, max_nodes):


heap = []
[Link](heap, (0, Node(start, None, 0, 0)))

visited = set()

node_counter = 0

while heap and node_counter < max_nodes:


(cost, current) = [Link](heap)

if [Link] == goal:
path = []
while current is not None:
[Link]([Link])
current = [Link]
return path[::-1]

if [Link] in visited:
continue

[Link]([Link])

node_counter += 1

for state, cost in graph[[Link]].items():


if state not in visited:
heuristic = 0
[Link](heap, (cost, Node(state, current, [Link] + cost, heuristic)))

return None

# Example usage

graph = {'A': {'B': 1, 'C': 4},


'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}}

start = 'A'
goal = 'D'
max_nodes = 10

result = astar(start, goal, graph, max_nodes)


print(result)

Output

['A', 'B', 'C', 'D']

Result:

Thus the python program for memory-bounded A* search was developed and the
output was verified successfully.
[Link].3 Implement Naive Bayes models

Aim:
To write a python program to implement Naïve Bayes model.

Algorithm:

Step 1. Load the libraries: import the required libraries such as pandas, numpy, and
sklearn.
Step 2. Load the data into a pandas dataframe.
Step 3. Clean and preprocess the data as necessary. For example, you can handle missing
values, convert categorical variables into numerical variables, and normalize the
data.
Step 4. Split the data into training and test sets using the train_test_split function from
scikit-learn.
Step 5. Train the Gaussian Naive Bayes model using the training data.
Step 6. Evaluate the performance of the model using the test data and the accuracy_score
function from scikit-learn.
Step 7. Finally, you can use the trained model to make predictions on new data.

Program:
!pip install scikit-learn matplotlib
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, classification_report
from [Link] import load_iris
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
data = [Link](data=[Link], columns=iris.feature_names)
data['target'] = [Link]

# Features for plotting


X = data[['sepal length (cm)', 'sepal width (cm)']]
y = data['target']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

# Create a Gaussian Naive Bayes classifier


model = GaussianNB()

# Train the model


[Link](X_train, y_train)

# Make predictions on the test set


y_pred = [Link](X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
print(classification_report(y_test, y_pred))

# Plotting
[Link](figsize=(8, 6))
[Link](X_test['sepal length (cm)'], X_test['sepal width (cm)'], c=y_pred, cmap='viridis',
marker='o', edgecolor='k')
[Link]('Sepal Length (cm)')
[Link]('Sepal Width (cm)')
[Link]('Naive Bayes Predictions on Iris Dataset')
[Link](label='Predicted Class')
[Link]()

Output:
Accuracy: 0.8421052631578947
precision recall f1-score support
0 1.00 1.00 1.00 15
1 0.78 0.64 0.70 11
2 0.71 0.83 0.77 12
accuracy 0.84 38
macro avg 0.83 0.82 0.82 38
weighted avg 0.85 0.84 0.84 38

Result:

Thus the Python program for implementing Naïve Bayes model was developed and
the output was verified successfully.
[Link].4 BAYESIAN NETWORKS

Aim:

To write a python program to implement a Bayesian network for Traffic.

Algorithm:

Step 1. Start by importing the required libraries such as math and pomegranate.
Step 2. Define the discrete probability distribution for the guest's initial choice of
door Step 3. Define the discrete probability distribution for the prize door
Step 4. Define the conditional probability table for the door that Monty picks based on the
guest's choice and the prize door
Step 5. Create State objects for the guest, prize, and Monty's choice
Step 6. Create a Bayesian Network object and add the states and edges between them
Step 7. Bake the network to prepare for inference
Step 8. Use the predict_proba method to calculate the beliefs for a given set of
evidence Step 9. Display the beliefs for each state as a string.
Step 10. Stop

Program:
!pip install pgmpy
from [Link] import BayesianNetwork
from [Link] import TabularCPD
from [Link] import VariableElimination
import networkx as nx
import [Link] as plt

# Define the Bayesian Network structure


model = BayesianNetwork([('Rain', 'Traffic'), ('Accident', 'Traffic')])

# Define Conditional Probability Distributions (CPDs)


cpd_rain = TabularCPD(variable='Rain', variable_card=2, values=[[0.7], [0.3]]) # Prior
probability of rain (70% no rain, 30% rain)
cpd_accident = TabularCPD(variable='Accident', variable_card=2, values=[[0.9], [0.1]]) # Prior
probability of accident (90% no accident, 10% accident)
cpd_traffic = TabularCPD(variable='Traffic', variable_card=2,
values=[[0.9, 0.6, 0.7, 0.1], # [P(T=No|R=No, A=No), P(T=No|R=No, A=Yes),
P(T=No|R=Yes, A=No), P(T=No|R=Yes, A=Yes)]
[0.1, 0.4, 0.3, 0.9]], # [P(T=Yes|R=No, A=No), P(T=Yes|R=No, A=Yes),
P(T=Yes|R=Yes, A=No), P(T=Yes|R=Yes, A=Yes)]
evidence=['Rain', 'Accident'], evidence_card=[2, 2]) # Traffic depends on rain and
accident
# Add CPDs to the model
model.add_cpds(cpd_rain, cpd_accident, cpd_traffic)

# Check if the model is valid


model.check_model()

# Perform inference
infer = VariableElimination(model)

# Calculate the probability of traffic given rain


posterior_traffic_rain = [Link](variables=['Traffic'], evidence={'Rain': 1}) # Evidence: Rain
= Yes
print(posterior_traffic_rain)

# Plotting the Bayesian Network


pos = nx.circular_layout(model)
[Link](model, pos=pos, with_labels=True, node_size=3000, node_color="skyblue",
font_size=12, arrowsize=20)
[Link]("Bayesian Network for Traffic")
[Link]()

Output:
+------------+----------------+
| Traffic | phi(Traffic) |
+============+================+
| Traffic(0) | 0.6400 |
+------------+----------------+
| Traffic(1) | 0.3600 |
+------------+----------------+
Result:

Thus, the Python program for implementing Bayesian Networks was successfully
developed and the output was verified.
[Link].5 REGRESSION MODEL

Aim:
To write a Python program to build Regression models

Algorithm:

Step 1. Import necessary libraries: numpy, pandas, [Link],


LinearRegression, mean_squared_error, and r2_score.
Step 2. Create a numpy array for waist and weight values and store them in
separate variables.
Step 3. Create a pandas DataFrame with waist and weight columns using the numpy
arrays. Step 4. Extract input (X) and output (y) variables from the DataFrame.
Step 5. Create an instance of LinearRegression model.
Step 6. Fit the LinearRegression model to the input and output
variables. Step 7. Create a new DataFrame with a single value of
waist.
Step 8. Use the predict() method of the LinearRegression model to predict the weight for the
new waist value.
Step 9. Calculate the mean squared error and R-squared values using mean_squared_error()
and r2_score() functions respectively.
Step 10. Plot the actual and predicted values using [Link]() and
[Link]() functions.

Program:
!pip install scikit-learn matplotlib
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
from [Link] import fetch_california_housing

# Load the California housing dataset


housing = fetch_california_housing()
data = [Link](data=[Link], columns=housing.feature_names)
data['target'] = [Link]

# Select features and target variable


X = data[['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude',
'Longitude']]
y = data['target']

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the Linear Regression model


model = LinearRegression()
[Link](X_train, y_train)
# Make predictions on the test set
y_pred = [Link](X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse}")


print(f"R-squared: {r2}")

# Create a scatter plot of actual vs. predicted values


[Link](figsize=(8, 6))
[Link](y_test, y_pred, alpha=0.5) # Alpha for transparency
[Link]([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2) # Diagonal line
[Link]('Actual Values')
[Link]('Predicted Values')
[Link]('Actual vs. Predicted Values (Linear Regression)')
[Link]()

Output:
Mean Squared Error: 0.5558915986952444
R-squared: 0.5757877060324508

Result:

Thus the Python program to build a simple linear Regression model was developed
successfully.
[Link].6 DECISION TREE AND RANDOM FOREST

Aim:
To write a Python program to build decision tree and random forest.

Algorithm:

Step 1. Import necessary libraries: numpy, matplotlib, seaborn, pandas, train_test_split,


LabelEncoder, DecisionTreeClassifier, plot_tree, and RandomForestClassifier.
Step 2. Read the data from '[Link]' into a pandas DataFrame.
Step 3. Extract the features into an array X, and the target variable into an array y.
Step 4. Encode the target variable using the LabelEncoder.
Step 5. Split the data into training and testing sets using train_test_split function.
Step 6. Create a DecisionTreeClassifier object, fit the model to the training data, and visualize
the decision tree using plot_tree.
Step 7. Create a RandomForestClassifier object with 100 estimators, fit the model to the
training data, and visualize the random forest by displaying 6 trees.
Step 8. Print the accuracy of the decision tree and random forest models using the score
method on the test data.

Program:
!pip install scikit-learn matplotlib
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier, plot_tree
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, classification_report
from [Link] import load_iris
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
data = [Link](data=[Link], columns=iris.feature_names)
data['target'] = [Link]

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split([Link]('target', axis=1), data['target'],
test_size=0.3, random_state=42)

# Decision Tree
dt_classifier = DecisionTreeClassifier(random_state=42)
dt_classifier.fit(X_train, y_train)

# Random Forest
rf_classifier = RandomForestClassifier(random_state=42)
rf_classifier.fit(X_train, y_train)

# Predictions
dt_predictions = dt_classifier.predict(X_test)
rf_predictions = rf_classifier.predict(X_test)

# Evaluate Decision Tree


print("Decision Tree Accuracy:", accuracy_score(y_test, dt_predictions))
print(classification_report(y_test, dt_predictions))

# Evaluate Random Forest


print("\nRandom Forest Accuracy:", accuracy_score(y_test, rf_predictions))
print(classification_report(y_test, rf_predictions))

# Plot Decision Tree


[Link](figsize=(12, 8))
plot_tree(dt_classifier, feature_names=iris.feature_names, class_names=iris.target_names,
filled=True, rounded=True)
[Link]()

Output:
Decision Tree Accuracy: 1.0
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

Random Forest Accuracy: 1.0


precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
Result:
Thus the Python program to build decision tree and random forest was developed
successfully.
[Link].7 SVM MODELS

Aim:
To write a Python program to build SVM model.

Algorithm:

Step [Link] the necessary libraries ([Link], numpy, and svm from sklearn).
Step [Link] the features (X) and labels (y) for the fruit dataset.
Step [Link] an SVM classifier with a linear kernel using [Link](kernel='linear').
Step [Link] the classifier on the fruit data using [Link](X, y).
Step [Link] the fruits and decision boundary using [Link](X[:, 0], X[:, 1], c=colors),
where colors is a list of colors assigned to each fruit based on its label.
Step [Link] a meshgrid to evaluate the decision function using
[Link]([Link](xlim[0], xlim[1], 100), [Link](ylim[0], ylim[1], 100)).
Step [Link] the decision function to create a contour plot of the decision boundary and
margins using [Link](xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--']).
Step [Link] the plot using [Link]().

Program:
import [Link] as plt
import numpy as np
from sklearn import svm

# Define the fruit features (size and color)


X = [Link]([[5, 2], [4, 3], [1, 7], [2, 6], [5, 5], [7, 1], [6, 2], [5, 3], [3, 6], [2, 7], [6, 3], [3, 3],
[1, 5], [7, 3], [6, 5], [2, 5], [3, 2], [7, 5], [1, 3], [4, 2]])

# Define the fruit labels (0=apples, 1=oranges)


y = [Link]([0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0])
colors = ['red' if label == 0 else 'yellow' for label in y]
[Link](X[:, 0], X[:, 1], c=colors)
ax = [Link]()
ax.set_xlabel('Size')
ax.set_ylabel('Color')
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# Assuming 'clf' is your trained SVM model (from previous code)


# Create a meshgrid to evaluate the decision function
xx, yy = [Link]([Link](xlim[0], xlim[1], 100), [Link](ylim[0], ylim[1], 100))
Z = clf.decision_function(np.c_[[Link](), [Link]()])

# Reshape Z based on number of classes if it has multiple columns


if [Link] > 1 and [Link][1] > 1:
Z = Z[:, 0].reshape([Link]) # Select the first column (class 0) and reshape
else:
Z = [Link]([Link])
# Plot the decision boundary and margins
[Link](xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])

# Display the plot


[Link]()

Output:

Result:
Thus, the Python program to build an SVM model was developed, and the output was
successfully verified.
[Link].8 Implement Ensembling techniques

Aim:

To implement the ensembling technique of Blending with the given Alcohol QCM
Dataset.

Algorithm:

1. Split the training dataset into train, test and validation dataset.
2. Fit all the base models using train dataset.
3. Make predictions on validation and test dataset.
4. These predictions are used as features to build a second level model
5. This model is used to make predictions on test and meta-features.

Program:
!pip install scikit-learn matplotlib
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import accuracy_score
from [Link] import load_iris
import [Link] as plt

# Load the Iris dataset


iris = load_iris()
X = [Link][:, :2] # Use only two features for
visualization
y = [Link]

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)

# Create a Random Forest classifier


rf_classifier = RandomForestClassifier(n_estimators=100,
random_state=42) # 100 trees in the forest

# Train the model


rf_classifier.fit(X_train, y_train)

# Make predictions on the test set


y_pred = rf_classifier.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

# Plot the decision boundary


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, 0.1),
[Link](y_min, y_max, 0.1))
Z = rf_classifier.predict(np.c_[[Link](), [Link]()])
Z = [Link]([Link])
!pip install scikit-learn matplotlib
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import accuracy_score
from [Link] import load_iris
import [Link] as plt
import numpy as np

# Load the Iris dataset


iris = load_iris()
X = [Link][:, :2] # Use only two features for
visualization
y = [Link]

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)

# Create a Random Forest classifier


rf_classifier = RandomForestClassifier(n_estimators=100,
random_state=42) # 100 trees in the forest

# Train the model


rf_classifier.fit(X_train, y_train)

# Make predictions on the test set


y_pred = rf_classifier.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

# Plot the decision boundary


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, 0.1),
[Link](y_min, y_max, 0.1))
Z = rf_classifier.predict(np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.8, cmap=[Link])


[Link](X[:, 0], X[:, 1], c=y, edgecolors='k', s=20)
[Link]()
Output:
Accuracy: 0.7555555555555555

Result:
Thus the program to implement ensembling technique have been executed successfully
and the output got verfied.

[Link].9 Implement clustering algorithms

Aim:
To implment K Mean Clustering algorithm to classify the Iris Dataset.

Algorithm:
Step-1: Select the number K of the neighbors
Step-2: Calculate the Euclidean distance of K number of neighbors
Step-3: Take the K nearest neighbors as per the calculated Euclidean distance. Step-4:
Among these k neighbors, count the number of the data points in each category.
Step-5: Assign the new data points to that category for which the number of the
neighbor is maximum.
Step-6: Our model is ready.
Program:
!pip install scikit-learn matplotlib
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import KMeans
from [Link] import load_iris
from [Link] import StandardScaler

# Load the Iris dataset


iris = load_iris()
X = [Link] # Features
y = [Link] # Target (true labels)
# Standardize the features (important for K-Means)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Create a KMeans model with 3 clusters (for Iris dataset)


kmeans = KMeans(n_clusters=3, random_state=42)
# Fit the model to the scaled data
[Link](X_scaled)

# Get cluster assignments for each data point


labels = kmeans.labels_

# Visualize the clusters


[Link](X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis')
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200, c='red',
marker='X', label='Centroids')
[Link]('Sepal Length (Standardized)')
[Link]('Sepal Width (Standardized)')
[Link]('K-Means Clustering of Iris Dataset')
[Link]()
[Link]()
Output:

Result:
Thus the program to implement K-Means clustering Algorithm for Iris dataset have
been executed successfully and output got verified.

[Link].10 Implement EM for Bayesian Networks

Aim:
To write a python program to implement EM for Bayesian Networks.

Algorithm:

1. Start with a Bayesian network that has some variables that are not directly observed.
For example, suppose we have a Bayesian network with variables A, B, C, and D,
where A and D are observed and B and C are latent.
2. Initialize the parameters of the network. This includes the conditional
probabilities for each variable given its parents, as well as the prior
probabilities for the root variables.
3. E-step: Compute the expected sufficient statistics for the latent variables. This
involves computing the posterior probability distribution over the latent variables
given the observed data and the current parameter estimates. This can be done
using the forward-backward algorithm or the belief propagation algorithm.
4. M-step: Update the parameter estimates using the expected sufficient statistics
computed in step 3. This involves maximizing the likelihood of the data with
respect to the parameters of the network, given the expected sufficient statistics.
5. Repeat steps 3-4 until convergence. Convergence can be measured by
monitoring the change in the log-likelihood of the data, or by monitoring the
change in the parameter estimates.

Program :
!pip install pgmpy
from [Link] import BayesianNetwork
from [Link] import ExpectationMaximization as EM
from [Link] import get_example_model
import networkx as nx
import [Link] as plt

# Load the 'asia' dataset


model = get_example_model('asia')
data = [Link](n_samples=1000)

# Learn parameters using EM


estimator = EM(model, data)
estimated_cpds = estimator.get_parameters()

# Reset the model's CPDs and add the estimated ones


[Link] = []
for cpd in estimated_cpds:
model.add_cpds(cpd)

# Plotting the Bayesian Network


pos = nx.circular_layout(model)
[Link](model, pos=pos, with_labels=True, node_size=3000, node_color="skyblue",
font_size=12, arrowsize=20)
[Link]("Bayesian Network Learned using EM (Asia Dataset)")
[Link]()

Output:
Result:
Thus the Python program to Implement EM for Bayesian Networks was
developed successfully.
[Link].11 Build Simple NN Models

Aim:

To write a python program to build simple NN models.

Algorithm:

1. Define the input and output data.


2. Choose the number of layers and neurons in each layer. This depends on the
problem you are trying to solve.
3. Define the activation function for each layer. Common choices are ReĮU, sigmoid,
and tanh.
4. Initialize the weights and biases for each neuron in the network. This can be
done randomly or using a pre-trained model.
5. Define the loss function and optimizer to be used during training. The loss
function measures how well the model is doing, while the optimizer updates the
weights and biases to minimize the loss.
6. Train the model on the input data using the defined loss function and optimizer. This
involves forward propagation to compute the output of the model, and
backpropagation to compute the gradients of the loss with respect to the weights and
biases. The optimizer then updates the weights and biases based on the gradients.
7. Evaluate the performance of the model on new data using metrics such as
accuracy, precision, recall, and F1 score.

Program:
!pip install tensorflow keras matplotlib
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
import [Link] as plt
import numpy as np

# Generate some sample data


X = [Link](1000, 10) # 1000 samples with 10 features
y = [Link](0, 2, size=(1000,)) # Binary classification (0 or 1)

# Create a simple neural network model


model = [Link]([
[Link](32, activation='relu', input_shape=(10,)),
[Link](16, activation='relu'),
[Link](1, activation='sigmoid') # Output layer for binary classification
])

# Compile the model


[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model
history = [Link](X, y, epochs=10, validation_split=0.2)

# Plot the training history


[Link]([Link]['accuracy'], label='accuracy')
[Link]([Link]['val_accuracy'], label='val_accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link](loc='lower right')
[Link]('Training and Validation Accuracy')
[Link]()

Output:

Result:
Thus the Python program to build simple NN Models was developed
successfully.
[Link].12 Deep Learning NN Models

Aim:

To write a python program to implement deep learning of NN models.

Algorithm:

1. Import the necessary libraries, such as numpy and keras.


2. Įoad or generate your dataset. This can be done using numpy or any other data
manipulation library.
3. Preprocess your data by performing any necessary normalization, scaling, or other
transformations.
4. Define your neural network architecture using the Keras Sequential API. Add
layers to the model using the add() method, specifying the number of units,
activation function, and input dimensions for each layer.
5. Compile your model using the compile() method. Specify the loss function,
optimizer, and any evaluation metrics you want to use.
6. Train your model using the fit() method. Specify the training data, validation
data, batch size, and number of epochs.
7. Evaluate your model using the evaluate() method. This will give you the loss and
accuracy metrics on the test set.
8. Use your trained model to make predictions on new data using the predict()
method.

Program:
!pip install tensorflow keras matplotlib
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
import [Link] as plt
import numpy as np

# 1. Generate sample data (replace with your actual data)


X = [Link](1000, 10)
y = [Link](0, 2, size=(1000,))

# 2. Build a simple deep NN model (fewer layers and units)


model = [Link]([
[Link](32, activation='relu', input_shape=(10,)),
[Link](16, activation='relu'),
[Link](1, activation='sigmoid')
])

# 3. Compile the model


[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# 4. Train the model


history = [Link](X, y, epochs=10, validation_split=0.2)

# 5. Plot the training history


[Link]([Link]['accuracy'], label='accuracy')
[Link]([Link]['val_accuracy'], label='val_accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link]('Training and Validation Accuracy')
[Link]()
[Link]()

Output:

Result:
Thus the Python program to implement deep learning of NN Models was developed
successfully.

Common questions

Powered by AI

Naive Bayes assumes independence between features given the target variable and is primarily used for classification tasks due to its simplicity and effectiveness. In contrast, Bayesian Networks model conditional dependencies between variables, offering more flexibility and applicability in domains where such dependencies are critical, such as diagnostics and probabilistic reasoning .

Choosing a high value for K in K-Means can lead to overfitting where clusters may segment even small fluctuations in the data, resulting in a loss of generalizability. It can also increase computational cost without significant improvement in clustering quality. Finding the optimal K typically requires balancing cluster accuracy with computational efficiency and scalability .

Ensemble techniques like Random Forest improve prediction accuracy by aggregating the predictions from multiple decision trees, which reduces the variance associated with individual trees. This aggregation mitigates overfitting to the training data and enhances generalization by averaging out biases and errors from individual models .

SVM models are advantageous for classification as they are effective in high-dimensional spaces and can handle cases where the number of dimensions exceeds the number of samples. They also use kernel functions, allowing flexibility in decision surface shapes. However, SVMs can be computationally intensive and less effective on very large datasets, and they may also suffer in scenarios with significant noise in the data .

Using real datasets like Iris provides practical insights into the model's ability to generalize from training to real-world data. Evaluation metrics such as accuracy score, precision, recall, and F1-score are used to assess classification performance, with accuracy measuring overall correctness and other metrics providing detail on specific class performance .

A* search improves over uninformed methods by using heuristics to efficiently find the least-cost path to a goal. Unlike BFS, which expands all nodes at a uniform depth before any nodes at the next depth, A* uses a priority queue to prioritize paths based on the sum of path cost and heuristic value, allowing it to potentially find the shortest path more quickly. It also generally expands fewer nodes than DFS, which blindly follows a path until it ends .

The Expectation-Maximization (EM) algorithm is used to find maximum likelihood estimates of parameters in Bayesian Networks when the model involves latent variables. EM iteratively applies the expectation step to estimate missing data and the maximization step to update the parameters, facilitating model fitting when not all variables are directly observed .

Feature scaling is crucial for K-Means clustering because it uses Euclidean distance to assign data points to clusters. If features are on different scales, the clustering output can be biased towards features with larger scales. Scaling ensures equal weighting of features and prevents distortion of distances in multidimensional space .

The efficiency of BFS can be evaluated by measuring time complexity, which is O(V + E) where V is the number of vertices and E is the number of edges in the graph. Additionally, memory usage can be assessed, as BFS stores all generated nodes in memory, leading to high memory consumption for large graphs .

A memory-bounded A* search is beneficial when working with large graphs or state spaces where memory resources are limited. It limits the number of nodes retained in memory, which is particularly advantageous in resource-constrained environments or when solving pathfinding problems where the state space might quickly exceed available memory .

You might also like