Docsity

Genetic algorithm and Naive bayes, Assignments of Artificial Intelligence

Partial preview of the text

Download Genetic algorithm and Naive bayes and more Assignments Artificial Intelligence in PDF only on Docsity!

Date: May 9, 2022

Artificial

Intelligenc

e

LAB TASK

Question 1:

Solve Traveling Salesman Problem using Genetic algorithm for set of 5 cities to find a minimum shortest path cost (optimal).

Code:

-- coding: utf-8 --

""" Created on Sun May 8 2:26:13 2022 @author: laraib """ import random def randomSolution(n): numbers = list(range(len(n))) sol = [] for i in range(len(n)): randomNumber = numbers[random.randint(0, len(numbers) - 1)] sol.append(randomNumber) numbers.remove(randomNumber) return sol def routeLength(n, sol): routeLength = 0 for i in range(len(sol)): routeLength += n[sol[i - 1]][sol[i]] return routeLength def getNeighbours(sol): neighbours = [] for i in range(len(sol)): for j in range(i + 1, len(sol)): neighbour = sol.copy() neighbour[i] = sol[j] neighbour[j] = sol[i] neighbours.append(neighbour) return neighbours def getBestNeighbour(n, neighbours): bestRouteLength = routeLength(n, neighbours[0]) bestNeighbour = neighbours[0] for neighbour in neighbours: currentRouteLength = routeLength(n, neighbour) if currentRouteLength < bestRouteLength:

Question 2:

Implement Naïve bayes classifier algorithm for text classification.

You may use any type of Naïve bayes classifier for text

classification.

Code:

-- coding: utf-8 --

""" Created on Sun May 6 23:42:21 2022 @author: laraib """

Create the dataset

data ='''Example No.,Color,Type,Origin,Stolen 1,Red,Sports,Domestic,Yes 2,Red,Sports,Domestic,No 3,Red,Sports,Domestic,Yes 4,Yello,Sports,Domestic,No 5,Yello,Sports,Imported,Yes 6,Yellow,SUV,Imported,No 7,Yellow,SUV,Imported,Yes 8,Yellow,SUV,Domestic,No 9,Red,SUV,Imported,No 10,Red,Sports,Imported,Yes''' filename = "a.csv" f = open(filename,"w") f.write(data) f.close() import pandas as pd class Naive_Bayes_Classifier: def init(self): pass def fit(self, data): features = data.drop(['Stolen'], axis=1) self.ds_features = {}

get P(Yes) and P(No)

total_yes = len(data['Stolen'].loc[data['Stolen'] == 'Yes']) total_no = len(data['Stolen'].loc[data['Stolen'] == 'No']) self.p_yes = total_yes / len(data['Stolen']) self.p_no = total_no / len(data['Stolen'])

create the data structure and populate it

for c in features.columns: self.ds_features[c] = {} for item in list(set(features[c])):

self.ds_features[c][item] = {} x = data[(data[c]==item)] y = len(x['Stolen'].loc[x['Stolen']=='Yes']) self.ds_features[c][item]['YES'] = y / total_yes n = len(x['Stolen'].loc[x['Stolen']=='No']) self.ds_features[c][item]['NO'] = n / total_no def predict(self, l): ans = {'Yes': 1, 'No': 1} async def func_name(*args, **kwargs):pass ans['Yes'] *= self.ds_features['Color'][l[0]]['YES'] * self.ds_features['Type'][l[1]]['YES'] * self.ds_features['Origin'][l[2]]['YES'] * self.p_yes ans['No'] *= self.ds_features['Color'][l[0]]['NO'] * self.ds_features['Type'][l[1]]['NO'] * self.ds_features['Origin'][l[2]]['NO'] * self.p_no return max(ans, key=lambda x: ans[x]) def main(): naive_bayes = Naive_Bayes_Classifier() # create the object of the class data = pd.read_csv(filename) # Read the dataset naive_bayes.fit(data) # Fit the naive bayes classifier pred = naive_bayes.predict(['Red', 'SUV', 'Domestic']) # Predict the probability print(f'For the data Red , Domestic, SUV, the prediction was: STOLEN = {pred}') main()