{"id":13099,"date":"2021-10-27T21:48:59","date_gmt":"2021-10-28T04:48:59","guid":{"rendered":"https:\/\/www.springboard.com\/?p=13099"},"modified":"2024-02-09T06:03:28","modified_gmt":"2024-02-09T14:03:28","slug":"naive-bayes-classification","status":"publish","type":"post","link":"https:\/\/www.springboard.com\/blog\/data-analytics\/naive-bayes-classification\/","title":{"rendered":"Naive Bayes Classification Using Scikit-learn In Python"},"content":{"rendered":"\n<p>Data Classification is one of the most common problems to solve in data analytics. While the process becomes simpler using platforms like R &amp; Python, it is essential to understand which technique to use. In this blog post, we will speak about one of the most powerful &amp; easy-to-train classifiers, \u2018Naive Bayes Classification. This is a classification technique that determines the probability of an outcome, given a set of conditions using the Bayes theorem. <\/p>\n\n\n\n<p>We have studied its possible applications and even tried our hand at the email spam filtering dataset on Python. One of the most important libraries that we use in Python, the Scikit-learn provides three Naive Bayes implementations: Bernoulli, multinomial, and Gaussian.  <\/p>\n\n\n\n<p>Before we dig deeper into Naive Bayes classification in order to understand what each of these variations in the Naive Bayes Algorithm will do, let us understand them briefly&#8230;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Bernoulli\u2019s is a binary algorithm particularly useful when a feature can be present or not.<\/li>\n\n\n\n<li>Multinomial Naive Bayes assumes a feature vector where each element represents the number of times it appears (or, very often, its frequency). <\/li>\n\n\n\n<li>The Gaussian Naive Bayes, instead, is based on a continuous distribution characterised by mean &amp; variance. It is suitable for more generic classification tasks.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s dig into each of these techniques, and see the best use of them in our data analytics problems.<\/p>\n\n\n<style>.blog-cta-salsey-03 {\toverflow: hidden;\t}\t.blog-cta-salsey-03-img {\tmax-width: 160px !important;\t}\t@media (min-width: 768px) {\t.blog-cta-salsey-03-content {\tmax-width: calc(100% - 281px);\t}\t.blog-cta-salsey-03-img {\tposition: absolute;\tmax-width: 100% !important;\tright: -10px;\tbottom: -10px;\t}\t}<\/style><div class=\"blog-cta-salsey-03 bg-blue-50 p-3 my-5 position-relative\"><div class=\"d-block d-md-flex\"><img decoding=\"async\" loading=\"lazy\" width=\"212\" height=\"232\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/08\/dac-student.png\" alt=\"Data Analyst student\" class=\"blog-cta-salsey-03-img mb-3 mb-md-0\" \/><div class=\"blog-cta-salsey-03-content\"><div class=\"d-flex align-items-center mb-2\"><img decoding=\"async\" class=\"pe-2\" width=\"86\" height=\"71\" loading=\"lazy\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/04\/job-guarantee-heading-badge.png\" alt=\"Job Guarantee\" style=\"mix-blend-mode: multiply\"><h4 class=\"fw-bold mb-0\">Become a Data Analyst. Land a Job or Your Money Back.<\/h4><\/div><p>Transform real-world datasets into actionable recommendations. Master technical and strategic thinking skills and frameworks. Land a job \u2014 or your money back.<\/p><p class=\"mb-sm-0\"><a class=\"btn btn-primary btn-lg\" href=\"https:\/\/www.springboard.com\/courses\/data-analytics-career-track\/#job-guarantee\">Explore course<\/a><\/p><\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Naive Bayes Classification Using Bernoulli<\/strong><\/h2>\n\n\n\n<p>If \u2018A\u2019 is a random variable then under Naive Bayes classification using Bernoulli distribution, it can assume only two values (for simplicity, let\u2019s call them 0 and 1). Their probability is: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">P(A) = p  if A = 1<br>P(A) = q  if A = 0<br>Where q = 1 - p &amp; 0 &lt; p &lt; 1<\/pre>\n\n\n\n<p>Let\u2019s try this algorithm on a dummy dataset that we create. We will use the scikit-learn library to implement the Bernoulli Naive Bayes algorithm. Do remember, Bernoulli naive Bayes expects binary feature vectors, however, the class Bernoulli Naive Bayes Algorithm has a binarize parameter. This parameter allows specifying a threshold that will be used internally to transform the features:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.datasets import make_classification<br><br><br>&gt;&gt;&gt; nb_samples = 300<br>&gt;&gt;&gt; X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0)<\/pre>\n\n\n\n<p>It generates a bidimensional dataset as below: <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"966\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-1200x966.png\" alt=\"Naive Bayes Classification\" class=\"wp-image-46737\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-1200x966.png 1200w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-400x322.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-768x618.png 768w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-380x306.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-700x564.png 700w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification.png 1272w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-380x306.png 420w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><figcaption class=\"wp-element-caption\">This image is created after implementing the code Python<\/figcaption><\/figure>\n\n\n\n<p>We have decided to use 0.0 as a binary threshold. This way, each point can be characterised by the quadrant where it\u2019s located. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.naive_bayes import BernoulliNB\nfrom sklearn.model_selection import train_test_split\n&gt;&gt;&gt; X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25)\n&gt;&gt;&gt; bnb = BernoulliNB(binarize=0.0)\n&gt;&gt;&gt; bnb.fit(X_train, Y_train)\n&gt;&gt;&gt; bnb.score(X_test, Y_test)\n0.85333333333333339<\/pre>\n\n\n\n<p>This score is rather good! To understand how the binary classifier worked, it\u2019s useful to see how the data have been internally binarized:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"920\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-1200x920.png\" alt=\"Naive Bayes Classification Using \u2018scikit-learn\u2019 In Python\" class=\"wp-image-46739\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-1200x920.png 1200w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-400x307.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-768x589.png 768w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-380x291.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-700x537.png 700w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python.png 1309w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python-380x291.png 420w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><figcaption class=\"wp-element-caption\">This image is created after implementing the code in Python<\/figcaption><\/figure>\n\n\n\n<p>Let&#8217;s check the naive Bayes predictions we obtain:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])<br>&gt;&gt;&gt; bnb.predict(data)<br>array([0, 0, 1, 1])<\/pre>\n\n\n\n<p>This is the output that was expected from Bernoulli\u2019s naive Bayes!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Data Classification Using Multinomial Naive Bayes<\/strong> Algorithm<\/h2>\n\n\n\n<p>A multinomial Naive Bayes algorithm is useful to model feature vectors where each value represents the number of occurrences of a term or its relative frequency. For example, if a feature vector has n elements and each of them can assume k different values with probability pk, then:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"113\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/multinomial-naive-bayes.png\" alt=\"Multinomial Naive Bayes\" class=\"wp-image-46740\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/multinomial-naive-bayes.png 696w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/multinomial-naive-bayes-400x65.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/multinomial-naive-bayes-380x62.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/multinomial-naive-bayes-380x62.png 420w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><figcaption class=\"wp-element-caption\">Source: <a href=\"http:\/\/wikipedia.com\" target=\"_blank\" data-type=\"URL\" data-id=\"wikipedia.com\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/figcaption><\/figure>\n\n\n\n<p>The conditional probabilities P(xi | y) are computed with a frequency count. The frequency count corresponds to applying a maximum likelihood approach. During Multinomial Bayes Formula,  Laplace smoothing factor is to be kept in mind. Its default value is 1.0 and prevents the model from setting null probabilities when the frequency is zero.<\/p>\n\n\n\n<p>Let\u2019s understand this with an example, using the DictVectorizer. We consider only two records: the first one representing a city, while the second one countryside. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.feature_extraction import DictVectorizer\n&gt;&gt;&gt; data = [\n{'house': 100, 'street': 50, 'shop': 25, 'car': 100, 'tree': 20},\n{'house': 5, 'street': 5, 'shop': 0, 'car': 10, 'tree': 500, 'river': 1}\n] \n\n&gt;&gt;&gt; dv = DictVectorizer(sparse=False)\n&gt;&gt;&gt; X = dv.fit_transform(data)\n&gt;&gt;&gt; Y = np.array([1, 0])\n\n&gt;&gt;&gt; X\narray([[ 100.,  100.,    0.,   25.,   50.,   20.],[  10.,    5.,    1.,    0.,    5.,  500.]])\n<\/pre>\n\n\n\n<p>Note that the term \u2018river\u2019 is missing from the first set, so it\u2019s useful to keep alpha equal to 1.0 to give it a small probability. The output classes are 1 for city and 0 for the countryside. Now we can train a Multinomial Naive Bayes instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.naive_bayes import MultinomialNB\n\n&gt;&gt;&gt; mnb = MultinomialNB()\n&gt;&gt;&gt; mnb.fit(X, Y)\nMultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)<\/pre>\n\n\n\n<p>To test the model, we create a dummy city with a river and a dummy country place without any river.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; test_data = data = [<br>{'house': 80, 'street': 20, 'shop': 15, 'car': 70, 'tree': 10, 'river': <br>1},<br><br>]<br>{'house': 10, 'street': 5, 'shop': 1, 'car': 8, 'tree': 300, 'river': 0} <br><br>&gt;&gt;&gt; mnb.predict(dv.fit_transform(test_data))<br>array([1, 0])<\/pre>\n\n\n\n<p>As we can see, this prediction is correct!<\/p>\n\n\n<div class=\"bg-leaf-50 p-4 my-3\"><h4 class=\"fw-bold text-center\">Get To Know Other\tData Analytics Students<\/h4><div class=\"row row-cols-1 row-cols-lg-3\"><div class=\"col\"><div class=\"card success-story-card h-100 d-flex justify-content-between mb-0\"><div class=\"flex-grow-1 text-center\"><a class=\"d-inline-block rounded-circle\" href=\"\/success\/jo-liu\" style=\"width:125px;height:125px;overflow:hidden\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/res.cloudinary.com\/springboard-images\/image\/upload\/v1654053530\/Student%20Success\/Jo_Liu.jpg\" alt=\"Jo Liu\" style=\"object-fit:contain;max-width:170px;height:125px\" \/><\/a><p class=\"fw-bold mb-0\">Jo Liu<\/p><p class=\"text-muted lh-1\">App Quality Analyst at Snap Inc.<\/p><\/div><div class=\"w-100 d-block d-md-none mt-3\"><\/div><p class=\"mb-0 mx-auto text-center\"><a class=\"btn btn-primary mx-auto\" href=\"\/success\/jo-liu\">Read Story<\/a><\/p><\/div><\/div><div class=\"col d-none d-md-block\"><div class=\"card success-story-card h-100 d-flex justify-content-between mb-0\"><div class=\"flex-grow-1 text-center\"><a class=\"d-inline-block rounded-circle\" href=\"\/success\/joel-antolijao\" style=\"width:125px;height:125px;overflow:hidden\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/res.cloudinary.com\/springboard-images\/image\/upload\/v1648514795\/Student%20Success\/Joel_Antolijao.jpg\" alt=\"Joel Antolijao\" style=\"object-fit:contain;max-width:170px;height:125px\" \/><\/a><p class=\"fw-bold mb-0\">Joel Antolijao<\/p><p class=\"text-muted lh-1\">Data Analyst at FanDuel<\/p><\/div><p class=\"mb-0 mx-auto text-center\"><a class=\"btn btn-primary mx-auto\" href=\"\/success\/joel-antolijao\">Read Story<\/a><\/p><\/div><\/div><div class=\"col d-none d-md-block\"><div class=\"card success-story-card h-100 d-flex justify-content-between mb-0\"><div class=\"flex-grow-1 text-center\"><a class=\"d-inline-block rounded-circle\" href=\"\/success\/reagan-tatsch\" style=\"width:125px;height:125px;overflow:hidden\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/res.cloudinary.com\/springboard-images\/image\/upload\/v1629203194\/Student%20Success\/Reagan_Tatsch_125x125.png\" alt=\"Reagan Tatsch\" style=\"object-fit:contain;max-width:170px;height:125px\" \/><\/a><p class=\"fw-bold mb-0\">Reagan Tatsch<\/p><p class=\"text-muted lh-1\">Data Operations Manager at ISS<\/p><\/div><p class=\"mb-0 mx-auto text-center\"><a class=\"btn btn-primary mx-auto\" href=\"\/success\/reagan-tatsch\">Read Story<\/a><\/p><\/div><\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Naive Bayes Classification Using Gaussian<\/strong><\/h2>\n\n\n\n<p>Gaussian Naive Bayes is useful when working with continuous values where probabilities can be modeled using a Gaussian distribution:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"133\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/gaussian-naive-bayes.png\" alt=\"Gaussian Naive Bayes\" class=\"wp-image-46741\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/gaussian-naive-bayes.png 406w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/gaussian-naive-bayes-400x131.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/gaussian-naive-bayes-380x124.png 380w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><figcaption class=\"wp-element-caption\">Source: <a href=\"http:\/\/wikipedia.com\" target=\"_blank\" data-type=\"URL\" data-id=\"wikipedia.com\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/figcaption><\/figure>\n\n\n\n<p>The conditional probabilities P(xi | y) are also Gaussian distributed and, therefore, it\u2019s necessary to estimate the mean and variance of each of them using the maximum likelihood approach. On considering the property of a Gaussian, we get:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"102\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/06\/naive-bayes-classification-using-scikit-learn-in-python_2.png\" alt=\"\" class=\"wp-image-46742\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/06\/naive-bayes-classification-using-scikit-learn-in-python_2.png 696w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/06\/naive-bayes-classification-using-scikit-learn-in-python_2-400x59.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/06\/naive-bayes-classification-using-scikit-learn-in-python_2-380x56.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2023\/06\/naive-bayes-classification-using-scikit-learn-in-python_2-380x56.png 420w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><figcaption class=\"wp-element-caption\">Source: <a href=\"http:\/\/wikipedia.com\" target=\"_blank\" data-type=\"URL\" data-id=\"wikipedia.com\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>k index refers to the samples in our dataset<\/li>\n\n\n\n<li>P(xi|y) is a Gaussian itself<\/li>\n<\/ul>\n\n\n\n<p>From this, we get mean and variance for each Gaussian associated with P(xi | y), &amp; the model is hence trained.<\/p>\n\n\n\n<p>Let\u2019s compare Gaussian Naive Bayes with logistic regression using the ROC curves as an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.datasets import make_classification<br>&gt;&gt;&gt; nb_samples = 300<br>&gt;&gt;&gt; X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0)<\/pre>\n\n\n\n<p>Here is the dataset that you may obtain:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"560\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python_3.png\" alt=\"Naive Bayes Classification Using \u2018scikit-learn\u2019 In Python_3\" class=\"wp-image-46743\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python_3.png 696w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python_3-400x322.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python_3-380x306.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/naive-bayes-classification-using-scikit-learn-in-python_3-380x306.png 420w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><figcaption class=\"wp-element-caption\">This image is created after implementing the code in Python<\/figcaption><\/figure>\n\n\n\n<p>Let\u2019s train both models and generate the ROC curves:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.naive_bayes import GaussianNB\nfrom sklearn.linear_model import LogisticRegression from sklearn.metrics import roc_curve, auc\nfrom sklearn.model_selection import train_test_split\n\n&gt;&gt;&gt; X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25)\n&gt;&gt;&gt; gnb = GaussianNB()\n&gt;&gt;&gt; gnb.fit(X_train, Y_train)\n&gt;&gt;&gt; Y_gnb_score = gnb.predict_proba(X_test)\n&gt;&gt;&gt; lr = LogisticRegression() \n&gt;&gt;&gt; lr.fit(X_train, Y_train)\n&gt;&gt;&gt; Y_lr_score = lr.decision_function(X_test)\n&gt;&gt;&gt; fpr_gnb, tpr_gnb, thresholds_gnb = roc_curve(Y_test, Y_gnb_score[:, 1])\n&gt;&gt;&gt; fpr_lr, tpr_lr, thresholds_lr = roc_curve(Y_test, Y_lr_score)<\/pre>\n\n\n\n<p>The resulting ROC curves would be like this:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"709\" src=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/roc-curve-output.png\" alt=\"ROC Curve output\" class=\"wp-image-46744\" srcset=\"https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/roc-curve-output.png 696w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/roc-curve-output-400x407.png 400w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/roc-curve-output-380x387.png 380w, https:\/\/www.springboard.com\/blog\/wp-content\/uploads\/2021\/10\/roc-curve-output-380x387.png 420w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><figcaption class=\"wp-element-caption\">This image is created after implementing the code in Python<\/figcaption><\/figure>\n\n\n\n<p>As you can see, the Naive Bayes performances are slightly better than logistic regression. Both the classifiers have similar accuracy and Area Under the Curve.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sklearn.datasets import load_digits<br>from sklearn.model_selection import cross_val_score<br><br>&gt;&gt;&gt; digits = load_digits()<br><br>&gt;&gt;&gt; gnb = GaussianNB()<br>&gt;&gt;&gt; mnb = MultinomialNB()<br><br>&gt;&gt;&gt; cross_val_score(gnb, digits.data, digits.target, scoring='accuracy', cv=10).mean()<br>0.81035375835678214<br><br>&gt;&gt;&gt; cross_val_score(mnb, digits.data, digits.target, scoring='accuracy', cv=10).mean()<br>0.88193962163008377<\/pre>\n\n\n\n<p>When trying the multinomial Naive Bayes &amp; the Gaussian variant as well, the results come very similar. You will realise that the multinomial distribution was better fitting the data, while a Gaussian was slightly more limited by its mean and variance.<\/p>\n\n\n\n<p>We have now understood the limitations and implications of the variations in Naive Bayes Algorithm techniques. While implementing, we need to note the possible constraints of each type, so that the algorithm generates the best outcomes. <\/p>\n\n\n\n<p>Does this classifier algorithm solve the data problem that you have been having? If not, then check out some more techniques like k-means or knn that can help you classify data. You can learn the applications of these algorithms in Springboard\u2019s <a href=\"https:\/\/www.springboard.com\/courses\/data-analytics-career-track\/\">Data Analytics Bootcamp<\/a>. With 1:1 mentoring and project-based curriculum that comes with a job guarantee, you can kickstart your career in <a href=\"https:\/\/www.springboard.com\/blog\/data-analytics\/what-is-data-analytics\/\" data-type=\"URL\" data-id=\"https:\/\/www.springboard.com\/blog\/data-analytics\/what-is-data-analytics\/\">Data Analytics<\/a> with this specially designed program.  And guide you to become a successful <a href=\"https:\/\/www.springboard.com\/blog\/data-analytics\/what-does-data-analyst-do\/\" data-type=\"URL\" data-id=\"https:\/\/www.springboard.com\/blog\/data-analytics\/what-does-data-analyst-do\/\">Data Analyst<\/a>.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#efeff6\"><strong>Since you&#8217;re here&#8230;<br><\/strong>Switching to a career in data analytics is possible, no matter your background. We\u2019ve helped <a href=\"https:\/\/www.springboard.com\/success\/\" target=\"_blank\" rel=\"noreferrer noopener\">over 10,000 students <\/a>make it happen. Check out our <a href=\"https:\/\/www.springboard.com\/resources\/learning-paths\/data-analysis\/\" target=\"_blank\" rel=\"noreferrer noopener\">free data analytics curriculum<\/a> to gauge your interest, or go all-in with our <a href=\"https:\/\/www.springboard.com\/courses\/data-analytics-career-track\/\" target=\"_blank\" rel=\"noreferrer noopener\">Data Analytics Bootcamp<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data Classification is one of the most common problems to solve in data analytics. While the process becomes simpler using platforms like R &amp; Python, it is essential to understand which technique to use. In this blog post, we will speak about one of the most powerful &amp; easy-to-train classifiers, \u2018Naive Bayes Classification. This is [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":46746,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_eb_data_table":"","footnotes":""},"categories":[134],"tags":[],"marketing_tags":[],"class_list":{"0":"post-13099","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-analytics"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/posts\/13099"}],"collection":[{"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/comments?post=13099"}],"version-history":[{"count":4,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/posts\/13099\/revisions"}],"predecessor-version":[{"id":53642,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/posts\/13099\/revisions\/53642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/media\/46746"}],"wp:attachment":[{"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/media?parent=13099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/categories?post=13099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/tags?post=13099"},{"taxonomy":"marketing_tags","embeddable":true,"href":"https:\/\/www.springboard.com\/blog\/wp-json\/wp\/v2\/marketing_tags?post=13099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}