0% found this document useful (0 votes)
10 views12 pages

Machine Learning Algorithms Overview

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)
10 views12 pages

Machine Learning Algorithms Overview

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

#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
[Link]")
print(data, "\n")

# Making an array of all the attributes


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

# Segregating the target that has positive and negative examples


target = [Link](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
[Link]")
concepts = [Link]([Link][:, 0:-1])
print("\nInstances are:\n", concepts[:10]) # Show only the first 10 instances for
brevity
target = [Link]([Link][:, -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/[Link]")
data = [Link](columns=["Unnamed: 0"])
features = [feat for feat in [Link] if feat != "PlayTennis"]

class Node:
def __init__(self):
[Link] = []
[Link] = ""
[Link] = False
[Link] = ""
def entropy(examples):
pos = 0.0
neg = 0.0
for _, row in [Link]():
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 * [Link](p, 2) + n * [Link](n, 2))

def info_gain(examples, attr):


uniq = [Link](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
[Link] = max_feat

uniq = [Link](examples[max_feat])
for u in uniq:
subdata = examples[examples[max_feat] == u]
if entropy(subdata) == 0.0:
newNode = Node()
[Link] = True
[Link] = u
[Link] = [Link](subdata["PlayTennis"])[0]
[Link](newNode)
else:
dummyNode = Node()
[Link] = u
new_attrs = [Link]()
new_attrs.remove(max_feat)
child = ID3(subdata, new_attrs)
[Link](child)
[Link](dummyNode)
return root

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


for i in range(depth):
print("\t", end="")
print([Link], end="")
if [Link]:
print(" -> ", [Link])
print()
for child in [Link]:
printTree(child, depth + 1)

root = ID3(data, features)


printTree(root)

#4 Back Propagtion Algorithm

import numpy as np

X = [Link](([2, 9], [1, 5], [3, 6]), dtype=float)


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

# Sigmoid Function
def sigmoid(x):
return 1 / (1 + [Link](-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 = [Link](size=(inputlayer_neurons, hiddenlayer_neurons))
bh = [Link](size=(1, hiddenlayer_neurons))
wout = [Link](size=(hiddenlayer_neurons, output_neurons))
bout = [Link](size=(1, output_neurons))

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


for i in range(epoch):
# Forward Propagation
hinp1 = [Link](X, wh)
hinp = hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1 = [Link](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.[Link](d_output) * lr # dot product of next layer error and


current layer output
wh += [Link](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 [Link] as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score
from [Link] import Pipeline

dataset = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/[Link]")

X = [Link][:, [0,1]].values
y = [Link][:, 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] = [Link](X_test[:, 0])


X_test[:, 1] = [Link](X_test[:, 1])

clf = GaussianNB()

[Link](X_train, y_train)

y_pred1 = [Link](X_test)

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

#6 Naive Bayesian Classification and Documnetation

import numpy as np
import [Link] as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler, LabelEncoder
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, recall_score, precision_score,
confusion_matrix

dataset = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/[Link]")

# Check the shape of the dataset


print([Link])

# Assuming the last column is the target variable


X = [Link][:, :-1]
y = [Link][:, -1]

# Convert categorical variables into numerical variables


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

X = [Link]
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()

[Link](X_train, y_train)

y_pred1 = [Link](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 [Link] import MaximumLikelihoodEstimator
from [Link] import BayesianNetwork
from [Link] import VariableElimination

# Load the dataset


heartDisease = pd.read_csv("/content/drive/MyDrive/Colab
Notebooks/Datasets/[Link]")

# Replace missing values with NaN


heartDisease = [Link]('?', [Link])
# Print sample instances from the dataset
print('Sample instances from the dataset are given below')
print([Link]())

# Print attributes and datatypes


print('\nAttributes and datatypes')
print([Link])

# 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')
[Link](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 [Link] import KMeans


from [Link] import GaussianMixture
import [Link] as metrics
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import load_iris

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

# Load the iris dataset


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

X = [Link][:, :-1]
y = [Link][:, -1]

[Link](figsize=(14,7))
colormap=[Link](['red','lime','black'])

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

model=KMeans(n_clusters=3, random_state=0).fit(X)
[Link](1,3,2)
[Link]('KMeans')
[Link](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=[Link](X)
[Link](1,3,3)
[Link]('GMM Classification')
[Link](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))

[Link]()

#9 K-Nearest Neighbours

import numpy as np
import pandas as pd
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from [Link] 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 = [Link]([Link], columns=iris.feature_names)
df['target'] = [Link]

# Features and target variable


X = [Link][:, :-1]
y = [Link][:, -1]

print([Link]())

# 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 = [Link](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):
[Link] = state
[Link] = parent
[Link] = cost
[Link] = heuristic

def __lt__(self, other):


return [Link] < [Link]

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))
[Link](open_list, start_node)

while open_list:
current_node = [Link](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:
[Link](open_list, neighbor_node)

return None

def reconstruct_path(node):
path = []
while node:
[Link]([Link])
node = [Link]
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:
[Link]((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