How does Naive Bayes classifier work for continuous variables?












2












$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?










share|improve this question











$endgroup$

















    2












    $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?










    share|improve this question











    $endgroup$















      2












      2








      2


      1



      $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?










      share|improve this question











      $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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 11 at 18:45









      Esmailian

      1,421113




      1,421113










      asked Mar 10 at 8:22









      user214user214

      18417




      18417






















          1 Answer
          1






          active

          oldest

          votes


















          3












          $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)$$






          share|improve this answer











          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            });
            });
            }, "mathjax-editing");

            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            3












            $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)$$






            share|improve this answer











            $endgroup$


















              3












              $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)$$






              share|improve this answer











              $endgroup$
















                3












                3








                3





                $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)$$






                share|improve this answer











                $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)$$







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 11 at 12:59

























                answered Mar 10 at 9:46









                EsmailianEsmailian

                1,421113




                1,421113






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?