<?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: NarayanAdithya</title>
    <description>The latest articles on DEV Community by NarayanAdithya (@narayanadithya).</description>
    <link>https://dev.to/narayanadithya</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%2F954422%2F34097e9c-4d1a-4f2a-8da2-f3510bc534c6.png</url>
      <title>DEV Community: NarayanAdithya</title>
      <link>https://dev.to/narayanadithya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/narayanadithya"/>
    <language>en</language>
    <item>
      <title>Using MindsDB for Time Series Forecasting - Honey Production in the USA</title>
      <dc:creator>NarayanAdithya</dc:creator>
      <pubDate>Tue, 25 Oct 2022 04:45:42 +0000</pubDate>
      <link>https://dev.to/narayanadithya/using-mindsdb-for-time-series-forecasting-honey-production-in-the-usa-4p05</link>
      <guid>https://dev.to/narayanadithya/using-mindsdb-for-time-series-forecasting-honey-production-in-the-usa-4p05</guid>
      <description>&lt;h2&gt;
  
  
  The Pre-Requisites
&lt;/h2&gt;

&lt;p&gt;The very first thing to do is to create your &lt;strong&gt;30-day free&lt;/strong&gt; to use &lt;a href="https://mindsdb.com/" rel="noopener noreferrer"&gt;Mindsdb account&lt;/a&gt;. Once this is done you are all set to explore and understand the MindsDB way of building machine learning models.&lt;/p&gt;

&lt;h2&gt;
  
  
  About MindsDB
&lt;/h2&gt;

&lt;p&gt;MindsDB in simple words is basically machine learning but in the database. The usual machine learning pipeline involves:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loading Data&lt;/li&gt;
&lt;li&gt;Pre-Processing Data&lt;/li&gt;
&lt;li&gt;Fitting Data&lt;/li&gt;
&lt;li&gt;Fine Tuning the Model&lt;/li&gt;
&lt;li&gt;Predicting the Results&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With MindsDB the entire pipeline is shrunk to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Connect to Data&lt;/li&gt;
&lt;li&gt;Describe the Predictor&lt;/li&gt;
&lt;li&gt;Predict The Data&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The entire &lt;strong&gt;ETL Pipeline&lt;/strong&gt; has been encapsulated by MindsDB thus allowing a very &lt;strong&gt;SQL Like, Low Code Paradigm&lt;/strong&gt; for Machine Learning. &lt;/p&gt;

&lt;h2&gt;
  
  
  About the Dataset
&lt;/h2&gt;

&lt;p&gt;For this tutorial we will be using the &lt;a href="https://www.kaggle.com/datasets/jessicali9530/honey-production" rel="noopener noreferrer"&gt;Honey Production in the USA Dataset&lt;/a&gt; from Kaggle. &lt;br&gt;
The dataset consists of honey production values for different states from the year 1998 to 2012. There are several other columns in the dataset like colony count, yield per colony etc. Since the aim of the tutorial is to just forecast the total production of honey statewise we will be only needing the &lt;strong&gt;state, year and totalprod&lt;/strong&gt; columns. We can ignore the rest of the columns.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using MindsDB For TimeSeries
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Uploading the Data
&lt;/h3&gt;

&lt;p&gt;After you have logged in on the MindsDB website, you'll see the MindsDB Editor.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6b1mkk9mayh5acb2qsb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6b1mkk9mayh5acb2qsb.png" alt="MindsDB Editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the  &lt;code&gt;Add Data&lt;/code&gt; button on the navbar. Then Select &lt;code&gt;Files&lt;/code&gt; and proceed to upload the &lt;code&gt;honeyproduction.csv&lt;/code&gt; which you will get after downloading and extracting the zip file from Kaggle. Name the table as &lt;code&gt;honey_prod&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facdxzec7w4dhl17nqtx8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facdxzec7w4dhl17nqtx8.png" alt="Upload File"&gt;&lt;/a&gt;&lt;br&gt;
Click on &lt;code&gt;save and continue&lt;/code&gt;.&lt;br&gt;
We have uploaded the data into the MindsDB database.&lt;/p&gt;
&lt;h3&gt;
  
  
  Playing Around with the Data
&lt;/h3&gt;

&lt;p&gt;We have uploaded the data.&lt;br&gt;
How do we see it ? &lt;br&gt;
How do we know where the data is ? &lt;br&gt;
What is the piece of code you see after uploading the dataset?&lt;/p&gt;

&lt;p&gt;MindsDB stores files uploaded in a files database. Each file uploaded is a tabel. Inroder to see the list of files that you have uploaded you can simply write and execute the following query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SHOW TABLES FROM files;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Now that we have found our table. We can try to query the first 10 rows of our table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM files.honey_prod LIMIT 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Hurrayyy🥳🥳🥳!!!, Our Table exists and now we can move on to the main part of the tutorial....&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the PREDICTOR
&lt;/h3&gt;

&lt;p&gt;In MindsDB creating the model is as simple as running the PREDICTOR command. &lt;/p&gt;

&lt;p&gt;Since we are working on a &lt;strong&gt;TimeSeries&lt;/strong&gt; application we will be using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE PREDICTOR mindsdb.&amp;lt;name-of-model&amp;gt;
FROM files
    (&amp;lt;DATA-ON-WHICH-MODEL-HAS-TO-BE-TRAINED&amp;gt;)
PREDICT &amp;lt;COLUMN-TO-BE-FORECASTED&amp;gt;
ORDER BY &amp;lt;TIME-COLUMN&amp;gt;
GROUP BY &amp;lt;INDEPENDENT_VARIABLE1&amp;gt;
GROUP BY &amp;lt;INDEPENDENT_VARIABLE1&amp;gt;
...

WINDOW &amp;lt;Use Previous x years of data for prediction&amp;gt;
HORIZON &amp;lt;Predict for the next k years&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I believe the not so official syntax that I have given is pretty self-explanatory. But if you feel confused don't worry the actual query will be more clear and even if that doesn't help check out the youtube tutorial link at the end of this article.&lt;/p&gt;

&lt;p&gt;Create Honey Predictor Command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE PREDICTOR mindsdb.honey_prod_predictor
FROM files
  ( SELECT state, totalprod, year FROM honey_prod)
PREDICT totalprod
ORDER BY year
GROUP BY state

WINDOW 12
HORIZON 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In simple words the predictor will be trained using the columns state, totalprod and year based on the past 12(WINDOW) years values and the model will be built for the forecasting of totalprod for the next 4(HORIZON) years.&lt;/p&gt;

&lt;p&gt;Once you execute the query you can check the status of the model using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM predictors;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7kqwfzgptcobi9arxge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7kqwfzgptcobi9arxge.png" alt="Check Predictor Status"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait for the model to finish training.......&lt;/p&gt;

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

&lt;p&gt;And just like that you have created a machine learning model.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time To Forecast
&lt;/h3&gt;

&lt;p&gt;Forecasting is basically done using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT m.year AS Year, m.totalprod AS Forecast
FROM mindsdb.honey_prod_predictor AS m 
JOIN files.honey_prod AS t
WHERE t.year &amp;gt; LATEST 
AND t.state = 'AL' ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The query can seem intimidating at first, but it is quite straightforward. So initially you have the columns you want in the output called off the predictor which is &lt;code&gt;m&lt;/code&gt;. The join is required to fill in the dependent variable &lt;code&gt;state&lt;/code&gt; and the year from which the forecast starts. &lt;code&gt;LATEST&lt;/code&gt; is a keyword that represents the latest year in the dataset. Hence the predictions will be made 2 years from that year which is 2 years from &lt;code&gt;2012&lt;/code&gt; i.e., 2013 and 2014.&lt;/p&gt;

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

&lt;p&gt;We can see the production value that has been forecasted for the years 2013, 2014. To predict for a different state it is as easy as replacing the state value and running the command again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have seen how easy it is to create a machine learning model from scratch using MindsDB. If you are much used to code-along tutorials I would recommend you to checkout my channel &lt;a href="https://www.youtube.com/playlist?list=PLIp0C2zDy-fVtKJ3umVsL_8XpoJQhIuZA" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Check out the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://mindsdb.com/" rel="noopener noreferrer"&gt;MindsDB Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mindsdb/mindsdb" rel="noopener noreferrer"&gt;MindsDB GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interested in being a part of MindsDB? &lt;br&gt;
Join the &lt;a href="https://mindsdb.com/joincommunity?_ga=2.263433930.790547227.1666614251-1540291534.1666362762&amp;amp;_gl=1*u355dy*_ga*MTU0MDI5MTUzNC4xNjY2MzYyNzYy*_ga_7LGFPGV6XV*MTY2NjY3MDMzMS41LjEuMTY2NjY3MzA3Ni4wLjAuMA.." rel="noopener noreferrer"&gt;MindsDB Slack&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mindsdb</category>
      <category>hacktoberfest</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
