Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

AI Lab File

This document contains code snippets to implement various graph search algorithms like BFS, DFS, Best First Search and A* Search. It also contains code to load data from Excel and CSV files into a Pandas DataFrame and implement operations on the Titanic dataset using Pandas.

Uploaded by

Rythm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

AI Lab File

This document contains code snippets to implement various graph search algorithms like BFS, DFS, Best First Search and A* Search. It also contains code to load data from Excel and CSV files into a Pandas DataFrame and implement operations on the Titanic dataset using Pandas.

Uploaded by

Rythm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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):
self.graph = defaultdict(list)

def add_edge(self, u, v):


self.graph[u].append(v)

def bfs(self, start):


visited = defaultdict(bool)
queue = deque([start])
visited[start] = True

while queue:
node = queue.popleft()
print(node, end=' ')

for neighbor in self.graph[node]:


if not visited[neighbor]:
queue.append(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:")


graph.bfs(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):
self.graph = defaultdict(list)

def add_edge(self, u, v):


self.graph[u].append(v)

def dfs_util(self, node, visited):


visited[node] = True
print(node, end=' ')

for neighbor in self.graph[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:")


graph.dfs(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):
self.graph = {}

def add_edge(self, u, v, weight):


if u not in self.graph:
self.graph[u] = []
self.graph[u].append((v, weight))

def best_first_search(self, start, goal):


visited = set()
pq = PriorityQueue()
pq.put((0, start))

while not pq.empty():


cost, node = pq.get()
print(node, end=' ')
visited.add(node)

if node == goal:
print("\nGoal reached!")
return

for neighbor, weight in self.graph.get(node, []):


if neighbor not in visited:
pq.put((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 = []
heapq.heappush(frontier, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0

while frontier:
current = heapq.heappop(frontier)[1]

if current == goal:
break

for next in graph.neighbors(current):


new_cost = cost_so_far[current] + graph.cost(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)
heapq.heappush(frontier, (priority, next))
came_from[next] = current

return came_from, cost_so_far

# Example usage
class SquareGrid:
def __init__(self, width, height):
self.width = width
self.height = height
self.walls = []

def in_bounds(self, id):


(x, y) = id
return 0 <= x < self.width and 0 <= y < self.height

def passable(self, id):


return id not in self.walls

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(self.passable, results)
return results

def cost(self, a, b):


return 1

grid = SquareGrid(30, 15)


grid.walls = [(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:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()

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 google.colab import drive
drive.mount('/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(df.head())

WAP to load data in Colab from:

● CSV File

# Mount Google Drive to access files


from google.colab import drive
drive.mount('/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(df.head())

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("https://raw.githubusercontent.com/datasciencedojo/datasets/
master/titanic.csv")

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 'pandas.core.frame.DataFrame'>
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 matplotlib.pyplot as plt
import seaborn as sns

# Plotting the count of survivors


sns.countplot(x='Survived', data=titanic_df)
plt.title('Count of Survivors')
plt.show()

# Plotting the distribution of age


sns.histplot(x='Age', data=titanic_df, bins=20)
plt.title('Distribution of Age')
plt.show()

# Plotting the survival rate by passenger class


sns.barplot(x='Pclass', y='Survived', data=titanic_df)
plt.title('Survival Rate by Passenger Class')
plt.show()

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 = sqlite3.connect('example.db')

# Create a cursor object to execute SQL queries


cursor = conn.cursor()

Create tables and insert data:


# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS employees
(id INTEGER PRIMARY KEY, name TEXT, salary REAL)''')

# Insert data into the table


cursor.execute("INSERT INTO employees (name, salary) VALUES (?, ?)", ('John Doe', 5000))
cursor.execute("INSERT INTO employees (name, salary) VALUES (?, ?)", ('Jane Smith',
6000))

# Commit changes
conn.commit()

Execute SQL queries:


# Execute SQL query to fetch data
cursor.execute("SELECT * FROM employees")

# Fetch all rows


rows = cursor.fetchall()

# 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
cursor.close()

# Close the connection


conn.close()

1
2
Q8 Build a Basic Linear Regression Model.
Import necessary libraries:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

Prepare data:
# Generate some random data
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(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


model.fit(X_train, y_train)

LinearRegression
LinearRegression()
Make predictions:
# Predict on the test set
y_pred = model.predict(X_test)

Visualize the results:


# Plot the data points
plt.scatter(X_test, y_test, color='blue', label='Actual')

# Plot the regression line


plt.plot(X_test, y_pred, color='red', label='Predicted')

plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression')
plt.legend()
plt.show()

1
3
1
4
Q9 Implement Data visualisation techniques such as plotting, bar, histogram, using matplatlib.
Scatter Plot:
import matplotlib.pyplot as plt

# Generate some random data


x = [1, 2, 3, 4, 5]
y = [5, 7, 8, 2, 4]

# Plot the data points


plt.scatter(x, y)

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
plt.show()

1
5
Bar Plot:
import matplotlib.pyplot as plt

# Data
categories = ['A', 'B', 'C', 'D']
values = [7, 3, 12, 5]

# Plot the bar chart


plt.bar(categories, values)

plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()

1
6
Histogram:
import matplotlib.pyplot as plt
import numpy as np

# Generate some random data


data = np.random.randn(1000)

# Plot the histogram


plt.hist(data, bins=30)

plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

1
7

You might also like