<?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 Ran Pelta on Medium]]></title>
        <description><![CDATA[Stories by Ran Pelta on Medium]]></description>
        <link>https://medium.com/@ranpelta?source=rss-d11771749e5d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Eesptb5RvJXCUsji4_kqeQ.jpeg</url>
            <title>Stories by Ran Pelta on Medium</title>
            <link>https://medium.com/@ranpelta?source=rss-d11771749e5d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 02:42:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ranpelta/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[Explain Deep Learning Neural Networks to your grandma]]></title>
            <link>https://medium.com/data-science/explain-deep-learning-neural-networks-to-your-grandma-80b726bf6124?source=rss-d11771749e5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/80b726bf6124</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Ran Pelta]]></dc:creator>
            <pubDate>Sat, 22 Aug 2020 17:09:40 GMT</pubDate>
            <atom:updated>2020-08-23T18:45:12.970Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Explain Deep Learning Neural Networks to Your Grandma</strong></h3><blockquote><em>“You do not really understand something unless you can explain it to your grandmother”</em></blockquote><p>Not sure where this quote originally came from, it is sometimes kind of half-attributed to Albert Einstein.</p><p>Anyway, this post is my attempt of explaining (to myself and others) how the neural networks (NN) algorithm works in a simple, novice, straightforward, and high-level fashion, without any formulas, equations, or codes. I am fully aware that there might be some inaccuracies in the text below, but this is naturally to happen in order to avoid complicated explanations and to just keep things simple, otherwise your grandma won’t understand…</p><p>Before we begin, I would like to acknowledge <a href="https://www.fast.ai/">fast.ai</a>, founded by Jeremy Howard and Rachel Thomas, which is a non-profit research group focused on deep learning and artificial intelligence. I have learnt and I am still learning a lot from their free online courses.</p><p>Oh.. and I wrote a summary of this post at the end in case you want to skip to it.</p><p>OK, I assume that if you read this post you have at least some familiarity with NN, so I’ll save you the introduction about how NN became popular in recent years and how they can help you solve problems.</p><p>So how does the NN algorithm work?</p><p>In one word: math.</p><p>In two words: matrix multiplication.</p><p>If your grandma did not understand this explanation then you can tell her that we start with an input layer, which is the input data for a NN model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/173/1*UUg14vo2xNA1Zc4yO8CCcg.png" /></figure><p>The input layer shape is (<em>1,n)</em>, meaning <em>1</em> row and <em>n</em> columns. We can also call it a tensor, think of a tensor as a container that can house data in N dimensions. A vector is a 1D tensor, a matrix is a 2D tensor.</p><p><strong><em>It is important to pay attention to the shape of the tensors in NN</em></strong></p><p>If our data is tabular, then this input layer represents one row in our table, meaning one observation with <em>n</em> features. Let’s say we would like to use NN to predict the price of a house, then the <em>Xs</em> can be the number of rooms, the location, whether it’s a private house or an apartment in a building, the number of floors, etc. If our data is an image, then the input layer represents a single pixel where the <em>Xs</em> are the bands/channels of that pixel, for example, red, greed, and blue.</p><p>You know what? Let’s make it more concrete, I’ll add to the schematic figure also one with numbers. So now we have this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*Ehk6xxWLPSe--_L4C9eJSQ.png" /></figure><p>As you can see, in the below example <em>n</em>=6. By the way, I just made these numbers, they do not belong to a specific dataset.</p><p>Next, we multiply (the @ sign) our input layer by a matrix, which can be called a weight matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*pKwjAAFjE4QERCHDxvAo9w.png" /></figure><p>For this multiplication to be valid, we need to make sure that the number of columns in the input layer will be equal to the number of rows in the weight matrix. This is the case here, the input layer shape is <em>(1,n)</em> or <em>(1,6)</em> and weight matrix shape is <em>(n,m)</em> or <em>(6,4)</em>. Although we cannot play with the number of rows in the weight matrix (it must be equal to the number of columns in the input layer), we are allowed to set whichever number we like to the number of columns for the weight matrix. Here, for simplicity, I set the number of columns for the weight matrix to be 4. The numbers in the weight matrix are completely random (this is how NN starts). The letter <em>P</em> in the schematic figure was also randomly chosen because I needed to represent numbers schematically, and <em>X</em> was already taken by the input layer.</p><p>The result of that multiplication:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*PQom9ztx1mLChO9MBhmG_w.png" /></figure><p>The result is a tensor with the shape of <em>(1,m)</em> or <em>(1,4)</em>, this is just how matrix multiplication goes… the number of the columns in the first tensor must be the same as the number of rows in the second tensor, and the result is a tensor with the number of rows equal to the number of rows in the first tensor and the number of columns equals to the number of columns in the second tensor.</p><p>In other words: <em>(n,m) @ (m,k) →(n,k)</em></p><p>Now we take this result and pass it through a function called an activation function. There are all kinds of activation functions but here we will use a common one called ReLU. The name is really not important. This function takes the numbers in our result (the blue tensor) and changes the numbers inside according to a very simple rule: if the value is above 0, keep the value, if the value is below 0, change it to 0. This is how it is mathematically written: <em>max(0,X).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jd5pGUJlpY6NMVJnM7nGuQ.png" /></figure><p>Note that the shape did not change. What we just did is called a hidden layer. You can add as many as you want. For example, if we would like to add another hidden layer, we would just repeat what we did before. Meaning, take the (rightmost) blue tensor, multiple it by a weight matrix (with random numbers) in the shape of 4 rows and whichever number of columns you with (remember the shape “rules”), then in the resulted tensor change values lower than 0 to 0, and that’s it. Hope it’s not confusing, but the letters I chose as values in the blue tensors in the schematic figure are again just so you know that the numbers are different.</p><p>Once you added all the hidden layers you want, it is time to converge to final output, meaning the prediction or estimation of the model. For that, we need to know what the shape of our desired output is. If it is a regression problem like predicting the price of a house, then we just need one number — shape <em>(1,1)</em>. From that, we understand what the shape of the tensor we will multiply it by should be. If it is a classification problem, we need a tensor with the shape <em>(m, how_many_categories_we_have)</em>, and in our example, <em>m = 4</em>.</p><p>To keep things simple, assume that we have a regression problem, then:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4CTTNK9yElH7NVtL0NU8g.png" /></figure><p>Again, pay attention to the shape, in order to get our prediction in the desired shape <em>(1,1)</em> we have to multiply our blue tensor <em>(1,4)</em> with another tensor with the shape <em>(m,1)</em>, which here means <em>(4,1)</em>. Now we got our prediction of the model and we use a function called loss function to compare the predicted value versus the true or actual value. This comparison will give us an idea of how good our model is. After we have our first estimation of our model accuracy (via the loss function) we (I mean the NN algorithm) change the numbers in the weight matrices (the yellow ones) so that the model accuracy will be better. We keep repeating this process until we have our desired accuracy.</p><p><strong>How does NN work — summary</strong></p><p>1) We take the input data</p><p>2) We multiply it by a matrix with random numbers</p><p>3) We change all values &lt; 0 to 0, and do not change when value&gt;0</p><p>4) We repeat step 2 and 3 how many times we want</p><p>5) We multiple the results by a final tensor to get the model prediction</p><p>6) We use a function (called loss function) to compare the model prediction with the true value, which will give a value of model accuracy</p><p>7) We change all the numbers in the matrix from step 2 and step 5 so that the accuracy will be a bit better</p><p>8) We repeat all the steps until we are satisfied with the accuracy</p><p>Well, that’s it. I hope this was clear and simple enough. If you understood, or at least you think you understood, there is only one way to verify it. Try to explain it to your grandma, or any random grandma (but with her full consent 😊)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80b726bf6124" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/explain-deep-learning-neural-networks-to-your-grandma-80b726bf6124">Explain Deep Learning Neural Networks to your grandma</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Convert Pandas Dataframe to Keras RNN and Back to Pandas for Multivariate Regression…]]></title>
            <link>https://medium.com/data-science/how-to-convert-pandas-dataframe-to-keras-rnn-and-back-to-pandas-for-multivariate-regression-dcc34c991df9?source=rss-d11771749e5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcc34c991df9</guid>
            <category><![CDATA[time-series-forecasting]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[predictions]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[multivariate-regression]]></category>
            <dc:creator><![CDATA[Ran Pelta]]></dc:creator>
            <pubDate>Sat, 18 Jul 2020 19:17:32 GMT</pubDate>
            <atom:updated>2020-07-23T13:54:24.763Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Convert Pandas Dataframe to Keras RNN and Back to Pandas for Multivariate Regression Problems</h3><h4>This post provides a straightforward Python code that takes data in Pandas dataframe and outputs predictions in the same format using Keras RNN LSTM model.</h4><p>The problem I encountered was rather common (I think): Taking data in a pandas dataframe format and making predictions using a time series regression model with keras RNN where I have more than one independent X (AKA features or predictors) and one dependent y. To be more precise, the problem was not to build the model, rather to convert the data from a pandas dataframe format to a format that an RNN model (in keras) requires and obtaining predictions from the keras model back as a pandas dataframe.</p><p>It felt that wherever I looked for a solution I got explanations on how RNN works or solutions for uni-variate regression problem. Hence, I will try to keep this post as concise and focused as possible. The coding here assumes that you already did all the necessary preprocessing (e.g., data cleansing, feature engineering etc.) and have a ready-for-analysis time series in a pandas dataframe format.</p><p>What you <strong>WILL NOT</strong> find here:</p><p>· Theoretical explanations</p><p>· Nice illustrations of an RNN model</p><p>· Preprocessing techniques</p><p>· A complicated or sophisticated model</p><p>There are many sources online that well explain these issues, I highly recommend to check the <a href="https://stackoverflow.com/">StackOverflow</a> questions and Jason Brownlee’s, <a href="https://machinelearningmastery.com/">Machine Learning Mastery</a> blog post.</p><p>What you <strong>WILL</strong> find here:</p><p>· A straightforward Python code that takes a pandas dataframe and outputs predictions in the same format using a keras RNN LSTM model for multivariate regression problems.</p><p>This post will describe snippets of code with explanations and a full seamless code will be provided at the end.</p><p>Let’s begin:</p><p>Step 1, let’s import all needed packages and check the keras version</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/272bcfc2cfba9c42b9ddf04df6908140/href">https://medium.com/media/272bcfc2cfba9c42b9ddf04df6908140/href</a></iframe><p>As you can see, my keras version is 2.3.1, so if you have some problems with the code I post here, please check that you have the same version or a higher one.</p><p>Step 2, read the data</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fc63e12b95d4f43f619afa84b446303/href">https://medium.com/media/3fc63e12b95d4f43f619afa84b446303/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*PEh9Nj6kQ9CFKnH0H6CcSQ.png" /></figure><p>As you can see, I have 32,128 rows and four columns, with one y<em> </em>and three X. The code here can work on any number of X<em> </em>including just one<em> </em>X<em>. </em>Note that you need to define your y column in order to make things easier and more generic.</p><p>Optional step — plot the data</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7551bad041aca17470b00ec38edba8c/href">https://medium.com/media/d7551bad041aca17470b00ec38edba8c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSIAAp8lciGysVCyO33V3Q.png" /></figure><p>I know, the resolution is not great here but you get the idea of how my data looks like.</p><p>Step 3, split the data to train and test</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7eec20f22257fdbbefd6bb53d1be1817/href">https://medium.com/media/7eec20f22257fdbbefd6bb53d1be1817/href</a></iframe><p>Please note here the comment in line #3. Let’s plot again to see if our split makes sense.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0606e5fbf2eddbc9bd4f61299a286c1/href">https://medium.com/media/c0606e5fbf2eddbc9bd4f61299a286c1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JSn_Eze91JrqXTajxR01cg.png" /></figure><p>Again, don’t mind the resolution, it’s not important. The plot looks good, the past (blue) is our training data and recent dates are our test data (orange).</p><p>Step 4, separate X and y only for the training data. We will handle the test data later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1db71ed2eed65e1715a441212fed8b69/href">https://medium.com/media/1db71ed2eed65e1715a441212fed8b69/href</a></iframe><p>Now, the X_train looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*Wl14htMn2QgwGd8WAZWOlA.png" /></figure><p>and the y_train looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*uGzZFnyGw206R0gCfJoXrA.png" /></figure><p>Step 5, scale and prepare X and y data for keras</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4097d9bec3d26eb5636b13da8b870d67/href">https://medium.com/media/4097d9bec3d26eb5636b13da8b870d67/href</a></iframe><p>This part requires some explanations. Here we convert the data from pandas dataframe to numpy arrays which is required by keras. In line 1–8 we first scale X and y using the sklearn MinMaxScaler model, so that their range will be from 0 to 1. The next lines are some shape manipulation to the y in order to make it applicable for keras. We need the shape of y to be (n, ), where n is the number of rows. Line 12 “pushes” the y data one step forward by adding zero in the first position and line 13 keeps the shape of y by deleting the last time step (last row). Here is a simplified example of what happens in line 12–13:</p><pre>#let&#39;s say y = [1,2,3,4]<br># y = np.insert(y,0,0) --&gt; [0,1,2,3,4]<br># y = np.delete(y,-1) --&gt; [0,1,2,3]</pre><p>If this is explanation is not clear enough, I refer you to Jason Brownlee’<em>s </em>blog post <a href="https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/">Multivariate Time Series Forecasting with LSTMs in Keras</a>. Look for the section with the title: <em>Multivariate Inputs and Dependent Series Example.</em></p><p>To sum up the shape manipulation of y let’s have a quick look on what happened. We started with y data as a dataframe:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*YakHr8WMoWgnPudVHn02Ng.png" /></figure><p>And now it should look like this:</p><pre>array([0.        , 0.12779697, 0.12401905, ..., 0.59237795, 0.6018512 , 0.61132446])</pre><p>Step 6, combine X and y using the keras TimeseriesGenerator</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30c0cb0b6097032cd75303ca1e65b42b/href">https://medium.com/media/30c0cb0b6097032cd75303ca1e65b42b/href</a></iframe><p>The TimeseriesGenerator transforms the separate X and y into a structure of samples ready to train deep learning models. I would recommend to print the shape of the generator object to make sure it worked. The shape should be (batch_size,n_input,n_features)exactly how it shows in step 6 in line 8.</p><p>The hard part of converting our data from pandas dataframe to something ready to use for deep learning models is behind us. Now we can move on to step 7, instantiate the model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ca3b22abfea0445c92bcce1511ce1b5/href">https://medium.com/media/8ca3b22abfea0445c92bcce1511ce1b5/href</a></iframe><p>Note that I used here a very simple model, with only one hidden layer and without a dropout layer. This is because I wanted to keep this post concise and the actual model architecture is not the focus here. But feel free to experiment with more layers.</p><p>Step 8, fit the model and plot the losses</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ba0c63db327671b486700b44575e68e/href">https://medium.com/media/8ba0c63db327671b486700b44575e68e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*KYAe_b7xlj9gRJB52GmB0g.png" /></figure><p>Also here, I used simple settings, only 5 epochs, just to illustrate the entire process.</p><p>Now the model is ready to use and we can make predictions on the test set.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74edc168d583a64f5ac9ab3bf04cd3eb/href">https://medium.com/media/74edc168d583a64f5ac9ab3bf04cd3eb/href</a></iframe><p>The first line generates the X_test data by dropping the y<em> </em>from the test set, we do not want the y<em> </em>data to be included in the X. Then we scale the X_test according to the MinMaxScaler model which was fitted on the X_train earlier. Line 3 is important because we need to create a TimeseriesGenerator for the test data. I was struggling with this part because in the examples that I saw the y_test was included in here , but I do not want the model to have any knowledge whatsoever about the y_test data. I did not want the slightest chance of data leakage that will result in bias predictions. Thanks to a great help I received from Marco Cerliani on <a href="https://stackoverflow.com/questions/62876780/how-to-shape-test-data-in-keras-lstm-prediction-for-multivariate-inputs-and-depe?noredirect=1#comment111192708_62876780">StackOverflow </a>I understood that the second argument in the TimeseriesGenerator, which is the y_test is just a prediction method and that the actual values of the y_test don’t matter (in this specific place), so you can insert a dummy y_test → an array of zeros that has the same shape of the actual y_test data. The rest of the TimeseriesGenerator is similar to the training data, and also here I printed the shape to make sure it’s OK.</p><p>Line 10 calls the predict method and line 11 rescales the predictions. Remember that we earlier scaled the y data between 0 and 1, so we need to scale it back. In line 12 we construct a dataframe from y_true and y_pred. Note that we only call for a subset of y_true (test[y_col].values[n_input:]), this is because the model needs n_input timesteps (rows or observations) to start predict, so it takes these n_input (in this case 25 timesteps) from X_test and only then start to predict. For example, if we had 50 timesteps in our test set (or 50 rows or observations), then we will have only 25 predictions because the first 25 were used by the model according to its architecture that we set.</p><p>Now we have our results in a nice pandas dataframe structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*yWYHSMefXKJg0p879Y6o-Q.png" /></figure><p>And we can plot them using results.plot();:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*fy4048arsFjoedxDtqZpiw.png" /></figure><p>That’s it, we began with data in a pandas dataframe format and finished with predictions in the same format.</p><p>That’s the entire code in one block</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da79d5f324c74dcb60ed438cd2e8e8d/href">https://medium.com/media/4da79d5f324c74dcb60ed438cd2e8e8d/href</a></iframe><p>I hope this is useful and will help you in your machine learning missions. Please write me below if you have any comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcc34c991df9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-convert-pandas-dataframe-to-keras-rnn-and-back-to-pandas-for-multivariate-regression-dcc34c991df9">How to Convert Pandas Dataframe to Keras RNN and Back to Pandas for Multivariate Regression…</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>