AI Lab File
AI Lab File
● 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.
class Graph:
def __init__(self):
self.graph = defaultdict(list)
while queue:
node = queue.popleft()
print(node, end=' ')
# 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)
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)
# 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)
3
Q3 WAP to Implement Best First Search algorithm for given graph G.
class Graph:
def __init__(self):
self.graph = {}
if node == goal:
print("\nGoal reached!")
return
# 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)
4
Q4 WAP to Implement A* Search Algorithms .
import heapq
while frontier:
current = heapq.heappop(frontier)[1]
if current == goal:
break
# Example usage
class SquareGrid:
def __init__(self, width, height):
self.width = width
self.height = height
self.walls = []
5
results = filter(self.passable, results)
return results
start = (0, 0)
goal = (29, 14)
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
● CSV File
7
Q6 WAP to Implement various Pandas Commands on Titanic Dataset
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
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
Data Manipulation
9
# Select specific columns
selected_columns = ['Survived', 'Pclass', 'Sex', 'Age', 'Fare']
selected_df = titanic_df[selected_columns]
# Sort values
sorted_age_group_survival_rate = age_group_survival_rate.sort_values(ascending=False)
# Drop columns
titanic_df.drop(['SibSp', 'Parch'], axis=1, inplace=True)
Data Visualisation
import matplotlib.pyplot as plt
import seaborn as sns
1
0
1
1
Q7 WAP to integrate SQL & Python
# Commit changes
conn.commit()
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)
LinearRegression
LinearRegression()
Make predictions:
# Predict on the test set
y_pred = model.predict(X_test)
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
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]
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()
1
6
Histogram:
import matplotlib.pyplot as plt
import numpy as np
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
1
7