<?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 Arka Mukherjee on Medium]]></title>
        <description><![CDATA[Stories by Arka Mukherjee on Medium]]></description>
        <link>https://medium.com/@arkamukherjee?source=rss-2833fbb57dc5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vIcy250-X4hLTryDuBRfgw.jpeg</url>
            <title>Stories by Arka Mukherjee on Medium</title>
            <link>https://medium.com/@arkamukherjee?source=rss-2833fbb57dc5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:25:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arkamukherjee/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[A guide on migrating “mgo” APIs to “mongo-go-driver” in a GoLang server]]></title>
            <link>https://medium.com/@arkamukherjee/a-guide-on-migrating-mgo-apis-to-mongo-go-driver-in-a-golang-server-32a4e6f0fc5e?source=rss-2833fbb57dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/32a4e6f0fc5e</guid>
            <category><![CDATA[mgo]]></category>
            <category><![CDATA[mongo]]></category>
            <category><![CDATA[bds]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Arka Mukherjee]]></dc:creator>
            <pubDate>Thu, 27 Feb 2020 16:05:27 GMT</pubDate>
            <atom:updated>2020-02-28T14:46:37.382Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qkPPwhQrMtoL-e18k0TXRA.png" /></figure><p>While the community Mongo GoLang Driver (mgo) supports almost all MongoDB APIs and functionalities, it has been largely unmaintained for the last few years. <a href="https://github.com/mongodb/mongo-go-driver">Mongo-Go-Driver</a>, on the other hand, is the only driver that is officially supported by MongoDB engineers, and it supports all the newest MongoDB features and is guaranteed to work with the newer MongoDB versions (v4). mgo does have partial support for MongoDB v4, but as stated in the <a href="https://github.com/globalsign/mgo">repository</a>, the support is experimental.</p><p>While it was in <a href="https://www.mongodb.com/blog/post/go-migration-guide">BETA till 2019</a>, the Mongo-Go-Driver currently has all the newest features of MongoDB, and it gets regular commits.</p><p>In this article, we will go through several code snippets and analyze how the same mgo functionality is mirrored in mongo-go-driver. This article is intended to make the driver transition for a codebase quick and smooth, and ensure that there are fewer hitches while running automated tests.</p><h3>Establishing a Mongo connection:</h3><h4>mgo:</h4><p>In mgo, we use the mgo.Dial API to establish a session identified by given seed server(s). We can look at a code snippet as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd6d8fe7b274c0cc0716903cede75d4f/href">https://medium.com/media/fd6d8fe7b274c0cc0716903cede75d4f/href</a></iframe><h4>mongo-go-driver:</h4><p>In the official driver, we can use the following lines of code for the same operation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3344680421312c266608c0da4166b8be/href">https://medium.com/media/3344680421312c266608c0da4166b8be/href</a></iframe><p>Please note that a context is passed to every mongo-go-driver API call that you make, so you will need to define it as per your DB use-case. If you are unsure about which context to use, you can make use of a TODO() operation to use a non-nil empty context interface for the API calls. This is done using the following line of code:</p><pre>ctx := context.TODO()</pre><p>For this guide, I will use the context.TODO() operation to keep the flow simple.</p><h3>CRUD Operations:</h3><h4>Find:</h4><p>Find prepares a query using the provided document, and the data can be retrieved into a model using methods like One, Tail or Iter.</p><p><strong>mgo:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/950cebab58764586dadc9968bbd0e18d/href">https://medium.com/media/950cebab58764586dadc9968bbd0e18d/href</a></iframe><p><strong>mongo-go-driver:</strong></p><p>This change is very straight forward. All you have to do is replace Find with FindOne. A code sample is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/060a3945ec9e0cb8bb6b34929476aa72/href">https://medium.com/media/060a3945ec9e0cb8bb6b34929476aa72/href</a></iframe><p>We use the Decode method to get the result into an interface.</p><h4>Find and Modify:</h4><p>The findAndModify collection method in MongoDB modifies and returns a single document.</p><p><strong>mgo:</strong></p><p>In mgo, we use the <a href="https://godoc.org/labix.org/v2/mgo#Query.Apply">Apply</a> method to run the findAndModify command. Apply runs the findAndModify MongoDB command, which allows updating, upserting or removing a document matching a query and atomically returning either the old version (the default) or the new version of the document (when ReturnNew is true).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/431245597824df2c8b0378dccffacab1/href">https://medium.com/media/431245597824df2c8b0378dccffacab1/href</a></iframe><p><strong>mongo-go-driver:</strong></p><p>The FindOneAndUpdate API internally uses theFindAndModify operation. When I was working on porting my project from mgo to the mongo-go-driver in early 2019, they <a href="https://stackoverflow.com/questions/55725327/using-findandmodify-to-run-queries-in-the-official-mongo-go-driver">did not</a> have this explicitly mentioned in the documentation, so I had to dive into the source code of the driver to figure this out.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a61328bbbe07c910ef2821a064b48c8/href">https://medium.com/media/5a61328bbbe07c910ef2821a064b48c8/href</a></iframe><h4>Updating data:</h4><p>The APIs for updating are pretty similar in both the drivers in consideration. The only difference is that the UpdateOne API in mongo-go-driver returns an interface document as well, which you may disregard if your use-case does not need it.</p><p><strong>mgo:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25350c7d12770c48ac03f38b6a81f94/href">https://medium.com/media/a25350c7d12770c48ac03f38b6a81f94/href</a></iframe><p><strong>mongo-go-driver:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efb991cd73a3d563d46d87cd571c3919/href">https://medium.com/media/efb991cd73a3d563d46d87cd571c3919/href</a></iframe><p>Insert and Delete are very similar too, and examples are as follows.</p><h4>Insert:</h4><p><strong>mgo:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a3c3c066f7fde00ba37934bca907b0d/href">https://medium.com/media/8a3c3c066f7fde00ba37934bca907b0d/href</a></iframe><p><strong>mongo-go-driver:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7c7d7ef54cfd0a4998d093249af5f45/href">https://medium.com/media/d7c7d7ef54cfd0a4998d093249af5f45/href</a></iframe><h4>Delete:</h4><p><strong>mgo:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/440b8c43175652c55027778c8b128d35/href">https://medium.com/media/440b8c43175652c55027778c8b128d35/href</a></iframe><p><strong>mongo-go-driver:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec8fc9d2df9c61b9087d19368f3b0666/href">https://medium.com/media/ec8fc9d2df9c61b9087d19368f3b0666/href</a></iframe><h4>Select and Projection operations:</h4><p><strong>mgo:</strong></p><p>The Select operation is used to get a <a href="https://docs.mongodb.com/manual/tutorial/query-documents/#projection">subset of fields </a>and it internally uses a Projection operation, as highlighted in the official MongoDB documentation. The following query only gets us the baz field.</p><pre>err = c.Find(bson.M{&quot;foo&quot;: &quot;alice&quot;}).Select(bson.M{&quot;baz&quot;: 1}).One(&amp;model)</pre><p><strong>mongo-go-driver:</strong></p><p>The SetProjection is the counterpart for the Select operation, and it adds an option to limit the fields returned for all documents. In this case, we limit it to one, and apply the same on the “baz” bson field.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de0baf3f271abbd38ba9d742e8130825/href">https://medium.com/media/de0baf3f271abbd38ba9d742e8130825/href</a></iframe><h3>Obtaining a MongoDB ObjectID from a hex value:</h3><h4>mgo:</h4><p>In mgo, we make use of the ObjectIdHex API to get an ObjectID (which is typed as string). Passing an invalid hex representation causes a runtime panic, which can result in a server break, so it advisable to look into the IsObjectIdHex to determine whether a string is is a valid hex representation.</p><p>A sample use-case of this can be seen in the update operation done below:</p><pre>uid := &quot;XXXXX&quot; //valid hex<br>update := bson.M {...} //valid bson.M containing $inc and $set maps<br>err := session.DB(&quot;db&quot;).C(&quot;collection&quot;).UpdateId(bson.ObjectIdHex(uid), update)</pre><p>Additionally, we use the IsObjectIdHex function to determine if a string is a valid hex.</p><pre>if !bson.IsObjectIdHex(userID) {<br>    fmt.Println(&quot;userID is not a valid hex&quot;)<br>}</pre><h4><strong>mongo-go-driver:</strong></h4><p>The way to obtain an objectID from a hex value is slightly different in the mongo-go-driver.</p><pre>userObjectID, err := primitive.ObjectIDFromHex(aID)<br> if err != nil {<br>    fmt.Println(&quot;Error in ObjectIDFromHex&quot;)<br>}</pre><p>The second return argument of the function tells us if the string is a valid hex or not, hence this method acts as a two-in-one replacement for the mgo method above.</p><h3>Footnotes:</h3><p>The bson type in mgo is compatible with the bson type in the official driver as both the implementations are based on the official <a href="http://bsonspec.org/">bson</a> specification. Despite that, I would implore you to thoroughly run tests on your code, as there are some subtle changes which may need you to re-write some pieces of code relying on bson data structures.</p><p>Porting the driver is indeed a pretty big change for your server, and whenever you have any doubts about an API/MongoDB operation, you should scramble through the source code of the drivers on Github to find your answers. I personally spent a lot of time finding method replacements and spent some time re-engineering existing DB algorithms while adhering to ACID principles.</p><p>Additionally, I have created a mini Github repository to complement this article. The repository is as follows:</p><p><a href="https://github.com/Arka161/MongoMigration">Arka161/MongoMigration</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32a4e6f0fc5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clannad After Story — How Jun Maeda portrays Hedonism]]></title>
            <link>https://medium.com/@arkamukherjee/clannad-after-story-how-jun-maeda-portrays-hedonism-1f84db2b9c81?source=rss-2833fbb57dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f84db2b9c81</guid>
            <category><![CDATA[clannad]]></category>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[anime]]></category>
            <category><![CDATA[videogames]]></category>
            <category><![CDATA[vn]]></category>
            <dc:creator><![CDATA[Arka Mukherjee]]></dc:creator>
            <pubDate>Sat, 28 Apr 2018 18:48:17 GMT</pubDate>
            <atom:updated>2021-09-05T22:56:30.996Z</atom:updated>
            <content:encoded><![CDATA[<p>Before you start reading this article, I will tell you that this article does contain spoilers to Clannad After Story. I also reference other KEY Visual Novels and anime, but only touch on minor plot points without spoiling anything here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IgZyBceQlCk46rbGVPymnA.jpeg" /></figure><p>When I had finished watching Star Wars for the first time as a kid, I was nothing short of being flabbergasted. The lightsaber battles and space sequences were incredible. But the thing that I loved the most about the saga was the continuity. The prequels weren’t great films by any means, but the way Revenge of The Sith and the New Hope were tied together as one saga gave an entirely new perspective.<br>A continuity that ranges over a time period of several years is not a very common thing in anime and Visual Novels. But Clannad is a story which took the risk and ended up creating one of the most emotionally enriching final arcs in any story ever written. Most regular romantic anime end with the main characters finally going out together or having a kiss. However, Clannad chooses something completely different — it chooses to explore life through its continuity. Jun Maeda, one of the lead writers of KEY VNs often gets inspired by songs and bands while naming his games. The name Clannad is the same as an Irish Band which he mistakenly thought meant ‘Clan’ or ‘Family’. The theme of the game does heavily revolve around the concept of family and friends from the start to finish. We get to see the significance of the nomenclature of the game more and more as we finish more character routes or come across important plot points. Tomoya Okazaki’s life takes almost a dramatic turn as we progress through the story. We see his character grow as a friend, employee, a father, a husband, and a son over the course of the game. When we start the game, he is yet another high-school delinquent who is frustrated with the day to day monotonous life. But as we progress, we see him slowly mature as he helps several people out with their problems and tragedies. When we begin After Story, he is almost an entirely different character and he envisions life in a different way. However, he still had a lot of walls to climb.</p><p>Clannad is not KEY’s first work to introduce a family centric theme. We can see similar themes in both AIR and Kanon. AIR’s final route shares a lot of similarities with Clannad’s After Story route. Similarly, the Little Busters come closer to a family than a group of friends. I highly recommend everyone to ‘read’ AIR and Little Busters instead of watching the anime counterparts because the Visual Novel has much better writing and presentation and does not feel rushed or milked.</p><p>Clannad’s After Story route has a completely different tone in the writing compared to the rest of the routes. The Clannad After Story anime picks up the route a bit later when Tomoya decides to get an apartment and move in with Nagisa. We have lesser humour, casual moments and comic relief, and see that the story has a complete change of tide just as Tomoya faces the real world. He was just a high school student in Clannad, but he is a mature family man in After Story. The main character routes establish the foundation for Clannad’s main message, but After Story firmly re-establishes it in the end. Let us look at a small clip in the Clannad anime, which gives us information about Tomoyo Sakagami’s past.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FqjTO2I4chjo%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DqjTO2I4chjo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqjTO2I4chjo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6edc631c0a321f38e0ec782b1a9c81b5/href">https://medium.com/media/6edc631c0a321f38e0ec782b1a9c81b5/href</a></iframe><p>This clip tries to tell the viewers that family plays a much more critical role in a person’s life than many other abstract and theoretical things. The joy of spending time with family is priceless. Family can mean anything. It doesn’t necessarily have to be someone who is related to you by blood. As long as someone is dear to you and cares about you and you care about the person as well, you can be a family. Human beings seek companionship and close friends or relatives are meant to help you out during the darkest of times. In this clip, Tomoyo asks Tomoya what keeps a person sane despite the dynamic nature of life. After that, Tomoya starts talking about finding a niche field one is talented in. Kinetic pleasures in Epicurean philosophy mean satisfying a desire. It can be anything, from eating at an expensive restaurant, getting promoted to a senior position at work, or even scratching an itch. Katastematic pleasures mean getting pleasure after realization or looking at the world differently. Happiness already exists around everyone. But we are so busy chasing our dreams and kinetic pleasures that we often fail to visualize the beauty of life. When Tomoya mentions relationships, after talking about talent, he comes closer to the ground in which Tomoyo’s state of mind resides in. Despite the issues Tomoyo’s parents had, they could come together again as a family as Takafumi made them realize that happiness was always in front of them. In the end, Tomoyo compares family with the ever-present Sakura trees in the town of Clannad. This can be compared to the nature of life. Life changes as we grow up. We might get new jobs, interests, lose jobs, lose interests, get in relationships, lose relationships, etc. We keep on moving forward. But happiness always stays in an encapsulated form which is readily available to us. We just do not always realize it. The final hour of the Tomoyo route tells us the same message as well.</p><p>Clannad also strongly emphasizes on the notion that life is worth it despite whatever hardships come in the way. When Nagisa dies of childbirth in Clannad After Story, Tomoya is completely shattered and broken down. He loses his ability to perceive both kinetic and katastematic pleasures and he drowns in a pool of addictions. What he does not realize is that there was something he was the only person in the world could do. He evades his responsibilities and lives his life in random spikes. In this phase after Nagisa’s death, Tomoya’s character is very similar to that of Apu in Satyajit Ray’s Apu’s Sansar(an old Indian movie which is a spiritual predecessor to Clannad). Akio Furukawa and Sanae Furukawa are much more mature and they did not lose sight of life despite their daughter’s demise. They took up the responsibility themselves to instill clarity of thought in Tomoya’s mind. After the trip with his daughter, Tomoya had finally metaphorically reached out to Nagisa and had found out that Ushio is someone only he can protect. Ushio was the purpose of his life and meaning been there in front of him for all these years and he failed to see it all along. During this trip, Tomoya also finally understands his dad’s suffering after talking to his grandmother. The title of the ending song of Clannad After Story (The Palm of a Tiny Hand) comes from the old trip his dad took him to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RhrIhLPuPksal835DQxzpA.png" /><figcaption>Tomoya’s trip with his dad</figcaption></figure><p>We all have our comedies, tragedies, failures, bad luck spurts, panic attacks, break-ups, etc. But there’s always a meaning of everything. Quoting Jun Maeda, “Even if we forget the faces of our friends, we never forget the bonds that were carved into our souls”. Nagisa was the best thing that happened to Tomoya and it gave a three hundred and sixty degrees turn to Tomoya’s life. Clannad After Story has a happy true ending. If it had a sad true ending, Jun Maeda and KEY would be low-key telling us, “Life’s sad and depressing. Bad things can happen anytime and destroy us. Don’t get attached to anyone. Life does terrible things”. That’s what Mayday Parade’s song ‘Terrible Things’ tell us. But Jun Maeda wanted to tell us something along these lines, “Life’s worth it. There are hardships, but it’s a part of it. There are beauties in life right under your nose which you aren’t looking at. Money, power and growth does not equate to eternal happiness. You need to open your eyes and see how much your friends and family love you. You don’t need to build a company as big as Microsoft or do a PhD in a highly complex field to achieve happiness. A simple coffee date with a friend, or reading a good book can bring in happiness. There’s more to life than how society guides us. Always be happy and try to help people be happy.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cTHJ4LOXU6Ctztg9Jc2iwg.jpeg" /><figcaption>The feels train — literally.</figcaption></figure><p>Clannad also gives us a supernatural insight to karma. Most of KEY’s works take place in universes where everything doesn’t work by the simple laws of science, and so does Clannad. So let us lay out the definition of karma according to Wikipedia. “Karma means action, work or deed; it also refers to the spiritual principle of cause and effect where intent and actions of an individual influence the future of that individual”. In Clannad, at the end of each route, the player is awarded with an orb of light if the correct decisions are made throughout the course of the game. If the player manages to get all the orbs of lights, he/she gets to see the true ending of Clannad. An ending in which Nagisa and Ushio are saved and we are moved to a perfect timeline (Similar to the Steins;Gate timeline where nothing ill-fated happened).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*9iOz0wyl5uYe4U2WS3Fqlw.jpeg" /><figcaption>A town where earnest wishes are answered</figcaption></figure><p>Quoting Jacob Chapman from AnimeNewsNetwork in his article <a href="https://www.animenewsnetwork.com/feature/2015-09-25/why-clannad-made-you-cry/.93370">‘Why Clannad Made you Cry’</a>, “Logically speaking, this could never happen, but Maeda wants us to believe in a world where it should happen. He wants us to believe that good karma can break the laws of time and space”. A more realistic portrayal of karma can be seen in Quentin Tarantino’s Pulp Fiction where Jules and Vincent end up in completely different paths because Jules plans to quit his job and Vincent doesn’t share the same views as Jules.</p><p>Lastly, I want to discuss something which isn’t directly related to the nature of this article, but I always wanted to write about it. Here, I will talk about the music split-up of Clannad. The three main music composers: Jun Maeda, Shinji Orito, and Magome Togoshi had split up the work pretty smartly and efficiently making the end result a musical masterpiece. Jun Maeda primarily handled OSTs related to family and nostalgia. Most of the scores he had composed were variations of three particular pieces: ‘Nagisa’, ‘Toki Wo Kizaku Uta’ and ‘Distant Years’. Shinji Orito on the other had the job of giving the world of Clannad life to enrich the emotions in the character routes and compliment the writing. His OSTs were also the most tragic ones and would stick to your head long after you finish watching Clannad. Some of his best OSTs were ‘Shionari’, ‘Yuki Nohara’ and ‘Town, Flow of Time, People’. Magome Togoshi, on the other hand had composed almost all the major leitmotifs in the series. Leitmotifs are musical scores which can be associated with characters, places or even objects. The leitmotif that you hear the most in the anime and the Visual Novel is Ushio’s theme in the Illusionary World(Fantasy). Some of the other notable leitmotifs are those OSTs of Fuko, Kotomi and Tomoyo. Due to this brilliant split of the themes of the soundtracks, the score of Clannad really works out and is able to make the viewer/reader really immerse into the vast world of Clannad. Clannad wouldn’t be the same without the gorgeous score.</p><p>Finally, I would thank the entire team behind Clannad’s VN and the anime for creating this masterpiece. This VN which has impacted my life a lot and I doubt any anime/VN will ever come close. Also, if you enjoy Jun Maeda’s writing and composition style, I highly recommend you to check out the Flaming June songs and KEY’s other Visual Novels. A lot of them are available in English on Steam. Thank you for reading this article till the end.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f84db2b9c81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A study of Classification Problems using Logistic Regression and an insight to the admissions…]]></title>
            <link>https://medium.com/data-science/a-study-of-classification-problems-using-logistic-regression-and-an-insight-to-the-admissions-ec69ddf93f36?source=rss-2833fbb57dc5------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec69ddf93f36</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Arka Mukherjee]]></dc:creator>
            <pubDate>Mon, 03 Apr 2017 21:40:05 GMT</pubDate>
            <atom:updated>2017-04-05T00:46:32.122Z</atom:updated>
            <content:encoded><![CDATA[<h3>A study of Classification Problems using Logistic Regression and an insight to the admissions problem</h3><h3><strong>Abstract:</strong></h3><p>In our world, many of the commonly encountered problems are classification problems. We are often confused between definite values or rigid choices of things. In this article, we will discuss about an algorithm used to solve simple classification problems effectively using Machine Learning. Also, we will analyze a hypothetical Binary Class problem involving Grad-School outcomes based on the Entrance Exam Marks and the Undergrad Marks.</p><h3>Introduction:</h3><p>Supervised Learning is a machine learning technique in which we associate our inputs with our targets in the given dataset. We already have a definite intuition regarding our final output. We broadly have two types of Supervised Learning problems, “Regression” and “Classification”. We will be discussing about Classification problems in this article. A Classification problem is a problem in which we separate our input data into discrete categories. By discrete, we mean separate classes. <br>Example: Think of a hypothetical dataset based of Grad School admissions having three columns. The first column being the Entrance Exams Score(<strong>Input X1</strong>), the second column being the UG Exams(<strong>Input X2</strong>), and the third column being the result, admit/reject(<strong>Target Y</strong>). A segment of the dataset used in this article is shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/295/1*eKNr7D_ZfxgmMOadT9qoaA.png" /><figcaption>Entrance Exam Score, UG Score, Outcome(Binary)</figcaption></figure><p>We define our target class Y as a binary class. Thus we define the set as, <strong>Y={0,1} </strong>where the set elements are:</p><p><strong>0 implies Reject(Negative Class),<br>1 implies Accept(Positive Class)</strong></p><p>Thus, this is a Classification problem. We can divide the dataset into two categories based on the outcome of the admission process. The algorithm which generally resolves a dataset into discrete categories is defined as a <em>classifier</em>. In this example, the dependent variable is <em>dichotomous</em>. In such cases, we prefer Logistic Regression. We can plot a scatter-plot of our Dataset in MATLAB/Octave and observe the following graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*OrCLHXg0PKRfYav4JVLKKA.png" /><figcaption>Scatter plot of our dataset</figcaption></figure><h3>Activation Function and Mapping Function involved:</h3><p>First of all, consider the function that maps our output and input and is hence used for computing predictions while calculating the Squared Error Function. Let us call that function a Mapping Function or a Hypothesis Function, <strong>h(X)</strong>. The term “Hypothesis” was coined for various historical reasons. Also, the term “Logistic” is a synonym for Sigmoid. Thus, we use a Sigmoid activation function with an “S Shaped curve” in this algorithm. A curve generated via our programmed function is depicted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ichW46MbHPkvmuXMocHf9A.png" /><figcaption>Sigmoid Activation Function</figcaption></figure><p>Mathematically, we define our function as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*rIcxY0VM156U9UpFdRVC8w.png" /></figure><p>The Sigmoid function also has several other mathematical properties. For example, we can represent the derivative of the function by the function itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*KN77REKVWMnsDUGN7fgpjw.png" /></figure><p>This, the Sigmoid/Logistic function simplifies the mathematics involved during optimization and is an ideal choice for a small scale classification problem. Using the Sigmoid function, we can effectively limit our range so that it represents probability effectively. We can represent our Hypothesis/Mapping function mathematically as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*svLDc1TyGqAI177fV7mt0Q.png" /></figure><p>This is essentially a very basic linear cost function, just that it’s coupled with a sigmoid function.</p><p>Our Hypothesis function gives us the numerical value for the probability any given event happening. We can define the range of the mapping function as: <strong>0≤h(X)≤1</strong>.</p><h3><strong>Cost Function Intuition:</strong></h3><p>A cost function is a mathematical function to estimate how well the hypothesis suits the input set and target set. Graphically, we know that a better cost function will fit our given data better. It is generally denoted as a function of our parameters. In this case, we refer to it as <strong>J(θ)</strong>. <br>In this algorithm, we use a<strong> logarithmic cost function </strong>which is derived from the principle of<em> Maximum Likelihood Estimation(M.S.E)</em> to ensure that the function we get as an output is a <strong>Convex function.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*zDG1C8af7a4-n-CVfj40tA.png" /></figure><p>A brief intuition is to think that the logarithm involved in the cost function roughly counter-acts the <strong>exp</strong> involved.</p><p>As Christopher M. Bishop stated in his work,</p><blockquote>“When we assume that our positive and negative training samples come from two different Gaussian clusters (different location but same covariance) then we can develop a perfect classifier.”</blockquote><p>Optimization is easier in a Convex Function as it is easier to reach a minima in a curve and get an optimal value for the parameter, θ. <br>However, we observe that we get the same Grad function as a Linear Regression method after we differentiate J(θ).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*77TY8V8hyEfsBVhltblcHQ.png" /></figure><p>However, the Grad value is not the same thing as we possess a different mapping function for Linear and Logistic Regression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*350fgnH2uOPN0SprzOwXGw.png" /><figcaption>Prototype code for a Cost Function</figcaption></figure><p>Therefore using the parameter vector θ and the Cost Function J(θ), we can apply any given optimization algorithm to implement a simple decision boundary.</p><h3><strong>Optimizing the parameters:</strong></h3><p>Now if we want to fit the parameters(In this case, θ), we would want to apply minimization(Optimization) algorithms.<br>We could take simple mini-steps<em>(</em><strong><em>limit(θ)-&gt;0</em></strong><em>)</em> to our given minima which is usually called <strong>Gradient Descent</strong>, or go for advanced optimization algorithms such as<strong><em> BFGS </em></strong>or <strong><em>Limited-Memory BFGS</em></strong><em>.<br></em> The advanced optimization methods are a part of the <strong><em>Quasi Newton </em></strong>methods where we compute an optimized value of the <strong>Hessian Matrix</strong>. L-BFGS is very similar to BFGS, other than the fact that it has better memory optimization and is a more scalable algorithm. Also when juxtaposed with Gradient Descent, the Advanced Algorithms are less prone to trial and error conditions as there is no need to pick an arbitrary value of <em>alpha(</em>Learning Rate) to get more optimal results. The mathematical notation for the Hessian Matrix is shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*qnZ3oYNHLFTobQhBwFGPmg.png" /></figure><p>Generally, we apply advanced optimization via libraries. One particular library in C++ used for the applying advanced optimization algorithms is the<a href="http://eigen.tuxfamily.org/index.php?title=Main_Page"> Eigen C++ Library</a>.</p><p>However, different libraries might have different time complexities. Not choosing a library wisely might end up degrading our algorithm’s overall performance and having an abysmal effect on our overall program.</p><p>Thus, we can plot a decision boundary after effectively minimizing our parameters using an optimization method. Thus, we use MATLAB’s generic <em>“fminunc”</em> to apply advanced optimization. After applying our function with θ and the Grad value as the parameters, we can plot our Decision Boundary as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*MN6bdduQeuUCv9gvqaKxqQ.png" /><figcaption>Decision Boundary separating the data. In this case, a linear boundary fits better.</figcaption></figure><h3>The concept of Overfitting a curve:</h3><p>Overfitting is basically a condition in which our learned hypothesis somehow manages to fit all the points in the dataset and generates little or no error but has a major flaw. Our algorithm <strong>fails to predict </strong>the discrete class of <strong>new testing examples.<br>The solution to this issue:<br></strong>We can penalize certain parameters(θ)to fix this. We add a regulation parameter( λ) to our optimization algorithm which can optimize a curve. This technique is called <em>Regularization</em>. Thus, we modify our cost function and Optimization algorithms to penalize the parameters and obtain an optimal fit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*srxDyAIgZBi0qks-JQxSvw.png" /></figure><h3>Goodness of a Fit:</h3><p>In <strong>Goodness of Fit(G.O.F)</strong>, we basically check whether a given model is correctly specified and how well it fits the statistical observations. There are usually two commonly used methods to estimate the Goodness of Fit. One test is the famous Pearson Chi-Square Test, and the other test is the <em>Hosmer and Lemeshow Test</em>. Straightforward <strong>Pearson Chi-Square</strong> test is a simpler technique and hence we’ll discuss the intuition of the test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*IHuQCSs1fpVVczJKyVBtBA.png" /></figure><p>The Chi-Square(χ2) has a good deal of mathematical accuracy for computing statistical parameters from a given dataset. Also, this test is asymptotically true. Its main role is to calculate frequencies for every observation in a dataset.<br>Thus, we can calculate the statistical p-value by comparing the value to the value of the distribution.<br>We could also use a variety of other tests such as the <strong>Stukel Test or Information Matrix </strong>Test to measure the fit of Logistic Regression effectively.</p><p>Since this is my first article on Machine Learning, I discussed about an elementary concept in the field. In the next article, I will write about Alpha Beta Pruning using a U64 Bitboard based Chess-Engine.</p><h3>References:</h3><ol><li>W.S Lee and B Liu,“Learning with positive and unlabeled examples using weighted logistic regression”-ICML, 2003 — vvvvw.aaai.org</li><li>DC Liu and J Nocedal, “On the limited memory BFGS method for large scale optimization”-Mathematical programming, 1989 — Springer</li><li>MA Babyak, “What you see may not be what you get: a brief, nontechnical introduction to overfitting in regression-type models”,-Psychosomatic medicine, 2004 — journals.lww.com</li><li>Andrew NG, “Machine Learning, Stanford University course”.</li><li>Paul D. Allison,”Measures of Fit for Logistic Regression”,-SAS Global Forum, Washington, DC, 2014 — statisticalhorizons.com</li><li>Christopher M Bishop, “Pattern Recognition and Machine Learning”.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec69ddf93f36" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/a-study-of-classification-problems-using-logistic-regression-and-an-insight-to-the-admissions-ec69ddf93f36">A study of Classification Problems using Logistic Regression and an insight to the admissions…</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>