import queue as Q
from RMP import dict_gn
start = 'Arad'
goal = 'Bucharest'
result = ''
def BFS(city, cityq, visitedq):
global result
if city == start:
result = result + ' '+city
for eachcity in dict_gn[city].keys():
if eachcity == goal:
result = result+' '+eachcity
return
if eachcity not in cityq.queue and eachcity not in visitedq.queue:
cityq.put(eachcity)
result = result+' '+eachcity
visitedq.put(city)
BFS(cityq.get(), cityq, visitedq)
def DLS(city, visitedstack, startlimit, endlimit):
global result
found = 0
result = result+city+''
visitedstack.append(city)
if city == goal:
return 1
if startlimit == endlimit:
return 0
for eachcity in dict_gn[city].keys():
if eachcity not in visitedstack:
found = DLS(eachcity, visitedstack, startlimit+1, endlimit)
if found:
return found
def IDDFS(city, visitedstack, endlimit):
global result
for i in range(0, endlimit):
print("Searching at Limit: ", i)
found = DLS(city, visitedstack, 0, 1)
if found:
print("Found")
break
else:
print("Not found!")
print(result)
print("--------")
result = ''
visitedstack = []
import queue as Q
from RMP import dict_gn
from RMP import dict_hn
start='Arad'
goal='Bucharest'
result=''
def get_fn(citystr):
cities=citystr.split(',')
hn=gn=0
for ctr in range(0,len(cities)-1):
gn=gn+dict_gn[cities[ctr]][cities[ctr+1]]
hn=dict_hn[cities[len(cities)-1]]
return(hn+gn)
def printout(cityq):
for i in range(0,cityq.qsize()):
print(cityq.queue[i])
def expand(cityq):
global result
tot,citystr,thiscity=cityq.get()
nexttot=999
if not cityq.empty():
nexttot,nextcitystr,nextthiscity=cityq.queue[0]
if thiscity==goal and tot<nexttot:
result=citystr+'::'+str(tot)
return
print("Expanded city---------------",thiscity)
print("Second best f(n)--------------",nexttot)
tempq=Q.PriorityQueue()
for cty in dict_gn[thiscity]:
tempq.put((get_fn(citystr+','+cty),citystr+','+cty,cty))
for ctr in range(1,3):
ctrtot,ctrcitystr,ctrthiscity=tempq.get()
if ctrtot<nexttot:
cityq.put((ctrtot,ctrcitystr,ctrthiscity))
else:
cityq.put((ctrtot,citystr,thiscity))
break
printout(cityq)
expand(cityq)
def main():
cityq=Q.PriorityQueue()
thiscity=start
cityq.put((999,"NA","NA"))
cityq.put((get_fn(start),start,thiscity))
expand(cityq)
print(result)
main()
import numpy as np
import pandas as pd
import sklearn as sk
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
def splitdataset(balance_data):
# seperating the target variable
X = balance_data.values[:, 1:5]
Y = balance_data.values[:, 0]
# splitting the dataset into train and test
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size=0.3, random_state=100)
return X, Y, X_train, X_test, y_train, y_test
# function to perform training with entropy
def train_using_entropy(X_train, X_test, y_train, y_test):
# decision tree with entropy
clf_entropy = DecisionTreeClassifier(
criterion="entropy", random_state=100, max_depth=3, min_samples_leaf=5)
# performing training
clf_entropy.fit(X_train, y_train)
return clf_entropy
def tot(attribute, value):
count = 0
for key, val in rwp_examples.items():
for key1, val1 in val.items():
if key1 == attribute:
if val1 == value:
count += 1
return count
def getProbab(attribute, attribval, value):
count = 0
for key, val in rwp_examples.items():
val1 = rwp_examples[key][attribute]
val2 = rwp_examples[key]['ans']
if val1 == attribval and val2 == value:
count += 1
probab = count / tot('ans', value)
return probab
def main():
PAltYes = tot('Alt', 'Y') / total_exp
PAltNo = tot('Alt', 'N') / total_exp
PBarYes = tot('Bar', 'Y') / total_exp
PBarNo = tot('Bar', 'N') / total_exp
PFriYes = tot('Fri', 'Y') / total_exp
PFriNo = tot('Fri', 'N') / total_exp
PHunYes = tot('Hun', 'Y') / total_exp
PHunNo = tot('Hun', 'N') / total_exp
PPatSome = tot('Pat', 'S') / total_exp
PPatFull = tot('Pat', 'F') / total_exp
PPatNone = tot('Pat', 'N') / total_exp
PPriceCheap = tot('Price', '$') / total_exp
PPriceAvg = tot('Price', '$$') / total_exp
PPriceExp = tot('Price', '$$$') / total_exp
PRainYes = tot('Rain', 'Y') / total_exp
PRainNo = tot('Rain', 'N') / total_exp
PResYes = tot('Res', 'Y') / total_exp
PResNo = tot('Res', 'N') / total_exp
PTypeFrench = tot('Type', 'F') / total_exp
PTypeItalian = tot('Type', 'I') / total_exp
PTypeBurger = tot('Type', 'B') / total_exp
PTypeThai = tot('Type', 'T') / total_exp
PEstFew = tot('Est', '0-10') / total_exp
PEstMore = tot('Est', '10-30') / total_exp
PEstStillMore = tot('Est', '30-60') / total_exp
PEstTooMuch = tot('Est', '>60') / total_exp
PAnsYes = tot('ans', 'Y') / total_exp
PAnsNo = tot('ans', 'N') / total_exp
print('Probability for will wait if there is an Alternate Restaurant Nearby: ')
print('Yes: Will Wait ', (getProbab(
'Alt', 'Y', 'Y') * PAnsYes/PAltYes) * 100, '%')
print('No: Will Wait ', (getProbab('Alt', 'Y', 'N') * PAnsNo/PAltYes) * 100, '%')
print('Probability for will wait if there No is an Alternate Restaurant Nearby: ')
print('Yes: Will Wait ', (getProbab('Alt', 'N', 'Y') * PAnsYes/PAltNo) * 100, '%')
print('No: Will Wait ', (getProbab('Alt', 'N', 'N') * PAnsNo/PAltNo) * 100, '%')
print('Probability for will wait if Estimated Wait time is 0-10 minutes: ')
print('Yes: Will Wait ', (getProbab(
'Est', '0-10', 'Y') * PAnsYes/PEstFew) * 100, '%')
print('No: Will Wait ', (getProbab(
'Est', '0-10', 'N') * PAnsNo/PEstFew) * 100, '%')
print('Probability for will wait if Estimated Wait time is 10-30 minutes ')
print('Yes: Will Wait ', (getProbab('Est', '10-30', 'Y')
* PAnsYes/PEstMore) * 100, '%')
print('No: Will Wait ', (getProbab('Est', '10-30', 'N')
* PAnsNo/PEstMore) * 100, '%')
print("Probability for Will Wait if the Estimated Wait Time is 30-60 mins: ")
print("Yes: Will Wait: ", (getProbab('Est', '30-60', 'Y')
* PAnsYes/PEstStillMore)*100, "%")
print("No: Will Wait: ", (getProbab('Est', '30-60', 'N')
* PAnsNo/PEstStillMore)*100, "%")
print("Probability for Will Wait if the Estimated Wait Time is >60 mins: ")
print("Yes: Will Wait: ", (getProbab(
'Est', '>60', 'Y')*PAnsYes/PEstTooMuch)*100, "%")
print("No: Will Wait: ", (getProbab(
'Est', '>60', 'N')*PAnsNo/PEstTooMuch)*100, "%")
print('Probability for will wait if there are Some Patrons ')
print('Yes: Will Wait ', (getProbab('Pat', 'S', 'Y')
* PAnsYes/PPatSome) * 100, '%')
print('No: Will Wait ', (getProbab('Pat', 'S', 'N') * PAnsNo/PPatSome) * 100, '%')
print("Probability for Will Wait if there are None Patrons: ")
print("Yes: Will Wait: ", (getProbab('Pat', 'N', 'Y')*PAnsYes/PPatNone)*100, "%")
print("No: Will Wait: ", (getProbab('Pat', 'N', 'N')*PAnsNo/PPatNone)*100, "%")
print("Probability for Will Wait if there are Full Patrons: ")
print("Yes: Will Wait: ", (getProbab('Pat', 'F', 'Y')*PAnsYes/PPatFull)*100, "%")
print("No: Will Wait: ", (getProbab('Pat', 'F', 'N')*PAnsNo/PPatFull)*100, "%")
print('Probability for will wait if the place is Thai ')
print('Yes: Will Wait ', (getProbab('Type', 'T', 'Y')
* PAnsYes/PTypeThai) * 100, '%')
print('No: Will Wait ', (getProbab('Type', 'T', 'N')
* PAnsNo/PTypeThai) * 100, '%')
main()
Practical 7
AIM: Implement feed forward back propagation neural network learning algorithm.