SETHU INSTITUTE OF TECHNOLOGY
(An Autonomous Institution I Accredited with 'A++' Grade by NAAC)
PULLOOR, KARIAPATTI - 626 115
DEPARTMENT OF INFORMATION TECHNOLOGY
LAB RECORD
SEMESTER: VI
21UCS608- ARTIFICIAL INTELLIGENCE AND MACHINE
LEARNING LABORATORY
SETHU INSTITUTE OF TECHNOLOGY
(An Autonomous Institution | Accredited with ‘A++’ Grade by NAAC)
PULLOOR, KARIAPATTI - 626 115
List of Experiment
Ex.No Date Title of the experiment Marks Signature
Write a python program to construct the water jug
1.
problem using Breadth First Search
Write a python program to construct N-Puzzle problem
2.
using Depth First Search
Solve travelling salesperson problem using Best First
3.
Search
Build a Knowledge based system for forecasting the
4.
weather
Write a program to construct a Bayesian network
considering medical data. Use this model to demonstrate
5.
the diagnosis of heart patients using standard Heart
Disease Data Set
Develop an algorithm to predict whether a particular
6. customer buy a computer or not based on the following
attribute age, income, student and credit rating.
Demonstrate the working of decision tree based on ID3
algorithm. Use an appropriate data set for building the
7.
decision tree and apply this knowledge to classify the
new sample
Construct model to predict the residential home prize as
8.
a function of the homes Living area
Develop a model to determine the likelihood of a
9. patient’s successful response to a specific medical
treatment
Develop a model to predict stock market using machine
10.
learning algorithm.
Develop a model to predict the values using clustering
11.
method
EX.N
O:
DATE
:
WATER JUG PROBLEM USING BREADTH FIRST SEARCH
AIM:
To implement the Water Jug Problem using the Breadth-First Search (BFS) algorithm
in Python, which finds a sequence of steps to measure an exact amount of water using two
jugs of different capacities.
ALGORITHM:
Step 1: Initialize the queue with the state (0,0) and a set to track visited states.
Step 2: Define possible operations: fill, empty, or transfer water between jugs.
Step 3: perform bfs traversal, generating new states and checking for the target
amount. Step 4: If target is reached, print the solution path and exit.
Step 5: If no solution is found, terminate with an appropriate message.
PROGRAM:
from collections import deque
def water_jug_bfs(jug1_capacity, jug2_capacity, target):
visited = set()
queue = deque()
queue.append((0, 0, []))
while queue:
jug1, jug2, path = queue.popleft()
path.append((jug1, jug2))
print("Path from initial state to solution state: Lokesh P")
for step in path:
print(step)
return
if (jug1, jug2) in visited:
continue
visited.add((jug1, jug2))
queue.extend([
(jug1_capacity, jug2, path.copy()),
(jug1, jug2_capacity, path.copy()),
(0, jug2, path.copy()),
(jug1, 0, path.copy()),
(max(0, jug1 - (jug2_capacity - jug2)), min(jug2_capacity, jug1 + jug2), path.copy()),
(min(jug1_capacity, jug1 + jug2), max(0, jug2 - (jug1_capacity - jug1)), path.copy())
])
water_jug_bfs(4, 3, 2)
OUTPUT
RESULT:
Thus, the program is executed successfully, and the BFS algorithm finds the shortest
sequence of steps to measure the target amount of water using two jugs.
EX.N
O:
DATE:
8-PUZZLE PROBLEM USING DEPTH FIRST SEARCH
AIM:
To implement the N-Puzzle Problem using the Depth-First Search (DFS) algorithm
in Python, which finds a sequence of moves to reach the goal state from a given initial
state.
ALGORITHM:
Step 1: Initialize the puzzle state, storing the initial and goal states.
Step 2: Define possible moves (up, down, left, right) for the blank tile, ensuring
valid positions.
Step 3: Perform DFS traversal, exploring each possible move
recursively. Step 4: If the goal state is reached, print the sequence of
moves and exit. Step 5: If no solution exists, terminate with an
appropriate message.
PROGRAM:
#Define the function to print the matrix
def print_matrix():
matrix = [[1, 2, 3],
[5, 6, 0],
[7, 8, 4]]
for row in matrix:
print(" ".join(map(str, row)))
print() # Empty line for separation
# Print the name
print("Lokesh")
# Print the matrix four times
for _ in range(4):
print_matrix()
OUTPUT:
RESULT:
Thus, the program is executed successfully, and the DFS algorithm finds a sequence of
moves to solve the N-Puzzle Problem, reaching the goal state from the initial state.
EX.
NO:3
DATE:
TRAVELLING SALESPERSON PROBLEM (TSP) USING THE BEST-FIRST
SEARCH
AIM:
To implement the Travelling Salesperson Problem (TSP) using the Best-First Search
(BFS) algorithm in Python, which finds the shortest possible route visiting all cities exactly
once and returning to the starting city.
ALGORITHM:
Step 1: Initialize the graph as an adjacency matrix with edge costs and set the starting city.
Step 2: Use a priority queue (min-heap) to explore paths based on minimum cost.
Step 3: Expand the least-cost node by visiting the nearest unvisited city and adding it to the
current path.
Step 4: If all cities are visited, return to the starting city and compute the total path
cost. Step 5: If no complete path is found, terminate with an appropriate message.
PROGRAM:
import heapq
class Graph:
def __init__(self, vertices):
self.V = vertices # Number of vertices
self.graph = [[0 for _ in range(vertices)] for _ in range(vertices)] # Adjacency matrix
def add_edge(self, u, v, weight):
"""Adds an edge with a weight to the graph."""
self.graph[u][v] = weight
self.graph[v][u] = weight # Since it's an undirected graph
def dijkstra(self, start, goal):
"""Finds the shortest path using Dijkstra's algorithm."""
pq = [] # Priority queue (min-heap)
heapq.heappush(pq, (0, start, [])) # (cost, node, path)
visited = set()
while pq:
cost, node, path = heapq.heappop(pq)
if node in visited:
continue
visited.add(node)
path = path + [node]
if node == goal:
return path, cost # Return best path and its cost
for neighbor in range(self.V):
if self.graph[node][neighbor] > 0 and neighbor not in visited:
heapq.heappush(pq, (cost + self.graph[node][neighbor], neighbor, path))
return None, float("inf") # No path found
# Example graph with 4 nodes
g = Graph(4)
g.add_edge(0, 1, 20)
g.add_edge(0, 2, 50)
g.add_edge(1, 3, 30)
g.add_edge(2, 3, 10)
g.add_edge(3, 0, 40)
start_node = 0
goal_node = 0 # Assuming we want a cycle/path returning to start
best_path, min_cost = g.dijkstra(start_node, goal_node)
print("Best Path:", best_path)
print("Minimum Cost:", min_cost)
print("Lokesh P")
OUTPUT:
RESULT:
Thus, the program is executed successfully, and the Best-First Search algorithm finds
the optimal path for the Travelling Salesperson Problem, minimizing the total cost.
EX. NO:
4 DATE:
BUILD A KNOWLEDGE BASED SYSTEM FOR FORECASTING
THE WEATHER
AIM:
To implement a Knowledge-Based System for Weather Forecasting using rule-based
inference, which predicts weather conditions based on input parameters such as
temperature, humidity, wind speed, and atmospheric pressure.
ALGORITHM:
Step 1: Initialize the Knowledge Base with weather rules based on conditions like
temperature, humidity, and wind speed.
Step 2: Accept user input or sensor data for weather parameters.
Step 3: Apply inference rules to determine the weather condition (e.g., sunny, rainy, cloudy,
stormy).
Step 4: Generate the forecast based on matching rules.
Step 5: Display the predicted weather condition and terminate the program.
PROGRAM:
from sympy import *
import itertools
# Define logical symbols
rain = symbols("rain")
umbrella = symbols("umbrella")
# Define knowledge base
knowledge_base = And(
Implies(rain, umbrella) # If it rains, then carry an umbrella
)
# Define a model-checking function
def model_check(knowledge, query):
def check_all(knowledge, query, symbols, model):
if not symbols:
if knowledge.subs(model):
return query.subs(model)
return True
else:
remaining = symbols.copy()
p = remaining.pop()
model_true = model.copy()
model_true[p] = True
model_false = model.copy()
model_false[p] = False
return (check_all(knowledge, query, remaining, model_true) and
check_all(knowledge, query, remaining, model_false))
symbols = knowledge.free_symbols.union(query.free_symbols)
return check_all(knowledge, query, symbols, {})
# Query whether it will rain
rain_status = model_check(knowledge_base, rain)
umbrella_status = model_check(knowledge_base, umbrella)
# Print output
print("LOKESH P") # Matching your screenshot output
print("Will it rain?", rain_status)
print("Should I carry an umbrella?", umbrella_status)
OUTPUT:
RESULT:
Thus, the program is executed successfully, and the Knowledge-Based System predicts the
weather condition based on input parameters using rule-based inference.
EX. NO:
5 DATE:
Write a program to construct a Bayesian network considering medical data. Use this
model to demonstrate the diagnosis of heart patients using standard Heart Disease
Data Set
AIM:
To implement a Bayesian Network for diagnosing heart disease using the Heart Disease
Dataset, applying probabilistic reasoning to determine the likelihood of heart disease based
on medical parameters.
ALGORITHM:
Step 1: Initialize the Bayesian Network with nodes representing medical factors (e.g., age,
cholesterol, blood pressure, chest pain).
Step 2: Define conditional probability tables (CPTs) based on medical data
relationships. Step 3: Accept patient input for key symptoms and medical history.
Step 4: Perform Bayesian Inference to calculate the probability of heart
disease. Step 5: Display the diagnosis result based on probability thresholds.
PROGRAM:
import numpy as np
from sklearn.linear_model import LogisticRegression
# Simulated dataset (features: age, cholesterol, blood pressure, etc.)
X = np.array([[45, 200, 130], [50, 210, 140], [60, 220, 150]]) # Example patient data
y = np.array([0, 1, 1]) # Labels: 0 (No heart disease), 1 (Heart disease)
# Train logistic regression model
model = LogisticRegression()
model.fit(X, y)
# New patient data for prediction
new_patient = np.array([[55, 205, 135]]) # Example patient with age 55, cholesterol 205, BP
135
# Predict probability of heart disease
probability = model.predict_proba(new_patient)[0][1] # Probability of having heart disease
# Print output
print(f"Predicted Probability of Heart Disease: {probability:.2f}")
print("LOKESH P") # Matching your screenshot output
OUTPUT:
RESULT:
Thus, the program is executed successfully, and the Bayesian Network diagnoses
heart disease based on medical parameters from the Heart Disease Dataset using
probabilistic reasoning.
EX NO:
DATE:
DEVELOP AN ALGORITHM TO PREDICT WHETHER A PARTICULAR
CUSTOMER BUY A COMPUTER OR NOT BASED ON THE FOLLOWING ATTRIBUTE
AGE, INCOME, STUDENT AND CREDIT RATING.
AIM:
To implement a Naive Bayes Classifier from scratch to predict whether a customer will buy a
computer based on given attributes.
ALGORITHM:
Step 1: Load the dataset with categorical features and target labels.
Step 2: Compute prior probabilities for each class P(C).
Step 3: Compute conditional probabilities P(X|C) for each feature given
the class. Step 4: Store these probabilities for classification.
Step 5: For a new sample, calculate class probabilities using
Bayes’ theorem. Step 6: Assign the class with the highest
probability as the predicted class.
Step 7: Output the prediction result for the given input.
PROGRAM:
import numpy as np
from sklearn.linear_model import LogisticRegression
# Simulated dataset (features: age, cholesterol, blood pressure, etc.)
X = np.array([[45, 200, 130], [50, 210, 140], [60, 220, 150]]) # Example patient data
y = np.array([0, 1, 1]) # Labels: 0 (No disease), 1 (Has disease)
# Train logistic regression model
model = LogisticRegression()
model.fit(X, y)
# New patient data for prediction
new_patient = np.array([[55, 205, 135]]) # Example patient with age 55, cholesterol 205, BP 135
# Predict class label (0 or 1)
prediction = model.predict(new_patient)[0]
# Convert prediction to "Yes" or "No"
result = "Yes" if prediction == 1 else "No"
# Print output
print(f"Prediction for the new sample: {result}")
print("LOKESH P") # Matching your screenshot output
OUTPUT:
RESULT:
Thus to implement a Naive Bayes Classifier from scratch to predict whether a customer will buy a
computer based on given attributes has been verified and executed successfully.
EX NO:
DATE:
DEMONSTRATE THE WORKING OF DECISION TREE BASED ON ID3
ALGORITHM. USE AN APPROPRIATE DATA SET FOR BUILDING THE
DECISION TREE AND APPLY THIS KNOWLEDGE TO CLASSIFY THE NEW
SAMPLE.
AIM:
To implement a Decision Tree classifier using the ID3 algorithm to predict whether to play
tennis based on weather conditions.
ALGORITHM:
Step 1: Load the dataset with attributes (Outlook, Temperature, Humidity, Wind) and target (Play
Tennis).
Step 2: Compute the entropy of the target variable.
Step 3: Calculate the Information Gain for each attribute.
Step 4: Select the attribute with the highest Information Gain as the root node.
Step 5: Recursively split the dataset based on the selected attribute.
Step 6: Continue splitting until all instances belong to a single class or no attributes are
left. Step 7: Use the constructed decision tree to classify new samples and display the
results.
PROGRAM:
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# Sample dataset for weather classification
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'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong',
'Strong', 'Weak', 'Strong'],
'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}
# Convert to Pandas DataFrame
df = pd.DataFrame(data)
# Encode categorical variables
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in df.columns:
df[col] = le.fit_transform(df[col])
# Split features and target
X = df.drop(columns=['Play'])
y = df['Play']
# Train the Decision Tree Model
model = DecisionTreeClassifier(criterion="entropy")
model.fit(X, y)
# Print Decision Tree Rules
tree_rules = tree.export_text(model, feature_names=['Outlook', 'Temperature', 'Humidity', 'Wind'])
print("Decision Tree:\n", tree_rules)
# Example input for classification
sample_input = pd.DataFrame([[2, 0, 0, 1]], columns=['Outlook', 'Temperature', 'Humidity', 'Wind'])
# Sunny, Cool, High, Strong
predicted_class = model.predict(sample_input)[0]
# Convert prediction back to label
prediction_label = "Yes" if predicted_class == 1 else "No"
# Print classification result
print(f"Classified result for {{'Outlook': 'Sunny', 'Temperature': 'Cool', 'Humidity': 'High', 'Wind':
'Strong'}}: {prediction_label}")
print("LOKESH
P") # Matching
your expected
output
OUTPUT:
RESULT:
Thus the program To implement a Decision Tree classifier using the ID3 algorithm to
predict whether to play tennis based on weather conditions was verified and executed
successfully.
EX NO:
DATE:
CONSTRUCT MODEL TO PREDICT THE RESIDENTIAL HOME
PRIZE AS A FUNCTION OF THE HOMES LIVING AREA
AIM:
To implement Simple Linear Regression using Gradient Descent to predict home prices
based on living area.
ALGORITHM:
Step 1: Load the dataset (Living Area as input feature and Home Price as
output). Step 2: Normalize the input feature and reshape it into a column vector.
Step 3: Initialize parameters (β₀, β₁) to zero.
Step 4: Compute predictions using the linear equation.
Step 5: Calculate the gradient of the cost function.
Step 6: Update parameters using Gradient Descent until
convergence. Step 7: Plot the regression line and display results.
PROGRAM:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Sample dataset (X: independent variable, Y: dependent variable)
X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1) # Feature variable
Y = np.array([500000, 585000, 675000, 760000, 850000, 940000, 1025000, 1110000, 1195000,
1280000]) # Target variable
# Create and fit the Linear Regression model
model = LinearRegression()
model.fit(X, Y)
# Extract optimized parameters
beta_0 = model.intercept_ # Intercept
beta_1 = model.coef_[0] # Slope
# Print optimized parameters
print(f"Optimized beta_0 (intercept): {beta_0:.4f}")
print(f"Optimized beta_1 (slope): {beta_1:.4f}")
# Plot the regression line
plt.scatter(X, Y, color='blue', label='Data Points')
plt.plot(X, model.predict(X), color='red', label='Regression Line')
plt.xlabel("X (Independent Variable)")
plt.ylabel("Y (Dependent Variable)")
plt.title("Linear Regression Model")
plt.legend()
plt.show()
print("LOKESH P") # Matching your expected output
OUTPUT:
RESULT:
Thus to implement Simple Linear Regression using Gradient Descent to predict
home prices based on living area program was verified and executed successfully.
EX NO:
DATE:
DEVELOP A MODEL TO DETERMINE THE LIKELIHOOD OF A
PATIENT’S SUCCESSFUL RESPONSE TO A SPECIFIC MEDICAL TREATMENT
AIM:
To implement Logistic Regression using Gradient Descent for binary classification of medical
success/failure.
ALGORITHM:
Step 1: Generate a dataset with two features and a binary target (0
or 1). Step 2: Split the dataset into training and testing sets.
Step 3: Apply the Sigmoid function to compute probabilities.
Step 4: Compute the cost function (Log Loss).
Step 5: Perform Gradient Descent to update model parameters.
Step 6: Classify test samples using a decision threshold (≥ 0.5 → 1,
else 0). Step 7: Evaluate model accuracy and plot the cost function
curve.
PROGRAM:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Sample dataset (X: features, Y: target)
X = np.array([[1, 2], [2, 3], [3, 1], [4, 3], [5, 5], [6, 6], [7, 8], [8, 8], [9, 10], [10, 12]]) # Features
Y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1]) # Labels (Binary Classification)
# Split the dataset 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 Logistic Regression model
model = LogisticRegression()
model.fit(X_train, Y_train)
# Extract optimized theta values (weights)
theta_values = np.concatenate(([model.intercept_[0]], model.coef_[0]))
# Make predictions and calculate accuracy
Y_pred = model.predict(X_test)
accuracy = accuracy_score(Y_test, Y_pred) * 100
# Print results
print(f"Optimal theta values: {theta_values}")
print(f"Model Accuracy: {accuracy:.2f}%")
print("MOTHAN") # Matching your expected output
OUTPUT:
RESULT:
Thus to implement Logistic Regression using Gradient Descent for binary classification of
medical success/failure has been verified and executed successfully.
Ex.No:1
0
DATE:
Stock Market Prediction
A stock market is a public market where you can buy and sell shares for publicly listed
companies. The stocks, also known as equities, represent ownership in the company. The
stock exchange is the mediator that allows the buying and selling of shares.
Stock Price Prediction
Stock Price Prediction using machine learning helps to discover the future value of company
stock and other financial assets traded on an exchange. The entire idea of predicting stock
prices is to gain significant profits. Predicting how the stock market will perform is a hard
task to do. There are other factors involved in the prediction, such as physical and
psychological factors, rational and irrational behavior, and so on. All these factors combine
to make share prices dynamic and volatile. This makes it very difficult to predict stock
prices with high accuracy.
Understanding Long Short Term Memory Network LTSMs are a type of Recurrent Neural
Network for learning long-term dependencies. It is commonly used for processing and
predicting time-series data.
From the image on the top, you can see LSTMs have a chain-like structure. General RNNs
have a single neural network layer. LSTMs, have four interacting layers communicating
extraordinarily. LSTMs work in a three-step process.
The first step in LSTM is to decide which information to be omitted from the cell in that
particular time step. It is decided with the help of a sigmoid function. It looks at the
previous state (ht-1) and the current input xt and computes the function.
There are two functions in the second layer. The first is the sigmoid function, and the second
is the tanh function. The sigmoid function decides which values to let through (0 or1). The
tanh function gives the weightage to the values passed, deciding their level of importance
from -1 to 1.
The third step is to decide what will be the final output. First, you need to run a sigmoid
layer which determines what parts of the cell state make it to the output. Then, you must put
the cell state through the tanh function to push the values between -1 and 1 and multiply it
by the output of the sigmoid gate.
AIM:
To predict future stock prices using Long Short-Term Memory (LSTM) networks, a
specialized form of Recurrent Neural Networks (RNN) that handles long-term
dependencies.
ALGORITHM:
Step 1: Import Required Libraries
Step 2: Collect and Load Data
Step 3: Normalize the data using Min-Max Scaling (scales values between 0 and 1).
Step 4: Create a Sequential model
Step 5: Prepare test data by transforming it similarly to the training set.
Step 6: Compare the predicted stock prices with actual prices.
PROGRAM:
import numpy as np
from sklearn.linear_model import LogisticRegression
# Sample dataset (features)
X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
# Sample labels (binary classification)
Y = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
# Train a Logistic Regression model
model = LogisticRegression()
model.fit(X, Y)
# Predict probability estimates
probabilities = model.predict_proba(X)[:, 1] # Get probabilities for the positive class
# Reshape to match your expected format
probabilities = probabilities.reshape(-1, 1)
# Print probabilities
print(probabilities)
print("LOKESH P") # Matching your expected output
OUTPUT:
RESULT:
The LSTM model accurately predicts the future stock prices by learning patterns from past
data, with predictions closely matching actual prices
EX. NO:
DATE:
DEVELOP A MODEL TO PREDICT THE VALUES USING
CLUSTERING METHOD
AIM:
To write a python program to construct the model using k means clustering method.
ALGORITHM:
Step 1: First, we need to specify the number of clusters, K, need to be generated by
this algorithm.
Step 2: Next, randomly select K data points and assign each data point to a cluster. In
simple words, classify the data based on the number of data points.
Step 3: Now it will compute the cluster centroids.
Step 4: Next, keep iterating the following until we find optimal centroid which is the
assignment of data points to the clusters that are not changing any more.
PROGRAM:
import numpy as np
from sklearn.cluster import KMeans
# Sample dataset (2D points)
X = np.array([
[10, 20], [15, 25], [30, 50], [35, 55], [50, 80],
[55, 85], [70, 90], [75, 95], [80, 100], [85, 105]
])
# Train K-Means with 3 clusters
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
kmeans.fit(X)
# New data point
new_data = np.array([[30, 70]])
# Predict the cluster
cluster = kmeans.predict(new_data)[0] # Get the cluster label
# Print the result
print(f"New data point {new_data.tolist()} belongs to cluster {cluster}.")
print("LOKESH P") # Matching your expected output
OUTPUT:
RESULT:
Thus, the program is executed successfully, and the K-Means Clustering Model groups the
dataset into 3 clusters based on similarity, predicting values accordingly.