<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by chandan kumar singha on Medium]]></title>
        <description><![CDATA[Stories by chandan kumar singha on Medium]]></description>
        <link>https://medium.com/@cskkman?source=rss-b31bf32dabce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*DVG33yXMvCFM4_lNw3wuMg.jpeg</url>
            <title>Stories by chandan kumar singha on Medium</title>
            <link>https://medium.com/@cskkman?source=rss-b31bf32dabce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 09:59:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cskkman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ingress configuration to route http to https]]></title>
            <link>https://medium.com/@cskkman/ingress-configuration-to-route-http-to-https-8ae9f85b34d3?source=rss-b31bf32dabce------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ae9f85b34d3</guid>
            <category><![CDATA[ingress]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[chandan kumar singha]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 09:17:53 GMT</pubDate>
            <atom:updated>2023-01-02T09:17:53.140Z</atom:updated>
            <content:encoded><![CDATA[<p>Redirecting http traffic to https via nginx-ingress.</p><pre>apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br>  annotations:<br>    kubernetes.io/ingress.class: nginx<br>    nginx.ingress.kubernetes.io/server-snippet: |<br>      if ($http_x_forwarded_proto = &#39;http&#39;) {<br>        return 301 https://$server_name$request_uri;<br>      }<br>    nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;<br>  generation: 6<br>  name: my-route<br>  namespace: ns<br>spec:<br>  rules:<br>  - host: example.com<br>    http:<br>      paths:<br>      - backend:<br>          serviceName: my-svc<br>          servicePort: 80<br>        path: /<br>  - host: www.example.com<br>    http:<br>      paths:<br>      - backend:<br>          serviceName: my-svc<br>          servicePort: 80<br>        path: /</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ae9f85b34d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Enum Serialization in Golang]]></title>
            <link>https://medium.com/@cskkman/enum-serialization-in-golang-d82bce0a4030?source=rss-b31bf32dabce------2</link>
            <guid isPermaLink="false">https://medium.com/p/d82bce0a4030</guid>
            <dc:creator><![CDATA[chandan kumar singha]]></dc:creator>
            <pubDate>Fri, 21 Jan 2022 04:48:01 GMT</pubDate>
            <atom:updated>2022-01-21T04:51:38.887Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://pkg.go.dev/encoding/json">https://pkg.go.dev/encoding/json</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f885bdf0e68e0619371af814ef2a07fe/href">https://medium.com/media/f885bdf0e68e0619371af814ef2a07fe/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d82bce0a4030" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Text Classification Using Naive Bayes]]></title>
            <link>https://medium.com/analytics-vidhya/text-classification-using-naive-bayes-e889dbf1edd?source=rss-b31bf32dabce------2</link>
            <guid isPermaLink="false">https://medium.com/p/e889dbf1edd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[naive-bayes]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[bayes-theorem]]></category>
            <dc:creator><![CDATA[chandan kumar singha]]></dc:creator>
            <pubDate>Fri, 18 Oct 2019 12:34:35 GMT</pubDate>
            <atom:updated>2019-10-24T12:14:31.587Z</atom:updated>
            <content:encoded><![CDATA[<p>I bet most of us, reading this post, have solved a problem involving Bayes theorem either in the near or distant past. Problems like,</p><p><em>A deadly disease has been discovered (let&#39;s call it DiseaseXities), its diagnostic test is 99% accurate. It is known that 1% of the population already has the disease. You have taken the test and tested positive. What is the probability that you actually have the disease?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YvV8y-HMZfWhUuAN0k2X8g.png" /><figcaption>Bayes Theorem</figcaption></figure><p>Before decluttering this, let&#39;s have a look into the basics first, and then we will solve the above problem and then try to see how Bayes becomes Naive Bayes and used for classification.</p><h4>The Basics: Three Types Of Probabilities</h4><p>Probability is a measure quantifying the likelihood that events will occur. This event can be anything from getting a tail when a coin is tossed or getting 6 when a dice is rolled. Tossing of a coin or rolling of dice, both are <strong>experiments</strong> whereas getting a tail or 6 are <strong>events</strong> (set of outcomes) of those experiments.</p><p>Generally X is used as a variable to denote an event. Thus &quot;<em>what is the probability of getting a 6 when a dice is rolled?</em>&quot; will be denoted as &quot;<strong>P(X=6)</strong>&quot;.</p><p>The set of all outcomes in an experiment is called the <strong><em>sample space</em></strong> of that experiment. Thus the set {1, 2, 3, 4, 5, 6} is the sample space for a dice rolled. Formally, an <strong><em>event</em></strong> is a subset of the <strong><em>sample space</em></strong>.</p><p>Probability in its simplest form is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*9k3HgaLQBMWAlS2itPBBaw.png" /><figcaption>FIG — 1: the probability of an event E</figcaption></figure><p><strong><em>Marginal probability</em></strong> is the probability of an event irrespective of the outcome of another variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*m8IozU3Bo9qs1OGwPj1-eg.png" /><figcaption>FIG — 2: joint probability</figcaption></figure><p>A <strong><em>joint probability</em></strong> is the probability of two or more events occurring simultaneously. <strong>P(X=</strong><strong>ace &amp; heart)</strong> ?? when a card is drawn from a deck of cards, what is the probability that the drawn card is an Ace &amp; of Heart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*6lyZzUMDu3dA9so7ru63-w.png" /><figcaption>FIG — 3: probability of A given that B has already happened</figcaption></figure><p><strong><em>Conditional probability</em></strong> (key element understanding Bayes theorem) is the probability of an event given some other event already has happened. <br>eg. <em>Suppose a jar contains 5 balls of two colors (2 red 3 black), you pick two balls and found that one of them is red. what is the probability that the other ball is also red?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*wUUaveQMpcXb1dyHxpicvg.png" /><figcaption>FIG — 4</figcaption></figure><p>sample space= {RR, RB, BB}</p><p>P(red|red) = 1/2 ie {RR}/{RR,RB}</p><p>Now let us consider <strong><em>P(A|B)</em></strong>. Here we are particularly interested in <strong><em>A</em></strong>. since B has already happened, possible outcomes has to come from the shaded region in FIG-IV, restricting the scope to the region shared (blue shaded region) by both A and B ie P(A ∩ B), as per classical probability theory P = relevant/total, thus</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*p5jhHSMy5KdqfUimYM-Mbw.png" /><figcaption>FIG — 5: conditional Probability. (blue region/ yellow region)</figcaption></figure><p>Further,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*9h7pIJjv_3acNFr4EhzEng.png" /><figcaption>FIG — 6</figcaption></figure><p>Similarly,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*XF2ufBk6GjarXO0zKU__UQ.png" /><figcaption>FIG-7</figcaption></figure><p>Thus, from FIG-6 and FIG-7</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*COFZ_P6woW6MbWB-5nWM8w.png" /></figure><p>And this is Bayes theorem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*0JyuHfU_oIEiWDeEtbAaXQ.png" /><figcaption>FIG — 8</figcaption></figure><h4>Solving The Problem</h4><p>Now that we have arrived at the formula, let us use this to solve the problem above. let’s reiterate the question again</p><p><em>A deadly disease has been discovered (let’s call it DiseaseXities), its diagnostic test is 99% accurate. It is known that 1% of the population already has the disease. You have taken the test and tested positive. What is the probability that you actually have the disease?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*EjZ4aB4gvRFhh5JK70p7zA.png" /><figcaption>FIG — 9: image src: <a href="https://brilliant.org/wiki/bayes-theorem/">https://brilliant.org/wiki/bayes-theorem/</a></figcaption></figure><p>Let&#39;s formulate the problem mathematically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*zNhPIMZZt-sugoQSerV9dg.png" /><figcaption>FIG — 10</figcaption></figure><p>As the test is 99% accurate — an infected person will be detected positive 99% of the time and for a healthy person, the test will detect negative 99% of the time. And it is known that 1% of the population has the disease. Thus, the probability that a random person has the disease P(+ve) is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*huxXrQ5IhUCSJ6yflYfSqQ.png" /><figcaption>FIG — 11</figcaption></figure><p>As the test is 99% accurate</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*MLTTaGKuizsdJ8kXSX-wxw.png" /><figcaption>FIG — 12</figcaption></figure><p>As per Bayes’ Theorem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*JY2fKLhfhP6DDUyu3tZVqQ.png" /><figcaption>FIG — 13</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*nDnsmZtPi-GnM7TPHvbJBg.png" /><figcaption>FIG — 14</figcaption></figure><p>Thus the probability that you have the disease is 50%.</p><h4>Text Classification &amp; Sentiment Analysis</h4><p><strong><em>Text classification</em></strong> the task of assigning predefined categories to text documents. Sentiment analysis can be thought of as an area within text classification, where the focus lies in identifying the subjective information in text and classifying each piece of data as positive, negative, or neutral. Often referred to as <strong>opinion mining</strong>.</p><p>One of the classical examples of text classification is detecting whether an email is a spam or not. Here we will work with sample reviews restaurant reviews.</p><p>Before we move forward let us look into the Bayes theorem again, however with changed notations.</p><p>In mathematical terms, the problem of text categorization is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*z8IEgRDtYYBFJ99uR3Q9UA.png" /></figure><p>apply Bayes Theorem and we get</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*Lj-pO7eLPKkAHNXpO5gYBw.png" /></figure><p>As the denominator is the same in all the terms, it would be wise to write it as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*mOGLBu_uMZxkaBdGdBxEmQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*53GfeP13xGRDd5GHmRznGA.png" /></figure><p>For individual terms, P(H) the prior and P(D|H) the probability of seeing the text under the hypothesis has to be calculated. The prior helps you put prior beliefs of the hypothesis. P(+ve), the probability that any new review is a positive review. Thus</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*NKIQa_jb9vPg98YN6PeDCQ.png" /></figure><p>Similarly for other categories. Now that we have the way to get prior, let&#39;s find out the probability of getting the text under a particular hypothesis ie. P(Text|Hypothesis).</p><p>It would be very naive to say that words in a sentence do not depend on each other. However, we make the same assumption while calculating P(text|Hypothesis). Thus the name <strong><em>Naive Bayes</em></strong>.</p><blockquote>The assumption that features are independent, is very naive. Thus the name Naive Bayes.</blockquote><p>Now under the<em> Naive Assumption,</em> P(Text|Hypothesis) is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*cRxEwwyQgZdXwrLmQrBleg.png" /><figcaption>P(A ∩ B) = P(A) * P(B) if A and B are independent</figcaption></figure><p>You now know how a Naive Bayes classifier works.</p><h4>Getting our hands dirty</h4><p>Now let&#39;s try to put our learnings into building a classifier that we will train on a real dataset.</p><p>Yelp reviews dataset is readily available, just one google search away. To get an overview of the data you can refer to the Exploratory Data Analysis(EDA) section in this <a href="https://www.kaggle.com/suzanaiacob/sentiment-analysis-of-the-yelp-reviews-data/data">kaggle</a> notebook.</p><p>The reviews data contains many columns, however, we will pick relevant columns only ie text and stars . text contains the individual reviews from users along with their stars rated on a scale of 1 to 5. In order to simplify the task, stars can be relabelled. star count above 3 marked as 3 (positive), star count below 3 as 1 (negative) and 3 as 2 (neutral). Let’s look at how an individual review looks.</p><pre><em>“Love the staff, love the meat, love the place. Prepare for a long line around lunch or dinner hours. \n\nThey ask you how you want you meat, lean or something maybe, I can’t remember. Just say you don’t want it too fatty. \n\nGet a half sour pickle and a hot pepper. Hand cut french fries too.”</em></pre><p>We will start by converting the text into lowercase and then removing stop words. It would be wise to normalize words as well, word normalization like stemming(reducing the words to its <strong><em>stem</em></strong>), lemmatizing(reducing tow words to its root, the words is , am, are belongs to the same root be.</p><p>It is faster to deal with numbers compared to strings. we will turn the text content into numerical feature vectors. The <strong><em>bag-of-words</em></strong> model is commonly used in methods of document classification where the (frequency of) occurrence of each word is used as a feature for training a classifier. Please refer to <a href="https://stackabuse.com/python-for-nlp-creating-bag-of-words-model-from-scratch/">this article</a> on bag of words.</p><p>In simpler words, bag-of-words is a combination of two data structures one containing a list of the words in the corpus(in all the documents/texts) along with their indexes and another is a matrix M, where M[i,j] stores the count of j th word (a word with j as an index in the first ds) in the i th document. The first data structure (ie the list of words) would easily contain thousands of words, however, a sentence seldom crosses 100 words. Thus, making the matrix highly sparse, as most of the entries would be zeros.</p><p>scipy.sparse matrices are data structures, that store the same information with much ease(memory-optimized), and scikit-learn has built-in support for these structures.</p><pre># train_df is a pandas dataframe containing &quot;text&quot;, &quot;stars&quot; as<br># columns (stars being revised stars positive, neutral or negative)</pre><pre># test_df is similar to the train_df, however it will be used for <br># testing(evaluating the classifier)</pre><pre>from sklearn.feature_extraction.text import CountVectorizer<br>bog_d = CountVectorizer()<br>bog_m = bog_d.fit_transform(train_df.text.get_values())</pre><pre># get the index of the word good<br>&gt;&gt;&gt; bog_d.vocabulary_.get(u&quot;good&quot;)<br>&gt;&gt;&gt; 85492</pre><pre># thus the number 85492 represents &quot;good&quot;</pre><p>Occurrence count (frequency) is good if word counts of all the documents are more or less the same. However, reviews text length varies greatly. For longer documents, it will have higher average count values than shorter documents, even though they might talk about the same topics.</p><p>To avoid these potential discrepancies it suffices to divide the number of occurrences of each word in a document by the total number of words in the document: these new features are called <strong>Term Frequencies(tf)</strong>.</p><p>Another refinement on top of tfis to downscale weights for words that occur in many documents in the corpus and are therefore less informative than those that occur only in a smaller portion of the corpus. This downscaling is called <strong>tf–idf</strong> for <strong>“Term Frequency times Inverse Document Frequency”.</strong></p><p>So let&#39;s find the tf-idfs.</p><pre>from sklearn.feature_extraction.text import TfidfTransformer<br>tfidf_transformer = TfidfTransformer()<br>train_tfidf = tfidf_transformer.fit_transform(bog_m)</pre><p>Now that we have the features in proper format, let’s train the classifier.</p><pre>from sklearn.naive_bayes import MultinomialNB</pre><pre>clf = MultinomialNB().fit(train_tfidf, train_df.stars.get_values())</pre><p>yes, training the classifier with sklearnis this simple.</p><p>Let’s test.</p><pre>bog_tm = bog_d.transform(test_texts.get_values())<br>test_tfidf = tfidf_transformer.transform(bog_tm)</pre><pre>predicted = clf.predict(test_tfidf)</pre><pre>print(&quot;the accuracy of the classifier is&quot;, <br>             np.mean(predicted == test_targets))</pre><p>One of the salient features of Naive Bayes is it is lightning fast while training. It is super easy to understand.</p><h4>Citations &amp; References</h4><ol><li><a href="https://brilliant.org/wiki/bayes-theorem/">https://brilliant.org/wiki/bayes-theorem/</a></li><li><a href="https://en.wikipedia.org/wiki/Probability">https://en.wikipedia.org/wiki/Probability</a></li><li><a href="https://stackabuse.com/python-for-nlp-creating-bag-of-words-model-from-scratch/">https://stackabuse.com/python-for-nlp-creating-bag-of-words-model-from-scratch/</a></li></ol><p>Special thanks to <a href="https://medium.com/u/2fbc374a08b5">prajwal gurumurthy</a> for proof -reading this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e889dbf1edd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/text-classification-using-naive-bayes-e889dbf1edd">Text Classification Using Naive Bayes</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>