Aiml Sem Practicals
Aiml Sem Practicals
PROGRAM:
def expert_system(symptoms):
rules = {
"fever and cough": "You might have the flu.",
"fever and rash": "You might have measles.",
"cough and sore throat": "You might have a cold.",
"fever and headache": "You might have dengue."
}
for condition, diagnosis in rules.items():
if all(symptom in symptoms for symptom in condition.split(" and ")):
return diagnosis
return "No diagnosis found. Consult a doctor."
# Example usage
symptoms = ["fever", "cough"]
print(expert_system(symptoms))
---
2.Decision Tree
A decision tree is a model used to make decisions based on rules.
PPROGRAM:
from sklearn import tree
---
3.A* Algorithm
A* finds the shortest path in a graph using a heuristic.
PROGRAM:
from queue import PriorityQueue
return None
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('D', 1), ('E', 2)],
'C': [('F', 5)],
'D': [('G', 6)],
'E': [('G', 2)],
'F': [('G', 1)],
'G': []
}
heuristic = {'A': 7, 'B': 5, 'C': 8, 'D': 4, 'E': 2, 'F': 1, 'G': 0}
---
PROGRAM:
from collections import deque
while queue:
node = queue.popleft()
if node not in visited:
visited.add(node)
result.append(node)
queue.extend(graph[node])
return result
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
print(bfs(graph, 'A'))
---
PROGRAM:
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
result = [start]
for neighbor in graph[start]:
if neighbor not in visited:
result += dfs(graph, neighbor, visited)
return result
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
print(dfs(graph, 'A'))
---
6. Naive Bayes
Naive Bayes is a probabilistic classifier.
PROGRAM:
from sklearn.naive_bayes import GaussianNB
X = [[1, 20], [2, 18], [1, 17], [2, 19]] # Training data (features)
y = [0, 1, 0, 1] # Labels (0 = No, 1 = Yes)
clf = GaussianNB()
clf.fit(X, y)
---
NAIVE BAYES:
import pandas as pd
import re
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
# Main execution
if __name__ == "__main__":
# Load the dataset
file_path = "customer_reviews.xlsx"
data = load_data(file_path)
MAP COLORING:
def is_safe(node, color, graph, colors):
for neighbor in graph[node]:
if colors[neighbor] == color:
return False
return True
return False
num_colors = 3
solution = solve_map_coloring(graph, num_colors)
print("Color assignment:", solution)
return dls(start, 0)
start = 'A'
goal = 'F'
limit = 2