AI Practical Exam
AI Practical Exam
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix
import matplotlib.pyplot as plt
graph = {
'A': ['B', 'C'],
'B': ['D', 'C'],
'C': ['D'],
'D': []
}
path = queue.popleft()
city = path[-1]
if city == goal:
return path
graph = {
'A': ['B', 'C'],
'B': ['D', 'C'],
'C': ['D'],
'D': []
}
while stack:
path = stack.pop()
city = path[-1]
if city == goal:
return path
return None
graph = {
'A': ['B', 'C'],
'B': ['D', 'C'],
'C': ['D'],
'D': []
}
while queue:
path = queue.popleft()
city = path[-1]
if city == goal:
return path
return None
graph = {
'Arad': [('Sibiu', 140), ('Timisoara', 118)],
'Sibiu': [('Arad', 140), ('Fagaras', 99), ('Oradea', 151)],
'Timisoara': [('Arad', 118), ('Lugoj', 111)],
'Lugoj': [('Timisoara', 111), ('Mehadia', 70)],
'Mehadia': [('Lugoj', 70), ('Drobeta', 75)],
'Drobeta': [('Mehadia', 75), ('Craiova', 120)],
'Craiova': [('Drobeta', 120), ('Rimnicu Vilcea', 146)],
'Rimnicu Vilcea': [('Craiova', 146), ('Sibiu', 80), ('Pitesti',
97)],
'Fagaras': [('Sibiu', 99), ('Bucharest', 211)],
'Oradea': [('Sibiu', 151), ('Zerind', 71)],
'Zerind': [('Oradea', 71), ('Arad', 75)],
'Pitesti': [('Rimnicu Vilcea', 97), ('Bucharest', 101)],
'Bucharest': [('Fagaras', 211), ('Pitesti', 101), ('Giurgiu',
90)],
}
heuristics = {
'Arad': 366, 'Sibiu': 253, 'Timisoara': 329, 'Lugoj': 244,
'Mehadia': 241, 'Drobeta': 242, 'Craiova': 160, 'Rimnicu
Vilcea': 193,
'Fagaras': 176, 'Oradea': 380, 'Zerind': 374, 'Pitesti': 100,
'Bucharest': 0, 'Giurgiu': 77,
}
while open_set:
f_cost, path = heapq.heappop(open_set)
city = path[-1]
if city == goal:
return path
if city not in visited:
visited.add(city)
for neighbor, cost in graph[city]:
g_cost = len(path)
f_neighbor = g_cost + 1 + heuristics[neighbor]
heapq.heappush(open_set, (f_neighbor, path +
[neighbor]))
return None
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
data = {
'Patrons': ['Few', 'Full', 'Full', 'Few', 'Full', 'Full', 'Few'],
'Reservation': ['Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No'],
'WaitEstimate': ['Short', 'Long', 'None', 'Medium', 'Short',
'Medium', 'Long'],
'Weather': ['Sunny', 'Rainy', 'Sunny', 'Rainy', 'Sunny', 'Rainy',
'Sunny'],
'WillWait': ['Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'No']
}
df = pd.get_dummies(pd.DataFrame(data).drop('WillWait', axis=1))
y = (pd.DataFrame(data)['WillWait'] == 'Yes').astype(int).values
X_train, X_test, y_train, y_test = train_test_split(df.values, y,
test_size=0.3, random_state=42)
class NeuralNetwork:
def __init__(self, input_size, hidden_size, learning_rate=0.1):
self.learning_rate = learning_rate
self.weights_input_hidden = np.random.rand(input_size,
hidden_size)
self.weights_hidden_output = np.random.rand(hidden_size, 1)
nn = NeuralNetwork(X_train.shape[1], 5)
nn.train(X_train, y_train)
y_pred = nn.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test,
y_pred))