0% found this document useful (0 votes)
8 views

AIML

Python and Prolong code for some AI ML Lab practicals

Uploaded by

apple.slc.chn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AIML

Python and Prolong code for some AI ML Lab practicals

Uploaded by

apple.slc.chn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

#1 Find-S Algorithm

import pandas as pd
import numpy as np

# To read the data in the CSV file


data = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/Datasets/Sport car
price.csv")
print(data, "\n")

# Making an array of all the attributes


d = np.array(data)[:, :-1]
print("\nThe attributes are: ", d)

# Segregating the target that has positive and negative examples


target = np.array(data)[:, -1]
print("\nThe target is: ", target)

# Training function to implement find-s algorithm


def train(c, t):
specific_hypothesis = None
for i, val in enumerate(t):
if val == "Yes":
specific_hypothesis = c[i].copy()
break

for i, val in enumerate(c):


if t[i] == "Yes":
for x in range(len(specific_hypothesis)):
if val[x] != specific_hypothesis[x]:
specific_hypothesis[x] = '?'
else:
pass

return specific_hypothesis

# Obtaining the final hypothesis


print("\nThe final hypothesis is:", train(d, target))

#2 Candidate Elimination Algorithm

import numpy as np
import pandas as pd

# Load the dataset


data = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/Datasets/Sport car
price.csv")
concepts = np.array(data.iloc[:, 0:-1])
print("\nInstances are:\n", concepts[:10]) # Show only the first 10 instances for
brevity
target = np.array(data.iloc[:, -1])
print("\nTarget Values are: ", target[:10]) # Show only the first 10 target values
for brevity

def learn(concepts, target):


specific_h = concepts[0].copy()
print("\nInitialization of specific_h and general_h")
print("\nSpecific Boundary: ", specific_h)
general_h = [["?" for i in range(len(specific_h))] for i in
range(len(specific_h))]
print("\nGeneric Boundary: ", general_h)

# Limit the number of instances to process


num_instances = min(10, len(concepts))

for i, h in enumerate(concepts[:num_instances]):
print("\nInstance", i+1, "is ", h)
if target[i] == "yes":
print("Instance is Positive ")
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'

if target[i] == "no":
print("Instance is Negative ")
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'

print("Specific Boundary after ", i+1, "Instance is ", specific_h)


print("Generic Boundary after ", i+1, "Instance is ", general_h)
print("\n")

indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?',


'?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])

return specific_h, general_h

# Run the learning algorithm


s_final, g_final = learn(concepts, target)

# Display the final results


print("Final Specific_h: ", s_final, sep="\n")
print("Final General_h: ", g_final, sep="\n")

#3 Decision Tree

import pandas as pd
import math
import numpy as np

data = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/PlayTennis.csv")
data = data.drop(columns=["Unnamed: 0"])
features = [feat for feat in data.columns if feat != "PlayTennis"]

class Node:
def __init__(self):
self.children = []
self.value = ""
self.isLeaf = False
self.pred = ""
def entropy(examples):
pos = 0.0
neg = 0.0
for _, row in examples.iterrows():
if row["PlayTennis"] == "Yes":
pos += 1
else:
neg += 1
if pos == 0.0 or neg == 0.0:
return 0.0
else:
p = pos / (pos + neg)
n = neg / (pos + neg)
return -(p * math.log(p, 2) + n * math.log(n, 2))

def info_gain(examples, attr):


uniq = np.unique(examples[attr])
gain = entropy(examples)
for u in uniq:
subdata = examples[examples[attr] == u]
sub_e = entropy(subdata)
gain -= (float(len(subdata)) / float(len(examples))) * sub_e
return gain

def ID3(examples, attrs):


root = Node()
max_gain = 0
max_feat = ""
for feature in attrs:
gain = info_gain(examples, feature)
if gain > max_gain:
max_gain = gain
max_feat = feature
root.value = max_feat

uniq = np.unique(examples[max_feat])
for u in uniq:
subdata = examples[examples[max_feat] == u]
if entropy(subdata) == 0.0:
newNode = Node()
newNode.isLeaf = True
newNode.value = u
newNode.pred = np.unique(subdata["PlayTennis"])[0]
root.children.append(newNode)
else:
dummyNode = Node()
dummyNode.value = u
new_attrs = attrs.copy()
new_attrs.remove(max_feat)
child = ID3(subdata, new_attrs)
dummyNode.children.append(child)
root.children.append(dummyNode)
return root

def printTree(root: Node, depth=0):


for i in range(depth):
print("\t", end="")
print(root.value, end="")
if root.isLeaf:
print(" -> ", root.pred)
print()
for child in root.children:
printTree(child, depth + 1)

root = ID3(data, features)


printTree(root)

#4 Back Propagtion Algorithm

import numpy as np

X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)


y = np.array(([92], [86], [89]), dtype=float)
X = X / np.amax(X, axis=0) # maximum of X array longitudinally
y = y / 100

# Sigmoid Function
def sigmoid(x):
return 1 / (1 + np.exp(-x))

# Derivative of Sigmoid Function


def derivatives_sigmoid(x):
return x * (1 - x)

# Variable initialization
epoch = 5 # Setting training iterations
lr = 0.1 # Setting learning rate

inputlayer_neurons = 2 # number of features in data set


hiddenlayer_neurons = 3 # number of hidden layers neurons
output_neurons = 1 # number of neurons at output layer

# weight and bias initialization


wh = np.random.uniform(size=(inputlayer_neurons, hiddenlayer_neurons))
bh = np.random.uniform(size=(1, hiddenlayer_neurons))
wout = np.random.uniform(size=(hiddenlayer_neurons, output_neurons))
bout = np.random.uniform(size=(1, output_neurons))

# draws a random range of numbers uniformly of dim x*y


for i in range(epoch):
# Forward Propagation
hinp1 = np.dot(X, wh)
hinp = hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1 = np.dot(hlayer_act, wout)
outinp = outinp1 + bout
output = sigmoid(outinp)

# Backpropagation
EO = y - output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act) # how much hidden layer wts
contributed to error
d_hiddenlayer = EH * hiddengrad

wout += hlayer_act.T.dot(d_output) * lr # dot product of next layer error and


current layer output
wh += X.T.dot(d_hiddenlayer) * lr

print("-----------Epoch-", i + 1, "Starts----------")
print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n", output)
print("-----------Epoch-", i + 1, "Ends----------\n")

print("Input: \n" + str(X))


print("Actual Output: \n" + str(y))
print("Predicted Output: \n", output)

#5 Naive Bayesian Classifier

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline

dataset = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/tennisdata.csv")

X = dataset.iloc[:, [0,1]].values
y = dataset.iloc[:, 2].values

le1 = LabelEncoder()
le2 = LabelEncoder()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.25,


random_state=0)

X_train[:, 0] = le1.fit_transform(X_train[:, 0])


X_train[:, 1] = le2.fit_transform(X_train[:, 1])

X_test[:, 0] = le1.transform(X_test[:, 0])


X_test[:, 1] = le2.transform(X_test[:, 1])

clf = GaussianNB()

clf.fit(X_train, y_train)

y_pred1 = clf.predict(X_test)

print("Accuracy:", accuracy_score(y_test, y_pred1))

#6 Naive Bayesian Classification and Documnetation

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, recall_score, precision_score,
confusion_matrix

dataset = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/document.csv")

# Check the shape of the dataset


print(dataset.shape)

# Assuming the last column is the target variable


X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

# Convert categorical variables into numerical variables


le = LabelEncoder()
for col in X.columns:
if X[col].dtype == 'object':
X[col] = le.fit_transform(X[col])

X = X.values
y = le.fit_transform(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.25,


random_state=0)

sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

classifer1 = GaussianNB()

classifer1.fit(X_train, y_train)

y_pred1 = classifer1.predict(X_test)

print('Accuracy Metrics: \n')


print('Accuracy: ', accuracy_score(y_test, y_pred1))
print('Recall: ', recall_score(y_test, y_pred1))
print('Precision: ', precision_score(y_test, y_pred1))
print('Confusion Matrix: \n', confusion_matrix(y_test, y_pred1))

#7 Bayesian Network

#!pip install pgmpy

import numpy as np
import pandas as pd
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination

# Load the dataset


heartDisease = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/heartdisease.csv")

# Replace missing values with NaN


heartDisease = heartDisease.replace('?', np.nan)
# Print sample instances from the dataset
print('Sample instances from the dataset are given below')
print(heartDisease.head())

# Print attributes and datatypes


print('\nAttributes and datatypes')
print(heartDisease.dtypes)

# Define the Bayesian Network model


model = BayesianNetwork([('age', 'heartdisease'),
('Gender', 'heartdisease'),
('Family', 'heartdisease'),
('diet', 'heartdisease'),
('Lifestyle', 'heartdisease'),
('cholestrol', 'heartdisease')])

# Learn the Conditional Probability Distributions (CPDs) using Maximum Likelihood


Estimator
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(heartDisease, estimator=MaximumLikelihoodEstimator)

# Perform inference with Bayesian Network


HeartDiseasetest_infer = VariableElimination(model)

# Query the model


print('\n1. Probability of HeartDisease given evidence= cholestrol')
q1 = HeartDiseasetest_infer.query(variables=['heartdisease'],
evidence={'cholestrol': 1})
print(q1)

print('\n2. Probability of HeartDisease given evidence= cholestrol')


q2 = HeartDiseasetest_infer.query(variables=['heartdisease'],
evidence={'cholestrol': 2})
print(q2)

#8 EM and KMeans Algorithm

from sklearn.cluster import KMeans


from sklearn.mixture import GaussianMixture
import sklearn.metrics as metrics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

names = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width', 'Class']

# Load the iris dataset


iris = load_iris()
dataset = pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=names)

X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

plt.figure(figsize=(14,7))
colormap=np.array(['red','lime','black'])

plt.subplot(1,3,1)
plt.title('Real')
# Convert y to integers for indexing
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y.astype(int)])

model=KMeans(n_clusters=3, random_state=0).fit(X)
plt.subplot(1,3,2)
plt.title('KMeans')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_])

print('The accuracy score of K-Mean: ',metrics.accuracy_score(y, model.labels_))


print('The Confusion matrix of K-Mean:\n',metrics.confusion_matrix(y,
model.labels_))

gmm=GaussianMixture(n_components=3, random_state=0).fit(X)
y_cluster_gmm=gmm.predict(X)
plt.subplot(1,3,3)
plt.title('GMM Classification')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm])

print('The accuracy score of EM: ',metrics.accuracy_score(y, y_cluster_gmm))


print('The Confusion matrix of EM:\n ',metrics.confusion_matrix(y, y_cluster_gmm))

plt.show()

#9 K-Nearest Neighbours

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.datasets import load_iris

# Load dataset
iris = load_iris()

# Define column names


names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']

# Read dataset into pandas dataframe


df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target

# Features and target variable


X = df.iloc[:, :-1]
y = df.iloc[:, -1]

print(X.head())

# Split dataset into training and testing sets


Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.10)

# Initialize and train the classifier


classifier = KNeighborsClassifier(n_neighbors=5).fit(Xtrain, ytrain)

# Make predictions
ypred = classifier.predict(Xtest)

# Print results
i = 0
print("\
n-------------------------------------------------------------------------")
print('%-25s %-25s %-25s' % ('Original Label', 'Predicted Label', 'Correct/Wrong'))
print("-------------------------------------------------------------------------")
for label in ytest:
print('%-25s %-25s' % (label, ypred[i]), end="")
if label == ypred[i]:
print(' %-25s' % ('Correct'))
else:
print(' %-25s' % ('Wrong'))
i = i + 1
print("-------------------------------------------------------------------------")

# Print evaluation metrics


print("\nConfusion Matrix:\n", metrics.confusion_matrix(ytest, ypred))
print("-------------------------------------------------------------------------")
print("\nClassification Report:\n", metrics.classification_report(ytest, ypred))
print("-------------------------------------------------------------------------")
print('Accuracy of the classifier is %0.2f' % metrics.accuracy_score(ytest, ypred))
print("-------------------------------------------------------------------------")

#12 Depth First Search

% solve(Node, Solution) - Find an acyclic path (in reverse order) from Node to a
goal
solve(Node, Solution) :-
depthfirst([], Node, Solution).

% depthfirst(Path, Node, Solution)


% Extending the path [Node | Path] to a goal gives Solution
depthfirst(Path, Node, [Node | Path]) :-
goal(Node).

depthfirst(Path, Node, Sol) :-


s(Node, Node1),
\+ member(Node1, Path), % Prevent a cycle
depthfirst([Node | Path], Node1, Sol).

% depthfirst2(Node, Solution, Maxdepth)


% Perform depth-limited search with depth Maxdepth
depthfirst2(Node, [Node], _) :-
goal(Node).

depthfirst2(Node, [Node | Sol], Maxdepth) :-


Maxdepth > 0,
s(Node, Node1),
Max1 is Maxdepth - 1,
depthfirst2(Node1, Sol, Max1).

% Example definitions (for completeness)


% Define the goal node
goal(goal_node).

% Define the graph


s(start_node, node1).
s(node1, node2).
s(node2, goal_node).

% Initialization Directive
:- initialization(main).

% Main Predicate for Testing


main :-
% Test the depth-first search
solve(start_node, Solution1),
format('Depth-First Search Solution: ~w~n', [Solution1]),

% Test the depth-limited search with a maximum depth of 3


depthfirst2(start_node, Solution2, 3),
format('Depth-Limited Search Solution (depth 3): ~w~n', [Solution2]),

halt. % Ensure the program halts after execution

#14 BFS

import heapq

class Node:
def __init__(self, state, parent, cost, heuristic):
self.state = state
self.parent = parent
self.cost = cost
self.heuristic = heuristic

def __lt__(self, other):


return self.heuristic < other.heuristic

def best_first_search(start, goal, heuristic_fn, get_neighbors_fn):


open_list = []
closed_list = set()
start_node = Node(start, None, 0, heuristic_fn(start, goal))
heapq.heappush(open_list, start_node)

while open_list:
current_node = heapq.heappop(open_list)
if current_node.state == goal:
return reconstruct_path(current_node)

closed_list.add(current_node.state)

for neighbor, cost in get_neighbors_fn(current_node.state):


if neighbor in closed_list:
continue

neighbor_node = Node(neighbor, current_node, current_node.cost + cost,


heuristic_fn(neighbor, goal))

for open_node in open_list:


if open_node.state == neighbor and open_node.cost <=
neighbor_node.cost:
break
else:
heapq.heappush(open_list, neighbor_node)

return None

def reconstruct_path(node):
path = []
while node:
path.append(node.state)
node = node.parent
return path[::-1]

def manhattan_distance(state, goal):


return abs(state[0] - goal[0]) + abs(state[1] - goal[1])

def get_neighbors(state):
neighbors = []
x, y = state
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for move in moves:
neighbor = (x + move[0], y + move[1])
if 0 <= neighbor[0] < 5 and 0 <= neighbor[1] < 5:
neighbors.append((neighbor, 1))
return neighbors

start = (0, 0)
goal = (4, 4)
path = best_first_search(start, goal, manhattan_distance, get_neighbors)
print("Path found:", path)

#15 Travelling Salesman Problem

% Production Rules
route(Town1, Town2, Distance) :-
road(Town1, Town2, Distance).

route(Town1, Town2, Distance) :-


road(Town1, X, Dist1),
route(X, Town2, Dist2),
Distance is Dist1 + Dist2,
!.

% Domains
% town = symbol
% distance = integer

% Predicates
% nondeterm road(town, town, distance)
% nondeterm route(town, town, distance)

% Clauses
road("tampa", "houston", 200).
road("gordon", "tampa", 300).
road("houston", "gordon", 100).
road("houston", "kansas_city", 120).
road("gordon", "kansas_city", 130).

% Initialization Directive
:- initialization(main).

% Main Predicate for Testing


main :-
% Example query
route("tampa", "kansas_city", Distance),
format('Distance from tampa to kansas_city is ~w~n', [Distance]),
% You can add more test queries here
fail. % To ensure it doesn’t stop at the first result, you can use fail or
remove it if you only want the first result

You might also like