-
Notifications
You must be signed in to change notification settings - Fork 15
Categorical naive bayes #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhinavmalhotra01
wants to merge
24
commits into
PEC-CSS:main
Choose a base branch
from
abhinavmalhotra01:categorical_naive_bayes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f954413
#53 categorical nb
abhinavmalhotra01 030c1df
Update CMakeLists.txt
abhinavmalhotra01 bc9b3f6
clang format changes
abhinavmalhotra01 0162b34
Update docs/methods/neighbors/categorical_nb/categorical_nb.md
abhinavmalhotra01 9ed78a7
Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp
abhinavmalhotra01 69ba1a3
Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp
abhinavmalhotra01 c8c8da3
Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp
abhinavmalhotra01 e5ac202
Update src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp
abhinavmalhotra01 a02a433
some changes
abhinavmalhotra01 67aec95
Merge branch 'categorical_naive_bayes' of https://github.com/abhinavm…
abhinavmalhotra01 39f867c
clang changes
abhinavmalhotra01 53bfca4
nb
abhinavmalhotra01 4e8fa93
cl
abhinavmalhotra01 32e096a
md
abhinavmalhotra01 4efac62
m
abhinavmalhotra01 fcc3623
o
abhinavmalhotra01 9f30155
Update categorical_nb.md
abhinavmalhotra01 462a229
Update categorical_nb.md
abhinavmalhotra01 8ac9880
Update categorical_nb.hpp
abhinavmalhotra01 5822850
Merge remote-tracking branch 'upstream/main' into categorical_naive_b…
abhinavmalhotra01 f78c85f
changes
abhinavmalhotra01 d5c782a
changes
abhinavmalhotra01 1587a44
clang
abhinavmalhotra01 6f54c87
m
abhinavmalhotra01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # 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<vector<T>>`| | ||
| | `yTrain` |The set containing the class correspoding to respective xTrain instance|`vector<string`| | ||
| | `xTest` |The sample set , whose class will be predicted|`vector<T>`| | ||
|
|
||
| ## Methods | ||
|
|
||
| | Name | Definition | Return value | | ||
| |--------------------------------------------------|---------------------------------------------|-----------| | ||
| | `fit(vector<vector<T>> xTrain,vector<T> yTrain)` |fit the class instance with the training data|`NULL`| | ||
| | `predict(vector<T> xTest` |predict the label for xTest vector of features|`string`| | ||
|
|
||
| ## Example | ||
|
|
||
| ```cpp | ||
| std::vector <std::vector<std::string>> 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 <std::string> yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; | ||
| std::vector <std::string> xTest = {"fifa", "no", "yes", "yes"}; | ||
| categoricalNB<std::string> classifier; | ||
| classifier.fit(xTrain,yTrain); | ||
| std::cout<<classifier.predict(xTest); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //#include "../../src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp" | ||
| //#include "../../src/slowmokit/core.hpp" | ||
| // | ||
| //signed main() { | ||
| // | ||
| // std::vector <std::vector<std::string>> 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 <std::string> yTrain = {"m", "m", "m", "m", "f", "f", "f", "f"}; | ||
| // std::vector <std::string> xTest = {"fifa", "no", "yes", "yes"}; | ||
| // categoricalNB<std::string> classifier; | ||
| // classifier.fit(xTrain,yTrain) | ||
| // std::cout<<classifier.predict(xTest); | ||
| // | ||
| //} |
79 changes: 79 additions & 0 deletions
79
src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * @file methods/neighbors/gaussian_nb/categorical_nb.cpp | ||
| * | ||
| * Implementation of the Categorical Naive Bayes main program | ||
| */ | ||
|
|
||
| #include "categorical_nb.hpp" | ||
| std::map<std::string, double> priors; | ||
| std::map<std::string, std::map<std::string, double>> | ||
| likelihoods; // for each label we will store a map , containing n features | ||
| // and their corresponding probability | ||
| template<class T> | ||
| void fit(std::vector<std::vector<T>> xTrain, std::vector<std::string> yTrain) | ||
| { | ||
| // 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<std::string, int> occurences; | ||
|
|
||
| for (auto category : yTrain) | ||
| { | ||
| occurences[category]++; | ||
| } | ||
| for (auto current : occurences) | ||
| { | ||
| priors[current.first] = double(current.second) / yTrain.size(); | ||
| } | ||
|
|
||
| std::map<std::string, std::map<T, int>> counts; | ||
|
|
||
| for (int i = 0; i < xTrain.size(); i++) | ||
| { | ||
| std::vector<T> 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 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template<class T> std::string predict(std::vector<T> xTest) | ||
| { | ||
| std::map<std::string, double> 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; | ||
| } | ||
| } | ||
|
|
||
| return out; | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/slowmokit/methods/neighbors/categorical_nb/categorical_nb.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * @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 T> class CategoricalNB | ||
| { | ||
| CategoricalNB() | ||
| { | ||
| std::map<std::string, double> priors; | ||
| std::map<std::string, std::map<T, double>> likelihoods; | ||
| } | ||
|
|
||
| public: | ||
| /** | ||
| * @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 | ||
| * @return NULL | ||
| */ | ||
| void fit(std::vector<std::vector<T>> xTrain, std::vector<std::string> 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<T> xTest); | ||
| }; | ||
|
|
||
| #endif // SLOWMOKIT_CATEGORICAL_NB_HPP | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.