Skip to content

Commit

Permalink
Completed KNN and GNB
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamran authored and Kamran committed Dec 3, 2019
1 parent fda4f22 commit fefc280
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
20 changes: 18 additions & 2 deletions project3/Code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ def bayes_naive(self, predictData, trainData):
f_score.append(h.findFMeasure(tmpPrecision, tmpRecall))
return accuracy, precision, recall, f_score

def bayes_naive_demo(self, predictData, trainData):
h = hp()
nb = bayes()
classPriorProbabilities = nb.findClassPriorProbability(trainData)
classes = nb.segregateClasses(trainData)
occurences, means, stdDev = nb.findDescriptorPosteriorProbabilites(classes, trainData)
probabilities = nb.classify_demo(predictData, classPriorProbabilities, occurences, means, stdDev)
for key in probabilities:
print("P(X|H{})*P(H{}) = {}".format(key,key,probabilities[key]))

def decision_tree(self, kCrossValidation = 10):
print("\nRunning Decision Tree Classifier ....................\n")
from decision_tree import decisionTree
h = hp()
fileName = h.get_fileName()
# filePath = "../Data/"+fileName+".txt"
filePath = "CSE-601/project3/Data/"+fileName+".txt"
filePath = "../Data/"+fileName+".txt"
# filePath = "CSE-601/project3/Data/"+fileName+".txt"
data, labels = h.readData(filePath)
data = h.oneHotEncoding(data, labels)
dt = decisionTree()
Expand Down Expand Up @@ -204,6 +214,7 @@ def random_forest(self, kCrossValidation = 10):
predictData = h.get_file_demo(h.get_fileName(), fileType='predictData')
accuracy, precision, recall, f_score = m.knnDemo(predictData, trainData)
h.calculateMetricsDemo(accuracy, precision, recall, f_score)

elif algorithm == 1:
print("Enter train File name")
trainData = h.get_file(h.get_fileName(), kCrossValidation = 10)
Expand All @@ -215,8 +226,10 @@ def random_forest(self, kCrossValidation = 10):
predictData = h.get_file(name, fileType='predictData')
accuracy, precision, recall, f_score = m.knn(predictData, trainData)
h.calculateMetrics(accuracy, precision, recall, f_score)

elif algorithm == 2:
m.decision_tree()

elif algorithm == 3:
print("Enter train File name")
trainData = h.get_file_bayes(h.get_fileName(), kCrossValidation = 10)
Expand All @@ -228,13 +241,16 @@ def random_forest(self, kCrossValidation = 10):
predictData = h.get_file_bayes(name, fileType='predictData')
accuracy, precision, recall, f_score = m.bayes_naive(predictData, trainData)
h.calculateMetrics(accuracy, precision, recall, f_score)

elif algorithm == 4:
print("Enter train File name")
trainData = h.get_file_bayes_demo(h.get_fileName())
print("Enter test File name")
predictData = h.get_file_bayes_demo(h.get_fileName(),fileType = 'predictData')
m.bayes_naive_demo(predictData, trainData)

elif algorithm == 5:
m.random_forest()

else:
print("\nWrong input")
14 changes: 0 additions & 14 deletions project3/Data/d4.txt

This file was deleted.

1 change: 1 addition & 0 deletions project3/Data/d4test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rain hot high weak
15 changes: 14 additions & 1 deletion project3/Data/project3_dataset4.txt
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
rain hot high weak
sunny hot high weak 0
sunny hot high strong 0
overcast hot high weak 1
rain mild high weak 1
rain cool normal weak 1
rain cool normal strong 0
overcast cool normal strong 1
sunny mild high weak 0
sunny cool normal weak 1
rain mild normal weak 1
sunny mild normal strong 1
overcast mild high strong 1
overcast hot normal weak 1
rain mild high strong 0

0 comments on commit fefc280

Please sign in to comment.