diff --git a/project3/Code/main.py b/project3/Code/main.py index 296fbb7..c67359c 100644 --- a/project3/Code/main.py +++ b/project3/Code/main.py @@ -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() @@ -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) @@ -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) @@ -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") diff --git a/project3/Data/d4.txt b/project3/Data/d4.txt deleted file mode 100644 index 664a0e9..0000000 --- a/project3/Data/d4.txt +++ /dev/null @@ -1,14 +0,0 @@ -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 \ No newline at end of file diff --git a/project3/Data/d4test.txt b/project3/Data/d4test.txt new file mode 100644 index 0000000..8b6a44a --- /dev/null +++ b/project3/Data/d4test.txt @@ -0,0 +1 @@ +rain hot high weak \ No newline at end of file diff --git a/project3/Data/project3_dataset4.txt b/project3/Data/project3_dataset4.txt index 8b6a44a..664a0e9 100644 --- a/project3/Data/project3_dataset4.txt +++ b/project3/Data/project3_dataset4.txt @@ -1 +1,14 @@ -rain hot high weak \ No newline at end of file +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 \ No newline at end of file