<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jhalyl Mason</title>
    <description>The latest articles on DEV Community by Jhalyl Mason (@jhalylm).</description>
    <link>https://dev.to/jhalylm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1466991%2Fdf12a7df-1022-4841-b7b4-b29bd3564db7.png</url>
      <title>DEV Community: Jhalyl Mason</title>
      <link>https://dev.to/jhalylm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhalylm"/>
    <language>en</language>
    <item>
      <title>Machine Learning Models: Linear Regression</title>
      <dc:creator>Jhalyl Mason</dc:creator>
      <pubDate>Fri, 03 May 2024 17:13:25 +0000</pubDate>
      <link>https://dev.to/jhalylm/machine-learning-models-linear-regression-56lp</link>
      <guid>https://dev.to/jhalylm/machine-learning-models-linear-regression-56lp</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Linear Regression&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Linear Regression is the simplest of machine learning algorithms and is usually the first one you will learn in any course or class on the subject. However, it’s simplicity does not deprive it of any power. On the contrary, despite their simple nature they are still renowned for their ability in prediction; according to the International Business Machines Corporation (IBM), “Linear regression is the most commonly used method of predictive analysis”. It is a type of &lt;em&gt;supervised learning&lt;/em&gt; model, meaning it learns from a labeled training set of data, sometimes known as targets, in order to make a prediction. Specifically, linear regression tries to fit a straight line through the given data points in order to best model the relationship between the inputs and the given targets. To do this, it calculates a weighted sum of the inputs plus a &lt;em&gt;bias term.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd42zfkzun3wwqpozutj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd42zfkzun3wwqpozutj.png" alt="Linear Regression Formula" width="628" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The bias term is usually denoted with a θ₀, with the weights for each input denoted as θ₁ through θₙ for n input values. Thus, linear regression can be represented as a linear function ŷ = θ₁x₁+ … + θₙxₙ + θ₀, with ŷ being the prediction. &lt;em&gt;Training&lt;/em&gt; the model means finding the parameters (θ) that best fit the training data. In order to best train the model, we first must find the difference between the predicted value (the model’s output) and the expected value (the actual target value) for a data point of the training set (yᵢ — ŷᵢ). This is called finding the &lt;em&gt;error&lt;/em&gt; for that particular prediction. Finding the error for one specific point isn’t too helpful on it’s own. What’s most important is the total error the model has made, which is known as the &lt;em&gt;cost.&lt;/em&gt; The equation used to find the cost of a given model is known as the &lt;em&gt;cost function.&lt;/em&gt; The cost function associated with a linear regression model is the &lt;em&gt;Mean Squared Error.&lt;/em&gt; In simpler terms, this just means take the average (mean) of all of the errors squared; hence the name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1u67897a6w1js3acux3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1u67897a6w1js3acux3t.png" alt="Mean Squared Error Equation" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this gives the cost of the model, or how far your model is away from the target values. What’s next is to find out how to minimize the result of this equation. Training a linear regression model consists of finding the best values for the weights and biases that give the smallest possible MSE for the training set.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Normal Equation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One way to find the ideal set of parameters for a linear regression model is by using the &lt;em&gt;normal equation&lt;/em&gt;. This is an equation that gives the direct result automatically after computation, in contrast to another method that will be discussed later. The way to compute the normal equation usually starts with arranging all of the features (x) for every data point in the training data into a matrix (&lt;strong&gt;X&lt;/strong&gt;), with each row representing an instance of recorded data. Then create a vector (&lt;strong&gt;y&lt;/strong&gt;) containing all of the target values of the training set. Afterwards, complete the equation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fna5htpx6qgsm1tqf4qff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fna5htpx6qgsm1tqf4qff.png" alt="Normal Equation for Linear Regression" width="779" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To explain exactly what that means, multiply the &lt;em&gt;transpose&lt;/em&gt; of your matrix &lt;strong&gt;X&lt;/strong&gt; by the matrix itself, take the inverse, and multiply that by the product of the transpose of matrix &lt;strong&gt;X&lt;/strong&gt; and vector &lt;strong&gt;y&lt;/strong&gt;. This will give you the optimal value/values for θ to minimize the cost function. This is an effective way for computing the optimal θ values when the number of inputs isn’t that large, however, as the amount of features or instances of data grow this computation becomes slower and less efficient. This brings up the next common way to train this model, and many others as well.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Gradient Descent&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Gradient descent is a common optimization algorithm that is widely used for a vast amount of different machine learning models. The idea behind gradient descent is to iteratively change the parameters of the model in order to minimize the overall cost. To do this, the gradient descent algorithm calculates how much the cost function changes if you change a parameter slightly. It does this by computing the &lt;em&gt;partial derivative&lt;/em&gt; of the cost with respect to the parameter. The best analogy I’ve seen to represent gradient descent was by Luis Serrano in the &lt;em&gt;Math for Machine Learning &amp;amp; Data Science Specialization&lt;/em&gt; hosted by DeepLearning.Ai on Coursera. To summarize: imagine you are in a really hot room and want to get to the coldest spot possible. The way you might go about doing this, is to take a step in any direction and see if it is hotter or colder than where you were before. You would keep doing this until every spot you could go to next is hotter than the spot you are currently at; this is when you have found the coldest spot in the room. This is essentially how gradient descent works, slowly taking steps, the size of which are dictated by the &lt;em&gt;learning rate,&lt;/em&gt; to find the minimum cost function for the model. The amount of steps taken, or &lt;em&gt;iterations&lt;/em&gt; of the training algorithm, are known as &lt;em&gt;epochs&lt;/em&gt;. In order to implement gradient descent, you would compute the partial derivative of the cost function with regards to each parameter, using the equation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9g2egqgqd6jornguw3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9g2egqgqd6jornguw3b.png" alt="Partial Derivative of Cost Function" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that &lt;strong&gt;θ&lt;/strong&gt;ᵀ&lt;strong&gt;x&lt;/strong&gt; is another way of representing the prediction ŷ that is commonly used. It is just expressing the prediction as the product of the transpose of the parameter matrix &lt;strong&gt;θ&lt;/strong&gt; and the feature vector &lt;strong&gt;x&lt;/strong&gt;. Now instead of computing these partial derivatives individually, a common method is to use &lt;em&gt;batch gradient descent.&lt;/em&gt; This method of gradient descent calculates the derivatives over the whole training set at each step. This involves creating a vector of gradients:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fflm01wpo4qvag94vmv9j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fflm01wpo4qvag94vmv9j.png" alt="Gradient Vector" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have the gradient vector, you begin the steps to use the gradient vector to step in the correct direction. This is when the learning rate comes into play. The step algorithm involves subtracting your parameter vector θ by the product of the gradient vector and the learning rate. This is how the learning rate influences the size of the steps you take away from the gradient. The equation looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqf813l37ni3x2y2tsxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqf813l37ni3x2y2tsxf.png" alt="Gradient Descent Step Algorithm" width="744" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The η represents the learning rate. The size of the learning rate is important; too large and you may continuously jump over the lowest point, but too small and it may take forever to converge on it. However, since the MSE is a convex function, the algorithm is guaranteed to get close to the &lt;em&gt;global minima&lt;/em&gt; (lowest point) with a small learning rate as long as you wait long enough (and run through enough epochs). Thus it is usually safer to go with a smaller learning rate to start with and experiment from there on what works best.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code Implementation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In order to fully demonstrate how/when to implement and train a linear regression model, I will go through the steps of a regression/prediction task. The dataset and project itself come from the dataset published in the book &lt;em&gt;Hands On Machine Learning with SciKit-Learn and TensorFlow&lt;/em&gt; by Aurelien Geron, however all code presented here is typed and published solely by me. &lt;em&gt;The author has expressed his intent to keep the code for the datasets and projects available open-source through his github, which I will have linked in the reference list at the bottom of this text.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This project is assuming you want to find out if money correlates to happiness. In order to find out, you collect data on the life satisfaction of certain countries along with their GDP (gross domestic product). Your goal is to find if their is some correlation, and if so, to create a model that can predict someone’s expected happiness based off of their country’s GDP.&lt;/p&gt;

&lt;p&gt;We will start by importing the necessary libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.linear_model&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LinearRegression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we go through the steps of downloading and preprocessing the data. This includes opening the data and determining our features/targets. Since we want to be able to determine happiness given money, it stands to reason that our features would be the GDP of the country and our targets will be the life satisfaction of that country.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://github.com/ageron/data/raw/main/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;life_satisfaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lifesat/lifesat.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;life_satisfaction&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GDP per capita (USD)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]].&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;life_satisfaction&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Life satisfaction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]].&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will then go through the steps of visualizing our data; an important step before model selection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;life_satisfaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;scatter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                       &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GDP per capita (USD)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Life satisfaction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;axis&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;23_500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62_500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6rhsqnem7ntoo1ngn5j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6rhsqnem7ntoo1ngn5j.png" alt="Scatterplot of Data" width="688" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While it doesn’t look like a complete straight line, their definitely looks like a linear correlation between the GDP and life satisfaction of the given countries. Therefore a linear regression model will due well to make predictions. So now that we know which model we want to use, let’s train (or fit) the model to the training set. We’ll use the normal equation first:&lt;/p&gt;

&lt;p&gt;Normal Equation Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;add_dummy_feature&lt;/span&gt;

&lt;span class="n"&gt;X_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_dummy_feature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;best_theta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linalg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="n"&gt;X_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="n"&gt;X_b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a code implementation of the same normal equation explained above, with the output being mapped to a variable called “best_theta”. Although knowing the actual numbers themselves isn’t all too helpful, let’s look at what the normal equation says anyways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;best_theta&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmijidt0l0sdcpb22m31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwmijidt0l0sdcpb22m31.png" alt="Best Parameter Values" width="293" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now that we have the optimal parameter values, we are able to start making predictions. So given Cyprus’ GDP of 37,655.2; what would the model predict their life satisfaction to be?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;X_new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mf"&gt;37_655.2&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;X_new_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_dummy_feature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_new&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;y_prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X_new_b&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="n"&gt;best_theta&lt;/span&gt;
&lt;span class="n"&gt;y_prediction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawiqdy6ntdh91mmrpw2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawiqdy6ntdh91mmrpw2f.png" alt="Prediction for Cyprus" width="281" height="41"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So it’s looking like our model expects Cyprus to have a life satisfaction rating of about 6.3. Now let’s look at a visualization of the prediction line that the normal equation has fit to our data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;life_satisfaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;scatter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                       &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GDP per capita (USD)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Life satisfaction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;best_theta&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;best_theta&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;axis&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;23_500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62_500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkc7z9fpy8ph9t2xmgm78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkc7z9fpy8ph9t2xmgm78.png" alt="Prediction Plot" width="786" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that is pretty much all there is to linear regression with the normal equation. Now let’s look solving the same problem with gradient descent.&lt;/p&gt;

&lt;p&gt;Gradient Descent Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; 
&lt;span class="n"&gt;epochs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;theta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epochs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;gradients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;X_b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_b&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt; &lt;span class="n"&gt;theta&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;theta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;theta&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;gradients&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This randomizes the initial values of our parameter vector, implements the cost function, and goes through the steps for the amount of epochs we had set. Now let’s see what parameters the algorithm returns this time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cbt0que9dzkme7k1b2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cbt0que9dzkme7k1b2z.png" alt="Gradient Descent Optimal Parameters" width="308" height="79"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We got the same thing we did as with the normal equation. Remember, gradient descent is useful for many models, not just linear regression. Now there’s actually one more way to implement and train this model, and it’s the easiest.&lt;/p&gt;

&lt;p&gt;SciKit Learn Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LinearRegression&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intercept_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coef_&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlv3s0m0qlb9fsfj14yv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlv3s0m0qlb9fsfj14yv.png" alt="SciKit Learn Optimal Parameters" width="605" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The SciKit Learn library already has a built in linear regression model. Using the “.fit” function, we can train the model in one line of code. Using this way, you can simplify the previous other methods down to just three lines of code. Easier is not always better however, and although it is useful to have most of the details abstracted away, it is always important to understand what is going on beneath the simplified code. With that being said, this is usually the most common way you will see a linear regression model implemented and trained, as most people won’t have the need to code a model from the ground up from scratch. However, if you ever did need to, now you know how.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reference List&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;IBM&lt;/em&gt; — &lt;a href="https://www.ibm.com/docs/en/db2oc?topic=procedures-linear-regression"&gt;https://www.ibm.com/docs/en/db2oc?topic=procedures-linear-regression&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hands On Machine Learning 3rd Edition Github —&lt;/em&gt; &lt;a href="https://github.com/ageron/handson-ml3/tree/main"&gt;https://github.com/ageron/handson-ml3/tree/main&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>computerscience</category>
      <category>deeplearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>Architecture of Neural Networks</title>
      <dc:creator>Jhalyl Mason</dc:creator>
      <pubDate>Fri, 03 May 2024 16:57:44 +0000</pubDate>
      <link>https://dev.to/jhalylm/architecture-of-neural-networks-k1m</link>
      <guid>https://dev.to/jhalylm/architecture-of-neural-networks-k1m</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Introduction to Neural Networks&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is a Neural Network?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Neural networks are the fundamental machine learning algorithm responsible for spawning the field of deep learning. According to the International Business Machines Corporation (IBM), &lt;strong&gt;&lt;em&gt;“A neural network is a machine learning program, or model, that makes decisions in a manner similar to the human brain, by using processes that mimic the way biological neurons work&lt;/em&gt;”&lt;/strong&gt;. Sometimes referred to as &lt;em&gt;Artificial Neural Networks&lt;/em&gt; (ANNs) to differentiate them from their biological influence, neural networks have become extremely popular for machine learning due to their versatility and ability to handle large and especially complex tasks.&lt;/p&gt;

&lt;p&gt;While other algorithms are very useful for simple tasks, such as linear regression for price/cost prediction and support vector machines for binary classification, ANNs have paved the way for some of the largest and most impressive accomplishments in machine learning and Ai as a whole. These include: image classification like for Google Images, speech recognition like Apple’s Siri, and recommendation tasks like for videos on YouTube. The creation and widespread acceptance of neural networks has truly changed the field, and the world as a whole, and has helped shape what things we deem computationally plausible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Biological Neurons&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As can be extrapolated by their namesake, artificial neural networks are modeled after the neurons in the brains of animals, such as humans. Neurons are nerve cells that, according to the National Institute of Neurological Disorders and Stroke (NINDS) “&lt;strong&gt;&lt;em&gt;allow you to do everything from breathing to talking, eating, walking, and thinking&lt;/em&gt;&lt;/strong&gt;”. Each neuron has a long extension called an &lt;em&gt;axon&lt;/em&gt; which branches off into tips that have what are known as synaptic terminals, or &lt;em&gt;synapses&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;These synapses are what connects to other neurons and allows them to exchange information. Neurons produce electrical impulses which travel down their axons and to the synapses, causing them to release chemicals called &lt;em&gt;neurotransmitters&lt;/em&gt; to the other neurons. When a neuron receives enough neurotransmitters within a short span; it will either fire it’s own, or stop firing, depending on the type of neurotransmitter. This small action is the essential basis behind brain activity and the process that artificial neural networks intend to mimic.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;From Biological to Artificial&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Artificial Neuron&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idea behind ANNs has been around for a multitude of years. They were first introduced by neuropsychiatrist Warren McCulloch and mathematician Walter Pitts in their landmark paper “&lt;em&gt;A Logical Calculus Of The Ideas Immanent In Nervous Activity&lt;/em&gt;”, published in 1943. In the paper, they introduce the idea of a simple computational model that can mimic the function of neurons using propositional logic (true or false). The model of the neuron they created was comprised of one or more binary (on/off) inputs and one binary output. This paper was instrumental in demonstrating that, even with these relatively simple neurons, it was possible to create a network capable of computing any logical proposition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The TLU&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Building off of the early artificial neuron, the &lt;em&gt;threshold logic unit,&lt;/em&gt; or TLU, was the next big step for ANNs. The TLU differs from McCulloch and Pitts’ original model in that it’s inputs and output are numbers instead of just binary on/off signals. This model associates values, known as &lt;em&gt;weights,&lt;/em&gt; to each of it’s input values. It then calculates a linear function of it’s inputs and their weights, along with a bias term, and applies what’s known as a &lt;em&gt;step function&lt;/em&gt; to the result. This step function introduces a threshold to the output of the function, making it positive if above the threshold and negative if below. A single TLU can perform simple binary classification tasks, however they become more useful when stacked together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Perceptron&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Created by psychologist Frank Rosenblatt in 1957; the perceptron is comprised of one or more TLUs stacked in a layer, with each input connected to each unit. These layers are known as fully connected (or &lt;em&gt;dense)&lt;/em&gt; layers with the layer of inputs taking the name &lt;em&gt;input layer.&lt;/em&gt; A perceptron with just two inputs and three units can simultaneously classify instances of data into three different binary classes, making it useful for multilabel classification. It also became useful for multiclass classification for the same reason.&lt;/p&gt;

&lt;p&gt;Another benefit the perceptron had was the ability to adjust the weights, or &lt;em&gt;train,&lt;/em&gt; the model. In order to train it, the perceptron would be fed multiple training samples with each output being recorded. After each sample, the weights are adjusted to minimize the value between the output and the desired output. This allowed the model to get better, or &lt;em&gt;learn,&lt;/em&gt; from each instance it was trained on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Multilayer Perceptron&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One step up from the perceptron is the &lt;em&gt;multilayer perceptron&lt;/em&gt;, or MLP. An MLP is comprised of an input layer, multiple TLU layers in the center (called &lt;em&gt;hidden layers),&lt;/em&gt; and one more layer of units called the output layer. Neural networks with two or more hidden layers are known as &lt;em&gt;deep neural networks,&lt;/em&gt; and the study of deep neural networks became known as d*eep learning.* These MLPs were found to do increasingly well at complex tasks. They could still do things such as binary classification and regression, but they also showed promise in more difficult jobs such as image classification. Over time, researchers were able to modify and adapt these deep neural networks for a plethora of different functions, including: speech recognition, sentiment analysis, and image recognition.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Common Types of Neural Networks&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Feedforward Neural Networks&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Feedforward Neural Networks are some of the most simple types of ANNs. They get their name from the fact that the data that is input into the model goes only one direction: forward. That is to say that the data comes from the input layer, is transferred through it’s hidden layers, and is then fed through the output layer. Every neuron in one layer is connected to every neuron in the next, and none of the perceptron are connected to any others in the same layer. These networks are the foundation for more complex and specialized networks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Convolutional Neural Networks&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modeled after the visual cortex region of the brain, Convolutional Neural Networks, or CNNs, are networks specialized for image and audio inputs. They work by using a layer, known as the &lt;em&gt;convolutional layer,&lt;/em&gt; to detect important features within image or audio files. The data is then fed through a &lt;em&gt;pooling layer,&lt;/em&gt; which reduces the dimensions of the data, helping reduce complexity and increase efficiency. The data is then pushed through a fully connected layer, similar to a normal feedforward network. Convolutional neural networks are the backbone of &lt;em&gt;Computer Vision,&lt;/em&gt; the field of Ai dedicated to enabling computers to derive information from digital images and videos. Computer Vision is used in many industries such as: radiology- allowing doctors to better and more efficiently identify cancerous tumors; security- allowing cameras to identify and mark possibly threats; and the automotive industry- aiding the detection in systems such as lane detection and even self driving capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Recurrent Neural Networks&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recurrent Neural Networks, or RNNs, are networks that use sequential or time series data. They are most popular for their use in speech recognition and &lt;em&gt;natural language processing&lt;/em&gt; (NLP). They differ from other neural networks in that they have “memory”, they take information from prior inputs to influence the next output. This step is necessary for tasks like natural language processing, as the position of each character in a sentence is important in determining the purpose or sentiment of the sentence. Some of the most popular uses of RNNs are things like Siri for the iPhone, voice search, and Google translate.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Further Reading&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Hands-On Machine Learning with Scikit-Learn, Keras, &amp;amp; TensorFlow — Aurelien Geron&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;The Hundred Page Machine Learning Book — Andriy Burkov&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Deep Learning — Ian Goodfellow&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Machine Learning: A Probabilistic Perspective — Kevin P. Murphy&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;References&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;IBM&lt;/strong&gt;- &lt;a href="https://www.ibm.com/topics/neural-networks"&gt;https://www.ibm.com/topics/neural-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ibm.com/topics/recurrent-neural-networks"&gt;https://www.ibm.com/topics/recurrent-neural-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ibm.com/topics/convolutional-neural-networks"&gt;https://www.ibm.com/topics/convolutional-neural-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NINDS&lt;/strong&gt;- &lt;a href="https://www.ninds.nih.gov/health-information/public-education/brain-basics/brain-basics-life-and-death-neuron#:~:text=Neurons%20are%20nerve%20cells%20that,were%20ever%20going%20to%20have"&gt;https://www.ninds.nih.gov/health-information/public-education/brain-basics/brain-basics-life-and-death-neuron#:~:text=Neurons%20are%20nerve%20cells%20that,were%20ever%20going%20to%20have&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;McCulloch &amp;amp; Pitts&lt;/strong&gt; — &lt;a href="https://www.cs.cmu.edu/~./epxing/Class/10715/reading/McCulloch.and.Pitts.pdf"&gt;https://www.cs.cmu.edu/~./epxing/Class/10715/reading/McCulloch.and.Pitts.pdf&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
      <category>datascience</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>History of Math &amp; Machine Learning</title>
      <dc:creator>Jhalyl Mason</dc:creator>
      <pubDate>Fri, 03 May 2024 16:48:40 +0000</pubDate>
      <link>https://dev.to/jhalylm/history-of-math-machine-learning-23c8</link>
      <guid>https://dev.to/jhalylm/history-of-math-machine-learning-23c8</guid>
      <description>&lt;p&gt;With the increase in popularity of Ai and machine learning, more and more people are looking to break into the field.&lt;/p&gt;

&lt;p&gt;A quick look at the Google Trends stats for the keyword “machine learning” shows the interest in the term has quadrupled since March of 2016 (from a popularity score of 20 to a peak of 93 in March of 2024, at the time of writing).&lt;/p&gt;

&lt;p&gt;With this, there has been a recent discourse amongst people wanting to get into Ai/ML. With the tools necessary to build your own neural networks being so easily accessible and the multitude of premade models already out for use, do you even need to still learn the math to get into Machine Learning?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Background&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we dive into the arguments on both sides and I give my opinion on the topic, it’s important that we first get a background on what led us here.&lt;/p&gt;

&lt;p&gt;The creation of Artificial Intelligence is usually dated back to 1950. This was the year &lt;strong&gt;Alan Turing&lt;/strong&gt; published his paper “&lt;strong&gt;&lt;em&gt;Computing Machinery and Intelligence&lt;/em&gt;&lt;/strong&gt;”, all about the theory of building intelligent machines and how we could measure or test said intelligence. However at the time theory was far ahead of technology, so although &lt;strong&gt;Turing helped popularize the thought process&lt;/strong&gt;, his impact was stunted there.&lt;/p&gt;

&lt;p&gt;A few years later in 1956, a man named &lt;strong&gt;John McCarthy&lt;/strong&gt; would be the next to propel the field forward. McCarthy, &lt;strong&gt;who is also credited with actually coining the term “Artificial Intelligence”&lt;/strong&gt;, hosted a conference with Marvin Minsky at Dartmouth College in 1956 called “&lt;strong&gt;&lt;em&gt;Dartmouth Summer Research Project on Artificial Intelligence&lt;/em&gt;&lt;/strong&gt; (DSRPAI)”. McCarthy brought together researchers from multiple different fields to discuss the possibility and techniques necessary to make Artificial Intelligence happen. Although the conference didn’t exactly go to plan (it was reported that only six people there, including McCarthy and Minsky, stayed consistently present), &lt;strong&gt;it was integral in starting the field as we know it today&lt;/strong&gt;. However, like with Turing, the technology still wasn’t fast, accessible, or capable enough of turning theory into practice at that time. That would not stay the case for long.&lt;/p&gt;

&lt;p&gt;Nine years later in 1965, &lt;strong&gt;Gordon Moore&lt;/strong&gt;, co-founder of Intel, had noticed that the number of transistors on an integrated circuit they were manufacturing had increased by a factor of two in the previous 5 years. He later posited that at the rate they were at, the number of components on a single chip would double every two years. This came to be known as &lt;strong&gt;Moore’s Law&lt;/strong&gt; and would have massive effects on computing and society as a whole; but Ai especially. The exponential growth in computing power helped push the ML boom that occurred around the time. By 1970, Marvin Minsky was quoted as telling TIME Magazine “&lt;strong&gt;from three to eight years we will have a machine with the general intelligence of an average human being.&lt;/strong&gt;”&lt;/p&gt;

&lt;p&gt;There were several setbacks and rollercoasters the field has went on since which helped prove Minsky wrong: Ai winters, defunding of projects, lack of access to data, etc. However all of those events laid the foundation for the modern space of Ai/ML today. Skipping over multiple years and achievements later, we come to the surge in 2016 mentioned earlier. Google’s DeepMind team had created an Ai that successfully beat some of the best human players at Go, a feat so difficult people still questioned if it would be possible even with the release of Siri in iPhones 5 years earlier. This helped push the idea that &lt;strong&gt;not only were intelligent Ai models possible, they were here&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What’s Math Got to Do With It?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So we know what led us to where we are now, and it sounds like a lot to do with computers. So where does math come in? &lt;strong&gt;It had been there from the beginning&lt;/strong&gt;. Computer Science itself wasn’t it’s own discipline until the early 1960s. Even when it was created, it was considered the intersection between math and electrical engineering. &lt;strong&gt;Many of the important names we mentioned earlier were all mathematicians by degree&lt;/strong&gt;. McCarthy was a mathematics professor at Dartmouth when he organized the summer conference with Minsky, who had received his PhD in mathematics from Princeton 2 years prior. Alan Turing graduated from King’s College with a mathematics degree in 1934 before publishing “&lt;strong&gt;&lt;em&gt;Computing Machinery and Intelligence&lt;/em&gt;&lt;/strong&gt;” 2 years later. Math has always been an integral part in the study of computing and ML as an extension. In fact, the majority of what happens in Machine learning is all about a computer being fed data and using math to optimize that data around a certain metric. Just like with the rest of computer science, when you peel back the curtain of programming it’s all math.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Current Discourse&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So if it all started with math, and still consists of math, where’s the discourse come in? Well Ai has come a long way since it’s inception. Back in the early days of Ai, and even up until recently as Ai isn’t that old, it was assumed &lt;strong&gt;you needed a PhD to do anything regarding the field&lt;/strong&gt;. The complex nature of the science and the fact that many of the biggest names in it were all PhDs helped push that narrative. Due to this, many stayed away from studying or pursuing ML unless they were already in a highly quantitative degree like computer science, math, or physics. Due to the nature of having a higher level of math requirements for these degrees, math was just accepted as a necessity to get into ML. &lt;strong&gt;All that changed with this most recent boom in Ai.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With chatGPT helping bring Ai to the mainstream and Tesla showing it’s applications on self-driving cars, &lt;strong&gt;Ai has become the biggest buzzword&lt;/strong&gt; again and &lt;strong&gt;everybody wants in&lt;/strong&gt;. This, along with the advancements being made helping make ML more accessible for the common man, caused people to see Ai as a hugely lucrative opportunity. However, with this influx of people from different and less quantitative fields, those who may not have had to take Calculus II or Statistics in college, or perhaps just don’t remember or don’t care to. And these people had a good question: &lt;strong&gt;If there’s multiple different types of models already made, polished, and available on the internet for whatever I may need, why learn the math behind them?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To their credit, there is a case to be made there. The math behind Ai is used to make the algorithms, right? So &lt;strong&gt;if the algorithm is already premade then hasn’t the necessity for math been abstracted away?&lt;/strong&gt; And these aren’t just people from outside the field saying this either. Many data analysts and even ML engineers are saying that tough math is no longer needed to break into the field, citing the fact that &lt;strong&gt;the majority of the models used in production are premade models&lt;/strong&gt; and that focusing on data cleaning and prepping would be a better use of time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Case for Math&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So I just solved the problem right there, most the models are premade so the math is a thing of the past. Right?&lt;/p&gt;

&lt;p&gt;So here’s where I state my stance as well as make the case for learning the math first. As previously stated, &lt;strong&gt;math has always been a huge part in all of computer science, *especially Ai&lt;/strong&gt;*. The math is necessary in understanding what the algorithm for the model you’re using is, what it does, and how it does it. Now, it is 100% true that nowadays you can pickup a model online or load a GitHub repository and have an algorithm up and running without having to so much as know how it works. And oftentimes you can get by with most tasks doing just that. However, what happens when you want to change the algorithm to better fit your use? Or if it seems to break under certain use cases?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the math is important to knowing what is going on behind the scenes in an algorithm.&lt;/strong&gt; Taking the time out to get at least a good foundation in the math behind ML can make a huge difference in your ability to use Ai, when to use it, and how to use it. It will give you a better intuition on which models work best in which conditions, and how to tune and track your model accurately. &lt;strong&gt;Overall, the math is a crucial part in actually knowing what’s going on in Machine Learning.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So Where Do I Start?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So now that we’ve established why math is crucial for Machine Learning, how do you learn the math necessary? &lt;strong&gt;Most of the math necessary you can pick up in an average college degree&lt;/strong&gt;. It’s pretty much all just linear algebra, calculus, and statistics. What if you didn’t go to college, or don’t remember? &lt;strong&gt;Luckily, the internet is full of resources, both free and paid, to help you catch up and fill the gaps in your knowledge.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/u/2df6dfeb5a48?source=post_page-----935f3a3929ee--------------------------------"&gt;Abhishek (Adam) Divekar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;wrote an article with recommendations that I advocate you take a look at &lt;a href="https://medium.com/@ardivekar/high-quality-math-resources-that-helped-me-become-an-amazon-ml-scientist-82f7164500b2"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I also recommend the &lt;a href="https://www.coursera.org/specializations/mathematics-for-machine-learning-and-data-science?utm_campaign=websitecourses-m4ml-navbutton&amp;amp;utm_medium=institutions&amp;amp;utm_source=deeplearning-ai"&gt;&lt;strong&gt;&lt;em&gt;Mathematics for Machine Learning and Data Science&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; specialization by Andrew Ng hosted on Coursera. The course breaks the math up into 3 categories: Linear Algebra, Calculus, and Probability &amp;amp; Statistics. I will also be doing a summary/review of each section in the course separately here on Medium for if you want to see what each section is about or just want a reference guide to freshen up or come back to.&lt;/p&gt;

&lt;p&gt;Now that you know all about the history of math and Machine Learning, you know about the discourse, and have the resources to fill in the blanks, the only thing left is to get started studying. &lt;strong&gt;Enjoy the process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reference List&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://trends.google.com/trends/explore?date=all&amp;amp;geo=US&amp;amp;q=%2Fm%2F01hyh_&amp;amp;hl=en"&gt;https://trends.google.com/trends/explore?date=all&amp;amp;geo=US&amp;amp;q=%2Fm%2F01hyh_&amp;amp;hl=en&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.britannica.com/science/computer-science"&gt;https://www.britannica.com/science/computer-science&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.alumni.cam.ac.uk/news/cambridge-alumnus-alan-turing-to-be-the-face-of-new-%C2%A350-note-1#:~:text=Turing%20studied%20mathematics%20at%20King's,a%20first%2Dclass%20honours%20degree"&gt;https://www.alumni.cam.ac.uk/news/cambridge-alumnus-alan-turing-to-be-the-face-of-new-%C2%A350-note-1#:~:text=Turing%20studied%20mathematics%20at%20King's,a%20first%2Dclass%20honours%20degree&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://home.dartmouth.edu/about/artificial-intelligence-ai-coined-dartmouth?source=post_page-----935f3a3929ee--------------------------------"&gt;https://home.dartmouth.edu/about/artificial-intelligence-ai-coined-dartmouth?source=post_page-----935f3a3929ee--------------------------------&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ai100.stanford.edu/2016-report"&gt;https://ai100.stanford.edu/2016-report&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.investopedia.com/terms/m/mooreslaw.asp"&gt;https://www.investopedia.com/terms/m/mooreslaw.asp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bjc.edc.org/bjc-r/cur/programming/6-computers/2-history-impact/2-moore.html?topic=nyc_bjc%2F6-how-computers-work.topic&amp;amp;course=bjc4nyc.html&amp;amp;source=post_page-----935f3a3929ee--------------------------------"&gt;https://bjc.edc.org/bjc-r/cur/programming/6-computers/2-history-impact/2-moore.html?topic=nyc_bjc%2F6-how-computers-work.topic&amp;amp;course=bjc4nyc.html&amp;amp;source=post_page-----935f3a3929ee--------------------------------&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.theguardian.com/technology/2016/dec/28/2016-the-year-ai-came-of-age?source=post_page-----935f3a3929ee--------------------------------"&gt;https://www.theguardian.com/technology/2016/dec/28/2016-the-year-ai-came-of-age?source=post_page-----935f3a3929ee--------------------------------&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/AlphaGo?source=post_page-----935f3a3929ee--------------------------------"&gt;https://en.wikipedia.org/wiki/AlphaGo?source=post_page-----935f3a3929ee--------------------------------&lt;/a&gt;&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>math</category>
    </item>
  </channel>
</rss>
