AI_lab(manual)
AI_lab(manual)
while queue:
s = queue.pop(0)
print(s, end=" ")
# Driver Code
bfs(visited, graph, 'A')
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
return self.get_paths(dict)
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
print(ob.solve(matrix))
def solveNQ():
board = [[0, 0, 0, 0],
[0, 0, 0, 0],
4
[0, 0, 0, 0],
[0, 0, 0, 0]]
if not solveNQUtil(board, 0):
print("Solution does not exist")
return False
printSolution(board)
return True
if maximizingPlayer:
best = MIN
# Recur for left and right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
else:
best = MAX
# Recur for left and right children
for i in range(0, 2):
val = minimax(depth + 1, nodeIndex * 2 + i, True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)
# Alpha Beta Pruning
if beta <= alpha:
break
return best
# Driver Code
if name == " main ":
values = [3, 5, 6, 9, 1, 2, 0, -1]
print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX))
5
8. Write a program to implement KNN algorithm to classify Iris dataset. Print both correct
and wrong predictions.
# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Replace 'your_file_path.csv' with the actual path to your Iris dataset file
file_path = 'your_file_path.csv'
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
9. Train a random data sample using linear regression model and plot the graph.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Make predictions
X_new = np.array([[0], [2]])
y_pred = model.predict(X_new)
10. Implement the naïve Bayesian classifier for a sample training data set stored as a .csv file.
Compute the accuracy of the classifier, considering few test data sets.
# Replace 'your_file_path.csv' with the actual path to your Iris dataset file
file_path = 'your_file_path.csv'
# Standardize the features (not necessary for Naive Bayes, but doesn't hurt)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
11. Demonstrate the working of SVM classifier for a suitable data set(e.g., iris dataset)
# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.datasets import load_iris
# Replace 'your_file_path.csv' with the actual path to your Iris dataset file
file_path = 'your_file_path.csv'
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
12. Build a sample binary image classification model (cat and dog).
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Now, from the downloaded dataset, create train, validation, and test folders, and split cat and
#dog images into these folders in the ratio of 70:20:10.
# Define constants
batch_size = 32
img_height = 150
img_width = 150
epochs = 10
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary'
)
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary'
)
NOTE:
Cat Dog Dataset for Program No. 12 can be downloaded from the following link
https://www.kaggle.com/datasets/tongpython/cat-and-dog