0% found this document useful (0 votes)
20 views56 pages

Aiml Lab Manual - Not Aligned

The document outlines various experiments related to search algorithms and problem-solving using Python. It includes implementations of Breadth-First Search (BFS), Depth-First Search (DFS), the N-Queens problem, and propositional model checking algorithms. Each experiment details the aim, procedure, program code, and results, demonstrating successful execution and verification of the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views56 pages

Aiml Lab Manual - Not Aligned

The document outlines various experiments related to search algorithms and problem-solving using Python. It includes implementations of Breadth-First Search (BFS), Depth-First Search (DFS), the N-Queens problem, and propositional model checking algorithms. Each experiment details the aim, procedure, program code, and results, demonstrating successful execution and verification of the algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

AI and ML Lab Manual

EXPTNO :1(a) Implementation of Uninformed search algorithms -Breadth-First

Search(BFS)

Aim: To implements the simple uniformed search algorithm breadth first search methods

using python

Procedure:

1. Start by putting any one of the graph’s vertices at the back of the queue.

2. Now take the front item of the queue and add it to the visited list.

3. Create a list of that vertex's adjacent nodes. Add those which are not within the visited list

to the rear of the queue.

4. Keep continuing steps two and three till the queue is empty.

Program:

from collections import deque

class Graph:

def __init__(self, directed=True):

self.edges = {}

self.directed = directed

defadd_edge(self, node1, node2, reversed=False):

try:

neighbors = self.edges[node1]

exceptKeyError:

neighbors = set()

neighbors.add(node2)

self.edges[node1] = neighbors
if not self.directed and not reversed:

self.add_edge(node2, node1, True)

def neighbors(self, node):

try:

returnself.edges[node]

exceptKeyError:

return []

defbreadth_first_search(self, start, goal):

found = False

fringe = deque([start])

visited = set([start])

came_from = {start: None}

print('{:11s} | {}'.format('Expand Node', 'Fringe'))

print(' ')

print('{:11s} | {}'.format('-', start))

while not found and len(fringe):

current = fringe.pop()

print('{:11s}'.format(current), end=' | ')

if current == goal:

found = True

break
for node in self.neighbors(current):

if node not in visited:

visited.add(node)

fringe.appendleft(node)

came_from[node] = current

print(', '.join(fringe))

if found:

print()

returncame_from

else:

print('No path from {} to {}'.format(start, goal))

return None

@staticmethod

defprint_path(came_from, goal):

parent = came_from[goal]

if parent:

Graph.print_path(came_from, parent)

else:

print(goal, end='')

return

print(' =>', goal, end='')

def __str__(self):
returnstr(self.edges)

# Create the graph

graph = Graph(directed=False)

graph.add_edge('A', 'B')

graph.add_edge('A', 'S')

graph.add_edge('S', 'G')

graph.add_edge('S', 'C')

graph.add_edge('C', 'F')

graph.add_edge('G', 'F')

graph.add_edge('C', 'D')

graph.add_edge('C', 'E')

graph.add_edge('E', 'H')

graph.add_edge('G', 'H')

# Perform BFS

start, goal = 'A', 'H'

traced_path = graph.breadth_first_search(start, goal)

# Print the path if found

iftraced_path:

print('Path:', end=' ')

Graph.print_path(traced_path, goal)

print()

Output:
Expand Node | Fringe

- |A
A | S, B
B |S
S | G, C
C | D, E, F, G
G | H, D, E, F
F | H, D, E
E | H, D
D |H
H |
Path: A => S => G => H

Result:

Thus, the program for breadth first search was executed and output is verified.
EXPTNO :1(b) Implementation of Uninformed search algorithms -Depth-First

Search(DFS)

Aim: To implements the simple uniformed search algorithm Depth first search methods using

python

Procedure:

1. Start by putting any one of the graph's vertex on top of the stack.

2. After that take the top item of the stack and add it to the visited list of the vertex.

3. Next, 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.

4. Lastly, keep repeating steps 2 and 3 until the stack is empty.

Program:

from collections import deque

class Graph:

def __init__(self, directed=True):

self.edges = {}

self.directed = directed

defadd_edge(self, node1, node2, reversed=False):

try:

neighbors = self.edges[node1]

exceptKeyError:

neighbors = set()

neighbors.add(node2)

self.edges[node1] = neighbors
if not self.directed and not reversed:

self.add_edge(node2, node1, True)

def neighbors(self, node):

try:

returnself.edges[node]

exceptKeyError:

return []

defbreadth_first_search(self, start, goal):

found = False

fringe = deque([start])

visited = set([start])

came_from = {start: None}

print('{:11s} | {}'.format('Expand Node', 'Fringe'))

print(' ')

print('{:11s} | {}'.format('-', start))

while not found and len(fringe):

current = fringe.pop()

print('{:11s}'.format(current), end=' | ')

if current == goal:

found = True

break
for node in self.neighbors(current):

if node not in visited:

visited.add(node)

fringe.appendleft(node)

came_from[node] = current

print(', '.join(fringe))

if found:

print()

returncame_from

else:

print('No path from {} to {}'.format(start, goal))

return None

@staticmethod

defprint_path(came_from, goal):

parent = came_from[goal]

if parent:

Graph.print_path(came_from, parent)

else:

print(goal, end='')

return

print(' =>', goal, end='')

def __str__(self):

returnstr(self.edges)
# Create the graph

graph = Graph(directed=False)

graph.add_edge('A', 'B')

graph.add_edge('A', 'S')

graph.add_edge('S', 'G')

graph.add_edge('S', 'C')

graph.add_edge('C', 'F')

graph.add_edge('G', 'F')

graph.add_edge('C', 'D')

graph.add_edge('C', 'E')

graph.add_edge('E', 'H')

graph.add_edge('G', 'H')

# Perform BFS

start, goal = 'A', 'H'

traced_path = graph.breadth_first_search(start, goal)

# Print the path if found

iftraced_path:

print('Path:', end=' ')

Graph.print_path(traced_path, goal)

print()

Output:
Depth-First Search:
Expand Node | Fringe

- | A
A | S, B
B | S
S | G, C
C | G, D, E, F
F | G, D, E
E | G, D, H
H |
Path: A => S => C => E => H

Result:

Thus, the program for Depth first search was executed and output is verified
EXPTNO :2 Implementation of N-Queen Problem

AIM:

To Implement the task is to place N queens on an N×N chessboard in such a way that none of

the queens is under attack. Implement this N Queen problem using Python.

Procedure:

1. The code starts by asking the user to enter a number.

2. It then creates an NxN matrix with all elements set to 0.

3. The code then defines two functions: attack and N_queens.

4. The function attack checks vertically and horizontally, while the function N_queens checks

diagonally.

5. If either of these functions return true, it means that there is a queen in that position on the

board.

6. The code is a function that will check if there are enough queens on the chessboard.

7. The code starts by defining a function, N_queens (n), which will return true if there are

enough queens and False otherwise.

8. The variable n is used to define how many queens need to be placed on the board for it to be

considered complete.

Program:

print("Enter the number of queens")

N = int(input())

# Create a chessboard (NxN matrix) with all elements set to 0

board = [[0]*N for _ in range(N)]


def attack(i, j):

# Checking vertically and horizontally

for k in range(0, N):

if board[i][k] == 1 or board[k][j] == 1:

return True

# Checking diagonally

for k in range(0, N):

for l in range(0, N):

if (k + l == i + j) or (k - l == i - j):

if board[k][l] == 1:

return True

return False

defN_queens(n):

if n == 0:

return True

fori in range(0, N):

for j in range(0, N):

if (not attack(i, j)) and (board[i][j] != 1):

board[i][j] = 1

ifN_queens(n - 1) == True:

return True

board[i][j] = 0

return False

N_queens(N)

fori in board:
print(i)

output:

Enter the number of queens: 8

[1, 0, 0, 0, 0, 0, 0, 0]

[0, 0, 0, 0, 1, 0, 0, 0]

[0, 0, 0, 0, 0, 0, 0, 1]

[0, 0, 0, 0, 0, 1, 0, 0]

[0, 0, 1, 0, 0, 0, 0, 0]

[0, 0, 0, 0, 0, 0, 1, 0]

[0, 1, 0, 0, 0, 0, 0, 0]

[0, 0, 0, 1, 0, 0, 0, 0]

Result:

Thus the program to implement N queens search strategy is implemented and executed

successfully.
EXPTNO :3 Implementation of Propositional model

DATE

AIM:

To Construct and implement propositional model checking algorithms using python

Procedure:

1. Define a class Literal with attributes name and sign to denote whether the

literal is positive or negative.

2. Implement the __neg__ function to return a new literal with the same name

but the opposite sign of its parent literal.

3. Implement the __repr__ function to return the string of the literal name (or

the string with a negative sign) each time the instance of the literal is called.

4. Create the CNFConvert function to convert the knowledge base (KB) from a

list of sets to a list of lists for easier computing.

5. Create the VariableSet function to find all the used literals in the KB,

assisting with running the DPLL algorithm.

6. Implement the Negativeofx function to hold the negative form of the literal

for use in the DPLL algorithm.

7. Create the pickX function to pick a literal from the variable set and work

with it as a node in the tree.

8. Define the functions splitfalseliterals() and splitTrueLiteral().

9. Create the dpll() function to perform the DPLL algorithm recursively.

10. Finally, call the function to execute the code.

Code:
import re

class Literal:

# Class Literal, it has attributes name and sign to denote whether the literal is positive or

negative in use

def __init__(self, name, sign=True):

self.name = str(name)

self.sign = sign

def __neg__(self): # Returns a new literal with the same name but the opposite sign of its parent

literal

return Literal(self.name, False)

def __str__(self):

returnstr(self.name)

def __repr__(self):

# Returns the string of the literal name (or the string with a negative sign) each time the

instance of the literal is called

ifself.sign:

return '%r' % str(self.__str__())

else:

return '%r' % str("-" + self.__str__())

defCNFconvert(KB):

# This function converts the KB from a list of sets to a list of lists for easier computing

storage = []
fori in KB:

i = list(i)

for j in i:

j = str(j)

storage.append(i)

return storage

defVariableSet(KB):

# This function finds all the used literals in the KB, and in order to assist with running the

DPLL

KB = eval((CNFconvert(KB).__str__()))

storage = []

forobj in KB:

for item in obj:

if item[0] == '-' and item[1:] not in storage:

storage.append(str(item[1:]))

elif item not in storage and item[0] != '-':

storage.append(str(item))

return storage

defNegativeofx(x):

# This function is for holding the negative form of the literal, for use in the DPLL algorithm

check = re.match("-", str(x))

if check:

returnstr(x[1:])

else:

return "-" + str(x)


defpickX(literals, varList):

# This function picks a literal from the variable set and works with it as a node in the tree

for x in varList:

if x not in literals:

break

return x

defsplitFalseLiterals(cnf, x):

holder = []

for item in cnf:

if x in item:

item.remove(x)

holder.append(item)

return holder

defsplitTrueLiteral(cnf, x):

holder = []

for item in cnf:

if x in item:

continue

else:

holder.append(item)

return holder

defunitResolution(clauses):

literalholder = {} # Dictionary for holding the literal holder and their bool
i=0

# This part of the code goes through each and every clause until all literals in the KB are

resolved

whilei<len(clauses): # For each clause

newClauses = []

clause = clauses[i]

# Picks a clause to work on

iflen(clause) == 1:

literal = str(clause[0])

pattern = re.match("-", literal)

# Populates the dictionary

if pattern:

nx = literal[1:]

literalholder[nx] = False

else:

nx = "-" + literal

literalholder[literal] = True

# Checks for all other appearances of the literal or its opposite in the KB

for item in clauses:

if item != clauses[i]:

ifnx in item:

item.remove(nx)

newClauses.append(item)

i=0

clauses = newClauses

# No unit clause

else:
i += 1

returnliteralholder, clauses

defdpll(clauses, varList):

# Recursively performs the DPLL algorithm

literals, cnf = unitResolution(clauses)

ifcnf == []:

return literals

elif [] in cnf:

return "notsatisfiable"

else:

# Pick a literal which isn't set yet but has an impact on the KB, and then work on it

recursively

while True:

x = pickX(literals, varList)

x = str(x)

nx = Negativeofx(x)

ncnf = splitTrueLiteral(cnf, x)

ncnf = splitFalseLiterals(ncnf, nx)

ifncnf == cnf:

varList.remove(x)

else:

break

# Does the same DPLL recursively, but follows the true path for that variable

case1 = dpll(ncnf, varList)

if case1 != "notsatisfiable":

copy = case1.copy()
copy.update(literals)

copy.update({x: True})

return copy

# Does the DPLL recursively, but follows the false path for that variable

case1 = dpll(ncnf, varList)

if case1:

copy = case1.copy()

copy.update(literals)

copy.update({x: False})

return copy

else:

return "notsatisfiable"

def DPLL(KB):

# Finally restructures the output to fit the required output by the assignment description

KB = eval((CNFconvert(KB).__str__()))

varList = VariableSet(KB)

result = dpll(KB, varList)

if result == 'notsatisfiable':

return False

else:

fori in varList:

ifi in result and result[i] == True:

result[i] = 'true'

elifi in result and result[i] == False:

result[i] = 'false'

else:
result[i] = 'free'

return [True, result]

A = Literal('A')

B = Literal('B')

C = Literal('C')

D = Literal('D')

KB = [{A, B}, {A, -C}, {-A, B, D}]

print(DPLL(KB))

OUTPUT:

[True, {'A': 'true', 'B': True, 'C': 'free', 'D': 'free'}]

Result:

Thus the program to implement Propositional Model checking Algorithm is implemented and

executed successfully.
EXPTNO :4 Implementation of ChatbotModel

AIM:

To create a chatbot that can assist university students with their common queries

relatedto campus resources,course information, schedules, and general student life

Procedure:

1. Install Python: Ensure Python 3.6+ is installed.

2. Install Gradio: Run pip install gradio in your terminal.

3. Copy Code: Save the provided chatbot code into a file, e.g., mkce_chatbot.py.

4. Run the File: Execute the file using python mkce_chatbot.py.

5. Access URL: Open the displayed URL (e.g., http://127.0.0.1:7860) in a web browser.

6. Use Chatbot: Enter queries in the chat interface and view responses.

7. Reset Chat: Use the "Reset" tab to clear the chat history.

Code (Sample program)

pip install gradio

importgradio as gr

fromdifflib import get_close_matches

# Predefined responses for common queries

responses = {

"library hours": "The library is open from 8 AM to 8 PM on weekdays and 10 AM to 4 PM on

weekends.",

"course information": "You can find detailed course information on the college website or

contact the department office.",

"exam schedule": "The exam schedule will be uploaded to the college portal. Please check
your login dashboard.",

"clubs and events": "We have various clubs such as Robotics, Coding, and Cultural Clubs.

Upcoming events are posted on the college notice board.",

"hostel information": "Hostel facilities are available for both boys and girls. Contact the hostel

office for more details.",

"canteen menu": "Today's menu: Breakfast - Idli, Pongal; Lunch - Rice, Sambar, Poriyal;

Dinner - Chapati, Kurma."

# Chat history to enable multi-turn conversations

chat_history = []

defchatbot(user_query):

globalchat_history

user_query = user_query.lower()

# Fuzzy matching to find the closest predefined response

closest_match = get_close_matches(user_query, responses.keys(), n=1, cutoff=0.5)

ifclosest_match:

bot_response = responses[closest_match[0]]

else:

bot_response = "I'm sorry, I don't understand that. Please contact the administration office for

assistance."

# Update chat history

chat_history.append(f"You: {user_query}")

chat_history.append(f"Bot: {bot_response}")
# Return the complete chat history as a single string

return "\n".join(chat_history)

# Reset chat history function

defreset_chat():

globalchat_history

chat_history = []

return "Chat history has been cleared. How can I assist you?"

# Gradio interface

interface = gr.Interface(

fn=chatbot,

inputs="text",

outputs="text",

title="M. Kumarasamy College Chatbot",

description="This chatbot can assist you with library hours, course information, exam schedules,

hostel details, canteen menu, and more. Type your query below!",

examples=[

["What are the library hours?"],

["Tell me about hostel facilities."],

["What's on the canteen menu today?"]

],

live=True,

allow_flagging="never"

)
# Add a Reset button to clear chat history

reset_interface = gr.Interface(

fn=reset_chat,

inputs=None,

outputs="text",

live=True,

title="Reset Chat"

# Launch both interfaces in parallel

gr.TabbedInterface([interface, reset_interface], ["Chat", "Reset"]).launch()

output:

Chat Interface

Title:

M. Kumarasamy College Chatbot

Description:

This chatbot can assist you with library hours, course information, exam schedules, hostel

details, canteen menu, and more. Type your query below!

Examples (Buttons):

"What are the library hours?"

"Tell me about hostel facilities."

"What's on the canteen menu today?"

User Input:

What are the library hours?

Bot Response:
You: what are the library hours?

Bot: The library is open from 8 AM to 8 PM on weekdays and 10 AM to 4 PM on weekends.

Result:

The chatbot successfully answers predefined queries related to university resources like library

hours, hostel details, and canteen menus, using fuzzy matching for user inputs. It also allows

users to reset the chat history for a fresh start.


EXPTNO :5(a) Implementation of NaïveBayesusing Navie classifier methods

AIM:

To construct and implement Naviebayes using Navie classifier methods using Python.

Algorithm

1. The code starts by loading the iris dataset.

2. The data is then split into a training and test set of equal size.

3. Next, a Gaussian Naive Bayes classifier is trained using the training set.

4. Then predictions are made on the test set with accuracy scores calculated for each prediction.

5. Finally, a confusion matrix is created to show how well each prediction was classified as

correct or incorrect

6. The code is used to train a Gaussian Naive Bayes classifier and then use it to make

predictions.

7. The code prints the model's predictions, as well as the test set's output for comparison.

Program:

fromsklearn import datasets

fromsklearn.metrics import confusion_matrix, accuracy_score

fromsklearn.model_selection import train_test_split

fromsklearn.naive_bayes import GaussianNB

# Load the Iris dataset

iris = datasets.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=1/3, random_state=42)

# Train a Gaussian Naive Bayes classifier

model = GaussianNB()

model.fit(X_train, Y_train)

# Make predictions on the test set

model_predictions = model.predict(X_test)

# Print predictions and actual labels

print("\nPredictions:", model_predictions)

print("\nActual labels:", Y_test)

# Calculate and print the accuracy score

accuracyScore = accuracy_score(Y_test, model_predictions)

print("\nAccuracy Score:", accuracyScore)

# Create and print the confusion matrix

cm = confusion_matrix(Y_test, model_predictions)

print("\nConfusion Matrix:\n", cm)

Output:
Predictions: [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 2 2 1 1 2 0 2 0 2 2 2 2 2 0 0
0 0 1 0 0 2 1
0 0 0 2 1 1 0 0 1 1 2 1 2]

Actual labels: [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0
0 0 0 1 0 0 2 1
0 0 0 2 1 1 0 0 1 2 2 1 2]

Accuracy Score: 0.96

Confusion Matrix:
[[19 0 0]
[ 0 14 1]
[ 0 1 15]]

Result

Thus the program to implement Naïve Bayes Model is implemented and executed successfully
EXPTNO :5(b) Implementation of Bayesian Network using Python

Aim

To construct and implement Bayesian Network using Python.

Algorithm

1. !pip install torchbnn in a command prompt


2. Import Libraries: Import necessary libraries, including torch, torchbnn, datasets from sklearn, and
matplotlib.
3. Load Dataset: Load the Iris dataset using datasets.load_iris() and prepare the data and target as
tensors using torch.from_numpy().
4. Create Model: Define a Bayesian Neural Network model using bnn.BayesLinear layers and
nn.ReLU() activation function.
5. Define Loss Functions: Set up the cross-entropy loss function and Kullback-Leibler (KL)
divergence loss using nn.CrossEntropyLoss() and bnn.BKLLoss().
6. Initialize Optimizer: Use the Adam optimizer with a learning rate of 0.01 to update the model
parameters.
7. Training Loop: Run a loop for 3000 steps, where for each step, the model is fed the data tensor,
and the losses (cross-entropy + KL divergence) are calculated.
8. Backward Pass: Backpropagate the total loss and update the model parameters using
optimizer.step().
9. Compute Accuracy: After the loop ends, compute the model's accuracy by comparing predicted
values with the actual target values.
10. Output Final Results: Print the final accuracy, cross-entropy loss, and KL divergence after
training.
11. Plot Results: Define a function to plot two subplots, one showing the true labels and the other
showing the predicted labels for visual comparison.
Program:

importnumpy as np

fromsklearn import datasets

import torch

importtorch.nn as nn

importtorch.optim as optim

importtorchbnn as bnn

importmatplotlib.pyplot as plt

# Load Iris dataset

dataset = datasets.load_iris()

data = dataset.data
target = dataset.target

# Convert dataset to PyTorch tensors

data_tensor = torch.from_numpy(data).float()

target_tensor = torch.from_numpy(target).long()

# Define the Bayesian Neural Network model

model = nn.Sequential(

bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=4, out_features=100),

nn.ReLU(),

bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=100, out_features=3)

# Define the loss functions

cross_entropy_loss = nn.CrossEntropyLoss()

klloss = bnn.BKLLoss(reduction='mean', last_layer_only=False)

# Define the optimizer

optimizer = optim.Adam(model.parameters(), lr=0.01)

# Training loop

for step in range(3000):

# Forward pass

models = model(data_tensor)

# Calculate Cross Entropy and KL loss

cross_entropy = cross_entropy_loss(models, target_tensor)


kl = klloss(model)

# Total cost

total_cost = cross_entropy + 0.01 * kl

# Backward pass

optimizer.zero_grad()

total_cost.backward()

optimizer.step()

# Final prediction and accuracy calculation

models = model(data_tensor)

_, predicted = torch.max(models.data, 1)

correct = (predicted == target_tensor).sum()

accuracy = 100 * float(correct) / target_tensor.size(0)

# Final output

cross_entropy = cross_entropy_loss(models, target_tensor)

kl = klloss(model)

print(f"Final Accuracy: {accuracy:.2f}%")

print(f"Final CE: {cross_entropy.item():.2f}, Final KL: {kl.item():.2f}")

# Function to plot the results

defdraw_graph(predicted):

fig = plt.figure(figsize=(16, 8))

fig_1 = fig.add_subplot(1, 2, 1)
fig_2 = fig.add_subplot(1, 2, 2)

# Plot the true labels

z1_plot = fig_1.scatter(data[:, 0], data[:, 1], c=target, marker='v')

# Plot the predicted labels

z2_plot = fig_2.scatter(data[:, 0], data[:, 1], c=predicted)

plt.colorbar(z1_plot, ax=fig_1)

plt.colorbar(z2_plot, ax=fig_2)

fig_1.set_title("REAL")

fig_2.set_title("PREDICT")

plt.show()

# Final predictions and plot

draw_graph(predicted)

Output:

Final Accuracy: 96.67%


Final CE: 0.06, Final KL: 3.21
Result

Thus, the program to implement Bayesian Networks and perform inferences is implemented and
executed successfully
EXPTNO :6(a) Implementation of Regression Model using linear regression

Aim

To Construct and Implement python program for regression model using linear regression.

Algorithm

1. Import Libraries: Import necessary libraries like pandas, numpy, and sklearn.

2. Load and Preprocess Data: Load the dataset, handle missing values (e.g., fill missing

values with the median), and split it into features (X) and target (y).

3. Train the Model: Use Linear Regression to train the model on the training data.

4. Make Predictions: Use the trained model to predict prices for new data.

5. Evaluate the Model: Calculate performance metrics like Mean Squared Error (MSE)

and R-squared to evaluate the model.

Code:

import pandas as pd

importnumpy as np

fromsklearn.linear_model import LinearRegression

fromsklearn.model_selection import train_test_split

fromsklearn.metrics import mean_squared_error, r2_score

# Load the dataset

df = pd.read_csv('homeprices.csv') # Make sure your CSV file is correctly loaded

# Display the dataset

print("Original Dataset:")

print(df)

# Step 1: Handle missing values in 'bedrooms' column by filling it with the median value
df['bedrooms'] = df['bedrooms'].fillna(df['bedrooms'].median())

# Display the dataset after handling missing values

print("\nDataset after handling missing values:")

print(df)

# Step 2: Prepare the independent variables (X) and dependent variable (y)

X = df.drop('price', axis='columns') # Features: 'area', 'bedrooms', 'age'

y = df['price'] # Target: 'price'

# Step 3: Split the data into Training and Test sets (80% Training, 20% Test)

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

# Step 4: Create a Linear Regression model and fit it to the training data

reg = LinearRegression()

reg.fit(X_train, y_train)

# Step 5: Output the coefficients and intercept of the model

print("\nModel Coefficients:", reg.coef_)

print("Model Intercept:", reg.intercept_)

# Step 6: Predict the price for a home with 3000 sqft area, 3 bedrooms, 40 years old

predicted_price_1 = reg.predict([[3000, 3, 40]])

print("\nPredicted price for a home with 3000 sqft area, 3 bedrooms, 40 years old: $",

predicted_price_1[0])

# Step 7: Predict the price for a home with 2500 sqft area, 4 bedrooms, 5 years old
predicted_price_2 = reg.predict([[2500, 4, 5]])

print("\nPredicted price for a home with 2500 sqft area, 4 bedrooms, 5 years old: $",

predicted_price_2[0])

# Step 8: Evaluate the model on the test set

predictions = reg.predict(X_test)

print("\nModel Evaluation Metrics:")

print("Mean Squared Error:", mean_squared_error(y_test, predictions))

print("R-squared:", r2_score(y_test, predictions))

Output:

Original Dataset:

area bedrooms age price

0 2600 3.0 20 550000

1 3000 4.0 15 565000

2 3200NaN 18 610000

3 3600 3.0 30 595000

4 4000 5.0 8 760000

5 4100 6.0 8 810000

Dataset after handling missing values:

area bedrooms age price

0 2600 3.0 20 550000

1 3000 4.0 15 565000

2 3200 4.0 18 610000

3 3600 3.0 30 595000


4 4000 5.0 8 760000

5 4100 6.0 8 810000

Model Coefficients: [ 112.06244194 23388.88007794 -3231.71790863]

Model Intercept: 221323.00186540408

Predicted price for a home with 3000 sqft area, 3 bedrooms, 40 years old: $ 498408.25158031

Predicted price for a home with 2500 sqft area, 4 bedrooms, 5 years old: $ 578876.03748933

Model Evaluation Metrics:

Mean Squared Error: 1553245155.9513702

R-squared: 0.9481663636254594

Result:

Thus, the python program for linear regression model was executed successfully.
EXPTNO :6(b) Implementation of Regression Model using logisticregression

Construct and Implement python program for regression model using logistic regression.

AIM:

To Construct and Implement python program for regression model using logisticregression.

Algorithm

Step 1: Data Pre Processing

 Importing The Libraries.

 Importing the Data Set.

Step 2:Extracting Independent and dependent Variable

Step3:Splitting the dataset into training and test set.

Step4:feature Scaling

Step5:Fitting Logistic Regression to the training set

Step6:Predicting the test set result

Step 7:Usingweka tool correlation matrix is analysed.

Code:

import pandas as pd

frommatplotlib import pyplot as plt

fromsklearn.model_selection import train_test_split

fromsklearn.linear_model import LogisticRegression

import math

# Step 1: Importing the dataset

df = pd.read_csv("insurance_data.csv")

print(df.head())

# Step 2: Visualizing the data


plt.scatter(df.age, df.bought_insurance, marker='+', color='red')

plt.xlabel('Age')

plt.ylabel('Bought Insurance (0 = No, 1 = Yes)')

plt.show()

# Step 3: Splitting the dataset into training and test sets

X_train, X_test, y_train, y_test = train_test_split(df[['age']], df.bought_insurance, train_size=0.8)

print("Test data:")

print(X_test)

# Step 4: Creating the Logistic Regression model

model = LogisticRegression()

model.fit(X_train, y_train)

# Step 5: Predictions and evaluation

y_predicted = model.predict(X_test)

print("Predicted values:")

print(y_predicted)

# Model accuracy score

accuracy = model.score(X_test, y_test)

print("Model Accuracy: {:.2f}".format(accuracy))

# Step 6: Predict probabilities

print("Predicted probabilities for the test data:")

print(model.predict_proba(X_test))
# Step 7: Coefficients of the model

print("Model coefficient (m):", model.coef_)

print("Model intercept (b):", model.intercept_)

# Step 8: Sigmoid function and manual predictions

def sigmoid(x):

return 1 / (1 + math.exp(-x))

defprediction_function(age):

z = model.coef_[0][0] * age + model.intercept_

y = sigmoid(z)

return y

# Making manual predictions for certain ages

age = 35

print(f"Predicted probability for age {age}: {prediction_function(age)}")

age = 43

print(f"Predicted probability for age {age}: {prediction_function(age)}")

Dataset

Age Bought insurance

22 0

25 0

47 1

52 0

46 1

62 1

23 0
58 1

50 1

54 1

Output:

agebought_insurance
0 22 0
1 25 0
2 47 1
3 52 0
4 46 1

Test data:
age
4 46
8 62
26 23
17 58
24 50
25 54

Predicted values:
[0 1 0 1 1 1]

Model Accuracy: 1.00

Predicted probabilities for the test data:


[[0.40569485 0.59430515]
[0.26002994 0.73997006]
[0.63939494 0.36060506]
[0.29321765 0.70678235]
[0.36637568 0.63362432]
[0.32875922 0.67124078]]

Model coefficient (m): [[0.04150133]]


Model intercept (b): [-1.52726963]

Predicted probability for age 35: 0.4850044983805899


Predicted probability for age 43: 0.568565299077705

=== Confusion Matrix ===


a b <-- classified as
31 | Actual = 0
14 | Actual = 1
Result:

Thus, the python program for logistic regression model was executed successfully.
EXPTNO :7 (a) Implementation of Decision Tree

Construct python program Decision tree using Gaussian classifier.

Aim:

ToConstruct python program Decision tree using Gaussian classifier and visualize graph using

weka tool

Procedure:

1. import Python library packages

2. reading the dataset from the local folder

3. printing first 5 rows

4. As all the columns are categorical, check for unique values of each column

5. Check how these unique categories are distributed among the columns

6. Heatmap of the columns on dataset with each other. It shows Pearson's correlation

coefficient of column w.r.t other columns.

7. As scikit-learn algorithms do not generally work with string values, I've converted string

categories to integers.

8. printing the first 5 rows

9. X is the dataframe containing input data / features

10. y is the series which has results which are to be predicted.

11. Import train_test_split function

12. Split dataset into training set and test set

13. Create a Gaussian Classifier

14. Train the model using the training sets y_pred=model.predict(X_test

15. Import scikit-learn metrics module for accuracy calculation

16. Model Accuracy, how often is the classifier correct?


Code:

import pandas as pd

# Load the dataset

file_path = "/content/covid_19_india.csv" # Replace with the actual path to your file

data = pd.read_csv(file_path)

# Display basic information about the dataset

print("Dataset Information:")

print(data.info())

print("\nFirst 5 Rows of the Dataset:")

print(data.head())

# Convert 'Date' to datetime format for easier analysis

data['Date'] = pd.to_datetime(data['Date'], errors='coerce', infer_datetime_format=True)

# Example Operations:

# 1. Total confirmed cases per state

statewise_total = data.groupby('State/UnionTerritory')['Confirmed'].sum().reset_index()

print("\nTotal Confirmed Cases by State/UT:")

print(statewise_total)

# 2. Plot confirmed cases over time for a specific state (e.g., Kerala)

importmatplotlib.pyplot as plt

state = 'Kerala' # Replace with the desired state

state_data = data[data['State/UnionTerritory'] == state]


plt.figure(figsize=(10, 6))

plt.plot(state_data['Date'], state_data['Confirmed'], marker='o', label=f'Confirmed Cases in

{state}')

plt.title(f'COVID-19 Confirmed Cases Over Time in {state}')

plt.xlabel('Date')

plt.ylabel('Confirmed Cases')

plt.xticks(rotation=45)

plt.legend()

plt.grid()

plt.show()

# 3. Total deaths by state

statewise_deaths = data.groupby('State/UnionTerritory')['Deaths'].sum().reset_index()

print("\nTotal Deaths by State/UT:")

print(statewise_deaths)

# 4. Save processed data to a new CSV file

processed_file_path = "processed_data.csv"

data.to_csv(processed_file_path, index=False)

print(f"Processed data saved to {processed_file_path}")

Output:
Dataset Information:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 18110 entries, 0 to 18109
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Sno 18110 non-null int64
1 Date 18110 non-null object
2 Time 18110 non-null object
3 State/UnionTerritory 18110 non-null object
4 ConfirmedIndianNational 18110 non-null object
5 ConfirmedForeignNational 18110 non-null object
6 Cured 18110 non-null int64
7 Deaths 18110 non-null int64
8 Confirmed 18110 non-null int64
dtypes: int64(4), object(5)
memory usage: 1.2+ MB
None

First 5 Rows of the Dataset:


Sno Date Time State/UnionTerritoryConfirmedIndianNational \
0 1 2020-01-30 6:00 PM Kerala 1
1 2 2020-01-31 6:00 PM Kerala 1
2 3 2020-02-01 6:00 PM Kerala 2
3 4 2020-02-02 6:00 PM Kerala 3
4 5 2020-02-03 6:00 PM Kerala 3

ConfirmedForeignNational Cured Deaths Confirmed


0 0 0 0 1
1 0 0 0 1
2 0 0 0 2
3 0 0 0 3
4 0 0 0 3

Total Confirmed Cases by State/UT:


State/UnionTerritory Confirmed
0 Andaman and Nicobar Islands 1938498
1 Andhra Pradesh 392432753
2 Arunachal Pradesh 7176907
3 Assam 99837011
4 Bihar 132231166
5 Bihar**** 1430909
6 Cases being reassigned to states 345565
7 Chandigarh 10858627
8 Chhattisgarh 163776262
9 Dadra and Nagar Haveli 20722
10 Dadra and Nagar Haveli and Daman and Diu 1938632
11 Daman & Diu 2
12 Delhi 287227765
13 Goa 28240159
14 Gujarat 143420082
15 Haryana 134347285
16 Himachal Pradesh 30033289
17 Himanchal Pradesh 204516
18 Jammu and Kashmir 58117726
19 Jharkhand 62111994
20 Karanataka 2885238
21 Karnataka 485970693
22 Kerala 458906023
23 Ladakh 4054293
24 Lakshadweep 915784
25 Madhya Pradesh 135625265
26 Madhya Pradesh*** 791656
27 Maharashtra 1121491467
28 Maharashtra*** 6229596
29 Manipur 12617943
30 Meghalaya 7355969
31 Mizoram 2984732
32 Nagaland 5041742
33 Odisha 160130533
34 Puducherry 20065891
35 Punjab 99949702
36 Rajasthan 162369656
37 Sikkim 3186799
38 Tamil Nadu 431928644
39 Telangana 60571979
40 Telengana 69990668
41 Tripura 14050250
42 Unassigned 161
43 Uttar Pradesh 312625843
44 Uttarakhand 53140414
45 West Bengal 263107876
<ipython-input-2-8e0b59bf7d81>:15: UserWarning: The argument
'infer_datetime_format' is deprecated and will be removed in a future
version. A strict version of it is now the default, see
https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You
can safely remove this argument.
data['Date'] = pd.to_datetime(data['Date'], errors='coerce',
infer_datetime_format=True)

Total Deaths by State/UT:


State/UnionTerritory Deaths
0 Andaman and Nicobar Islands 27136
1 Andhra Pradesh 2939367
2 Arunachal Pradesh 26799
3 Assam 638323
4 Bihar 1093466
5 Bihar**** 18881
6 Cases being reassigned to states 0
7 Chandigarh 147694
8 Chhattisgarh 2063920
9 Dadra and Nagar Haveli 8
10 Dadra and Nagar Haveli and Daman and Diu 1014
11 Daman & Diu 0
12 Delhi 4943294
13 Goa 447801
14 Gujarat 2219448
15 Haryana 1502799
16 Himachal Pradesh 491348
17 Himanchal Pradesh 3507
18 Jammu and Kashmir 839694
19 Jharkhand 748641
20 Karanataka 36197
21 Karnataka 6053762
22 Kerala 1888177
23 Ladakh 45804
24 Lakshadweep 3908
25 Madhya Pradesh 1777752
26 Madhya Pradesh*** 10506
27 Maharashtra 23737432
28 Maharashtra*** 130753
29 Manipur 173056
30 Meghalaya 101950
31 Mizoram 9791
32 Nagaland 58460
33 Odisha 790814
34 Puducherry 312155
35 Punjab 2785594
36 Rajasthan 1473089
37 Sikkim 53150
38 Tamil Nadu 5916658
39 Telangana 349648
40 Telengana 400427
41 Tripura 150342
42 Unassigned 0
43 Uttar Pradesh 4143450
44 Uttarakhand 986001
45 West Bengal 3846989

Result:
Thusthe python program for Decision tree was executed successfully.
EXPTNO :7 (b) Implementation of Random Forest Tree.

Construct python program for random forest tree and build the model

Aim:

ToConstruct python program for random forest tree and build the model .

Procedure:

1. Import Python library packages.

2. Read the dataset from the local folder.

3. Print the first 5 rows of the dataset.

4. Since all columns are categorical, check for unique values in each column.

5. Check how these unique categories are distributed among the columns.

6. Create a heatmap of the columns in the dataset with each other, showing Pearson's

correlation coefficient of each column with respect to other columns.

7. Convert string categories to integers, as scikit-learn algorithms do not generally work

with string values.

8. Print the first 5 rows after conversion.

9. Define X as the dataframe containing input data/features.

10. Define y as the series containing the results to be predicted.

11. Import the train_test_split function.

12. Split the dataset into training and test sets.

13. Create a Gaussian Classifier.

14. Train the model using the training sets. Use y_pred=model.predict(X_test) to make

predictions.

15. Import the scikit-learn metrics module for accuracy calculation.

16. Calculate the model's accuracy to determine how often the classifier is correct
Code:

import pandas as pd

fromsklearn.ensemble import RandomForestClassifier

fromsklearn.model_selection import train_test_split

fromsklearn.metrics import accuracy_score, classification_report, confusion_matrix

importnumpy as np

# Upload the dataset

fromgoogle.colab import files

uploaded = files.upload() # Upload the CSV file

file_name = list(uploaded.keys())[0]

# Process dataset in chunks

chunk_size = 1000 # Number of rows to process at a time

chunks = pd.read_csv(file_name, chunksize=chunk_size)

# Initialize an empty DataFrame to hold processed chunks

processed_data = []

for chunk in chunks:

# Convert 'Date' to datetime format

chunk['Date'] = pd.to_datetime(chunk['Date'], errors='coerce')

# Drop irrelevant columns

chunk.drop(columns=['Sno', 'State/UnionTerritory', 'Date', 'Time'], inplace=True,

errors='ignore')
# Replace non-numeric values with NaN and fill with 0

chunk.replace('-', np.nan, inplace=True)

chunk.fillna(0, inplace=True)

# Ensure all data is numeric

chunk = chunk.apply(pd.to_numeric, errors='coerce')

processed_data.append(chunk)

# Combine all chunks into a single DataFrame

data = pd.concat(processed_data, ignore_index=True)

# Reduce memory usage by converting data types

for col in data.columns:

if data[col].dtype == 'float64':

data[col] = data[col].astype('float32')

elif data[col].dtype == 'int64':

data[col] = data[col].astype('int32')

# Define features (X) and target variable (y)

X = data.drop(columns=['Confirmed'], errors='ignore') # Features

y = data['Confirmed'] # Target variable

# Downsample the data for training (optional, if dataset is still too large)

sample_fraction = 0.1 # Use 10% of the data

X, y = X.sample(frac=sample_fraction, random_state=42), y.sample(frac=sample_fraction,

random_state=42)
# 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)

# Initialize and train the Random Forest model

rf_model = RandomForestClassifier(n_estimators=100, random_state=42)

rf_model.fit(X_train, y_train)

# Make predictions

y_pred = rf_model.predict(X_test)

# Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")

print("\nClassification Report:")

print(classification_report(y_test, y_pred))

print("\nConfusion Matrix:")

print(confusion_matrix(y_test, y_pred))

Output:

Confusion Matrix:
[[0 1 0 ... 0 0 0]
[0 1 0 ... 0 0 0]
[0 2 2 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 1 0]]
Result

Thus, the python program for random forest tree was executed successfully.

You might also like