From f954413a29d5749d9f6b53d7be4e91c4ef7db0da Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 11:31:43 +0530 Subject: [PATCH 01/22] #53 categorical nb tried to resolve #53 code for categorical nb using map and following gaussian nb convention --- CMakeLists.txt | 5 +- .../categorical_nb/categorical_nb.md | 41 +++++++++++ examples/neighbors/categorical_nb.cpp | 19 ++++++ .../categorical_nb/categorical_nb.cpp | 68 +++++++++++++++++++ .../categorical_nb/categorical_nb.hpp | 25 +++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 docs/methods/neighbors/categorical_nb/categorical_nb.md create mode 100644 examples/neighbors/categorical_nb.cpp create mode 100644 src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp create mode 100644 src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b423799..47c6adf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,4 +60,7 @@ add_library(slowmokit src/slowmokit/methods/metrics/recall.hpp src/slowmokit/methods/metrics/recall.cpp src/slowmokit/methods/metrics/mean_squared_error.hpp - src/slowmokit/methods/metrics/mean_squared_error.cpp) + src/slowmokit/methods/metrics/mean_squared_error.cpp + examples/neighbors/categorical_nb.cpp + src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp + src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md new file mode 100644 index 0000000..f02dca1 --- /dev/null +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -0,0 +1,41 @@ +# Categorical Naive Bayes + +Categorical Naive Bayes model. + +Categorical Naive Bayes computes likelihood by : occurences of instance in current label divided by total occurences of instance + +It computes the prior probability by : occurence of current class divided by size of training array + +And finally prior*likelihood gives the probabilty of class according to which output class is predicted. + + +## Parameters + +| Name | Definition | Type | +|--------|------------------------------------------|--------------------| +| `xTrain` | The training set containing the features | `vector>` | +| `yTrain` | The set containing the class correspoding to respective xTrain instance | `vector`| + +## Methods + +| Name | Definition | Return value | +| ------------------------------- | ----------------------------------------------------- |--------------| +| `fitPredict(vector>xTrain,vectoryTrain,vectorxTest)` | fit and predict the training and testing values | `string` | + +## Example + +``` + std::vector > xTrain = {{"fifa", "yes", "no", "no"}, + {"fifa", "no", "yes", "no"}, + {"fifa", "no", "no", "yes"}, + {"cc", "no", "no", "yes"}, + {"fifa", "yes", "yes", "yes"}, + {"cc", "yes", "yes", "yes"}, + {"cc", "no", "no", "yes"}, + {"cc", "yes", "no", "no"}}; + std::vector yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; + std::vector xTest = {"fifa", "no", "yes", "yes"}; + categoricalNB classifier; + std::cout << classifier.fitPredict(xTrain, yTrain, xTest); +``` diff --git a/examples/neighbors/categorical_nb.cpp b/examples/neighbors/categorical_nb.cpp new file mode 100644 index 0000000..33f1d10 --- /dev/null +++ b/examples/neighbors/categorical_nb.cpp @@ -0,0 +1,19 @@ +//#include "../../src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp" +//#include "../../src/slowmokit/core.hpp" +// +//signed main() { +// +// std::vector > xTrain = {{"fifa", "yes", "no", "no"}, +// {"fifa", "no", "yes", "no"}, +// {"fifa", "no", "no", "yes"}, +// {"cc", "no", "no", "yes"}, +// {"fifa", "yes", "yes", "yes"}, +// {"cc", "yes", "yes", "yes"}, +// {"cc", "no", "no", "yes"}, +// {"cc", "yes", "no", "no"}}; +// std::vector yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; +// std::vector xTest = {"fifa", "no", "yes", "yes"}; +// categoricalNB classifier; +// std::cout << classifier.fitPredict(xTrain, yTrain, xTest); +// +//} \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp new file mode 100644 index 0000000..cb68fb9 --- /dev/null +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -0,0 +1,68 @@ +/** + * @file methods/neighbors/gaussian_nb/categorical_nb.cpp + * + * Implementation of the Categorical Naive Bayes main program + */ + +#include "categorical_nb.hpp" + +template + +std:: string fitPredict(std::vector> xTrain, std::vector yTrain,std::vector xTest){ + // posterior = (prior * likelihood)/evidence + // since, evidence is same among all instances -> we can ignore it + + if (xTrain.size() == 0 || yTrain.size() == 0) + throw "Make sure that you have atleast one train example"; + if (xTrain.size() != yTrain.size()) + throw "Number of features and target must be equal"; + + std:: map priors; + std:: map occurences; + + for(auto category : yTrain){ + occurences[category]++; + } + for(auto current : occurences){ + priors[current.first] = double(current.second)/yTrain.size(); + } + // priors calculated + + std::map> likelihoods; + // for any ith label -> n instances -> for each differ probability + std::map> counts; + + for(int i=0;icurrent = xTrain[i]; // current row + for(auto curr : current){ + counts[yTrain[i]][curr]++; + // inc count of curr instance corresponding to ith label + } + } + for(auto current : counts){ + for(auto e : current.second){ + likelihoods[current.first][e.first]=((double(e.second))/(occurences[current.first])); + // likelihood[label][current feature]=occ in current/total no of occ + } + } + // likelihoods done + + std::map probs; + for(auto curr : priors){ + probs[curr.first] = curr.second; + for(auto feature : xTest){ + // if(likelihoods[curr.first][feature]==0){ continue;} + probs[curr.first] *= likelihoods[curr.first][feature]; + } + } + double maxProb = 0; + std:: string out = ""; + for(auto prob : probs){ + if(prob.second > maxProb){ + maxProb = prob.second; + out = prob.first; + } + } + + return out; +} \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp new file mode 100644 index 0000000..7b8c245 --- /dev/null +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -0,0 +1,25 @@ +/** + * @file methods/neighbors/categorical_nb/categorical_naive_bayes.hpp + * + * The header file including the Categorical Naive Bayes algorithm + */ + +#ifndef SLOWMOKIT_CATEGORICAL_NB_HPP +#define SLOWMOKIT_CATEGORICAL_NB_HPP + +#include "../../../core.hpp" + +template class categoricalNB{ +public: + /** + * @brief Fit predict at same time + * + * @param xTrain all training 2-d feature x values + * @param yTrain all training 1-d string y values + * @param xTest testing values + * @return class label predicted for test case + */ + std::string fitPredict(std::vector> xTrain, std::vector yTrain,std::vector xTest); +}; + +#endif //SLOWMOKIT_CATEGORICAL_NB_HPP From 030c1df4e962244823a9ee9b2ecbc21cd996024e Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 11:32:43 +0530 Subject: [PATCH 02/22] Update CMakeLists.txt --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47c6adf..b33c685 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,5 @@ add_library(slowmokit src/slowmokit/methods/metrics/recall.cpp src/slowmokit/methods/metrics/mean_squared_error.hpp src/slowmokit/methods/metrics/mean_squared_error.cpp - examples/neighbors/categorical_nb.cpp src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp) From bc9b3f6e3e98a4189427aff5965c9c6e2668de63 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 11:44:11 +0530 Subject: [PATCH 03/22] clang format changes --- .../categorical_nb/categorical_nb.cpp | 105 ++++++++++-------- .../categorical_nb/categorical_nb.hpp | 25 +++-- 2 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index cb68fb9..85e7b4c 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -8,61 +8,74 @@ template -std:: string fitPredict(std::vector> xTrain, std::vector yTrain,std::vector xTest){ - // posterior = (prior * likelihood)/evidence - // since, evidence is same among all instances -> we can ignore it +std::string fitPredict(std::vector> xTrain, + std::vector yTrain, std::vector xTest) +{ + // posterior = (prior * likelihood)/evidence + // since, evidence is same among all instances -> we can ignore it - if (xTrain.size() == 0 || yTrain.size() == 0) - throw "Make sure that you have atleast one train example"; - if (xTrain.size() != yTrain.size()) - throw "Number of features and target must be equal"; + if (xTrain.size() == 0 || yTrain.size() == 0) + throw "Make sure that you have atleast one train example"; + if (xTrain.size() != yTrain.size()) + throw "Number of features and target must be equal"; - std:: map priors; - std:: map occurences; + std::map priors; + std::map occurences; - for(auto category : yTrain){ - occurences[category]++; - } - for(auto current : occurences){ - priors[current.first] = double(current.second)/yTrain.size(); - } - // priors calculated + for (auto category : yTrain) + { + occurences[category]++; + } + for (auto current : occurences) + { + priors[current.first] = double(current.second) / yTrain.size(); + } + // priors calculated - std::map> likelihoods; - // for any ith label -> n instances -> for each differ probability - std::map> counts; + std::map> likelihoods; + // for any ith label -> n instances -> for each differ probability + std::map> counts; - for(int i=0;icurrent = xTrain[i]; // current row - for(auto curr : current){ - counts[yTrain[i]][curr]++; - // inc count of curr instance corresponding to ith label - } + for (int i = 0; i < xTrain.size(); i++) + { + std::vector current = xTrain[i]; // current row + for (auto curr : current) + { + counts[yTrain[i]][curr]++; + // inc count of curr instance corresponding to ith label } - for(auto current : counts){ - for(auto e : current.second){ - likelihoods[current.first][e.first]=((double(e.second))/(occurences[current.first])); - // likelihood[label][current feature]=occ in current/total no of occ - } + } + for (auto current : counts) + { + for (auto e : current.second) + { + likelihoods[current.first][e.first] = + ((double(e.second)) / (occurences[current.first])); + // likelihood[label][current feature]=occ in current/total no of occ } - // likelihoods done + } + // likelihoods done - std::map probs; - for(auto curr : priors){ - probs[curr.first] = curr.second; - for(auto feature : xTest){ - // if(likelihoods[curr.first][feature]==0){ continue;} - probs[curr.first] *= likelihoods[curr.first][feature]; - } + std::map probs; + for (auto curr : priors) + { + probs[curr.first] = curr.second; + for (auto feature : xTest) + { + // if(likelihoods[curr.first][feature]==0){ continue;} + probs[curr.first] *= likelihoods[curr.first][feature]; } - double maxProb = 0; - std:: string out = ""; - for(auto prob : probs){ - if(prob.second > maxProb){ - maxProb = prob.second; - out = prob.first; - } + } + double maxProb = 0; + std::string out = ""; + for (auto prob : probs) + { + if (prob.second > maxProb) + { + maxProb = prob.second; + out = prob.first; } + } - return out; + return out; } \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp index 7b8c245..6faa007 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -9,17 +9,18 @@ #include "../../../core.hpp" -template class categoricalNB{ -public: - /** - * @brief Fit predict at same time - * - * @param xTrain all training 2-d feature x values - * @param yTrain all training 1-d string y values - * @param xTest testing values - * @return class label predicted for test case - */ - std::string fitPredict(std::vector> xTrain, std::vector yTrain,std::vector xTest); +template class categoricalNB +{ + public: + /** + * @brief Fit predict at same time + * @param xTrain all training 2-d feature x values + * @param yTrain all training 1-d string y values + * @param xTest testing values + * @return class label predicted for test case + */ + std::string fitPredict(std::vector> xTrain, + std::vector yTrain, std::vector xTest); }; -#endif //SLOWMOKIT_CATEGORICAL_NB_HPP +#endif // SLOWMOKIT_CATEGORICAL_NB_HPP From 0162b34b645f26b44aefbf415712c58a91173430 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:41:55 +0530 Subject: [PATCH 04/22] Update docs/methods/neighbors/categorical_nb/categorical_nb.md Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- docs/methods/neighbors/categorical_nb/categorical_nb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index f02dca1..db6f6ff 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -25,7 +25,7 @@ And finally prior*likelihood gives the probabilty of class according to which ou ## Example -``` +```cpp std::vector > xTrain = {{"fifa", "yes", "no", "no"}, {"fifa", "no", "yes", "no"}, {"fifa", "no", "no", "yes"}, From 9ed78a7c4eb69d60680fbe6767164194cb531d74 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:42:16 +0530 Subject: [PATCH 05/22] Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 85e7b4c..2bc7094 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -30,7 +30,6 @@ std::string fitPredict(std::vector> xTrain, { priors[current.first] = double(current.second) / yTrain.size(); } - // priors calculated std::map> likelihoods; // for any ith label -> n instances -> for each differ probability From 69ba1a3681eb34cc9c91b2531018622ca0780e28 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:42:32 +0530 Subject: [PATCH 06/22] Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 2bc7094..52b0ff3 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -53,7 +53,6 @@ std::string fitPredict(std::vector> xTrain, // likelihood[label][current feature]=occ in current/total no of occ } } - // likelihoods done std::map probs; for (auto curr : priors) From c8c8da3095b3ee5e2092a5a1d776866a7fa0aa1c Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:42:42 +0530 Subject: [PATCH 07/22] Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 52b0ff3..268edc8 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -60,7 +60,6 @@ std::string fitPredict(std::vector> xTrain, probs[curr.first] = curr.second; for (auto feature : xTest) { - // if(likelihoods[curr.first][feature]==0){ continue;} probs[curr.first] *= likelihoods[curr.first][feature]; } } From e5ac202b9f0e87ee4775b937d4c497748235532c Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:42:51 +0530 Subject: [PATCH 08/22] Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 268edc8..ae839ad 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -64,7 +64,7 @@ std::string fitPredict(std::vector> xTrain, } } double maxProb = 0; - std::string out = ""; + std::string out; for (auto prob : probs) { if (prob.second > maxProb) From a02a433b37ab4c761d1cb0aa9c1eb8f2beed8dd7 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:57:55 +0530 Subject: [PATCH 09/22] some changes --- .../categorical_nb/categorical_nb.md | 28 +++++----- examples/neighbors/categorical_nb.cpp | 3 +- .../categorical_nb/categorical_nb.cpp | 51 +++++++++---------- .../categorical_nb/categorical_nb.hpp | 20 ++++++-- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index f02dca1..4611e18 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -11,22 +11,23 @@ And finally prior*likelihood gives the probabilty of class according to which ou ## Parameters -| Name | Definition | Type | -|--------|------------------------------------------|--------------------| -| `xTrain` | The training set containing the features | `vector>` | -| `yTrain` | The set containing the class correspoding to respective xTrain instance | `vector`| +| Name | Definition | Type | +|--------|----------------------------------------|-------------------| +| `xTrain` |The training set containing the features|`vector>`| +| `yTrain` |The set containing the class correspoding to respective xTrain instance|`vector`| ## Methods -| Name | Definition | Return value | -| ------------------------------- | ----------------------------------------------------- |--------------| -| `fitPredict(vector>xTrain,vectoryTrain,vectorxTest)` | fit and predict the training and testing values | `string` | +| Name | Definition | Return value | +|--------------------------------------------------|---------------------------------------------|-----------| +| `fit(vector> xTrain,vector yTrain)` |fit the class instance with the training data|`NULL| +| `predict(vector xTest` |predict the label for xTest vector of features|`string`| ## Example ``` - std::vector > xTrain = {{"fifa", "yes", "no", "no"}, +std::vector > xTrain = {{"fifa", "yes", "no", "no"}, {"fifa", "no", "yes", "no"}, {"fifa", "no", "no", "yes"}, {"cc", "no", "no", "yes"}, @@ -34,8 +35,9 @@ And finally prior*likelihood gives the probabilty of class according to which ou {"cc", "yes", "yes", "yes"}, {"cc", "no", "no", "yes"}, {"cc", "yes", "no", "no"}}; - std::vector yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; - std::vector xTest = {"fifa", "no", "yes", "yes"}; - categoricalNB classifier; - std::cout << classifier.fitPredict(xTrain, yTrain, xTest); +std::vector yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; +std::vector xTest = {"fifa", "no", "yes", "yes"}; +categoricalNB classifier; +classifier.fit(xTrain,yTrain); +std::cout< yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; // std::vector xTest = {"fifa", "no", "yes", "yes"}; // categoricalNB classifier; -// std::cout << classifier.fitPredict(xTrain, yTrain, xTest); +// classifier.fit(xTrain,yTrain) +// std::cout< priors; +std::map> likelihoods; // for each label we will store a map , containing n features and their corresponding probability template - -std::string fitPredict(std::vector> xTrain, - std::vector yTrain, std::vector xTest) +void fit(std::vector> xTrain, + std::vector yTrain) { // posterior = (prior * likelihood)/evidence // since, evidence is same among all instances -> we can ignore it @@ -19,7 +19,6 @@ std::string fitPredict(std::vector> xTrain, if (xTrain.size() != yTrain.size()) throw "Number of features and target must be equal"; - std::map priors; std::map occurences; for (auto category : yTrain) @@ -30,10 +29,7 @@ std::string fitPredict(std::vector> xTrain, { priors[current.first] = double(current.second) / yTrain.size(); } - // priors calculated - std::map> likelihoods; - // for any ith label -> n instances -> for each differ probability std::map> counts; for (int i = 0; i < xTrain.size(); i++) @@ -54,28 +50,31 @@ std::string fitPredict(std::vector> xTrain, // likelihood[label][current feature]=occ in current/total no of occ } } - // likelihoods done - std::map probs; - for (auto curr : priors) - { - probs[curr.first] = curr.second; - for (auto feature : xTest) + +} + +template +std::string predict(std::vector xTest){ + std::map probs; + for (auto curr : priors) { - // if(likelihoods[curr.first][feature]==0){ continue;} - probs[curr.first] *= likelihoods[curr.first][feature]; + probs[curr.first] = curr.second; + for (auto feature : xTest) + { + probs[curr.first] *= likelihoods[curr.first][feature]; + } } - } - double maxProb = 0; - std::string out = ""; - for (auto prob : probs) - { - if (prob.second > maxProb) + double maxProb = 0; + std::string out = ""; + for (auto prob : probs) { - maxProb = prob.second; - out = prob.first; + if (prob.second > maxProb) + { + maxProb = prob.second; + out = prob.first; + } } - } - return out; + return out; } \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp index 6faa007..142d0a2 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -11,16 +11,26 @@ template class categoricalNB { + categoricalNB(){ + std::map priors; + std::map> likelihoods; + } public: /** - * @brief Fit predict at same time + * @brief Fitting the training set into instance of class * @param xTrain all training 2-d feature x values * @param yTrain all training 1-d string y values - * @param xTest testing values - * @return class label predicted for test case + * @return NULL */ - std::string fitPredict(std::vector> xTrain, - std::vector yTrain, std::vector xTest); + void fit(std::vector> xTrain, + std::vector yTrain); + + /** + * @brief Predicting the class for xTest on the basis of training set + * @param xTest all testing feature x values + * @return string denoting the class label of xTest + */ + std::string predict(std::vector xTest); }; #endif // SLOWMOKIT_CATEGORICAL_NB_HPP From 39f867cd369f7de52a5d34c01b5617e9277b92bc Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:59:53 +0530 Subject: [PATCH 10/22] clang changes --- .../categorical_nb/categorical_nb.cpp | 44 +++++++++---------- .../categorical_nb/categorical_nb.hpp | 23 +++++----- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 8c8c2a2..388a5fc 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -6,10 +6,11 @@ #include "categorical_nb.hpp" std::map priors; -std::map> likelihoods; // for each label we will store a map , containing n features and their corresponding probability +std::map> + likelihoods; // for each label we will store a map , containing n features + // and their corresponding probability template -void fit(std::vector> xTrain, - std::vector yTrain) +void fit(std::vector> xTrain, std::vector yTrain) { // posterior = (prior * likelihood)/evidence // since, evidence is same among all instances -> we can ignore it @@ -70,30 +71,29 @@ void fit(std::vector> xTrain, out = prob.first; } } - } -template -std::string predict(std::vector xTest){ - std::map probs; - for (auto curr : priors) +template std::string predict(std::vector xTest) +{ + std::map probs; + for (auto curr : priors) + { + probs[curr.first] = curr.second; + for (auto feature : xTest) { - probs[curr.first] = curr.second; - for (auto feature : xTest) - { - probs[curr.first] *= likelihoods[curr.first][feature]; - } + probs[curr.first] *= likelihoods[curr.first][feature]; } - double maxProb = 0; - std::string out = ""; - for (auto prob : probs) + } + double maxProb = 0; + std::string out = ""; + for (auto prob : probs) + { + if (prob.second > maxProb) { - if (prob.second > maxProb) - { - maxProb = prob.second; - out = prob.first; - } + maxProb = prob.second; + out = prob.first; } + } - return out; + return out; } \ No newline at end of file diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp index 142d0a2..da3ce18 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -11,10 +11,12 @@ template class categoricalNB { - categoricalNB(){ - std::map priors; - std::map> likelihoods; - } + categoricalNB() + { + std::map priors; + std::map> likelihoods; + } + public: /** * @brief Fitting the training set into instance of class @@ -22,14 +24,13 @@ template class categoricalNB * @param yTrain all training 1-d string y values * @return NULL */ - void fit(std::vector> xTrain, - std::vector yTrain); + void fit(std::vector> xTrain, std::vector yTrain); - /** - * @brief Predicting the class for xTest on the basis of training set - * @param xTest all testing feature x values - * @return string denoting the class label of xTest - */ + /** + * @brief Predicting the class for xTest on the basis of training set + * @param xTest all testing feature x values + * @return string denoting the class label of xTest + */ std::string predict(std::vector xTest); }; From 53bfca4188ff5e2e71b876ae31f0df6fde2ae94d Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:03:07 +0530 Subject: [PATCH 11/22] nb --- .../categorical_nb/categorical_nb.cpp | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 388a5fc..b96f89d 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -52,25 +52,7 @@ void fit(std::vector> xTrain, std::vector yTrain) } } - std::map probs; - for (auto curr : priors) - { - probs[curr.first] = curr.second; - for (auto feature : xTest) - { - probs[curr.first] *= likelihoods[curr.first][feature]; - } - } - double maxProb = 0; - std::string out; - for (auto prob : probs) - { - if (prob.second > maxProb) - { - maxProb = prob.second; - out = prob.first; - } - } + } template std::string predict(std::vector xTest) From 4e8fa9345452d39cb7c9312f996bef40d7ce0cd5 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:04:14 +0530 Subject: [PATCH 12/22] cl --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index b96f89d..266d38b 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -51,8 +51,6 @@ void fit(std::vector> xTrain, std::vector yTrain) // likelihood[label][current feature]=occ in current/total no of occ } } - - } template std::string predict(std::vector xTest) From 32e096a272c67f6c00599e212f5f1bb727fcdfe7 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:05:48 +0530 Subject: [PATCH 13/22] md --- docs/methods/neighbors/categorical_nb/categorical_nb.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index 2c48861..b10b86b 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -27,7 +27,6 @@ And finally prior*likelihood gives the probabilty of class according to which ou ## Example ``` -std::vector > xTrain = {{"fifa", "yes", "no", "no"}, ```cpp std::vector > xTrain = {{"fifa", "yes", "no", "no"}, {"fifa", "no", "yes", "no"}, From 4efac62e09384a3b7f0a0c16a335ccf34d5509b7 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:06:36 +0530 Subject: [PATCH 14/22] m --- docs/methods/neighbors/categorical_nb/categorical_nb.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index b10b86b..7766205 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -27,8 +27,8 @@ And finally prior*likelihood gives the probabilty of class according to which ou ## Example ``` -```cpp - std::vector > xTrain = {{"fifa", "yes", "no", "no"}, +cpp +std::vector > xTrain = {{"fifa", "yes", "no", "no"}, {"fifa", "no", "yes", "no"}, {"fifa", "no", "no", "yes"}, {"cc", "no", "no", "yes"}, From fcc3623701bdce8751f42403674d19e24aa3e4ff Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:18:03 +0530 Subject: [PATCH 15/22] o --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 266d38b..14396f6 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -65,7 +65,7 @@ template std::string predict(std::vector xTest) } } double maxProb = 0; - std::string out = ""; + std::string out; for (auto prob : probs) { if (prob.second > maxProb) From 9f30155d79b623d6b5a26478094ca2d179a3e52b Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 22:17:08 +0530 Subject: [PATCH 16/22] Update categorical_nb.md --- docs/methods/neighbors/categorical_nb/categorical_nb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index 7766205..8cec45c 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -21,7 +21,7 @@ And finally prior*likelihood gives the probabilty of class according to which ou | Name | Definition | Return value | |--------------------------------------------------|---------------------------------------------|-----------| -| `fit(vector> xTrain,vector yTrain)` |fit the class instance with the training data|`NULL| +| `fit(vector> xTrain,vector yTrain)` |fit the class instance with the training data|`NULL`| | `predict(vector xTest` |predict the label for xTest vector of features|`string`| ## Example From 462a22900408062274e222053e09eee0bff94c17 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 23:01:41 +0530 Subject: [PATCH 17/22] Update categorical_nb.md --- docs/methods/neighbors/categorical_nb/categorical_nb.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/methods/neighbors/categorical_nb/categorical_nb.md b/docs/methods/neighbors/categorical_nb/categorical_nb.md index 8cec45c..1d8e1ca 100644 --- a/docs/methods/neighbors/categorical_nb/categorical_nb.md +++ b/docs/methods/neighbors/categorical_nb/categorical_nb.md @@ -26,8 +26,7 @@ And finally prior*likelihood gives the probabilty of class according to which ou ## Example -``` -cpp +```cpp std::vector > xTrain = {{"fifa", "yes", "no", "no"}, {"fifa", "no", "yes", "no"}, {"fifa", "no", "no", "yes"}, From 8ac9880d0b22b7199e8bd5fb5ba6f86a383b5c58 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Sun, 12 Feb 2023 23:03:21 +0530 Subject: [PATCH 18/22] Update categorical_nb.hpp --- .../methods/neighbors/categorical_nb/categorical_nb.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp index da3ce18..70e8cb1 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -9,9 +9,9 @@ #include "../../../core.hpp" -template class categoricalNB +template class CategoricalNB { - categoricalNB() + CategoricalNB() { std::map priors; std::map> likelihoods; From f78c85f0d63f9c67ddc8b6e0080ef1921f06bd9e Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Wed, 15 Feb 2023 21:30:25 +0530 Subject: [PATCH 19/22] changes --- .../categorical_nb/categorical_nb.cpp | 78 ++++++++++++++----- .../categorical_nb/categorical_nb.hpp | 5 -- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 14396f6..0c1e346 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -5,10 +5,15 @@ */ #include "categorical_nb.hpp" -std::map priors; -std::map> - likelihoods; // for each label we will store a map , containing n features - // and their corresponding probability + +template std::map priors; +template +std::map, double>> likelihoods; +// for each label we will store a map , containing n features +// and their corresponding probability + +int featureSize = 0; // for storing the number of columns(number of features) in + // xTrain and to compare it with the testing data template void fit(std::vector> xTrain, std::vector yTrain) { @@ -16,59 +21,90 @@ void fit(std::vector> xTrain, std::vector yTrain) // since, evidence is same among all instances -> we can ignore it if (xTrain.size() == 0 || yTrain.size() == 0) + { throw "Make sure that you have atleast one train example"; + } if (xTrain.size() != yTrain.size()) - throw "Number of features and target must be equal"; - - std::map occurences; + { + throw "Number of instances and target values must be equal"; + } + featureSize = xTrain[0].size(); + std::map + occurences; // to store the occurences of each label in the training set + // and then use it in finding the priors for (auto category : yTrain) { - occurences[category]++; + occurences[category]++; // incrementing the occurence of each label } for (auto current : occurences) { - priors[current.first] = double(current.second) / yTrain.size(); + priors[current.first] = + double(current.second) / + yTrain.size(); // calculating the priors of each label, dividing the + // occurence by the size of the total set } - std::map> counts; + std::map, int>> + counts; // to store the count of each cell corresponding to it's label, + // it's row and it's category - for (int i = 0; i < xTrain.size(); i++) + for (int i = 0; i < xTrain.size(); + i++) // iterating over the training data grid { std::vector current = xTrain[i]; // current row - for (auto curr : current) + int j = 0; // to keep track of the current column for each cell + for (auto curr : current) // iterating over current row { - counts[yTrain[i]][curr]++; - // inc count of curr instance corresponding to ith label + counts[yTrain[i]] + [{curr, j}]++; // incrementing the value based on it's label , it's + // category and it's column(denoting it's feature) + j++; } } - for (auto current : counts) + for (auto current : + counts) // iterating over the counts map , to calculate likelihoods { - for (auto e : current.second) + for (auto e : + current.second) // iterating over the map corresponding to each label + // to find the likelihood of each entry { - likelihoods[current.first][e.first] = + likelihoods[current.first][{e.first.first, e.first.second}] = ((double(e.second)) / (occurences[current.first])); // likelihood[label][current feature]=occ in current/total no of occ } } } -template std::string predict(std::vector xTest) +template +std::string predict( + std::vector xTest) // predicting the label on the basis of training set { + if (xTest.size() != featureSize) + { + throw "The number of features in training and testing set must be same"; + } std::map probs; - for (auto curr : priors) + for (auto curr : priors) // since, posterior = prior*likelihood , we will + // calculate the same for each label to give the + // label with highest probability as the output { probs[curr.first] = curr.second; + int j = 0; for (auto feature : xTest) { - probs[curr.first] *= likelihoods[curr.first][feature]; + probs[curr.first] *= likelihoods[curr.first][{ + feature, + j}]; // calculating posterior for each feature in Testing data + j++; } } double maxProb = 0; std::string out; for (auto prob : probs) { - if (prob.second > maxProb) + if (prob.second > + maxProb) // finding the highest probability among all options { maxProb = prob.second; out = prob.first; diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp index 70e8cb1..b8c3cf8 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp @@ -11,11 +11,6 @@ template class CategoricalNB { - CategoricalNB() - { - std::map priors; - std::map> likelihoods; - } public: /** From d5c782a64a7ce077197b1a24fa7cbcf149d3e7e4 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Wed, 15 Feb 2023 22:59:07 +0530 Subject: [PATCH 20/22] changes --- .../categorical_nb/categorical_nb.cpp | 30 ++++++++----------- .../categorical_nb/categorical_nb.hpp | 5 ++++ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 0c1e346..eadea40 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -5,17 +5,8 @@ */ #include "categorical_nb.hpp" - -template std::map priors; -template -std::map, double>> likelihoods; -// for each label we will store a map , containing n features -// and their corresponding probability - -int featureSize = 0; // for storing the number of columns(number of features) in - // xTrain and to compare it with the testing data template -void fit(std::vector> xTrain, std::vector yTrain) +void CategoricalNB :: fit(std::vector> xTrain, std::vector yTrain) { // posterior = (prior * likelihood)/evidence // since, evidence is same among all instances -> we can ignore it @@ -39,7 +30,7 @@ void fit(std::vector> xTrain, std::vector yTrain) } for (auto current : occurences) { - priors[current.first] = + priors[current.first] = double(current.second) / yTrain.size(); // calculating the priors of each label, dividing the // occurence by the size of the total set @@ -49,7 +40,7 @@ void fit(std::vector> xTrain, std::vector yTrain) counts; // to store the count of each cell corresponding to it's label, // it's row and it's category - for (int i = 0; i < xTrain.size(); + for (int i = 0; i < (int)(xTrain.size()); i++) // iterating over the training data grid { std::vector current = xTrain[i]; // current row @@ -69,7 +60,7 @@ void fit(std::vector> xTrain, std::vector yTrain) current.second) // iterating over the map corresponding to each label // to find the likelihood of each entry { - likelihoods[current.first][{e.first.first, e.first.second}] = + likelihoods[current.first][{e.first.first, e.first.second}] = ((double(e.second)) / (occurences[current.first])); // likelihood[label][current feature]=occ in current/total no of occ } @@ -77,15 +68,16 @@ void fit(std::vector> xTrain, std::vector yTrain) } template -std::string predict( + +std::string CategoricalNB:: predict( std::vector xTest) // predicting the label on the basis of training set { - if (xTest.size() != featureSize) + if (((int)(xTest.size())) != featureSize) { throw "The number of features in training and testing set must be same"; } std::map probs; - for (auto curr : priors) // since, posterior = prior*likelihood , we will + for (auto curr : priors) // since, posterior = prior*likelihood , we will // calculate the same for each label to give the // label with highest probability as the output { @@ -93,7 +85,7 @@ std::string predict( int j = 0; for (auto feature : xTest) { - probs[curr.first] *= likelihoods[curr.first][{ + probs[curr.first] *= likelihoods[curr.first][{ feature, j}]; // calculating posterior for each feature in Testing data j++; @@ -110,6 +102,8 @@ std::string predict( out = prob.first; } } - + for(auto e : probs){ + std::cout< class CategoricalNB * @return string denoting the class label of xTest */ std::string predict(std::vector xTest); +private : + std::map priors; + std::map, double>> likelihoods; + int featureSize; + }; #endif // SLOWMOKIT_CATEGORICAL_NB_HPP From 1587a44a315821000ca8669e7d1b33842f53e689 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Wed, 15 Feb 2023 23:00:19 +0530 Subject: [PATCH 21/22] clang --- .../categorical_nb/categorical_nb.cpp | 24 ++++++++++--------- .../categorical_nb/categorical_nb.hpp | 8 +++---- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index eadea40..4250e7c 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -6,7 +6,8 @@ #include "categorical_nb.hpp" template -void CategoricalNB :: fit(std::vector> xTrain, std::vector yTrain) +void CategoricalNB::fit(std::vector> xTrain, + std::vector yTrain) { // posterior = (prior * likelihood)/evidence // since, evidence is same among all instances -> we can ignore it @@ -40,7 +41,7 @@ void CategoricalNB :: fit(std::vector> xTrain, std::vector current = xTrain[i]; // current row @@ -69,25 +70,25 @@ void CategoricalNB :: fit(std::vector> xTrain, std::vector -std::string CategoricalNB:: predict( +std::string CategoricalNB::predict( std::vector xTest) // predicting the label on the basis of training set { - if (((int)(xTest.size())) != featureSize) + if (((int) (xTest.size())) != featureSize) { throw "The number of features in training and testing set must be same"; } std::map probs; for (auto curr : priors) // since, posterior = prior*likelihood , we will - // calculate the same for each label to give the - // label with highest probability as the output + // calculate the same for each label to give the + // label with highest probability as the output { probs[curr.first] = curr.second; int j = 0; for (auto feature : xTest) { - probs[curr.first] *= likelihoods[curr.first][{ - feature, - j}]; // calculating posterior for each feature in Testing data + probs[curr.first] *= + likelihoods[curr.first][{feature, j}]; // calculating posterior for + // each feature in Testing data j++; } } @@ -102,8 +103,9 @@ std::string CategoricalNB:: predict( out = prob.first; } } - for(auto e : probs){ - std::cout< class CategoricalNB * @return string denoting the class label of xTest */ std::string predict(std::vector xTest); -private : - std::map priors; - std::map, double>> likelihoods; - int featureSize; + private: + std::map priors; + std::map, double>> likelihoods; + int featureSize; }; #endif // SLOWMOKIT_CATEGORICAL_NB_HPP From 6f54c87254bc31c493ac67d901576e3dfd05b783 Mon Sep 17 00:00:00 2001 From: Abhinav Malhotra <113239170+abhinavmalhotra01@users.noreply.github.com> Date: Wed, 15 Feb 2023 23:58:41 +0530 Subject: [PATCH 22/22] m --- .../methods/neighbors/categorical_nb/categorical_nb.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp index 4250e7c..5c5f9c1 100644 --- a/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp +++ b/src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp @@ -103,9 +103,5 @@ std::string CategoricalNB::predict( out = prob.first; } } - for (auto e : probs) - { - std::cout << e.first << " " << e.second << std::endl; - } return out; } \ No newline at end of file