<?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 Kunwar Aditya on Medium]]></title>
        <description><![CDATA[Stories by Kunwar Aditya on Medium]]></description>
        <link>https://medium.com/@bw99214?source=rss-b7d0624786d6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PMrbnu_kwGPPSYH5j_pnow.jpeg</url>
            <title>Stories by Kunwar Aditya on Medium</title>
            <link>https://medium.com/@bw99214?source=rss-b7d0624786d6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 17:05:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bw99214/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[What are Context Free Grammars?]]></title>
            <link>https://medium.com/@bw99214/what-are-context-free-grammars-130b3d729361?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/130b3d729361</guid>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[theory-of-computation]]></category>
            <category><![CDATA[compiler-design]]></category>
            <category><![CDATA[linguistics]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Tue, 14 Jan 2020 17:30:11 GMT</pubDate>
            <atom:updated>2020-01-14T17:32:46.591Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dltTbTBSshbY9gO5Qw_qYg.png" /></figure><p>Have you ever noticed that, when you are writing code in some text editors like VS code, it recognizes things like unmatched braces? And it also sometimes warns you, with an irritating red highlight, about the incorrect syntax that you have written?</p><p>If not, then think about it. It is after all a piece of code. How can you write code for such a task? What could be the underlying logic behind it?</p><p>These are the kinds of questions that you will face if you have to write a compiler for a programming language. Writing a compiler is not an easy task. It is bulky job that demands a significant amount of time and effort.</p><p>In this article, we are not going to talk about how to build compilers. But we will talk about a concept that is a core component of the compiler: Context Free Grammar.</p><h3>Introduction</h3><p>All the questions we asked earlier represent a problem that is significant to compiler design called Syntax Analysis. As the name suggests, the challenge is to analyze the syntax and see if it is correct or not. This is where we use Context Free Grammars. A Context Free Grammar is a set of rules that define a language.</p><p>Here, I would like to draw a distinction between Context Free Grammars and grammars for natural languages like English.</p><p>Context Free Grammars or CFGs define a formal language. Formal languages work strictly under the defined rules and their sentences are not influenced by the context. And that’s where it gets the name <em>context free</em>.</p><p>Languages such as English fall under the category of Informal Languages since they are affected by context. They have many other features which a CFG cannot describe.</p><p>Even though CFGs cannot describe the context in the natural languages, they can still define the syntax and structure of sentences in these languages. In fact, that is the reason why the CFGs were introduced in the first place.</p><p>In this article we will attempt to generate English sentences using CFGs. We will learn how to describe the sentence structure and write rules for it. To do this, we will use a JavaScript library called Tracery which will generate sentences on the basis of rules we defined for our grammar.</p><p>Before we dive into the code and start writing the rules for the grammar, let’s just discuss some basic terms that we will use in our CFG.</p><p><strong>Terminals</strong>: These are the characters that make up the actual content of the final sentence. These can include words or letters depending on which of these is used as the basic building block of a sentence.</p><p>In our case we will use words as the basic building blocks of our sentences. So our terminals will include words such as “to”, “from”, “the”, “car”, “spaceship”, “kittens” and so on.</p><p><strong>Non Terminals</strong>: These are also called variables. These act as a sub language within the language defined by the grammar. Non terminals are placeholders for the terminals. We can use non terminals to generate different patterns of terminal symbols.</p><p>In our case we will use these Non terminals to generate noun phrases, verb phrases, different nouns, adjectives, verbs and so on.</p><p><strong>Start Symbol</strong>: a start symbol is a special non terminal that represents the initial string that will be generated by the grammar.</p><p>Now that we know the terminology let’s start learning about the grammatical rules.</p><p>While writing grammar rules, we will start by defining the set of terminals and a start state. As we learned before, that start symbol is a non-terminal. This means it will belong to the set of non-terminals.</p><pre>T: (&quot;Monkey&quot;, &quot;banana&quot;, &quot;ate&quot;, &quot;the&quot;)<br>S: Start state.</pre><p>And the rules are:</p><pre>S --&gt; nounPhrase verbPhrase<br>nounPhrase --&gt; adj nounPhrase | adj noun<br>verbPhrase --&gt; verb nounPhrase<br>adjective  --&gt; the<br>noun --&gt; Monkey | banana<br>verb --&gt; ate</pre><p>The above grammatical rules may seem somewhat cryptic at first. But if we look carefully, we can see a pattern that is being generated out of these rules.</p><p>A better way to think about the above rules is to visualise them in the form of a tree structure. In that tree we can put <em>S</em> in the root and <em>nounPhrase</em> and <em>verbPhrase</em> can be added as children of the root. We can proceed in the same way with <em>nounPhrase</em> and <em>verbPhrase </em>too. The tree will have terminals as its leaf nodes because that is where we end these derivations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*npRehlAKGWgNjhgo.png" /></figure><p>In the above image we can see that <em>S</em> (a nonterminal) derives two non terminals <em>NP</em>(<em>nounPhrase</em>) and <em>VP</em>(<em>verbPhrase</em>). In the case of <em>NP</em>, it has derived two non terminals, <em>Adj</em> and <em>Noun</em>.</p><p>If you look at the grammar, <em>NP</em> could also have chosen <em>Adj</em> and <em>nounPhrase</em>. While generating text, these choices are made randomly.</p><p>And finally the leaf nodes have terminals which are written in the bold text. So if you move from left to right, you can see that a sentence is formed.</p><p>The term often used for this tree is a Parse Tree. We can create another parse tree for a different sentence generated by this grammar in a similar way.</p><p>Now let’s proceed further to the code. As I mentioned earlier, we will use a JavaScript library called Tracery for text generation using CFGs. We will also write some code in HTML and CSS for the front-end part.</p><h3>The Code</h3><p>Let’s start by first getting the tracery library. You can clone the library from GitHub <a href="https://github.com/galaxykate/tracery">here</a>. I have also left the link to the GitHub repository by galaxykate at the end of the article.</p><p>Before we use the library we will have to import it. We can do this simply in an HTML file like this.</p><pre>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;script src=&quot;tracery-master/js/vendor/jquery-1.11.2.min.js&quot;&gt;&lt;/script&gt;<br>		&lt;script src=&quot;tracery-master/tracery.js&quot;&gt;&lt;/script&gt;<br>		&lt;script src=&quot;tracery-master/js/grammars.js&quot;&gt;&lt;/script&gt;<br>        &lt;script src=&#39;app.js&#39;&gt;&lt;/script&gt;<br>    &lt;/head&gt;<br>    <br>&lt;/html&gt;</pre><p>I have added the cloned tracery file as a script in my HTML code. We will also have to add JQuery to our code because tracery depends on JQuery. Finally, I have added <em>app.js</em> which is the file where I will add rules for the grammar.</p><p>Once that is done, create a JavaScript file where we will define our grammar rules.</p><pre>var rules = {<br>    	&quot;start&quot;: [&quot;#NP# #VP#.&quot;],<br>    	&quot;NP&quot;: [&quot;#Det# #N#&quot;, &quot;#Det# #N# that #VP#&quot;, &quot;#Det# #Adj# #N#&quot;],<br>    	&quot;VP&quot;: [&quot;#Vtrans# #NP#&quot;, &quot;#Vintr#&quot;],<br>    	&quot;Det&quot;: [&quot;The&quot;, &quot;This&quot;, &quot;That&quot;],<br>    	&quot;N&quot;: [&quot;John Keating&quot;, &quot;Bob Harris&quot;, &quot;Bruce Wayne&quot;, &quot;John Constantine&quot;, &quot;Tony Stark&quot;, &quot;John Wick&quot;, &quot;Sherlock Holmes&quot;, &quot;King Leonidas&quot;],<br>    	&quot;Adj&quot;: [&quot;cool&quot;, &quot;lazy&quot;, &quot;amazed&quot;, &quot;sweet&quot;],<br>    	&quot;Vtrans&quot;: [&quot;computes&quot;, &quot;examines&quot;, &quot;helps&quot;, &quot;prefers&quot;, &quot;sends&quot;, &quot;plays with&quot;, &quot;messes up with&quot;],<br>    	&quot;Vintr&quot;: [&quot;coughs&quot;, &quot;daydreams&quot;, &quot;whines&quot;, &quot;slobbers&quot;, &quot;appears&quot;, &quot;disappears&quot;, &quot;exists&quot;, &quot;cries&quot;, &quot;laughs&quot;]<br>    }</pre><p>Here you will notice that the syntax for defining rules is not much different from how we defined our grammar earlier. There are very minor differences such as the way non-terminals are defined between the hash symbols. And also the way in which different derivations are written. Instead of using the “|” symbol for separating them, here we will put all the different derivations as different elements of an array. Other than that, we will use the semicolons instead of arrows to represent the transition.</p><p>This new grammar is a little more complicated than the one we defined earlier. This one includes many other things such as Determiners, Transitive Verbs and Intransitive Verbs. We do this to make the generated text look more natural.</p><p>Let’s now call the tracery function “createGrammar” to create the grammar we just defined.</p><pre>let grammar = tracery.createGrammar(rules);</pre><p>This function will take the rules object and generate a grammar on the basis of these rules. After creating the grammar, we now want to generate some end result from it. To do that we will use a function called “flatten”.</p><pre>let expansion = grammar.flatten(&#39;#start#&#39;);</pre><p>It will generate a random sentence based on the rules that we defined earlier. But let’s not stop there. Let’s also build a user interface for it. There’s not much we will have to do for that part — we just need a button and some basic styles for the interface.</p><p>In the same HTML file where we added the libraries we will add some elements.</p><pre>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;title&gt;Weird Sentences&lt;/title&gt;<br>        &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;/&gt;<br>        &lt;link href=&quot;https://fonts.googleapis.com/css?family=UnifrakturMaguntia&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;<br>        &lt;link href=&quot;https://fonts.googleapis.com/css?family=Harmattan&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;<br>        <br>        &lt;script src=&quot;tracery-master/js/vendor/jquery-1.11.2.min.js&quot;&gt;&lt;/script&gt;<br>		&lt;script src=&quot;tracery-master/tracery.js&quot;&gt;&lt;/script&gt;<br>		&lt;script src=&quot;tracery-master/js/grammars.js&quot;&gt;&lt;/script&gt;<br>        &lt;script src=&#39;app.js&#39;&gt;&lt;/script&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;h1 id=&quot;h1&quot;&gt;Weird Sentences&lt;/h1&gt;<br>        &lt;button id=&quot;generate&quot; onclick=&quot;generate()&quot;&gt;Give me a Sentence!&lt;/button&gt;<br>        &lt;div id=&quot;sentences&quot;&gt;</pre><pre>&lt;/div&gt;<br>    &lt;/body&gt;<br>&lt;/html&gt;</pre><p>And finally we will add some styles to it.</p><pre>body {<br>    text-align: center;<br>    margin: 0;<br>    font-family: &#39;Harmattan&#39;, sans-serif;<br>}</pre><pre>#h1 {<br>    font-family: &#39;UnifrakturMaguntia&#39;, cursive;<br>    font-size: 4em;<br>    background-color: rgb(37, 146, 235);<br>    color: white;<br>    padding: .5em;<br>    box-shadow: 1px 1px 1px 1px rgb(206, 204, 204);<br>}</pre><pre>#generate {<br>    font-family: &#39;Harmattan&#39;, sans-serif;<br>    font-size: 2em;<br>    font-weight: bold;<br>    padding: .5em;<br>    margin: .5em;<br>    box-shadow: 1px 1px 1px 1px rgb(206, 204, 204);<br>    background-color: rgb(255, 0, 64);<br>    color: white;<br>    border: none;<br>    border-radius: 2px;<br>    outline: none;<br>}</pre><pre>#sentences p {<br>    box-shadow: 1px 1px 1px 1px rgb(206, 204, 204);<br>    margin: 2em;<br>    margin-left: 15em;<br>    margin-right: 15em;<br>    padding: 2em;<br>    border-radius: 2px;<br>    font-size: 1.5em;<br>}</pre><p>We will also have to add some more JavaScript to manipulate the interface.</p><pre>let sentences = []<br>function generate() {<br>    var data = {<br>    	&quot;start&quot;: [&quot;#NP# #VP#.&quot;],<br>    	&quot;NP&quot;: [&quot;#Det# #N#&quot;, &quot;#Det# #N# that #VP#&quot;, &quot;#Det# #Adj# #N#&quot;],<br>    	&quot;VP&quot;: [&quot;#Vtrans# #NP#&quot;, &quot;#Vintr#&quot;],<br>    	&quot;Det&quot;: [&quot;The&quot;, &quot;This&quot;, &quot;That&quot;],<br>    	&quot;N&quot;: [&quot;John Keating&quot;, &quot;Bob Harris&quot;, &quot;Bruce Wayne&quot;, &quot;John Constantine&quot;, &quot;Tony Stark&quot;, &quot;John Wick&quot;, &quot;Sherlock Holmes&quot;, &quot;King Leonidas&quot;],<br>    	&quot;Adj&quot;: [&quot;cool&quot;, &quot;lazy&quot;, &quot;amazed&quot;, &quot;sweet&quot;],<br>    	&quot;Vtrans&quot;: [&quot;computes&quot;, &quot;examines&quot;, &quot;helps&quot;, &quot;prefers&quot;, &quot;sends&quot;, &quot;plays with&quot;, &quot;messes up with&quot;],<br>    	&quot;Vintr&quot;: [&quot;coughs&quot;, &quot;daydreams&quot;, &quot;whines&quot;, &quot;slobbers&quot;, &quot;appears&quot;, &quot;disappears&quot;, &quot;exists&quot;, &quot;cries&quot;, &quot;laughs&quot;]<br>    }<br>    <br>    let grammar = tracery.createGrammar(data);<br>    let expansion = grammar.flatten(&#39;#start#&#39;);</pre><pre>    sentences.push(expansion);</pre><pre>    printSentences(sentences);<br>}</pre><pre>function printSentences(sentences) {<br>    let textBox = document.getElementById(&quot;sentences&quot;);<br>    textBox.innerHTML = &quot;&quot;;<br>    for(let i=sentences.length-1; i&gt;=0; i--) {<br>        textBox.innerHTML += &quot;&lt;p&gt;&quot;+sentences[i]+&quot;&lt;/p&gt;&quot;<br>    }<br>}</pre><p>Once you have finished writing the code, run your HTML file. It should look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0uSB9r1JeX0Krtuo.png" /></figure><p>Every time you click the red button it will generate a sentence. Some of these sentences might not make any sense. This is because, as I said earlier, CFGs cannot describe the context and some other features that natural languages possess. It is used only to define the syntax and structure of the sentences.</p><p>You can check out the live version of this <a href="https://aditya2000.github.io/weird-sentences/">here</a>.</p><h3>Conclusion</h3><p>If you have made it this far, I highly appreciate your resilience. It might be a new concept for some of you, and others might have learnt about it in their college courses. But still, Context Free Grammars have interesting applications that range widely from Computer Science to Linguistics.</p><p>I have tried my best to present the main ideas of CFGs here, but there is a lot more that you can learn about them. Here I have left links to some great resources:</p><ul><li><a href="https://youtu.be/C3EwsSNJeOE">Context Free Grammars</a> by Daniel Shiffman.</li><li><a href="https://youtu.be/R_OVyFrBhiU">Context Free Grammars Examples</a> by Fullstack Academy</li><li><a href="https://github.com/galaxykate/tracery">Tracery</a> by Galaxykate</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=130b3d729361" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a Neural Network from scratch]]></title>
            <link>https://medium.com/@bw99214/how-to-build-a-neural-network-from-scratch-d211620c861d?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d211620c861d</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Thu, 19 Dec 2019 08:46:58 GMT</pubDate>
            <atom:updated>2019-12-19T08:46:58.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gBc7Z1awifivgps4wB-Lvw.jpeg" /><figcaption><a href="https://unsplash.com/photos/d0AcxMk33is">Source</a></figcaption></figure><p>Neural Networks are like the workhorses of Deep learning. With enough data and computational power, they can be used to solve most of the problems in deep learning. It is very easy to use a Python or R library to create a neural network and train it on any dataset and get a great accuracy.</p><p>We can treat neural networks as just some black box and use them without any difficulty. But even though it seems very easy to go that way, it’s much more exciting to learn what lies behind these algorithms and how they work.</p><p>In this article we will get into some of the details of building a neural network. I am going to use Python to write code for the network. I will also use Python’s numpy library to perform numerical computations. I will try to avoid some complicated mathematical details, but I will refer to some brilliant resources in the end if you want to know more about that.</p><p>So let’s get started.</p><h3>Idea</h3><p>Before we start writing code for our Neural Network, let’s just wait and understand what exactly is a Neural Network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*g-MWJr9Z3mQw0t15fEmgWQ.png" /><figcaption><a href="https://miro.medium.com/max/980/1*oc1gaCFvgWXq_gHQFM63UQ.png">Source</a></figcaption></figure><p>In the image above you can see a very casual diagram of a neural network. It has some colored circles connected to each other with arrows pointing to a particular direction. These colored circles are sometimes referred to as <em>neurons</em>.</p><p>These <em>neurons</em> are nothing but mathematical functions which, when given some <em>input,</em>generate an <em>output</em>. The <em>output</em> of <em>neurons</em> depends on the <em>input</em> and the <em>parameters</em> of the <em>neurons</em>. We can update these <em>parameters</em> to get a desired value out of the network.</p><p>Each of these <em>neurons</em> are defined using <em>sigmoid function</em>. A <em>sigmoid function</em> gives an output between zero to one for every input it gets. These sigmoid units are connected to each other to form a neural network.</p><p>By connection here we mean that the output of one layer of sigmoid units is given as input to each sigmoid unit of the next layer. In this way our neural network produces an output for any given input. The process continues until we have reached the final layer. The final layer generates its output.</p><p>This process of a neural network generating an <em>output</em> for a given <em>input</em> is <em>Forward Propagation</em>. Output of final layer is also called the <em>prediction</em> of the neural network. Later in this article we will discuss how we <em>evaluate the predictions</em>. These evaluations can be used to tell whether our neural network needs improvement or not.</p><p>Right after the final layer generates its output, we calculate the <em>cost function</em>. The cost function computes how far our neural network is from making its desired predictions. The value of the cost function shows the difference between the <em>predicted value</em> and the <em>truth value</em>.</p><p>Our objective here is to minimize the value of the <em>cost function</em>. The process of minimization of the cost function requires an algorithm which can update the values of the <em>parameters</em> in the network in such a way that the cost function achieves its <em>minimum value</em>.</p><p>Algorithms such as <em>gradient descent </em>and <em>stochastic gradient descent </em>are used to update the <em>parameters</em> of the neural network. These algorithms update the values of weights and biases of each layer in the network depending on how it will affect the minimization of cost function. The effect on the minimization of the cost function with respect to each of the weights and biases of each of the input neurons in the network is computed by <em>backpropagation</em>.</p><h3>Code</h3><p>So, we now know the main ideas behind the neural networks. Let us start implementing these ideas into code. We will start by importing all the required libraries.</p><pre>import numpy as np<br>import matplotlib.pyplot as plt</pre><p>As I mentioned we are not going to use any of the deep learning libraries. So, we will mostly use numpy for performing mathematical computations efficiently.</p><p>The first step in building our neural network will be to initialize the parameters. We need to initialize two parameters for each of the neurons in each layer: 1) <em>Weight</em> and 2) <em>Bias</em>.</p><p>These weights and biases are declared in <em>vectorized</em> form. That means that instead of initializing weights and biases for each individual neuron in every single layer, we will create a vector (or a matrix) for weights and another one for biases, for each layer.</p><p>These <em>weights</em> and <em>bias</em> vectors will be combined with the input to the layer. Then we will apply the sigmoid function over that combination and send that as the input to the next layer.</p><p><strong>layer_dims</strong><em> </em>holds the dimensions of each layer. We will pass these dimensions of layers to the <strong>init_parms</strong><em> </em>function which will use them to initialize parameters. These parameters will be stored in a dictionary called <strong>params</strong>. So in the params dictionary <strong>params[‘W1’]</strong><em> </em>will represent the weight matrix for layer 1.</p><pre>def init_params(layer_dims):<br>    np.random.seed(3)<br>    params = {}<br>    L = len(layer_dims)<br>    <br>    for l in range(1, L):<br>        params[&#39;W&#39;+str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1])*0.01<br>        params[&#39;b&#39;+str(l)] = np.zeros((layer_dims[l], 1))<br>        <br>    return params</pre><p>Great! We have initialized the weights and biases and now we will define the <em>sigmoid function</em>. It will compute the value of the sigmoid function for any given value of <strong>Z</strong> and will also store this value as a cache. We will store cache values because we need them for implementing backpropagation. The<strong> Z</strong> here is the <em>linear hypothesis</em>.</p><p>Note that the sigmoid function falls under the class of <em>activation functions</em> in the neural network terminology. The job of an <em>activation function</em> is to shape the output of a neuron.</p><p>For example, the sigmoid function takes input with discrete values and gives a value which lies between zero and one. Its purpose is to convert the linear outputs to non-linear outputs. There are different types of <em>activation functions</em> that can be used for better performance but we will stick to sigmoid for the sake of simplicity.</p><pre># Z (linear hypothesis) - Z = W*X + b , <br># W - weight matrix, b- bias vector, X- Input </pre><pre>def sigmoid(Z):<br>	A = 1/(1+np.exp(np.dot(-1, Z)))<br>    cache = (Z)<br>    <br>    return A, cache</pre><p>Now, let’s start writing code for forward propagation. We have discussed earlier that <em>forward</em> <em>propagation</em> will take the values from the previous layer and give it as input to the next layer. The function below will take the <em>training data</em> and <em>parameters</em> as inputs and will generate output for one layer and then it will feed that output to the next layer and so on.</p><pre>def forward_prop(X, params):<br>    <br>    A = X # input to first layer i.e. training data<br>    caches = []<br>    L = len(params)//2<br>    for l in range(1, L+1):<br>        A_prev = A<br>        <br>        # Linear Hypothesis<br>        Z = np.dot(params[&#39;W&#39;+str(l)], A_prev) + params[&#39;b&#39;+str(l)] <br>        <br>        # Storing the linear cache<br>        linear_cache = (A_prev, params[&#39;W&#39;+str(l)], params[&#39;b&#39;+str(l)]) <br>        <br>        # Applying sigmoid on linear hypothesis<br>        A, activation_cache = sigmoid(Z) <br>        <br>         # storing the both linear and activation cache<br>        cache = (linear_cache, activation_cache)<br>        caches.append(cache)<br>    <br>    return A, caches</pre><p><strong>A_prev</strong><em> i</em>s input to the first layer. We will loop through all the layers of the network and will compute the linear hypothesis. After that it will take the value of <strong>Z </strong>(linear hypothesis) and will give it to the sigmoid activation function. Cache values are stored along the way and are accumulated in <strong>caches</strong>. Finally, the function will return the value generated and the stored cache.</p><p>Let’s now define our cost function.</p><pre>def cost_function(A, Y):<br>    m = Y.shape[1]<br>    <br>    cost = (-1/m)*(np.dot(np.log(A), Y.T) + np.dot(log(1-A), 1-Y.T)) <br>    <br>    return cost</pre><p>As the value of the cost function decreases, the performance of our model becomes better. The value of the cost function can be minimized by updating the values of the parameters of each of the layers in the neural network. Algorithms such as <em>Gradient Descent</em> are used to update these values in such a way that the cost function is minimized.</p><p>Gradient Descent updates the values with the help of some updating terms. These updating terms called <em>gradients</em> are calculated using the backpropagation. Gradient values are calculated for each neuron in the network and it represents the change in the final output with respect to the change in the parameters of that particular neuron.</p><pre>def one_layer_backward(dA, cache):<br>    linear_cache, activation_cache = cache<br>    <br>    Z = activation_cache<br>    dZ = dA*sigmoid(Z)*(1-sigmoid(Z)) # The derivative of the sigmoid function<br>    <br>    A_prev, W, b = linear_cache<br>    m = A_prev.shape[1]<br>    <br>    dW = (1/m)*np.dot(dZ, A_prev.T)<br>    db = (1/m)*np.sum(dZ, axis=1, keepdims=True)<br>    dA_prev = np.dot(W.T, dZ)<br>    <br>    return dA_prev, dW, db</pre><p>The code above runs the backpropagation step for one single layer. It calculates the gradient values for sigmoid units of one layer using the cache values we stored previously. In the activation cache we have stored the value of <strong>Z</strong> for that layer. Using this value we will calculate the <strong>dZ</strong>, which is the derivative of the cost function with respect to the linear output of the given neuron.</p><p>Once we have calculated all of that, we can calculate <strong>dW</strong>, <strong>db</strong> and <strong>dA_prev,</strong> which are the derivatives of cost function with respect the weights, biases and previous activation respectively. I have directly used the formulae in the code. If you are not familiar with calculus then it might seem too complicated at first. But for now think about it as any other math formula.</p><p>After that we will use this code to implement backpropagation for the entire neural network. The function <strong>backprop</strong> implements the code for that. Here, we have created a dictionary for mapping gradients to each layer. We will loop through the model in a backwards direction and compute the gradient.</p><pre>def backprop(AL, Y, caches):<br>    grads = {}<br>    L = len(caches)<br>    m = AL.shape[1]<br>    Y = Y.reshape(AL.shape)<br>    <br>    dAL = -(np.divide(Y, AL) - np.divide(1-Y, 1-AL))<br>    <br>    current_cache = caches[L-1]<br>    grads[&#39;dA&#39;+str(L-1)], grads[&#39;dW&#39;+str(L-1)], grads[&#39;db&#39;+str(L-1)] = one_layer_backward(dAL, current_cache)<br>    <br>    for l in reversed(range(L-1)):<br>        <br>        current_cache = caches[l]<br>        dA_prev_temp, dW_temp, db_temp = one_layer_backward(grads[&quot;dA&quot; + str(l+1)], current_cache)<br>        grads[&quot;dA&quot; + str(l)] = dA_prev_temp<br>        grads[&quot;dW&quot; + str(l + 1)] = dW_temp<br>        grads[&quot;db&quot; + str(l + 1)] = db_temp<br>        <br>    return grads</pre><p>Once, we have looped through all the layers and computed the gradients, we will store those values in the <strong>grads</strong> dictionary and return it.</p><p>Finally, using these gradient values we will update the parameters for each layer. The function <strong>update_parameters</strong> goes through all the layers and updates the parameters and returns them.</p><pre>def update_parameters(parameters, grads, learning_rate):<br>    L = len(parameters) // 2<br>    <br>    for l in range(L):<br>        parameters[&#39;W&#39;+str(l+1)] = parameters[&#39;W&#39;+str(l+1)] -learning_rate*grads[&#39;W&#39;+str(l+1)]<br>        parameters[&#39;b&#39;+str(l+1)] = parameters[&#39;b&#39;+str(l+1)] -  learning_rate*grads[&#39;b&#39;+str(l+1)]<br>        <br>    return parameters</pre><p>Finally, it’s time to put it all together. We will create a function called <strong>train</strong> for training our neural network.</p><pre>def train(X, Y, layer_dims, epochs, lr):<br>    params = init_params(layer_dims)<br>    cost_history = []<br>    <br>    for i in range(epochs):<br>        Y_hat, caches = forward_prop(X, params)<br>        cost = cost_function(Y_hat, Y)<br>        cost_history.append(cost)<br>        grads = backprop(Y_hat, Y, caches)<br>        <br>        params = update_parameters(params, grads, lr)<br>        <br>        <br>    return params, cost_history</pre><p>This function will go through all the functions step by step for a given number of <em>epochs</em>. After finishing that, it will return the final updated parameters and the cost history. Cost history can be used to evaluate the performance of your network architecture.</p><h3>Conclusion</h3><p>If you are still reading this, Thanks! This article was a little complicated, so what I suggest you to do is to try playing around with the code. You might get some more insights out of it and maybe you might find some errors in the code too. If that is the case or if you have some questions or both, feel free to hit me up on <a href="https://twitter.com/aditya_dehal">twitter</a>. I will do my best to help you.</p><h3>Resources</h3><ul><li><a href="https://youtu.be/aircAruvnKk">Neural Networks Playlist</a> — by 3Blue1Brown</li><li><a href="http://neuralnetworksanddeeplearning.com/chap1.html">Neural Networks and Deep Learning</a> — by Michael A. Nielsen</li><li><a href="https://www.quora.com/Whats-the-difference-between-gradient-descent-and-stochastic-gradient-descent/answer/Sebastian-Raschka-1">Gradient Descent and Stochastic Gradient Descent</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d211620c861d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a simple Image Classifier]]></title>
            <link>https://medium.com/@bw99214/how-to-create-a-simple-image-classifier-c47a5e3ccdd8?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c47a5e3ccdd8</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Thu, 18 Jul 2019 18:26:18 GMT</pubDate>
            <atom:updated>2019-07-18T18:28:00.150Z</atom:updated>
            <content:encoded><![CDATA[<p>Image classification is an amazing application of deep learning. We can train a powerful algorithm to model a large image dataset. This model can then be used to classify a similar but unknown set of images.</p><p>There is no limit to the applications of image classification. You can use it in your next app or you can use it to solve some real world problem. That’s all up to you. But to someone who is fairly new to this realm, it might seem very challenging at first. How should I get my data? How should I build my model? What tools should I use?</p><p>In this article we will discuss all of that — from finding a dataset to training your model. I will try to make things as simple as possible by avoiding some technical details (<em>PS: Please note that this doesn’t mean those details are not important. I will mention some great resources which you can refer to learn more about those topics</em>). The purpose of this article is to explain the basic process of building an image classifier and that’s what we will focus more on here.</p><p>We will build an Image classifier for the <a href="https://research.zalando.com/welcome/mission/research-projects/fashion-mnist/">Fashion-MNIST Dataset</a>. The Fashion-MNIST dataset is a collection of <a href="https://research.zalando.com/">Zalando’s</a> article images. It contains 60,000 images for the training set and 10,000 images for the test set data (<em>we will discuss the test and training datasets along with the validation dataset later</em>). These images belong to the labels of 10 different classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*GNLB2jtcfb_xTqgQd9ntJA.png" /><figcaption><a href="https://research.zalando.com/welcome/mission/research-projects/fashion-mnist/">Source</a></figcaption></figure><h3>Importing Libraries</h3><p>Our goal is to train a deep learning model that can classify a given set of images into one of these 10 classes. Now that we have our dataset, we should move on to the tools we need. There are many libraries and tools out there that you can choose based on your own project requirements. For this one I will stick to the following:</p><pre>1. <a href="https://www.numpy.org/">Numpy</a> - Python library for numerical computation<br>2. <a href="https://pandas.pydata.org/">Pandas</a> - Python library data manipulation<br>3. <a href="https://matplotlib.org/">Matplotlib</a> - Python library data visualisation<br>4. <a href="https://keras.io/">Keras</a> - Python library based on tensorflow for creating deep learning models<br>5. <a href="https://jupyter.org/">Jupyter</a> - I will run all my code on Jupyter Notebooks. You can install it via the link. You can use [Google Colabs](<a href="https://colab.research.google.com/">https://colab.research.google.com/</a>) also if you need better computational power.</pre><p>Along with these four, we will also use <a href="https://scikit-learn.org/">scikit-learn</a>. The purpose of these libraries will become more clear once we dive into the code.</p><p>Okay! We have our tools and libraries ready. Now we should start setting up our code.</p><p>Start with importing all the above mentioned libraries. Along with importing libraries I have also imported some specific modules from these libraries. Let me go through them one by one.</p><pre>import numpy as np <br>import pandas as pd<br>import matplotlib.pyplot as plt <br>import keras</pre><pre>from sklearn.model_selection import train_test_split <br>from keras.utils import to_categorical</pre><pre>from keras.models import Sequential <br>from keras.layers import Conv2D, MaxPooling2D <br>from keras.layers import Dense, Dropout <br>from keras.layers import Flatten, BatchNormalization</pre><p>\”apples\”, [0, 1, 0] -&gt; \”oranges\”, [0, 0, 1] -&gt; \”bananas\”.”]]],[1,”p”,[[0,[],0,”The rest of the Keras modules we have imported are convolutional layers. We will discuss convolutional layers when we start building our model. We will also give a quick glance to what each of these layers do.”]]]]}’&gt;</p><p><a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html"><strong>train_test_split</strong></a><strong>: </strong>This module splits the training dataset into training and validation data. The reason behind this split is to check if our model is <a href="https://en.wikipedia.org/wiki/Overfitting">overfitting</a> or not. We use a training dataset to train our model and then we will compare the resulting accuracy to validation accuracy. If the difference between both quantities is significantly large, then our model is probably overfitting. We will reiterate through our model building process and making required changes along the way. Once we are satisfied with our training and validation accuracies, we will make final predictions on our test data.</p><p><strong>to_categorical: </strong>to_categorical is a keras utility. It is used to convert the categorical labels into <a href="https://machinelearningmastery.com/why-one-hot-encode-data-in-machine-learning/">one-hot encodings</a>. Let’s say we have three labels (“apples”, “oranges”, “bananas”), then one hot encodings for each of these would be [1, 0, 0] -&gt; “apples”, [0, 1, 0] -&gt; “oranges”, [0, 0, 1] -&gt; “bananas”.</p><p>The rest of the Keras modules we have imported are convolutional layers. We will discuss convolutional layers when we start building our model. We will also give a quick glance to what each of these layers do.</p><h3>Data Pre-processing</h3><p>For now we will shift our attention to getting our data and analysing it. You should always remember the importance of pre-processing and analysing the data. It not only gives you insights about the data but also helps to locate inconsistencies.</p><p>A very slight variation in data can sometimes lead to a devastating result for your model. This makes it important to preprocess your data before using it for training. So with that in mind let’s start data preprocessing.</p><pre>train_df = pd.read_csv(&#39;./fashion-mnist_train.csv&#39;)<br>test_df = pd.read_csv(&#39;./fashion-mnist_test.csv&#39;)</pre><p>First of all let’s import our dataset (<a href="https://www.kaggle.com/zalando-research/fashionmnist"><em>Here</em></a><em> is the link to download this dataset on your system</em>). Once you have imported the dataset, run the following command.</p><pre>train_df.head()</pre><p>This command will show you how your data looks like. The following screenshot shows the output of this command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*enSpluelfCAz-Blg8vTuAg.png" /></figure><p>We can see how our image data is stored in the form of pixel values. But we cannot feed data to our model in this format. So, we will have to convert it into numpy arrays.</p><pre>train_data = np.array(train_df.iloc[:, 1:])<br>test_data = np.array(test_df.iloc[:, 1:])</pre><p>Now, it’s time to get our labels.</p><pre>train_labels = to_categorical(train_df.iloc[:, 0])<br>test_labels = to_categorical(test_df.iloc[:, 0])</pre><p>Here, you can see that we have used <em>to_categorical</em> to convert our categorical data into one hot encodings.</p><p>We will now reshape the data and cast it into <em>float32</em> type so that we can use it conveniently.</p><pre>rows, cols = 28, 28</pre><pre>train_data = train_data.reshape(train_data.shape[0], rows, cols, 1)<br>test_data = test_data.reshape(test_data.shape[0], rows, cols, 1)</pre><pre>train_data = train_data.astype(&#39;float32&#39;)<br>test_data = test_data.astype(&#39;float32&#39;)</pre><p>We are almost done. Let’s just finish preprocessing our data by normalizing it. Normalizing image data will map all the pixel values in each image to the values between 0 to 1. This helps us reduce inconsistencies in data. Before normalizing, the image data can have large variations in pixel values which can lead to some unusual behaviour during the training process.</p><pre>train_data /= 255.0<br>test_data /= 255.0</pre><h3>Convolutional Neural Networks</h3><p>So, data preprocessing is done. Now we can start building our model. We will build a <a href="http://cs231n.github.io/convolutional-networks/">Convolutional Neural Network</a> for modeling the image data. CNNs are modified versions of regular <a href="https://en.wikipedia.org/wiki/Neural_network">neural networks</a>. These are modified specifically for image data. Feeding images to regular neural networks would require our network to have a large number of input neurons. For example just for a 28x28 image we would require 784 input neurons. This would create a huge mess of training parameters.</p><p>CNNs fix this problem by already assuming that the input is going to be an image. The main purpose of convolutional neural networks is to take advantage of the spatial structure of the image and to extract high level features from that and then train on those features. It does so by performing a <a href="https://en.wikipedia.org/wiki/Convolution">convolution</a> operation on the matrix of pixel values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*Ff6mG3aPdWFkjb6hZPy3Xw.gif" /><figcaption><a href="https://mlnotebook.github.io/post/CNN1/">Source</a></figcaption></figure><p>The visualization above shows how convolution operation works. And the Conv2D layer we imported earlier does the same thing. The first matrix (<em>from the left</em>) in the demonstration is the input to the convolutional layer. Then another matrix called “filter” or “kernel” is multiplied (matrix multiplication) to each window of the input matrix. The output of this multiplication is the input to the next layer.</p><p>Other than convolutional layers, a typical CNN also has two other types of layers: 1) a p<a href="https://machinelearningmastery.com/pooling-layers-for-convolutional-neural-networks/">ooling layer</a>, and 2) a f<a href="https://stats.stackexchange.com/questions/182102/what-do-the-fully-connected-layers-do-in-cnns">ully connected layer</a>.</p><p>Pooling layers are used to generalize the output of the convolutional layers. Along with generalizing, it also reduces the number of parameters in the model by down-sampling the output of the convolutional layer.</p><p>As we just learned, convolutional layers represent high level features from image data. Fully connected layers use these high level features to train the parameters and to learn to classify those images.</p><p>We will also use the <a href="https://machinelearningmastery.com/dropout-for-regularizing-deep-neural-networks/">Dropout</a>, <a href="https://en.wikipedia.org/wiki/Batch_normalization">Batch-normalization</a> and <a href="https://stackoverflow.com/questions/43237124/role-of-flatten-in-keras">Flatten</a> layers in addition to the layers mentioned above. Flatten layer converts the output of convolutional layers into a one dimensional feature vector. It is important to flatten the outputs because Dense (Fully connected) layers only accept a feature vector as input. Dropout and Batch-normalization layers are for preventing the model from <a href="https://en.wikipedia.org/wiki/Overfitting">overfitting</a>.</p><pre>train_x, val_x, train_y, val_y = train_test_split(train_data, train_labels, test_size=0.2)</pre><pre>batch_size = 256<br>epochs = 5<br>input_shape = (rows, cols, 1)</pre><pre>def baseline_model():<br>    model = Sequential()<br>    model.add(BatchNormalization(input_shape=input_shape))<br>    model.add(Conv2D(32, (3, 3), padding=&#39;same&#39;, activation=&#39;relu&#39;))<br>    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))<br>    model.add(Dropout(0.25))<br>    <br>    model.add(BatchNormalization())<br>    model.add(Conv2D(32, (3, 3), padding=&#39;same&#39;, activation=&#39;relu&#39;))<br>    model.add(MaxPooling2D(pool_size=(2, 2)))<br>    model.add(Dropout(0.25))</pre><pre>model.add(Flatten())<br>    model.add(Dense(128, activation=&#39;relu&#39;))<br>    model.add(Dropout(0.5))<br>    model.add(Dense(10, activation=&#39;softmax&#39;))<br>    return model</pre><p>The code that you see above is the code for our CNN model. You can structure these layers in many different ways to get good results. There are many popular CNN architectures which give state of the art results. Here, I have just created my own simple architecture for the purpose of this problem. Feel free to try your own and let me know what results you get :)</p><h3>Training the model</h3><p>Once you have created the model you can import it and then compile it by using the code below.</p><pre>model = baseline_model()<br>model.compile(loss=&#39;categorical_crossentropy&#39;, optimizer=&#39;sgd&#39;, metrics=[&#39;accuracy&#39;])</pre><p><strong>model.compile </strong>configures the learning process for our model. We have passed it three arguments. These arguments define the <a href="https://machinelearningmastery.com/loss-and-loss-functions-for-training-deep-learning-neural-networks/">loss function</a> for our model, <a href="https://blog.algorithmia.com/introduction-to-optimizers/">optimizer</a> and <a href="https://keras.io/metrics/">metrics</a>.</p><pre>history = model.fit(train_x, train_y,<br>          batch_size=batch_size,<br>          epochs=epochs,<br>          verbose=1,<br>          validation_data=(val_x, val_y))</pre><p>And finally by running the code above you can train your model. I am training this model for just five epochs but you can increase the number of epochs. After your training process is completed you can make predictions on the test set by using the following code.</p><pre>predictions= model.predict(test_data)</pre><h3>Conclusion</h3><p>Congrats! You did it, you have taken your first step into the amazing world of computer vision.</p><p>You have created a your own image classifier. Even though this is a great achievement, we have just scratched the surface.</p><p>There is a lot you can do with CNNs. The applications are limitless. I hope that this article helped you to get an understanding of how the process of training these models works.</p><p>Working on other datasets on your own will help you understand this even better. I have also created a GitHub <a href="https://github.com/aditya2000/MNIST-Fashion-">repository</a> for the code I used in this article. So, if this article was useful for you please let me know.</p><p>If you have any questions or you want to share your own results or if you just want to say “hi”, feel free to hit me up on <a href="https://twitter.com/aditya_dehal">twitter</a>, and I’ll try to do my best to help you. And finally <strong>Thanks a lot for reading this article!!</strong> :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c47a5e3ccdd8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to assess the accuracy of a Machine Learning Model?]]></title>
            <link>https://medium.com/@bw99214/assessing-the-accuracy-of-a-model-5801ecfe8dc0?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5801ecfe8dc0</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 02:05:31 GMT</pubDate>
            <atom:updated>2019-08-02T12:44:56.090Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In recently came across a book called “<em>An Introduction to Statistical Learning</em>” by <em>Gareth James, Daniela Witten , Trevor Hastie Robert Tibshirani. </em>In this article, I am trying to explain what I have understood from one of the chapters of that book.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5m6O4h7_v2b0vtuStZL5YQ.jpeg" /><figcaption><a href="http://pexels.com/">Source</a></figcaption></figure><p>According to “<strong>No-free-lunch theorem</strong>”, no one model dominates all the models over all possible datasets. One model may work better on one dataset but may not work on some other dataset. Therefore, it is important for us to understand which model works best on the given dataset and hence we need to assess the accuracy of the model.</p><p>For evaluating the model we need to have a way to find the extent to which the predicted response value for a given observation is close to the true response value of the observation.</p><h3>Mean Squared Error</h3><p>Mean Squared Error can be used to find how close predicted response value is to the actual value of the observation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*wOtaeC2YkkWfj57n5kV_tg.png" /><figcaption><a href="https://www.google.com/url?sa=i&amp;source=images&amp;cd=&amp;ved=2ahUKEwjKqPejz77iAhUX4o8KHVsTBF0QjRx6BAgBEAU&amp;url=https%3A%2F%2Fwww.researchgate.net%2Ffigure%2FMean-Squared-Error-formula-used-to-evaluate-the-user-model_fig1_221515860&amp;psig=AOvVaw3WAxHO-jAAbfeSO5Uc0zuD&amp;ust=1559146444928241">Source</a></figcaption></figure><p>The value of mean squared error will be small if the predicted response and actual value is close and will have a high value if they differ a lot.</p><p>Mean Squared Error is computed using the training data that was used to fit the model, but, we are more interested in the accuracy of the model for the unseen data. We want to find out which model performs best for the previously unseen data.</p><p>The previously unseen data is often known as the test data. To find out the test MSE we can calculate the Average Squared Prediction Error for a large set of test observations.</p><pre>               Ave((y0 - f&#39;(x0))^2)</pre><p>Lower the value of MSE, better is the fit generated by the model. Linear models can give low MSE for data that can be modelled linearly but when data becomes more complicated it cannot be easily modelled by Linear models. In such cases we have to use models which are more flexible for the data given. Flexibility of the model determines how easily it can model a given set of data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*WyD8q_Rn18V1aNFqbRFEBA.png" /><figcaption><a href="https://www.google.com/url?sa=i&amp;source=images&amp;cd=&amp;ved=2ahUKEwjtwNmHjsHiAhVHk3AKHV4EBMoQjRx6BAgBEAU&amp;url=http%3A%2F%2F2datascientist.blogspot.com%2F2017%2F01%2Fsimple-explanation-of-bias-variance.html&amp;psig=AOvVaw1P66me5r2KQuCgWqYZyryZ&amp;ust=1559232005412429">Source</a></figcaption></figure><p>The plot above shows the relation between the MSE and flexibility of the different models. Gray Line is the training MSE and Red line is the test MSE. We can also observe from the graph that both training and test set MSE decreases in the start as the flexibility of the model increases. But as the flexibility starts increasing more, training set MSE keeps on decreasing whereas test set MSE starts increasing. This is called as <strong>overfitting</strong>.</p><h3>Bias-Variance Tradeoff</h3><p>The phenomenon of overfitting that we observed was because of two competing properties of statistics called Bias and Variance.</p><p>Expected test set MSE for a given value of <strong>x0</strong> can be decomposed into the sum of three fundamental quantities:</p><pre>1. Variance of f&#39;(x0)<br>2. Squared Bias of f&#39;(x0)<br>3. Variance of error terms E</pre><pre> E(y0 - f&#39;(x0))^2 = Var(f&#39;(x0)) + [Bias(f&#39;(x0))]^2 + Var(E)</pre><p>This equation tells us that in order to get the minimum test error we must have a model that achieves <em>low bias</em> and <em>low variance</em>.</p><h4>Variance</h4><p>Variance is the amount by which the prediction value changes if it was estimated using a different training dataset. If the model have high variance then small changes in dataset can lead to huge changes in the predictions.</p><p>More flexible methods usually have high variance.</p><h4>Bias</h4><p>Bias refers to the error that is introduced when we use a very simple model for a very complex problem. For example if we use a basic linear model for a sophisticated problem such as Image Classification or Object Detection. This can lead to a very high bias situation.</p><h3>Conclusion</h3><p>So, this was all I had for this article. If there was anything I missed or if I was wrong about something, please let me know. You can follow me on twitter</p><p><a href="https://twitter.com/aditya_dehal">Aditya Dehal (@aditya_dehal) | Twitter</a></p><p>Or you can leave a comment down in the comment section. If you liked this article please hit the claps. <strong>Thanks a lot</strong>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5801ecfe8dc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Supervised Learning — Classification]]></title>
            <link>https://medium.com/@bw99214/supervised-learning-classification-fc247bc55293?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc247bc55293</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[support-ve]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Tue, 01 Jan 2019 14:09:45 GMT</pubDate>
            <atom:updated>2019-08-02T12:41:38.843Z</atom:updated>
            <content:encoded><![CDATA[<h3>Supervised Learning — Classification</h3><p>Classification problems the types of machine learning problems in which algorithm gives some distinct set of values for some given arbitrary input. You can think of it in this way, that for every input the algorithm will classify that input and will determine which class it belongs to.</p><p>For example: For a problem in which the algorithm have to find out whether the given image is a cat image or a dog image. To do that we will train that algorithm with lots of labelled cat and dog images. On the basis of that given training data the algorithm will become capable of successfully classify the cats and dog images.</p><p>Classification algorithms are used to solve many different types of problems such as: identifying the spam emails, fraud detection in online transactions etc.</p><h3>Logistic Regression</h3><p>Logistic Regression is a classification algorithm. It this article we will try to understand the logistic regression for binary classifications(but it can be used to classify multiple classes).</p><p>So how does it works? The working of logistic regression is pretty much similar to that of linear regression with some modifications. In linear regression, we define a hypothesis as :</p><pre>           h(x) = mx + c</pre><p>This hypothesis works well for the problems which involves data with discrete output values but in case of classification problems this hypothesis doesn’t works well and it ends up predicting the wrong output values. So, to overcome this problem we put a slight modification to the hypothesis. Now this modification must be designed in a way that it could easily classify the data that belongs to different classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*W5UFcJUwDyFkD0Zdm4GrPA.jpeg" /><figcaption>Logistic Regression hypothesis</figcaption></figure><p>Bingo! the curve in the image can easily classify the data given to the algorithm. These types of curves are given by the function called <a href="https://en.wikipedia.org/wiki/Sigmoid_function"><strong>Sigmoid Functions</strong></a>. A sigmoid function is defined as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*Gp5E23P5d2PY5D5kOo8ePw.png" /><figcaption>A typical sigmoid function</figcaption></figure><p>Now, we can define a hypothesis for our algorithm in using the sigmoid function. This can be done easily:</p><pre>      z = h(x) = mx + c<br>      <br>      g(z) = 1/(1+e^(z))</pre><p>Not bad! Once we have defined our hypothesis, now our task is to figure out how to use this hypothesis to predict the output value. Since, it is very clear from the function (sigmoid) that value of g(z) can lie only between 0 and 1, but, we want it to predict a binary value (0 or 1)(yes or no)(true or false). We can define a threshold value so that if the algorithm outputs a value greater than the threshold then the value is 1 and if less than or equals to it then you can give it a value of 0. In this way we can get binary values as output.</p><p>Once we have done that, now it’s time to find our cost function. Here also we would not use the cost function similar to the one used in linear regression. The reason behind this is that if we used the similar cost function then the curve representing the values of the cost function will have many local minimums due to which our gradient descent will find it hard to reach the actual minimum value required. We can also say that in that case we will not get a <strong>convex</strong> curve due to which we might end up predicting values that are not accurate.</p><p>So, let’s write our cost function. Cost function for logistic regression can be written down as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*JWvYunYNRvkTz7ROGR43_w.png" /></figure><p>Huh! That’s big one. Let’s break it down. Now consider if the actual value of the y is 1, then the whole term next to the ‘+’ sign will be equal to zero. Now in the first equation term our value of y is 1, so only that term will be taken into cost function. And if y is 0 then second term will be considered.</p><p>So now we have our cost function, the next thing to do is to minimize this cost function. We can use gradient descent to minimize the cost function.</p><h3>Support Vector Machine</h3><p>Support Vector Machine or SVM is another classification algorithm. It can be used for classification between the two different classes of data. Support Vector Machines was created by <a href="https://en.wikipedia.org/wiki/Hava_Siegelmann">Hava Siegelmann</a> and <a href="https://en.wikipedia.org/wiki/Vladimir_Vapnik">Vladimir Vapnik</a> in 1963.</p><p>Support Vector Machines can be used in many real life problems such as handwriting recognition, image classification, bioinformatics and so on.</p><p>Now let’s start to understand the working of the SVM. Suppose that we have some data( for simplicity assume that data has only two classes). To divide that data we can say that a line can be drawn between the two classes of data such that the distance of the line from the extreme points of the both classes is maximum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*pCI_El4yrJLIo292ULujLg.jpeg" /></figure><p>This distance is called as the margin. And the aim of svm is to maximize the margin. And the line that is in between the margin is called as the <strong>Hyperplane</strong>.</p><p>Now our job is to define the hyperplane. Okay! So we know that a line can be defined as follows (we are defining the hyperplane in two dimensions only but this can be replicated for higher dimensions of data as well)</p><pre>              y = mx + c<br>       <br>    Let&#39;s make some modifications in this equation<br>              mx-y + c = 0<br>    Now we define two vectors w=(m, -1) and <strong>x</strong>=(x, y) such that <br>    their dot product becomes<br>              w.<strong>x</strong> = mx + (-1)y = mx-y<br>    So, in our previous equation we can substitute this value and<br>    we get<br>              w.<strong>x</strong> + c = 0</pre><p>Great! we have defined our hyperplane. So now how can we use this hyperplane to classify our data.</p><pre>    <br>       w.x + c &gt; 0, then the data input belongs to &#39;+&#39; class<br>       w.x + c &lt; 0, then the data input belongs to &#39;-&#39; class<br></pre><p>So above we have defined our hypothesis for the support vector machine. Now we have got our hyperplane but there can exist an infinite number of hyperplanes. The point here is to find the best one. To find the best hyperplane we need to define some metric. On the basis of that metric we will compare all the hyperplanes and we will find the best one.</p><pre>We will define a parameter <em>gamma</em> such that </pre><pre>                   <em>gamma</em> = y*((w/|w|)x + (c/|c|))</pre><p>The parameter <em>gamma</em> will help us tell that the value predicted by the hypothesis is correct or incorrect. If the value of gamma is positive then we can say that the value is correct and if it is negative then we can say that the value predicted by hypothesis is incorrect. For example if the hypothesis gives a positive value (which means that the data element belongs to ‘+’ class) and the actual value is also positive then, if we substitute both of them in equation above we will get a positive value and same is true for negative values.</p><p>To understand more about how we get the metric above you can take at this <a href="https://shuzhanfan.github.io/2018/05/understanding-mathematics-behind-support-vector-machines/">wonderful article</a>.</p><p>The equation above gives the value of <em>gamma</em> for one data element. Now we will find <em>gamma</em> for each data element in the dataset. We will find the smallest value of <em>gamma</em> which we can write down as</p><pre>    <br>               M = min(gamma<em>(i)</em>)</pre><p>This <strong>M</strong> that we have defined is also known as Geometric Margin. This is the metric on the basis of which we will compare the hyperplanes. The hyperplane with the largest <strong>M</strong> will be the one we need.</p><p>We can write down our optimization objective as</p><p><em>max(</em><strong><em>M</em></strong><em>)</em></p><p>So we can use Lagrangian Optimization to solve for this optimization. To get details about it check out this <a href="https://shuzhanfan.github.io/2018/05/understanding-mathematics-behind-support-vector-machines/">article</a> over here.</p><p>There are some other important concepts to understand in svm like the kernel trick. In simple words we can understand that we use kernel trick in svm when we cannot define a straight hyperplane for our data. So we try to shift our data to higher dimensions so that we can find a straight hyperplane for our data. To learn more about the kernels take a look at this <a href="https://www.youtube.com/watch?v=mTyT-oHoivA&amp;list=PLLssT5z_DsK-h9vYZkQkYNWcItqhlRJLN&amp;index=73">video</a>.</p><h3>Conclusion</h3><p>Finally! we did it. I know this article was pretty boring one with a bunch of mathematical equations in it and I haven’t explained few things to make this article concise but I have mentioned the resources where you can get better intuition about classification algorithms. If I made some mistake in the article then please correct me for that and if you liked it then please clap!! Thanks.</p><h3>Resources</h3><ol><li><a href="https://shuzhanfan.github.io/2018/05/understanding-mathematics-behind-support-vector-machines/"><em>Understanding the mathematics behind Support Vector Machines</em></a></li><li><a href="https://medium.com/machine-learning-for-humans/supervised-learning-2-5c1c23f3560d"><em>Machine Learning for humans-Part 2.2-Supervised Learning II</em></a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc247bc55293" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Supervised Learning]]></title>
            <link>https://medium.com/@bw99214/supervised-learning-bededec764cd?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bededec764cd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Sat, 22 Dec 2018 09:24:59 GMT</pubDate>
            <atom:updated>2019-08-02T12:42:32.166Z</atom:updated>
            <content:encoded><![CDATA[<p>Supervised Learning is a category of machine learning problems in which the system is trained to map the input data to the output data based on the mapping of input and output pairs in the training data.</p><p>What does that means? Let’s say we are building an app that could classify the dog and cat images. We will first provide the system with a lot of different images and will give them some labels(in this case cat and dog). Now the system will learn to map those images to the right labels. After that when someone would give it an image it could successfully classify if the animal in the image is a cat or a dog. This type of a problem comes under supervised learning. Supervised Learning finds the patterns in the training data and uses those patterns for test data to predict the correct output.</p><p>Supervised learning problems can be of two types: 1. Regression 2. Classification. In this article we will talk about Regression only.</p><h3>Regression</h3><p>Regression Problems have discrete input variables which are mapped to discrete output values. For example in a problem like predicting the price of an used car. This could include multiple input parameters such as total distance travelled, no. of times car was sent for service, how old the car is etc. These parameters are usually called as <strong>features </strong>and in this case the price of the car will be called as <strong>label</strong>.</p><p>One of the most common type of algorithm used to model regression problems is <strong>Linear Regression</strong>.</p><p>Linear Regression is a very trivial kind of algorithm. Let’s use a sample data-set which we can model using the linear regression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*De1iyt_bEy-BXD65_5aovw.png" /><figcaption>Linear Regression</figcaption></figure><p>Now the data given in the picture is pretty simple. On the one side we have a feature called <strong>x</strong> which is mapped to a label called <strong>y</strong>. In this case, every discrete value of x will give a discrete value for y. We can plot this data in the graph as given on the left side of the picture(for now don’t bother about that line and the equation, it will soon become very clear).</p><p>Since we have our data plotted now to find the pattern in the data, linear regression will try to find a line that best generalises the data. As you can see in the data there is already a line that perfectly generalises the data. And the equation in the bottom is used to represent the line.</p><pre>As we know that a line can be represented by an equation as follows.</pre><pre>                  y = mx + c</pre><pre>where, m is the slope of the line and c is the y-intercept<br>In the above data value of m is 1.041 and value of c is -0.075</pre><p>We can clearly see from the equation that <strong>m</strong> and <strong>c</strong> are the variables that governs the line and if we could somehow find a correct value for these two variables we can find the line that is the best fit for our data.</p><p>Now the question comes how can we find the value for these two variables? Think about it this way, how is a best fitted line different from other lines? Try thinking about it.</p><p>The answer is the average distance from each point to the line is minimum for a best fitted line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*dnFljsilN6-wpo0orHg3Bw.png" /></figure><p>So to find our ideal line, all we have to do is to find the values of m and c such that the average distance between each data point and the line is minimum.</p><p>Now to find the minimum distance we first need to find the distance of each point from the line.</p><pre> Let&#39;s write a new function h(x) = mx + c<br> h(x) is also called as a hypothesis.<br> We can represent the distance of each point from the line by <br>     error = y - h(x) , the actual term for this distance is error</pre><p>We have error value for each data point in the training data set. Now we have to average the error value for every data point in the training data.</p><pre>We can write a function J(m, c) = sum(y - h(x))/N<br>where, N is the total number of data points in the training data set</pre><pre>This function is commonly known as a cost function or loss function</pre><p>Finally we have our average error value. Now, our task is to minimise the loss function.</p><pre>min(J(m, c))</pre><p>To minimise the loss function we use a very popular algorithm called <strong>Gradient Descent</strong>. Job of gradient descent is to find the minimum value for the cost function against all the possible values of m and c.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*09kq2L23D9XM_9Xtr8gc8Q.png" /><figcaption>Gradient Descent</figcaption></figure><p>We can use the plot above to better understand the working of gradient descent. The coloured plane in the plot gives the value for the cost function against the value of m and c(in the plot m and c are represented by theta1 and theta2). The blue region represents the region where the value of cost function is lowest. We first give some initial value for m and c ( usually zero ) and then we iteratively decrease the value of J by changing the value of m and c until we reach the point where it’s value is minimum.</p><p>At this point someone could ask a question. How are we going to decrease the value of loss function? Answer is simple, we will decrease the values of m and c by some factor after each iteration.</p><pre>repeat {<br>  m := m - alpha*partial_Derivative(J(m, c))<br>  c := c - alpha*partial_Derivative(J(m, c))<br>}</pre><p>Now, let’s try to understand what this factor is. The factor we subtracted contains two variables- alpha and partial derivative of loss function. While moving towards the least value of the loss function we must know that how big our step should be and in which direction it must be(in our case it’s downwards). This factor alpha, which is called as <strong>learning rate</strong>, tells us that how big step should we take (Note: <em>Alpha</em> <em>should not be too big otherwise we will overpass the desired position</em>) and partial derivative of loss function with respect to one of the variables, tells the direction.</p><h3>Conclusion</h3><p>I appreciate your efforts if you are still reading this article. There are a lot of different topics that I haven’t talked about in this article such as Overfitting and underfitting, when does gradient descent works well and when it doesn’t, alternatives of gradient descent such as Newton’s Method etc. There are a lot of interesting things to talk about in Machine Learning and this article just scratches the surface of it. If you like it please do give me some claps. I would love to hear your thoughts on this article in the comments or in my personal inbox at(bw99214@gmail.com).</p><h3>Resources</h3><ol><li><a href="https://medium.com/machine-learning-for-humans/supervised-learning-740383a2feab"><em>Machine Learning for Humans by Vishal Maini</em></a></li><li><a href="https://www.youtube.com/watch?v=CtKeHnfK5uA"><em>Linear Regression- Augmented Startups</em></a></li><li><a href="https://www.youtube.com/watch?v=jc2IthslyzM&amp;t=155s"><em>Mathematics of Gradient Descent- Coding Train</em></a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bededec764cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Your First React Component.]]></title>
            <link>https://medium.com/@bw99214/creating-your-first-react-component-7d2e866c2f15?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d2e866c2f15</guid>
            <category><![CDATA[react-components]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[web-framework]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Sun, 05 Aug 2018 03:45:54 GMT</pubDate>
            <atom:updated>2018-08-05T03:45:54.631Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*kFUvxT3hg5ipNNMq7JYiNA.png" /></figure><p>Hello! I’m glad you are here to learn how to build your first React component. Since , you are here so I’m going to assume that you might have no or very little experience with React and you know JavaScript up-to some extent. And even if you aren’t then I don’t think it’s a problem(you are still welcomed).So, let’s get started!!</p><h4>Let’s first talk about react</h4><p>According to reactjs documentation, React is a JavaScript library for building user interfaces. Mostly, React is used to build single page applications and mobile applications. React have many cool features which will help you in development of cool user interfaces and complex applications. For more details you can go to official <a href="https://reactjs.org/docs/getting-started.html">docs</a> of reactjs.</p><h4>Let’s set things up</h4><p>Before we start building our first component in react we need to create a setup for react using create-react-app.</p><p>First things first, you must have nodejs installed in your system. If not, then you can visit the official website of<a href="https://nodejs.org/en/"> nodejs </a>and get it done.</p><p>If you have successfully installed nodejs then let’s move forward. Now, open your terminal/command prompt and type “npm install -g create-react-app”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n9A8F1dbXJIcCqmmZxyXhg.png" /></figure><p>It will take some time depending on your internet speed. Till then let me tell you what this line of code does. Now, npm or node package manager is, as the name says, is a package manager for JavaScript. It is quiet a huge collection of packages. So what it does is that it installs the create-react-app globally in your system(since we are using -g which commands npm to install the package globally). Create-react-app is an environment for creating single page applications with React.(Don’t panic if you don’t understand this phrase-“single page application”, you’ll soon understand what it means).</p><p>So now go to your preferred folder for development and type “create-react-app my-first-react-app”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C2aL1kkyCtRDKLtL9qcMZA.png" /></figure><p>and wait for few moments. When it’s done it will show something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o07oNH44omd7AUQ39ruthg.png" /></figure><p>Okay! Great we are all set. Let’s move further ahead.</p><h4>Let’s write some code!</h4><p>Now, open the folder “my-first-react-app” in your favorite text-editor and let’s start.</p><p>So, first we’ll go a little bit through the file structure in our setup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDPvdR9H4IHlP8O5oP3pKw.png" /></figure><p>So, inside the “src” folder we have some JavaScript and CSS files initially and now open the terminal(make sure that you are in the same directory) and type “npm start”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yEpGua3T-IoyZoBYn_tMKA.png" /></figure><p>It will start your development server and inside the browser your app will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u8biuwzPkvaAblCNMmCNvQ.png" /></figure><p>Okay!cool! now if you will look at your app.js file you will see some code already written there. We are going to remove some of the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oEggb0oMgTJpa2l1afWB1g.png" /><figcaption>Remove the highlighted code.</figcaption></figure><p>So, now it’s time to write some code.</p><pre>class App extends React.Component {<br>// some code</pre><pre>}</pre><p>Here, we are creating a new component named App which is extended from Component class of React. There are some other ways to write a component but this is the simpler and popular one.</p><p>So now next we are going to write some code inside it to make our component work.</p><pre>class App extends React.Component {<br>    render(){<br>             return(<br>               &lt;div&gt;<br>               &lt;/div&gt;</pre><pre>             );</pre><pre>    }</pre><pre>}</pre><p>Now, what we have done here is, we have created function render() which returns something which looks a little bit like HTML, but believe me it’s not what you are thinking it is. We call it JSX. JSX stands for JavaScript XML. It defines the layout of the web page. For more details on JSX you should go through some <a href="https://reactjs.org/docs/introducing-jsx.html">docs</a>.</p><p>Fine! we are almost done. Let’s add some small changes</p><pre>class App extends React.Component {<br>    render(){<br>             return(<br>               &lt;div&gt;<br>                &lt;h1&gt;This is my first react app.&lt;/h1&gt;</pre><pre>              &lt;/div&gt;</pre><pre>    );</pre><pre>   }</pre><pre>}</pre><p>Bravo! You have created your first component in react. Now let’s see how it looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tDVcHEFbL3NNa1hjk9fdJQ.png" /><figcaption>This is your first React component.</figcaption></figure><h4>Let’s Conclude</h4><p>So, finally you have created your first React component and I hope you have a little bit insight of how things work in React. This tutorial was a very abstract one and if you want to dig deep into React then you can check the official <a href="https://reactjs.org/docs/getting-started.html">documentation</a> of React. If you prefer videos more then just reading stuff then <a href="https://www.youtube.com/user/programmingwithmosh">Programming with Mosh</a> and <a href="https://www.youtube.com/watch?v=vYldnghykaU&amp;list=PLillGF-RfqbbKWfm3Y_RF57dNGsHnkYqO">Traversy Media</a> have some great content on You Tube, make sure to check them as well. And if you want to know what else you can build with react then go to the homepage of <a href="http://madewithreact.com/">Made with React</a>.</p><p>I hope this article was helpful. If you liked this article then please give some <strong>claps</strong>(I really appreciate that) and if not, then please mention what went wrong. And finally a big <strong>THANKS</strong> to all of you who are still reading(I really love you). If you have any questions then hit me up anytime at <a href="https://twitter.com/aditya_dehal">Twitter</a>, <a href="https://www.instagram.com/adityadehal/">Instagram</a>(don’t think twice before following me;) ) or you can email me at bw99214@gmail.com.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d2e866c2f15" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Time Complexity of Algorithms.]]></title>
            <link>https://medium.com/@bw99214/time-complexity-of-the-algorithms-5a3dae1cb873?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a3dae1cb873</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Sun, 24 Jun 2018 19:22:49 GMT</pubDate>
            <atom:updated>2019-07-19T00:54:37.667Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*eFKcvAA0TZrMDG8I2MHTMw.png" /></figure><p>In computer science, analysis of algorithms is a very crucial part. It is important to find the most efficient algorithm for solving a problem. It possible to have many different algorithms to solve a problem, but the challenge here is to choose the most efficient one. Now the point is how can we recognize the most efficient algorithm if we have a set of different algorithms? Here, the concept of space and time complexity of algorithms comes into existence. Space and time complexity acts a measurement scale for algorithms.We compare the algorithms in terms of their space(amount of memory) and time complexity(number of operations).</p><p>The total amount of the computer memory used by an algorithm when it is executed, is the space complexity of that algorithm. We’ll not discuss space complexity in this article(to make this article a little bit smaller). So, the time complexity is the number of operations an algorithm performs to complete its task(considering that each operation takes the same amount of time). The algorithm who performs the task in less number of operations is considered to be the efficient one in terms of the time complexity. However, the space and time complexity is also effected by the factors such as operating system, hardware but we are not including them.</p><p>Now in order to understand the time complexity we will take an example in which we’ll compare two different algorithms which are used to solve a particular problem.</p><p>The problem is searching. We have to search for an element in an array(in this problem we are going to assume that the array is sorted in ascending order). To solve this problem we have two algorithms :</p><ol><li><a href="https://www.hackerearth.com/practice/algorithms/searching/linear-search/tutorial/">Linear Search.</a></li><li><a href="https://www.hackerearth.com/practice/algorithms/searching/binary-search/tutorial/">Binary Search.</a></li></ol><p>Let’s say the array contains ten elements and we have to find the number ten in the array.</p><pre>const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];<br>const search_digit = 10;</pre><p>Linear search algorithm will compare each element of the array to the <strong>search_digit</strong> and when it finds the <strong>search_digit</strong> in the array it will return <strong>true</strong>. Now let’s count the number of operation it performs. Here, the answer is 10(since, it compares every element of the array). So, the Linear search uses ten operations to find the given element(these are the maximum number of operations for this array in the case of Linear search, this is also known as the <a href="https://www.geeksforgeeks.org/analysis-of-algorithms-set-2-asymptotic-analysis/">worst case</a> of an algorithm). In general, the Linear search will take <strong>n</strong><em> </em>number of operations in its worst case(where <strong>n</strong> is the size of the array).</p><p>Let’s examine Binary search algorithm for this case.</p><p>Binary search can be easily understood by this diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*e134hFR6MoocKMiX_OMU2g.png" /><figcaption>Binary Search</figcaption></figure><p>If we try to apply this logic on our problem then, first we’ll compare <strong>search_digit</strong> with the middle element of the array, that is 5. Now since, 5 is less than 10 then we will start looking for the <strong>search_digit</strong> in the array elements greater than 5, in the same way until we get the desired element 10.</p><p>Now, try to count the number of operations binary search took to find the desired element. It took approximately four operations. Now, this was the worst case for binary search. This shows that there is a <a href="https://www.khanacademy.org/math/algebra2/exponential-and-logarithmic-functions/introduction-to-logarithms/a/intro-to-logarithms">logarithmic</a> relation between number of operations performed and total size of array.</p><pre>number of operations = log(10) = 4(approx)</pre><pre><em>for base 2</em></pre><p>We can generalize this result for Binary search as:</p><p>For an array of size <strong>n</strong>, the number of operations performed by the Binary Search is: <strong>log(n)</strong></p><h3>Big O notation</h3><p>In the above statements, we saw that for an array of size <strong>n</strong>, linear search will perform <strong>n</strong> operations to complete the search, whereas , Binary search performed<strong> log(n)</strong> number of operations (both for their worst cases). We can represent this as a graph ( <strong>x-axis</strong>: number of elements,<strong> y-axis</strong>: number of operations).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HXLf1cALaHeM7B1M-EVKXQ.png" /><figcaption>Time complexity of Binary search and Linear search.</figcaption></figure><p>This is quiet clear from the figure that the rate by which the complexity increases for Linear search is much faster than that for binary search.</p><p>When we analyse an algorithm, we use a notation to represent its time complexity and that notation is Big O notation.</p><p>For Example: time complexity for Linear search can be represented as <strong>O(n)</strong> and <strong>O(log n)</strong> for Binary search, where, <strong>n</strong> and <strong>log(n)</strong> are the number of operations.</p><p>The Time complexity or Big O notation for some popular algorithms is listed below:</p><ol><li>Binary Search: <strong>O(log n)</strong></li><li>Linear Search: <strong>O(n)</strong></li><li>Quick Sort: <strong>O(n*log n)</strong></li><li>Selection Sort: <strong>O(n*n)</strong></li><li>Travelling salesperson : <strong>O(n!)</strong></li></ol><h3>Conclusion</h3><p>I really appreciate your efforts if you are still reading this article. Now, you must be thinking that, why time complexity is so important to understand? We know that for small number of elements (say 10) the difference between the number of operations performed by binary search and linear search is not so big, but in real world, most of the time we deal with the problems that have big chunks of data. For example if we have 4 billion elements to search for then, in its worst case, linear search will take 4 billion operations to complete its task whereas binary search will complete this task in just 32 operations. That’s a big difference. Now let’s assume that if one operation takes 1 ms for completion then binary search will take only 32 ms whereas linear search will take 4 billion ms that is 46 days (approx). That’s a significant difference. This is the reason why studying time complexity becomes important when it comes to such a big amount of data.</p><p>If you liked this article then please make sure to <strong>clap</strong> and if I was wrong about something or I missed something then please mention about that as well and finally Thanks a lot for your time.</p><p>Resources: <a href="https://www.manning.com/books/grokking-algorithms">Grokking Algorithms</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a3dae1cb873" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build apps for Google Assistant with no programming experience]]></title>
            <link>https://medium.com/free-code-camp/how-to-build-apps-for-google-assistant-with-no-programming-experience-42a490d63822?source=rss-b7d0624786d6------2</link>
            <guid isPermaLink="false">https://medium.com/p/42a490d63822</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Kunwar Aditya]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 05:20:04 GMT</pubDate>
            <atom:updated>2018-03-21T05:20:04.592Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*b-C4a_VPFWHv6A4imIvN-w.jpeg" /></figure><p>Recently, Google launched a developer program for Google Assistant. This program was launched with the incentive to engage users to Google Assistant. Google invited a ton of developers from all around the world to join their developer community to build some cool apps for Google Assistant.</p><p>There are two very cool things about this community program. First, you don’t need to be a developer to built apps. So, stay tuned because I’m going to tell you how you can build some cool apps and get them published even if you don’t know how to write code.</p><p>And second, if your app gets published, then Google will send you an exclusive Google Assistant t-shirt — and your account associated with that app will be credited with up to $200 of cloud services every month for the next year. And also you will get some more awards and perks when your app crosses certain milestones.</p><p>So, let’s get started. First of all, go to the <a href="https://developers.google.com/actions/community/overview?utm_source=devsite&amp;utm_medium=hpp&amp;utm_campaign=googleassistantdevcommunity_feb18">Google Assistant developer community program</a>. Then you will land on this page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_P3XHvyxtnIsuP9ZYW-sQ.png" /></figure><p>Click on the “Home” button in the navigation bar. This will take you to a page which looks like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XU6qPAhKQspsjxi5l4WLQA.png" /></figure><p>Now at this point, I suggest you go through the details given on this page. In the top-right corner of the page, you will find a button named “ACTIONS CONSOLE”. Click on that and it will now take you to this page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aXvdLWhOcHgEMYKOe8HmJA.png" /></figure><p>Now go forward and click on that “+” sign to Add or Import your project. After you clicked on that you will reach this stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PL2o2LT01MAKFF34ET5gSA.png" /></figure><p>Now write the name of your project and select your Country/region. And if you have time, make sure to read Terms of Service and Policies. After you have completed this, click on “CREATE PROJECT” and it will take you to the next page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KEDFT1Yhax8C7qtXYonNGQ.png" /></figure><p>Here you will find a lot of things to do. If you are a developer then you can check out the options like Dialogflow, Smart home, and Actions SDK — but don’t worry if you are a newbie and you don’t know how to work with these things. Now scroll down to the bottom and you’ll find some options there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*41Luy5bXRwA0hvNRHse8bQ.png" /></figure><p>Here, you have the option to choose one of these templates which are already built by Google. You have to just reinvent them in your own unique way. Now I’m going to choose trivia. So, click on the “BUILD” and proceed.</p><p>And now you’ll find yourself on this page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RRMsTzUQFa30Pt4AsDlwA.png" /></figure><p>Here, you have to choose a personality for your Google Assistant app. You can choose any of them according to your choice. I’ll choose the one on the right, “Beeps the Robot.” After you have chosen the personality for your app, click on the “NEXT” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gmlkex9jjlH3Qwii3RAsPg.png" /></figure><p>And you are here and now all you have to do is to click on the “Bulk upload the content for your app using Google Sheets.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SVJX_TLvYbOAuRuudP8E7Q.png" /></figure><p>Now, here you can do one thing which will make your life easier. Just click on the link which says “Make a copy of the pre-filled Google Sheet for editing.” It doesn’t mean that you are going to copy someone else’s idea, but Google has a defined format for the app templates they have provided us. So don’t think that you’re going to get into any plagiarism stuff. When you click on that link, you will get a Google sheet which is filled already and looks somewhat like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1D5BDQpa9FKuqehCJdp1Wg.png" /></figure><p>All you have to do is just edit this sheet in your own way, and when you are done just go to the previous tab and click “NEXT”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*puq9woFLvYNDiHArw0NX7A.png" /></figure><p>Here you have to paste the link to the sheet that you edited previously and then click upload. Then it will start validating. If your content is validated then you will see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctlSmbIl7qkpXf4bCVh6ng.png" /></figure><p>Click on the “Create App” and proceed and you will see this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t_dmbD9Lw1uEEyGVuqhQdg.png" /></figure><p>You can test your app by clicking on “Test App,” and when you are done testing your app come back and click on “DONE”.</p><p>Then you will see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2r3A1hdM1YCqfVcWSkF6tw.png" /></figure><p>On the top left corner of the page, you’ll find “Overview” and then click on that to move further.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hw75jXJ3Tf-uMg8b6dVGkQ.png" /></figure><p>After you have clicked on the overview, you will reach the page you can see in the image above. Now you can edit the information for your app. You will reach a point while filling out the details for your app where you have to fill the link to your privacy policy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5frFkzyQjZ-vw6DDflXsdg.png" /></figure><p>At this point be careful — you don’t want to mess with the Privacy of your user since it is the most important issue. If at this moment you want help with your privacy policy, then click “LEARN MORE” in the blue bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*5DVEzpPkOO78fRTK4CPPhQ.png" /></figure><p>When you click on it you will get a Google sheet, and all the information about the privacy policy will be given to you in that sheet. You have to carefully follow the steps given in that sheet.</p><p>When you are done with all the details of your app, then click on the “Save” at the bottom right corner of your page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*uHaal9aAmlVc7jvgD1D0kg.png" /></figure><p>Now go back and click on the “Test Draft” to test your app. When you are done with your final testing, then click on “Submit Draft for Review.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*VccTG0v29jycZuE1UYYGCA.png" /></figure><p>And you are done. Whoa! You have created an application for Google Assistant. You are awesome!</p><p>So this was all for the Google Assistant app. If you are still facing any problems with it, then make sure you contact me. This was my first ever article on Medium! Feel free to tell me your views about this article, and if you think it was helpful for you, then please make sure to clap! Also, make sure to check out my Google Assistant Apps too: 1. <a href="https://assistant.google.com/services/a/uid/0000004882ce65f5?hl=en&amp;source=web">Silly Quizz</a> 2.<a href="https://assistant.google.com/services/a/uid/0000007906e49354?hl=en&amp;source=web">Element Name</a> 3.<a href="https://assistant.google.com/services/a/uid/0000004afb06ddfc?hl=en&amp;source=web">Mr. Names</a>.</p><p>Thank You :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42a490d63822" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/how-to-build-apps-for-google-assistant-with-no-programming-experience-42a490d63822">How to build apps for Google Assistant with no programming experience</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>