1. Implement Breadth first search algorithm in python.
Consider following
tree. Start node = 2, Goal node = 11
=>graph = {
'2' : ['7','5'],
'7' : ['1','6'],
'1' : [],
'6' : ['5','11'],
'5' : [],
'11' :[],
'5' : ['9'],
'9' : ['4'],
'4' : []
}
visited = []
queue = []
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
m = queue.pop(0)
print(m, end = " ")
for neighbour in graph[m] :
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
print("Following is the Breadth-First Search")
bfs(visited, graph, '2')
2. Implement Breadth first search algorithm in python. Consider following
graph. (Root Node: 5, Goal Node = ?)
graph = {
'5' : ['3','7'],
'3' : ['2','4'],
'7' : ['5','8'],
'2' : ['3'],
'4' : ['3','8'],
'8' : ['7','4'],
}
visited = []
queue = []
def bfs (visited, graph, node):
visited.append(node)
queue.append(node)
while queue :
m=queue.pop(0)
print(m)
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
print("BFS is ")
bfs(visited, graph, '5')
3. Implement Depth first search algorithm in python. Consider following
graph. ( Goal Node= O )
graph ={
's' : ['a','k'],
'a' : ['b','c'],
'k' : ['i','j'],
'b' : ['h','m'],
'c' : ['n'],
'i' : ['o'],
'j' : [],
'h' : [],
'm' : [],
'n' : [],
'o' : []
}
visited =set()
def dfs (visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs (visited, graph, neighbour)
print("DfS of the graph is : ")
dfs (visited, graph, 's')
4. Implement Depth first search algorithm in python. Consider following
graph. (Goal Node= 6 ).
graph ={
'1' : ['2','3'],
'2' : ['4','5'],
'3' : ['6','7'],
'4' : [],
'5' : [],
'6' : [],
'7' : [],
}
visited =set()
def dfs (visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs (visited, graph, neighbour)
print("DfS of the graph is : ")
dfs (visited, graph, '1')
5. Implement best first search algorithm in python for following tree. Starting
node (10), Goal node (100).
6. Implement best first search algorithm in python for following graph.
Starting node(S), Goal node(G)
from queue import PriorityQueue
def best_first_search(graph, heuristic, start, goal):
visited = set()
pq = PriorityQueue()
pq.put((heuristic[start], start))
while pq:
_, current = pq.get()
if current in visited:
continue
print (current)
visited.add(current)
if current == goal:
print("Goal reached!")
return
for neighbor in graph[current]:
if neighbor not in visited:
pq.put((heuristic[neighbor], neighbor))
print("Goal not reachable.")
# Example graph represented as an adjacency list
graph = {
's': ['a', 'b'],
'a': ['s','g'],
'b': ['s','c'],
'c': ['b','g'],
'g': ['a','c']
}
# Heuristic values for each node
heuristic = {
's': 5,
'a': 2,
'b': 4,
'c': 2,
'g': 0
}
# Starting the search
best_first_search(graph, heuristic, start='s', goal='g')
7. Implement A* algorithm in python for following graph. Starting node (S),
Goal node(G).
Heuristic Value: H(S) = 5, H(A) = 3, H(B) = 4, H(C) = 2, H(D) = 6, H(G) = 0
=>
from queue import PriorityQueue
def a_star(graph, start, goal, h):
pq = PriorityQueue()
pq.put((h[start], start, 0, []))
while pq:
_, current, g, path = pq.get()
path = path + [current]
if current == goal:
return path, g
for neighbor, cost in graph[current]:
pq.put((g + cost + h[neighbor], neighbor, g + cost, path))
return None,
graph = {
's': [('a', 1), ('g', 10)],
'a': [('b', 2),('s', 1),('c',1)],
'b': [('d', 5),('a', 2)],
'c': [('g', 4),('a',1)],
'd': [('g', 2),('b', 5)],
'g': []
}
h = {'s': 5, 'a': 3, 'b': 4, 'c': 2, 'd': 6, 'g': 0}
path, cost = a_star(graph, 's', 'g', h)
print("Path: ",path)
print("Cost: ",cost)
8. Build model on data sets for Credit Card Fraud Detection Dataset.
9. Build model on data sets for E-mail Spam Detection.
10. Build model on data sets for Lung Cancer Detection.
11. Perform EDA on data sets for Gold Price Prediction.
12. Build model on NLP Emotion Detection Dataset.
13. Build model on NLP for Twitter Dataset.
14. Build model on NLP for Fake News Dataset.
15. Use Python to analyse & load a Flight price prediction dataset into training
and Testing sets.
16. Write a program to analyse & load Iris flower dataset using Python and
perform training and testing.
17. Write a Python program to implement Simple Linear Regression using a
real-world dataset for house prices.
18. Write a Python program to implement Simple Linear Regression using a
real-world dataset stock prices.
19. Write a Python program to implement Multiple Linear Regression for car
price prediction.
20. Write a Python program to implement Multiple Linear Regression for wine
quality prediction.
for logistic regression the code will be :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
df=pd.read_csv('Heart_disease_cleveland_new.csv')
df.head()
df.shape
df.info()
df.isnull.sum()
df.columns
df.hist(figsize=(15,15),color='blue')
sns.heatmap(df.corr(),annot=True)
x=df.drop(columns=['placed'])
y=df['placed']
X_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=42)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
model=LogisticRegression()
model.fit(X_train,y_train)
score=model.score(X_train,y_train)
score
y_pred=model.predict(X_test)
y_pred
for linear regression only change will be in model
model=LinearRegression()
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
y_pred
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)