How does Naive Bayes classifier work for continuous variables?
$begingroup$
I know that for categorical features we just calculate the prior and likelihood probability assuming conditional independence between the features.
How does it work for continuous variables? How can we calculate likelihood probability for continuous variable?
machine-learning python naive-bayes-classifier
$endgroup$
add a comment |
$begingroup$
I know that for categorical features we just calculate the prior and likelihood probability assuming conditional independence between the features.
How does it work for continuous variables? How can we calculate likelihood probability for continuous variable?
machine-learning python naive-bayes-classifier
$endgroup$
add a comment |
$begingroup$
I know that for categorical features we just calculate the prior and likelihood probability assuming conditional independence between the features.
How does it work for continuous variables? How can we calculate likelihood probability for continuous variable?
machine-learning python naive-bayes-classifier
$endgroup$
I know that for categorical features we just calculate the prior and likelihood probability assuming conditional independence between the features.
How does it work for continuous variables? How can we calculate likelihood probability for continuous variable?
machine-learning python naive-bayes-classifier
machine-learning python naive-bayes-classifier
edited Mar 11 at 18:45
Esmailian
3,191320
3,191320
asked Mar 10 at 8:22
user214user214
22818
22818
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
The difference boils down to "how we define $P(x_i|C_k)$?", where $x_i$ is a single feature, and $C_k$ is a class from a total of $K$ classes.
Discrete
In discrete case, $P(x_i|C_k)$ is represented by a table as follows:
x_i P(x_i|C_k)
a 0.5
b 0.2
c 0.3
We have one of these tables for each feature-class pair $(i, k)$.
Lets denote $i$-th feature of data point $n$ as $x_{n,i}$. Each row of this table can be estimated using
$$hat{P}(x_i=a|C_k) = frac{sum_{n:n in C_k}{mathbb{1}_{x_{n,i}=a}}}{N_k}$$
which divides the number of samples that have i-th feature equal to $a$ by total number of samples in class $C_k$ (of course from the training set).
Also, check out pseudocount that avoids zero estimations.
Continuous
In continuous case, we either discretize the continuous interval of $x_i$ into bins ${b_1,..,b_m}$ and proceed the same as discrete case, or we assume a function like Gaussian (or any other one), as follows:
$$P(x_i|C_k)=frac{1}{sqrt{2pi}sigma_{i,k}}e^{-(x_i-mu_{i,k})^2/2sigma_{i,k}^2}$$
This way, for each feature-class pair $(i, k)$, $P(x_i|C_k)$ is represented with two parameters ${mu_{i,k}, sigma_{i, k}}$ instead of a table in discrete case. The estimation of the parameters is the same as fitting a Gaussian distribution to one dimensional data, that is:
$$hat{mu}_{i,k} = frac{sum_{n:n in C_k}{x_{n,i}}}{N_k}, hat{sigma}^2_{i, k} = frac{sum_{n:n in C_k}{(x_{n,i} - hat{mu}_{i,k})^2}}{N_k-1}$$
Instead of Gaussian, we can opt for a more complex function, even a neural network. In that case, we should look for a technique to fit the function to data just like what we did with Gaussian.
The rest is independent of feature type
Representing $P(C_k)$ is the same for both discrete and continuous cases and is estimated as $hat{P}(C_k)=N_k/N$.
Finally, the classifier is $$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}hat{P}(C_k)prod_{i}hat{P}(x_i=x_{n,i}|C_k)$$
Or equivalently using log-probabilities,
$$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}mbox{log}hat{P}(C_k)+sum_{i}mbox{log}hat{P}(x_i=x_{n,i}|C_k)$$
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47027%2fhow-does-naive-bayes-classifier-work-for-continuous-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The difference boils down to "how we define $P(x_i|C_k)$?", where $x_i$ is a single feature, and $C_k$ is a class from a total of $K$ classes.
Discrete
In discrete case, $P(x_i|C_k)$ is represented by a table as follows:
x_i P(x_i|C_k)
a 0.5
b 0.2
c 0.3
We have one of these tables for each feature-class pair $(i, k)$.
Lets denote $i$-th feature of data point $n$ as $x_{n,i}$. Each row of this table can be estimated using
$$hat{P}(x_i=a|C_k) = frac{sum_{n:n in C_k}{mathbb{1}_{x_{n,i}=a}}}{N_k}$$
which divides the number of samples that have i-th feature equal to $a$ by total number of samples in class $C_k$ (of course from the training set).
Also, check out pseudocount that avoids zero estimations.
Continuous
In continuous case, we either discretize the continuous interval of $x_i$ into bins ${b_1,..,b_m}$ and proceed the same as discrete case, or we assume a function like Gaussian (or any other one), as follows:
$$P(x_i|C_k)=frac{1}{sqrt{2pi}sigma_{i,k}}e^{-(x_i-mu_{i,k})^2/2sigma_{i,k}^2}$$
This way, for each feature-class pair $(i, k)$, $P(x_i|C_k)$ is represented with two parameters ${mu_{i,k}, sigma_{i, k}}$ instead of a table in discrete case. The estimation of the parameters is the same as fitting a Gaussian distribution to one dimensional data, that is:
$$hat{mu}_{i,k} = frac{sum_{n:n in C_k}{x_{n,i}}}{N_k}, hat{sigma}^2_{i, k} = frac{sum_{n:n in C_k}{(x_{n,i} - hat{mu}_{i,k})^2}}{N_k-1}$$
Instead of Gaussian, we can opt for a more complex function, even a neural network. In that case, we should look for a technique to fit the function to data just like what we did with Gaussian.
The rest is independent of feature type
Representing $P(C_k)$ is the same for both discrete and continuous cases and is estimated as $hat{P}(C_k)=N_k/N$.
Finally, the classifier is $$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}hat{P}(C_k)prod_{i}hat{P}(x_i=x_{n,i}|C_k)$$
Or equivalently using log-probabilities,
$$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}mbox{log}hat{P}(C_k)+sum_{i}mbox{log}hat{P}(x_i=x_{n,i}|C_k)$$
$endgroup$
add a comment |
$begingroup$
The difference boils down to "how we define $P(x_i|C_k)$?", where $x_i$ is a single feature, and $C_k$ is a class from a total of $K$ classes.
Discrete
In discrete case, $P(x_i|C_k)$ is represented by a table as follows:
x_i P(x_i|C_k)
a 0.5
b 0.2
c 0.3
We have one of these tables for each feature-class pair $(i, k)$.
Lets denote $i$-th feature of data point $n$ as $x_{n,i}$. Each row of this table can be estimated using
$$hat{P}(x_i=a|C_k) = frac{sum_{n:n in C_k}{mathbb{1}_{x_{n,i}=a}}}{N_k}$$
which divides the number of samples that have i-th feature equal to $a$ by total number of samples in class $C_k$ (of course from the training set).
Also, check out pseudocount that avoids zero estimations.
Continuous
In continuous case, we either discretize the continuous interval of $x_i$ into bins ${b_1,..,b_m}$ and proceed the same as discrete case, or we assume a function like Gaussian (or any other one), as follows:
$$P(x_i|C_k)=frac{1}{sqrt{2pi}sigma_{i,k}}e^{-(x_i-mu_{i,k})^2/2sigma_{i,k}^2}$$
This way, for each feature-class pair $(i, k)$, $P(x_i|C_k)$ is represented with two parameters ${mu_{i,k}, sigma_{i, k}}$ instead of a table in discrete case. The estimation of the parameters is the same as fitting a Gaussian distribution to one dimensional data, that is:
$$hat{mu}_{i,k} = frac{sum_{n:n in C_k}{x_{n,i}}}{N_k}, hat{sigma}^2_{i, k} = frac{sum_{n:n in C_k}{(x_{n,i} - hat{mu}_{i,k})^2}}{N_k-1}$$
Instead of Gaussian, we can opt for a more complex function, even a neural network. In that case, we should look for a technique to fit the function to data just like what we did with Gaussian.
The rest is independent of feature type
Representing $P(C_k)$ is the same for both discrete and continuous cases and is estimated as $hat{P}(C_k)=N_k/N$.
Finally, the classifier is $$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}hat{P}(C_k)prod_{i}hat{P}(x_i=x_{n,i}|C_k)$$
Or equivalently using log-probabilities,
$$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}mbox{log}hat{P}(C_k)+sum_{i}mbox{log}hat{P}(x_i=x_{n,i}|C_k)$$
$endgroup$
add a comment |
$begingroup$
The difference boils down to "how we define $P(x_i|C_k)$?", where $x_i$ is a single feature, and $C_k$ is a class from a total of $K$ classes.
Discrete
In discrete case, $P(x_i|C_k)$ is represented by a table as follows:
x_i P(x_i|C_k)
a 0.5
b 0.2
c 0.3
We have one of these tables for each feature-class pair $(i, k)$.
Lets denote $i$-th feature of data point $n$ as $x_{n,i}$. Each row of this table can be estimated using
$$hat{P}(x_i=a|C_k) = frac{sum_{n:n in C_k}{mathbb{1}_{x_{n,i}=a}}}{N_k}$$
which divides the number of samples that have i-th feature equal to $a$ by total number of samples in class $C_k$ (of course from the training set).
Also, check out pseudocount that avoids zero estimations.
Continuous
In continuous case, we either discretize the continuous interval of $x_i$ into bins ${b_1,..,b_m}$ and proceed the same as discrete case, or we assume a function like Gaussian (or any other one), as follows:
$$P(x_i|C_k)=frac{1}{sqrt{2pi}sigma_{i,k}}e^{-(x_i-mu_{i,k})^2/2sigma_{i,k}^2}$$
This way, for each feature-class pair $(i, k)$, $P(x_i|C_k)$ is represented with two parameters ${mu_{i,k}, sigma_{i, k}}$ instead of a table in discrete case. The estimation of the parameters is the same as fitting a Gaussian distribution to one dimensional data, that is:
$$hat{mu}_{i,k} = frac{sum_{n:n in C_k}{x_{n,i}}}{N_k}, hat{sigma}^2_{i, k} = frac{sum_{n:n in C_k}{(x_{n,i} - hat{mu}_{i,k})^2}}{N_k-1}$$
Instead of Gaussian, we can opt for a more complex function, even a neural network. In that case, we should look for a technique to fit the function to data just like what we did with Gaussian.
The rest is independent of feature type
Representing $P(C_k)$ is the same for both discrete and continuous cases and is estimated as $hat{P}(C_k)=N_k/N$.
Finally, the classifier is $$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}hat{P}(C_k)prod_{i}hat{P}(x_i=x_{n,i}|C_k)$$
Or equivalently using log-probabilities,
$$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}mbox{log}hat{P}(C_k)+sum_{i}mbox{log}hat{P}(x_i=x_{n,i}|C_k)$$
$endgroup$
The difference boils down to "how we define $P(x_i|C_k)$?", where $x_i$ is a single feature, and $C_k$ is a class from a total of $K$ classes.
Discrete
In discrete case, $P(x_i|C_k)$ is represented by a table as follows:
x_i P(x_i|C_k)
a 0.5
b 0.2
c 0.3
We have one of these tables for each feature-class pair $(i, k)$.
Lets denote $i$-th feature of data point $n$ as $x_{n,i}$. Each row of this table can be estimated using
$$hat{P}(x_i=a|C_k) = frac{sum_{n:n in C_k}{mathbb{1}_{x_{n,i}=a}}}{N_k}$$
which divides the number of samples that have i-th feature equal to $a$ by total number of samples in class $C_k$ (of course from the training set).
Also, check out pseudocount that avoids zero estimations.
Continuous
In continuous case, we either discretize the continuous interval of $x_i$ into bins ${b_1,..,b_m}$ and proceed the same as discrete case, or we assume a function like Gaussian (or any other one), as follows:
$$P(x_i|C_k)=frac{1}{sqrt{2pi}sigma_{i,k}}e^{-(x_i-mu_{i,k})^2/2sigma_{i,k}^2}$$
This way, for each feature-class pair $(i, k)$, $P(x_i|C_k)$ is represented with two parameters ${mu_{i,k}, sigma_{i, k}}$ instead of a table in discrete case. The estimation of the parameters is the same as fitting a Gaussian distribution to one dimensional data, that is:
$$hat{mu}_{i,k} = frac{sum_{n:n in C_k}{x_{n,i}}}{N_k}, hat{sigma}^2_{i, k} = frac{sum_{n:n in C_k}{(x_{n,i} - hat{mu}_{i,k})^2}}{N_k-1}$$
Instead of Gaussian, we can opt for a more complex function, even a neural network. In that case, we should look for a technique to fit the function to data just like what we did with Gaussian.
The rest is independent of feature type
Representing $P(C_k)$ is the same for both discrete and continuous cases and is estimated as $hat{P}(C_k)=N_k/N$.
Finally, the classifier is $$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}hat{P}(C_k)prod_{i}hat{P}(x_i=x_{n,i}|C_k)$$
Or equivalently using log-probabilities,
$$C(x_n) = underset{k in {1,..,K}}{mbox{argmax }}mbox{log}hat{P}(C_k)+sum_{i}mbox{log}hat{P}(x_i=x_{n,i}|C_k)$$
edited Mar 11 at 12:59
answered Mar 10 at 9:46
EsmailianEsmailian
3,191320
3,191320
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47027%2fhow-does-naive-bayes-classifier-work-for-continuous-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown