Aiml Lab Manual-Civil-3-26
Aiml Lab Manual-Civil-3-26
SUPERVISED LEARNING
5 Implement the non-parametric locally weighted regression
algorithm in order to fit data points. Select appropriate data
set for your experiment and draw graphs
6 Write a program to demonstrate the working of the
decision tree-based algorithm.
7 Build an artificial neural network by implementing the
back propagation algorithm and test the same using
appropriate data sets.
8 Write a program to implement the naïve Bayesian
classifier.
UNSUPERVISED LEARNING
9 Implementing neural network using self-organizing maps
Aim:
To Implement Breadth First Search usingpython. Breadth-first search (BFS) is an algorithm for traversing
or searching tree or graph data structures..
Algorithm:
1. Start at the Initial Node: Begin by selecting an initial node to start the search.
2. Initialize Data Structures: Initialize a queue to keep track of the nodes to visit and a set or array to keep
track of visited nodes.
3. Enqueue the Initial Node: Add the initial node to the queue and mark it as visited.
5. Repeat Until Queue is Empty: Continue this process until the queue is empty, meaning all reachable
nodes have been visited.
6. Return Traversal Result: The order in which nodes are visited represents the breadth-first traversal of
the graph or tree.
Program:
while queue:
node = queue.popleft()
print(node, end=' ')
print("BFS Traversal:")
bfs(graph, 'A')
Output
BFS Traversal:
ABCDEF>
Result:
Thus the program for breadth first search is executed successfully and output is verified.
Ex.no:2
Date:
IMPLEMENT DEPTH FIRST SEARCH
Aim:
To Implement Depth First Search using python
Algorithm:
1. Start at the Initial Node: Begin by selecting an initial node to start the search.
2. Initialize Data Structures: Initialize a set or array to keep track of visited nodes.
3. Recursively Traverse: Implement a recursive function to traverse the graph or tree depth-first.
4. Visit the Current Node: Mark the current node as visited and process it. This may involve printing the
node, storing it in a list, or performing any other required operation.
5. Explore Unvisited Neighbors: For each unvisited neighbor of the current node, recursively call the
DFS function on that neighbor.
6. Base Case: Ensure there is a base case to terminate the recursion. This typically occurs when all
reachable nodes have been visited.
Program:
class Graph:
def __init__(self):
self.adj_list = {}
def dfs_util(node):
visited.add(node)
print(node, end=' ')
if node in self.adj_list:
for neighbor in self.adj_list[node]:
if neighbor not in visited:
dfs_util(neighbor)
dfs_util(start)
# Example usage:
graph = Graph()
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')
graph.add_edge('B', 'E')
graph.add_edge('C', 'F')
print("DFS Traversal:")
graph.dfs('A')
Output:
DFS Traversal:
ABDECF>
Result:
Thus, the program for depth first search is executed successfully and output is verified.
Ex.no:3
Date:
Both breadth-first search (BFS) and depth-first search (DFS) have different time and space
complexities, making them suitable for different types of problems and data structures. Here's an analysis of
BFS and DFS in terms of time and space complexities:
Comparison:
BFS typically requires more memory than DFS because it needs to store all vertices at the current level
in the queue, while DFS only needs to store the vertices along the current path.
BFS is generally preferred when finding the shortest path in unweighted graphs or trees, as it guarantees
the shortest path due to its level-by-level exploration.
DFS may be more memory-efficient and suitable for problems like topological sorting, cycle detection,
or traversing deeper into graphs or trees before backtracking.
In summary, BFS and DFS have different time and space complexities, making them suitable for different
types of problems and data structures. The choice between BFS and DFS depends on the specific requirements
of the problem and the characteristics of the graph or tree being traversed.
Ex.no:4
Date:
Aim:
Algorithm:
Program:
import heapq
while pq:
cost, node = heapq.heappop(pq)
if node == goal:
return cost
# Example usage:
graph = {
'A': [('B', 5), ('C', 3)],
'B': [('D', 6)],
'C': [('D', 2)],
'D': [('E', 4)],
'E': []
}
start = 'A'
goal = 'E'
Output:
A* Algorithms
Algorithm:
1. Initialize an empty priority queue pq, a dictionary cost_so_far, and a set visited.
2. Push the start node into pq with a priority of zero and a cost of zero.
3. While pq is not empty:
Pop the node with the lowest priority from pq.
If the node is the goal, return success.
Otherwise, mark the node as visited.
Expand the node by considering all its unvisited neighbors.
For each neighbor:
Calculate the total cost from the start node to the neighbor through the current node.
If the neighbor is not visited or the new cost is lower than the previously recorded cost:
Update the cost_so_far dictionary with the new cost.
Calculate the priority of the neighbor based on the total cost and a heuristic to the
goal.
Push the neighbor into pq with the calculated priority and update its cost.
4. If the priority queue is empty and the goal has not been found, return failure.
Program:
import heapq
while pq:
_, cost, node = heapq.heappop(pq) # Pop the node with the lowest priority
if node == goal:
return cost # Goal reached, return the cost
# Example usage:
graph = {
'A': [('B', 5), ('C', 3)],
'B': [('D', 6)],
'C': [('D', 2)],
'D': [('E', 4)],
'E': []
}
start = 'A'
goal = 'E'
Output:
Result:
Thus, the program to Implement and Compare Greedy and A* Algorithms is executed successfully
Ex.no:5
Date:
SUPERVISED LEARNING
IMPLEMENT THE NON-PARAMETRIC LOCALLY WEIGHTED REGRESSION ALGORITHM
IN ORDER TO FIT DATA POINTS. SELECT APPROPRIATE DATA SET FOR YOUR
EXPERIMENT AND DRAW GRAPHS
Aim
To Implement The Non-Parametric Locally Weighted Regression Algorithm In Order To Fit Data Points.
Select Appropriate Data Set for Your Experiment and Draw Graphs using python matplotlib.
Algorithm:
1. Define Data Structures: Define structures to represent nodes and the decision tree itself.
2. Read Data: Read the input data into your program. Each data point should have features and a label.
3. Splitting Criteria Selection: Implement functions to determine the best splitting criteria at each node.
Common criteria include Gini impurity, information gain, or entropy.
4. Build Tree: Write a recursive function to build the decision tree. At each step, you'll select the best
splitting criteria, split the data, and recursively call the function on the subsets.
5. Prediction: Implement a function to predict the label for new data points by traversing the decision tree.
Program:
import numpy as np
import matplotlib.pyplot as plt
for i in range(len(x_pred)):
weights = gaussian_kernel(x_pred[i], x_train, tau)
W = np.diag(weights)
X = np.vstack((np.ones(n), x_train)).T
theta = np.linalg.inv(X.T @ W @ X) @ (X.T @ W @ y_train)
y_pred[i] = np.dot(np.array([1, x_pred[i]]), theta)
return y_pred
Output:
Result:
Thus the Implement of the Non-Parametric Locally Weighted Regression Algorithm is executed in plot
diagram.
Ex.no:6
Date:
Aim:
Algorithm:
1. Load Dataset:
o Load a dataset containing features and corresponding labels.
2. Preprocess Data (if necessary):
o Handle missing values.
o Encode categorical variables.
o Normalize/standardize numerical features.
3. Split Dataset:
o Divide the dataset into training and testing sets.
4. Train Decision Tree Model:
o Create an instance of a decision tree classifier/regressor.
o Train the model on the training dataset.
5. Visualize Decision Tree (Optional):
o Visualize the trained decision tree to understand its structure and decision-making process.
6. Predictions:
o Use the trained decision tree model to make predictions on the testing dataset.
7. Evaluate Model Performance:
o Calculate relevant evaluation metrics (e.g., accuracy, precision, recall, F1-score for
classification, or mean squared error for regression) on the testing dataset.
8. Interpretation:
o Interpret the results and decision rules of the decision tree to gain insights into the dataset.
9. Tuning Parameters (Optional):
o Perform hyperparameter tuning (e.g., maximum depth, minimum samples per leaf) using
techniques like cross-validation to optimize the model's performance.
10. Repeat Steps 4-9 (if necessary):
o If the model's performance is not satisfactory, repeat steps 4 to 9 with different parameters or
feature engineering techniques.
11. Conclusion:
o Conclude by summarizing the model's performance and insights gained from the decision tree-
based algorithm.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Output:
Accuracy: 1.0
Result:
Thus, the above program for decision tree-based algorithm has been executed successfully
Ex.no:7
Date:
Aim:
To build an artificial neural network by implementing the back propagation algorithm and test the same
using appropriate data sets.
Algorithm:
Program:
import numpy as np
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# Example usage
X = np.array([[0,0], [0,1], [1,0], [1,1]])
y = np.array([[0], [1], [1], [0]])
input_size = 2
hidden_size = 4
output_size = 1
Output:
Result:
Thus the program to build an artificial neural network by implementing the back propagation algorithm and
test the same using appropriate data sets.
Ex.no:8
Date:
WRITE A PROGRAM TO IMPLEMENT THE NAÏVE BAYESIAN CLASSIFIER.
Aim:
To write a program to implement the naïve Bayesian classifier.
Algorithm:
1. Load the Dataset: We load the Iris dataset from scikit-learn's built-in datasets.
2. Split the Dataset: We split the dataset into training and testing sets using the train_test_split
function. This allows us to evaluate the performance of the model on unseen data.
3. Create the Naive Bayes Classifier: We create an instance of the GaussianNB class, which implements
the Naive Bayes classifier assuming Gaussian distribution of the features.
4. Train the Classifier: We train the Naive Bayes classifier on the training data using the fit method.
5. Make Predictions: We use the trained classifier to make predictions on the testing data using the
predict method.
6. Evaluate Accuracy: We calculate the accuracy of the model by comparing the predicted labels with the
true labels using the accuracy_score function.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split the dataset 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 Naive Bayes classifier
clf = GaussianNB()
# Train the classifier on the training data
clf.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = clf.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Output:
Accuracy: 0.9777777777777777
Result:
To write a program to implement the naïve Bayesian classifier.
Ex.no:9
Date:
UNSUPERVISED LEARNING
Aim:
To Implementing neural network using self-organizing maps.
Algorithm:
class SOM:
def __init__(self, input_size, output_size, learning_rate=0.1, sigma=1.0):
self.input_size = input_size
self.output_size = output_size
self.learning_rate = learning_rate
self.sigma = sigma
# Example usage
input_data = np.random.rand(100, 2) # Generate random input data
input_size = 2 # Dimensionality of input vectors
output_size = (10, 10) # Size of the SOM grid
epochs = 100 # Number of training epochs
Output:
[(5, 3), (0, 0), (6, 0), (4, 0), (2, 0), (7, 1), (3, 5), (9, 5), (3, 3), (7, 0), (5, 7), (2, 9), (2, 9), (2, 1), (5, 0), (9, 9),
(8, 3), (2, 9), (4, 3), (8, 7), (6, 3), (3, 1), (7, 1), (1, 0), (9, 6), (0, 3), (4, 0), (9, 5), (9, 6), (6, 7), (0, 9), (6, 1),
(2, 2), (6, 7), (3, 5), (0, 1), (9, 5), (4, 0), (1, 7), (6, 0), (6, 0), (1, 0), (5, 7), (7, 3), (9, 6), (2, 2), (2, 9), (2, 0),
(5, 5), (1, 0), (0, 4), (0, 1), (2, 9), (8, 3), (2, 7), (2, 1), (4, 5), (5, 1), (0, 0), (1, 8), (0, 1), (1, 6), (4, 9), (0, 8),
(6, 7), (0, 5), (2, 9), (0, 3), (6, 0), (2, 2), (7, 5), (1, 2), (0, 5), (0, 5), (3, 1), (5, 0), (5, 6), (0, 5), (3, 9), (9, 5),
(7, 5), (6, 7), (9, 6), (4, 5), (4, 5), (0, 4), (4, 5), (8, 3), (4, 5), (5, 5), (0, 0), (6, 8), (3, 8), (1, 9), (8, 8), (5, 3),
(9, 0), (9, 0), (6, 7), (1, 0)]
Result:
Thus the above program forImplementingneural networkusing self-organizing maps has been
executed successfully.
Ex.no:10
Date:
IMPLEMENTING K-MEANS ALGORITHM TO CLUSTER A SET OF DATA.
Aim:
To Implementing k-Means algorithm to cluster a set of data.
Algorithm:
1. Initialize Centroids:
o Randomly select k data points from the dataset to serve as initial centroids.
2. Assign Data Points to Clusters:
o For each data point in the dataset, calculate the Euclidean distance to each centroid.
o Assign each data point to the cluster corresponding to the nearest centroid.
3. Update Centroids:
o Calculate the mean of all data points assigned to each cluster.
o Update the centroids to be the mean values.
4. Repeat Steps 2-3:
o Iterate the process of assigning data points to clusters and updating centroids until convergence
or a maximum number of iterations is reached.
o Convergence can be determined by checking if the centroids no longer change significantly
between iterations.
5. Output:
o After convergence, the final centroids represent the centers of the clusters.
o Each data point is assigned to the cluster corresponding to the nearest centroid.
6. Optional: Choosing the Number of Clusters:
o In some cases, the number of clusters k may not be known beforehand.
o Various methods, such as the elbow method or silhouette score, can be used to select an optimal
value for k.
Program:
import numpy as np
class KMeans:
def __init__(self, n_clusters, max_iter=300):
self.n_clusters = n_clusters
self.max_iter = max_iter
for _ in range(self.max_iter):
# Assign each data point to the closest centroid
clusters = np.array([self.find_closest_centroid(x, centroids) for x in X])
# Update centroids
new_centroids = self.update_centroids(clusters, X)
centroids = new_centroids
self.centroids = centroids
self.clusters = clusters
# Example usage
X = np.array([[1, 2], [1, 3], [2, 2], [10, 8], [10, 10], [12, 8]])
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
print("Centroids:", kmeans.centroids)
print("Cluster assignments:", kmeans.clusters)
Output:
Centroids: [ 1.33333333 2.33333333]
[10.66666667 8.66666667]]
Cluster assignments: [0 0 0 1 1 1]
Result:
Thus, the above program to Implementing k-Means algorithm to cluster a set of data has been executed
successfully.
Ex.no:11
Date:
11. IMPLEMENTING HIERARCHICAL CLUSTERING ALGORITHM.
Aim:
To Implementing hierarchical clustering algorithm.
Algorithm:
1. Initialization:
o Start with each data point as a separate cluster.
2. Compute Pairwise Distances:
o Compute the distance matrix containing the pairwise distances between all data points.
3. Merge Clusters:
o Find the pair of clusters with the smallest distance between them.
o Merge these two clusters into a single cluster.
4. Update Distance Matrix:
o Update the distance matrix to reflect the distances between the new merged cluster and all other
clusters.
5. Repeat:
o Repeat steps 3-4 until only a single cluster remains, or until a specified number of clusters is
reached.
6. Output:
o The output of hierarchical clustering is typically represented as a dendrogram, a tree-like
structure that shows the order and distances at which clusters were merged.
PROGRAM:
import numpy as np
class HierarchicalClustering:
def __init__(self, n_clusters=None):
self.n_clusters = n_clusters
for i in range(len(clusters)):
for j in range(i+1, len(clusters)):
distance = self.complete_linkage_distance(X, clusters[i], clusters[j])
if distance <min_distance:
min_distance = distance
min_i, min_j = i, j
new_cluster = clusters[min_i].union(clusters[min_j])
del clusters[min_j]
clusters[min_i] = new_cluster
# Example usage
X = np.array([[1, 2], [1, 3], [2, 2], [10, 8], [10, 10], [12, 8]])
clustering = HierarchicalClustering(n_clusters=2)
clustering.fit(X)
print("Cluster assignments:", clustering.labels_)
OUTPUT:
Cluster assignments: [0 0 0 1 1 1]
RESULT:
Thus, the above program for Implementing hierarchical clustering algorithmhas been executed
successfully.