INDEX
Name:Rythm Jagga Enrollment No.:08419011922
Branch & Batch: Artificial Intelligence & Data Science
Paper Title: Introduction to AI Lab Paper Code: ARM 252
Sub Topic of Practical Signature/
Part Remarks
Q1
WAP to Implement Breadth First Search algorithm for given graph G.
Q2
WAP to Implement Depth First Search algorithm for given graph G.
Q3
WAP to Implement Best First Search algorithm for given graph G.
Q4
WAP to Implement A* Search Algorithms .
Q5
WAP to load data in Colab from:
● Excel Sheet
● CSV File
Q6
WAP to Implement various Pandas Commands on Titanic Dataset
Q7
WAP to integrate SQL & Python
Q8
Build a Basic Linear Regression Model.
Q9
Implement Data visualisation techniques such as plotting, bar, histogram,
using matplatlib.
1
Q1 WAP to Implement Breadth First Search algorithm for given graph G.
from collections import defaultdict, deque
class Graph:
def __init__(self):
[Link] = defaultdict(list)
def add_edge(self, u, v):
[Link][u].append(v)
def bfs(self, start):
visited = defaultdict(bool)
queue = deque([start])
visited[start] = True
while queue:
node = [Link]()
print(node, end=' ')
for neighbor in [Link][node]:
if not visited[neighbor]:
[Link](neighbor)
visited[neighbor] = True
# Example usage:
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
print("Breadth First Traversal starting from vertex 2:")
[Link](2)
Breadth First Traversal starting from vertex 2:
2031
2
Q2 WAP to Implement Depth First Search algorithm for given graph G.
from collections import defaultdict
class Graph:
def __init__(self):
[Link] = defaultdict(list)
def add_edge(self, u, v):
[Link][u].append(v)
def dfs_util(self, node, visited):
visited[node] = True
print(node, end=' ')
for neighbor in [Link][node]:
if not visited[neighbor]:
self.dfs_util(neighbor, visited)
def dfs(self, start):
visited = defaultdict(bool)
self.dfs_util(start, visited)
# Example usage:
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
print("Depth First Traversal starting from vertex 2:")
[Link](2)
Depth First Traversal starting from vertex 2:
2013
3
Q3 WAP to Implement Best First Search algorithm for given graph G.
from queue import PriorityQueue
class Graph:
def __init__(self):
[Link] = {}
def add_edge(self, u, v, weight):
if u not in [Link]:
[Link][u] = []
[Link][u].append((v, weight))
def best_first_search(self, start, goal):
visited = set()
pq = PriorityQueue()
[Link]((0, start))
while not [Link]():
cost, node = [Link]()
print(node, end=' ')
[Link](node)
if node == goal:
print("\nGoal reached!")
return
for neighbor, weight in [Link](node, []):
if neighbor not in visited:
[Link]((weight, neighbor))
# Example usage:
graph = Graph()
graph.add_edge('A', 'B', 3)
graph.add_edge('A', 'C', 6)
graph.add_edge('B', 'D', 2)
graph.add_edge('B', 'E', 9)
graph.add_edge('C', 'F', 5)
graph.add_edge('E', 'G', 7)
graph.add_edge('E', 'H', 8)
print("Best First Search:")
graph.best_first_search('A', 'H')
Best First Search:
ABDCFEGH
Goal reached!
4
Q4 WAP to Implement A* Search Algorithms .
import heapq
def heuristic(a, b):
"""Calculates the Manhattan distance between two points."""
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
def a_star_search(graph, start, goal):
frontier = []
[Link](frontier, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while frontier:
current = [Link](frontier)[1]
if current == goal:
break
for next in [Link](current):
new_cost = cost_so_far[current] + [Link](current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(next, goal)
[Link](frontier, (priority, next))
came_from[next] = current
return came_from, cost_so_far
# Example usage
class SquareGrid:
def __init__(self, width, height):
[Link] = width
[Link] = height
[Link] = []
def in_bounds(self, id):
(x, y) = id
return 0 <= x < [Link] and 0 <= y < [Link]
def passable(self, id):
return id not in [Link]
def neighbors(self, id):
(x, y) = id
neighbors = [(x+1, y), (x-1, y), (x, y-1), (x, y+1)]
results = filter(self.in_bounds, neighbors)
5
results = filter([Link], results)
return results
def cost(self, a, b):
return 1
grid = SquareGrid(30, 15)
[Link] = [(21, 0), (21, 1), (21, 2), (21, 3), (21, 4), (21, 5), (21, 6),
(21, 7), (21, 8), (21, 9), (21, 10), (21, 11), (21, 12), (21, 13)]
start = (0, 0)
goal = (29, 14)
came_from, cost_so_far = a_star_search(grid, start, goal)
path = []
current = goal
while current != start:
[Link](current)
current = came_from[current]
[Link](start)
[Link]()
print("Path:", path)
Path: [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12),
(0, 13), (0, 14), (1, 14), (2, 14), (3, 14), (4, 14), (5, 14), (6, 14), (7, 14), (8, 14), (9, 14), (10, 14),
(11, 14), (12, 14), (13, 14), (14, 14), (15, 14), (16, 14), (17, 14), (18, 14), (19, 14), (20, 14), (21,
14), (22, 14), (23, 14), (24, 14), (25, 14), (26, 14), (27, 14), (28, 14), (29, 14)]
6
Q 5 WAP to load data in Colab from:
● Excel Sheet
# Mount Google Drive to access files
from [Link] import drive
[Link]('/content/drive')
# Import pandas library to work with Excel files
import pandas as pd
# Specify the path to your Excel file
excel_file_path = '/content/drive/MyDrive/Rythm/FINAL450 (1).xlsx'
# Load data from the Excel file
df = pd.read_excel(excel_file_path)
# Display the loaded data
print("Data loaded from Excel file:")
print([Link]())
WAP to load data in Colab from:
● CSV File
# Mount Google Drive to access files
from [Link] import drive
[Link]('/content/drive')
# Import pandas library to work with Excel files
import pandas as pd
# Specify the path to your Excel file
excel_file_path = '/content/drive/MyDrive/Rythm/FINAL450 (1).xlsx'
# Load data from the Excel file
df = pd.read_excel(excel_file_path)
# Display the loaded data
print("Data loaded from Excel file:")
print([Link]())
7
Q6 WAP to Implement various Pandas Commands on Titanic Dataset
Load the Dataset
import pandas as pd
# Load Titanic dataset from seaborn library
titanic_df = pd.read_csv("[Link]
master/[Link]")
Explore the Dataset
# Display the first few rows of the dataset
print("First few rows of the dataset:")
print(titanic_df.head())
# Display the shape of the dataset
print("\nShape of the dataset:")
print(titanic_df.shape)
# Display basic statistics of numeric columns
print("\nSummary statistics of numeric columns:")
print(titanic_df.describe())
# Display information about the dataset
print("\nInformation about the dataset:")
print(titanic_df.info())
First few rows of the dataset:
PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
8
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
Shape of the dataset:
(891, 12)
Summary statistics of numeric columns:
PassengerId Survived Pclass Age SibSp \
count 891.000000 891.000000 891.000000 714.000000 891.000000
mean 446.000000 0.383838 2.308642 29.699118 0.523008
std 257.353842 0.486592 0.836071 14.526497 1.102743
min 1.000000 0.000000 1.000000 0.420000 0.000000
25% 223.500000 0.000000 2.000000 20.125000 0.000000
50% 446.000000 0.000000 3.000000 28.000000 0.000000
75% 668.500000 1.000000 3.000000 38.000000 1.000000
max 891.000000 1.000000 3.000000 80.000000 8.000000
Parch Fare
count 891.000000 891.000000
mean 0.381594 32.204208
std 0.806057 49.693429
min 0.000000 0.000000
25% 0.000000 7.910400
50% 0.000000 14.454200
75% 0.000000 31.000000
max 6.000000 512.329200
Information about the dataset:
<class '[Link]'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
Data Manipulation
9
# Select specific columns
selected_columns = ['Survived', 'Pclass', 'Sex', 'Age', 'Fare']
selected_df = titanic_df[selected_columns]
# Filter rows based on conditions
male_passengers = titanic_df[titanic_df['Sex'] == 'male']
female_survivors = titanic_df[(titanic_df['Sex'] == 'female') & (titanic_df['Survived']
== 1)]
# Group data and perform aggregation
age_group_survival_rate = titanic_df.groupby('Age')['Survived'].mean()
# Sort values
sorted_age_group_survival_rate = age_group_survival_rate.sort_values(ascending=False)
# Create new columns
titanic_df['Family_Size'] = titanic_df['SibSp'] + titanic_df['Parch']
# Drop columns
titanic_df.drop(['SibSp', 'Parch'], axis=1, inplace=True)
Data Visualisation
import [Link] as plt
import seaborn as sns
# Plotting the count of survivors
[Link](x='Survived', data=titanic_df)
[Link]('Count of Survivors')
[Link]()
# Plotting the distribution of age
[Link](x='Age', data=titanic_df, bins=20)
[Link]('Distribution of Age')
[Link]()
# Plotting the survival rate by passenger class
[Link](x='Pclass', y='Survived', data=titanic_df)
[Link]('Survival Rate by Passenger Class')
[Link]()
1
0
1
1
Q7 WAP to integrate SQL & Python
Connect to the SQLite database:
import sqlite3
# Connect to the SQLite database (creates the database if it doesn't exist)
conn = [Link]('[Link]')
# Create a cursor object to execute SQL queries
cursor = [Link]()
Create tables and insert data:
# Create a table
[Link]('''CREATE TABLE IF NOT EXISTS employees
(id INTEGER PRIMARY KEY, name TEXT, salary REAL)''')
# Insert data into the table
[Link]("INSERT INTO employees (name, salary) VALUES (?, ?)", ('John Doe', 5000))
[Link]("INSERT INTO employees (name, salary) VALUES (?, ?)", ('Jane Smith',
6000))
# Commit changes
[Link]()
Execute SQL queries:
# Execute SQL query to fetch data
[Link]("SELECT * FROM employees")
# Fetch all rows
rows = [Link]()
# Print fetched rows
for row in rows:
print(row)
(1, 'John Doe', 5000.0)
(2, 'Jane Smith', 6000.0)
Close the connection:
# Close the cursor
[Link]()
# Close the connection
[Link]()
1
2
Q8 Build a Basic Linear Regression Model.
Import necessary libraries:
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
Prepare data:
# Generate some random data
[Link](0)
X = 2 * [Link](100, 1)
y = 4 + 3 * X + [Link](100, 1)
Split data into training and test sets:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
Train the linear regression model:
# Create a linear regression model
model = LinearRegression()
# Train the model
[Link](X_train, y_train)
LinearRegression
LinearRegression()
Make predictions:
# Predict on the test set
y_pred = [Link](X_test)
Visualize the results:
# Plot the data points
[Link](X_test, y_test, color='blue', label='Actual')
# Plot the regression line
[Link](X_test, y_pred, color='red', label='Predicted')
[Link]('X')
[Link]('y')
[Link]('Linear Regression')
[Link]()
[Link]()
1
3
1
4
Q9 Implement Data visualisation techniques such as plotting, bar, histogram, using matplatlib.
Scatter Plot:
import [Link] as plt
# Generate some random data
x = [1, 2, 3, 4, 5]
y = [5, 7, 8, 2, 4]
# Plot the data points
[Link](x, y)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot')
[Link]()
1
5
Bar Plot:
import [Link] as plt
# Data
categories = ['A', 'B', 'C', 'D']
values = [7, 3, 12, 5]
# Plot the bar chart
[Link](categories, values)
[Link]('Categories')
[Link]('Values')
[Link]('Bar Plot')
[Link]()
1
6
Histogram:
import [Link] as plt
import numpy as np
# Generate some random data
data = [Link](1000)
# Plot the histogram
[Link](data, bins=30)
[Link]('Value')
[Link]('Frequency')
[Link]('Histogram')
[Link]()
1
7