<?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>Forem: Yaswanth Teja</title>
    <description>The latest articles on Forem by Yaswanth Teja (@yaswanthteja).</description>
    <link>https://forem.com/yaswanthteja</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%2F794385%2Fbb58886e-d526-4f35-aaf0-f425ee6b5b54.png</url>
      <title>Forem: Yaswanth Teja</title>
      <link>https://forem.com/yaswanthteja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yaswanthteja"/>
    <language>en</language>
    <item>
      <title>Predicting Stock Prices with AI: A Simple Guide to Using LSTM for Nifty50 Forecasting</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sat, 31 May 2025 07:18:32 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/predicting-stock-prices-with-ai-a-simple-guide-to-using-lstm-for-nifty50-forecasting-p9p</link>
      <guid>https://forem.com/yaswanthteja/predicting-stock-prices-with-ai-a-simple-guide-to-using-lstm-for-nifty50-forecasting-p9p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered if computers can predict stock prices? Well, they can—not perfectly, but they can learn from past data to make educated guesses about future prices. In this blog, we’ll explore how we can use a special kind of artificial intelligence (AI) called Long Short-Term Memory (LSTM) to predict the future prices of the Nifty50, India’s leading stock market index.&lt;/p&gt;

&lt;p&gt;Don’t worry if you’re not a tech expert—we’ll explain everything in simple terms, just like teaching a friend!&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Stock Prediction Work?
&lt;/h2&gt;

&lt;p&gt;Imagine you’re trying to predict the weather. You’d look at past weather patterns—like temperature, humidity, and rainfall—to guess if it’ll rain tomorrow. Similarly, stock prediction works by analyzing past stock prices to forecast future trends.&lt;/p&gt;

&lt;p&gt;Here’s how we do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Get Historical Data – We download years of Nifty50 stock prices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clean the Data – Remove errors or missing values (like a teacher correcting a messy notebook).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Train the AI Model – Teach the computer to recognize patterns in stock prices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make Predictions – Ask the AI to predict future prices based on what it learned.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let’s dive deeper into each step!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Getting the Data
&lt;/h2&gt;

&lt;p&gt;We use a library called yfinance (Yahoo Finance) to download Nifty50 stock prices from 2005 to 2025. This gives us a big table with daily prices—like a giant Excel sheet with dates and closing prices.&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;yfinance&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;yf&lt;/span&gt;

&lt;span class="n"&gt;start_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2005-05-16&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;end_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2025-05-15&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;nifty_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;yf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;^NSEI&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;start_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;end_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of this as downloading a history book of stock prices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Cleaning the Data
&lt;/h2&gt;

&lt;p&gt;Sometimes, data has missing or incorrect entries (like a torn page in a book). We fix this by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sorting dates correctly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Removing or filling missing values.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Missing values in the dataset: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isnull&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;nifty_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort_index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures our AI learns from clean, organized data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Scaling the Data (Making Numbers Easier to Work With)
&lt;/h2&gt;

&lt;p&gt;Stock prices can be huge (like ₹20,000), but AI works better with smaller numbers (between 0 and 1). We use MinMaxScaler to shrink the numbers while keeping their relationships intact.&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;MinMaxScaler&lt;/span&gt;

&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MinMaxScaler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature_range&lt;/span&gt;&lt;span class="o"&gt;=&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;scaled_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nifty_close&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# nifty_close = Closing prices
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is like converting kilometers into meters—same distance, just easier to handle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Preparing the Data for AI Learning
&lt;/h2&gt;

&lt;p&gt;AI learns from sequences. Imagine teaching a child to predict the next number in this sequence:&lt;/p&gt;

&lt;p&gt;1, 2, 3, …? (Answer: 4)&lt;/p&gt;

&lt;p&gt;Similarly, we give the AI sequences of 60 days’ stock prices and ask it to predict the 61st day.&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&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;i&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="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;time_step&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="c1"&gt;# 60 days of data
&lt;/span&gt;        &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;time_step&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="c1"&gt;# Next day's price
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&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;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&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;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, the AI learns patterns like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If prices rise for 10 days, will they fall soon?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do big drops usually recover?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5: Building the AI (LSTM Model)
&lt;/h2&gt;

&lt;p&gt;Our AI is an LSTM (Long Short-Term Memory) network — a type of neural network great at learning sequences (like stock prices over time).&lt;/p&gt;

&lt;p&gt;We build it like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First Layer (50 neurons) – Learns basic patterns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second Layer (50 neurons) – Learns deeper trends.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Third Layer (50 neurons) – Makes final predictions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dropout Layers – Prevents overfitting (like a student who memorizes answers instead of learning concepts).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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;Sequential&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_sequences&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;input_shape&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time_step&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="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_sequences&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;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dense&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Final prediction
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then train the model using past data, just like a student practicing with old exam papers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Making Predictions
&lt;/h2&gt;

&lt;p&gt;After training, the AI can predict future prices. We test it on unseen data (like a surprise test) to see how well it performs.&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;train_predict&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_predict&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We measure accuracy using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;RMSE (Root Mean Square Error) – How far predictions are from real prices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;R² Score – How well the model explains price movements (0% = random guess, 100% = perfect prediction).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;train_rmse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;test_r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;r2_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the AI gets ~90% accuracy, it’s doing well!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Predicting Future Prices
&lt;/h2&gt;

&lt;p&gt;Finally, we ask the AI: "What will Nifty50 prices be in the next 30 days?"&lt;/p&gt;

&lt;p&gt;We feed it the last 60 days’ data and let it predict day by day.&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;last_60_days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaled_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;span class="n"&gt;future_predictions&lt;/span&gt; &lt;span class="o"&gt;=&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;_&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="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;X_future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last_60_days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&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="n"&gt;time_step&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="n"&gt;future_pred&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_future&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;last_60_days&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_60_days&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="n"&gt;future_pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;future_predictions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future_pred&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then plot the predictions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjlh6368njf4lqofdiuw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjlh6368njf4lqofdiuw9.png" alt=" " width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The red line shows what the AI thinks will happen!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, the sample size is limited to get better accuracy. We need to train them with larger data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion: Can AI Really Predict Stocks?
&lt;/h2&gt;

&lt;p&gt;Yes — but with limitations.&lt;/p&gt;

&lt;p&gt;✅ Good at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Finding patterns in historical data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Making short-term predictions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❌ Not perfect at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Predicting sudden crashes (like COVID-19).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accounting for unexpected news (elections, wars).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try It Yourself!
&lt;/h2&gt;

&lt;p&gt;Want to run this code? Copy the full script from above and try it on Google Colab or Jupyter Notebook.&lt;/p&gt;

&lt;p&gt;click below link and run the below code &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://colab.research.google.com/" rel="noopener noreferrer"&gt;Google Colab&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&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;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;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;yfinance&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;yf&lt;/span&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;MinMaxScaler&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tensorflow.keras.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Sequential&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tensorflow.keras.layers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Dense&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Dropout&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tensorflow.keras.callbacks&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;EarlyStopping&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mean_absolute_error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r2_score&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;

&lt;span class="c1"&gt;# Download market data
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Downloading Nifty50 data...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;start_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2005-05-16&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;end_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2025-05-15&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;nifty_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;yf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;^NSEI&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;start_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;end_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Data cleaning and preprocessing
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Cleaning and preprocessing data...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Missing values in the dataset: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isnull&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;nifty_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort_index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;nifty_close&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Close&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="c1"&gt;# Scale the data
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MinMaxScaler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature_range&lt;/span&gt;&lt;span class="o"&gt;=&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;scaled_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nifty_close&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Split data into training and testing sets
&lt;/span&gt;&lt;span class="n"&gt;train_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&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;scaled_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_size&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;scaled_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;train_size&lt;/span&gt;
&lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaled_data&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="n"&gt;train_size&lt;/span&gt;&lt;span class="p"&gt;,:],&lt;/span&gt; &lt;span class="n"&gt;scaled_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;train_size&lt;/span&gt;&lt;span class="p"&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;scaled_data&lt;/span&gt;&lt;span class="p"&gt;),:]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Training data size: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;train_size&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Testing data size: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;test_size&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&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;i&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="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;time_step&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;time_step&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&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;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&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="c1"&gt;# Create the dataset with time steps
&lt;/span&gt;&lt;span class="n"&gt;time_step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;train_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;test_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Reshape input to be [samples, time steps, features] which is required for LSTM
&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&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="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;X_test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&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="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Build LSTM model
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Building LSTM model...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Sequential&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# First LSTM layer with 50 neurons and return sequences=True to stack another LSTM layer
&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_sequences&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;input_shape&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time_step&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="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# Dropout to prevent overfitting
&lt;/span&gt;
&lt;span class="c1"&gt;# Second LSTM layer with 50 neurons
&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_sequences&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;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Third LSTM layer with 50 neurons
&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LSTM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dropout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Output layer
&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dense&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;units&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Compile the model
&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;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;optimizer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;adam&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loss&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;mean_squared_error&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Early stopping to prevent overfitting
&lt;/span&gt;&lt;span class="n"&gt;early_stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EarlyStopping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;monitor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;val_loss&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;patience&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;restore_best_weights&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="c1"&gt;# Train the model
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Training the model...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;batch_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&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;50&lt;/span&gt;

&lt;span class="n"&gt;history&lt;/span&gt; &lt;span class="o"&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_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;epochs&lt;/span&gt;&lt;span class="o"&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;batch_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;batch_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;validation_split&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Use 10% of training data for validation
&lt;/span&gt;    &lt;span class="n"&gt;callbacks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;early_stop&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;verbose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Make predictions and evaluate the model
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Making predictions...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;train_predict&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_predict&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Inverse transform to get actual values
&lt;/span&gt;&lt;span class="n"&gt;train_predict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_predict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;y_train_actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="n"&gt;y_test_actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="c1"&gt;# Calculate performance metrics
&lt;/span&gt;&lt;span class="n"&gt;train_rmse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;test_rmse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;train_mae&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mean_absolute_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_mae&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mean_absolute_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;train_r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;r2_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;r2_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Display results
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Training RMSE: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;train_rmse&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Testing RMSE: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;test_rmse&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Training MAE: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;train_mae&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Testing MAE: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;test_mae&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Training R^2 Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;train_r2&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Testing R^2 Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;test_r2&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Calculate accuracy as a percentage (simplified for this context)
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_accuracy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;predicted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;within_threshold&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="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;predicted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt;
    &lt;span class="n"&gt;accuracy&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="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;within_threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;accuracy&lt;/span&gt;

&lt;span class="n"&gt;train_accuracy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_accuracy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_accuracy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_accuracy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Training Accuracy: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;train_accuracy&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;%&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Testing Accuracy: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;test_accuracy&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;%&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Visualize the results
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Visualizing results...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;train_dates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;train_size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;test_dates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;train_size&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;time_step&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&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;train_dates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Actual Training Data&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;train_dates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;train_predict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Training Predictions&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;test_dates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test_actual&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Actual Testing Data&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;test_dates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_predict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Testing Predictions&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;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Nifty50 Price Prediction&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;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Date&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;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Price (INR)&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;legend&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;span class="c1"&gt;# Future predictions
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Making future predictions...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;last_60_days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaled_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;span class="n"&gt;future_predictions&lt;/span&gt; &lt;span class="o"&gt;=&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;_&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="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Reshape data for prediction
&lt;/span&gt;    &lt;span class="n"&gt;X_future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last_60_days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&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="n"&gt;time_step&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="c1"&gt;# Make prediction
&lt;/span&gt;    &lt;span class="n"&gt;future_pred&lt;/span&gt; &lt;span class="o"&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;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_future&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Append to the input data
&lt;/span&gt;    &lt;span class="n"&gt;last_60_days&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_60_days&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="n"&gt;future_pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;last_60_days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last_60_days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="c1"&gt;# Store the prediction
&lt;/span&gt;    &lt;span class="n"&gt;future_predictions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future_pred&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Inverse transform to get actual values
&lt;/span&gt;&lt;span class="n"&gt;future_predictions&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="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future_predictions&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="n"&gt;future_predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;future_predictions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create future dates
&lt;/span&gt;&lt;span class="n"&gt;last_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;future_dates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;last_date&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;i&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;i&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="c1"&gt;# Visualize future predictions
&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;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&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;nifty_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:],&lt;/span&gt; &lt;span class="n"&gt;nifty_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Close&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="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:],&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Historical Data&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;future_dates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;future_predictions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Future Predictions&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;red&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;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Nifty50 Future Price Prediction&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;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Date&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;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Price (INR)&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;legend&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;span class="c1"&gt;# Print model summary
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Model Summary:&lt;/span&gt;&lt;span class="sh"&gt;"&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;summary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;AI is a powerful tool for stock prediction, but it’s not a crystal ball. It helps investors make educated guesses, not certainties.&lt;/p&gt;

&lt;p&gt;Would you trust AI for stock advice? Let us know in the comments! 🚀&lt;/p&gt;

&lt;p&gt;Happy investing! 📈&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Internal Working Of Python</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Fri, 01 Nov 2024 06:26:41 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/internals-of-python-11ed</link>
      <guid>https://forem.com/yaswanthteja/internals-of-python-11ed</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fhvbdd8y5ttutkvhfcnm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhvbdd8y5ttutkvhfcnm9.png" alt=" " width="765" height="845"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the full code file: &lt;a href="https://github.com/yaswanthteja/Complete_Python/blob/main/01_Introduction/04_Internal_working_of_python.md" rel="noopener noreferrer"&gt;Code&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Source Code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you write a Python script, it’s human-readable text. This source code is the starting point for everything.&lt;/p&gt;

&lt;p&gt;Your Python source code, written in a &lt;code&gt;.py&lt;/code&gt; file, is human-readable. This code defines what your program does, specifying variables, functions, loops, and so on.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Compilation to Bytecode (compiler)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you run a Python program, the first step is to compile the source code to bytecode. This is done by the Python interpreter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Syntax Check&lt;/strong&gt;: Ensures there are no syntax errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compilation&lt;/strong&gt;: Translates the high-level source code into bytecode, a lower-level, platform-independent representation. This bytecode typically resides in &lt;code&gt;.pyc&lt;/code&gt; files within the &lt;code&gt;__pycache__&lt;/code&gt; directory.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compiler&lt;/strong&gt;: Python uses an interpreter, but it first compiles your source code into a lower-level form known as bytecode.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Tokenizing&lt;/strong&gt;: Breaks down your code into small pieces called tokens (like keywords, operators, identifiers).&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Parsing&lt;/strong&gt;: Analyzes the tokens to ensure they follow Python's syntax rules.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Control Flow Graph (CFG)&lt;/strong&gt;: Represents all paths that might be traversed through a program during its execution.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Bytecode Generation&lt;/strong&gt;: Converts the parsed tokens into bytecode, a set of instructions for the Python Virtual Machine (PVM).&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2p6n4zf4nj547qducgi7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2p6n4zf4nj547qducgi7.png" alt=" " width="800" height="848"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s deep dive into this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python Compiler&lt;/strong&gt;: Even though Python is known as an interpreted language, it does have a compilation step. Here’s the breakdown:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tokenization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Breaks down your code into small pieces called tokens (like keywords, operators, identifiers).&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Source Code&lt;/strong&gt;: Starts with your written code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tokenizer (Lexer)&lt;/strong&gt;: This breaks the source code into smaller pieces called tokens, like keywords (&lt;code&gt;for&lt;/code&gt;, &lt;code&gt;if&lt;/code&gt;), operators (&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;), identifiers (variable names), and literals (like numbers or strings).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parsing&lt;/strong&gt;: Analyzes the tokens to ensure they follow Python's syntax rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Syntax Analysis&lt;/strong&gt;: The parser takes these tokens and checks them against Python's grammar rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parse Tree&lt;/strong&gt;: Builds a tree structure from the tokens, representing the grammatical structure of the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Semantic Analysis&lt;/strong&gt;: Ensures the code makes sense in terms of data types, scope, and other context-specific rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control Flow Graph (CFG)&lt;/strong&gt;: Represents all paths that might be traversed through a program during its execution.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Control Flow Graph&lt;/strong&gt;: Represents all possible paths that might be taken through the code during execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nodes and Edges&lt;/strong&gt;: Each node represents a basic block of code, and edges represent the flow of control from one block to another.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bytecode Generation&lt;/strong&gt;: Converts the parsed tokens into bytecode, a set of instructions for the Python Virtual Machine (PVM).

&lt;ul&gt;
&lt;li&gt;The bytecode is a more compact, lower-level representation of your source code, optimized for execution. It’s platform-independent, meaning it can be run on any system with a compatible PVM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bytecode&lt;/strong&gt;: The parsed code is converted into bytecode, a lower-level, platform-independent representation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Set&lt;/strong&gt;: This bytecode is a set of instructions that the Python Virtual Machine (PVM) can execute. Bytecode is stored in &lt;code&gt;.pyc&lt;/code&gt; files in the &lt;code&gt;__pycache__&lt;/code&gt; directory to speed up future executions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Loading Bytecode (Byte code)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After compilation, the Python Virtual Machine loads the bytecode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reading from Cache&lt;/strong&gt;: If the bytecode has been previously compiled and hasn’t changed, it’s read from the cache (&lt;code&gt;__pycache__&lt;/code&gt;). This speeds up execution by skipping the compilation step.

&lt;ul&gt;
&lt;li&gt;The bytecode is loaded into memory, ready to be executed. The bytecode is then executed by the PVM, interpreting the instructions to perform the program’s tasks.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Execution by PVM (PVM)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The PVM now interprets and executes the bytecode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Execution&lt;/strong&gt;: The PVM reads each bytecode instruction and executes it. Each instruction corresponds to a specific operation, like loading a value, performing arithmetic, or calling a function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Management&lt;/strong&gt;: Manages allocation and deallocation of memory for variables and objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Memory Management in Python&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reference Counting&lt;/strong&gt;: Python keeps track of how many references there are to an object in memory. When the reference count drops to zero, the memory occupied by the object can be reclaimed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object Allocation&lt;/strong&gt;: Python objects (like integers, strings, lists) are created in memory when the code is run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Garbage Collection&lt;/strong&gt;: Python has a garbage collector that helps manage memory by deallocating memory that is no longer in use (i.e., objects with a reference count of zero).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Pooling&lt;/strong&gt;: Python uses pools of memory to allocate small objects more efficiently. This pooling helps reduce the overhead of frequently allocating and deallocating small chunks of memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Optimization&lt;/strong&gt;: Python applies various optimizations to minimize memory usage, such as:

&lt;ul&gt;
&lt;li&gt;The PVM performs various runtime optimizations to improve efficiency, such as just-in-time (JIT) compilation in some implementations (like PyPy).&lt;/li&gt;
&lt;li&gt;Reusing small integers and interned strings.&lt;/li&gt;
&lt;li&gt;Efficiently managing data structures (e.g., tuples, lists, dictionaries).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bytecode Caching&lt;/strong&gt;: The PVM caches compiled bytecode to avoid recompiling the source code every time. This speeds up subsequent runs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constant Folding&lt;/strong&gt;: This involves simplifying constant expressions at compile time rather than runtime. For example, &lt;code&gt;3 * 2&lt;/code&gt; might be precomputed to &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, in summary: the PVM is like an orchestra conductor, seamlessly turning the bytecode into actions that your computer can execute. The beautiful thing about it is that Python code, thanks to the PVM, is portable and can run on different platforms without modification.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can we see bytecode generated or not?
&lt;/h2&gt;

&lt;p&gt;When you import a Python module, Python compiles the source code into bytecode and stores it in the &lt;code&gt;__pycache__&lt;/code&gt; directory. This helps speed up future imports by avoiding the need to recompile the module each time it's imported.&lt;/p&gt;

&lt;p&gt;Here's the process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;First Import&lt;/strong&gt;: When you first import a module, Python compiles the &lt;code&gt;.py&lt;/code&gt; file into bytecode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pycache Directory&lt;/strong&gt;: The bytecode is stored in the &lt;code&gt;__pycache__&lt;/code&gt; directory, named something like &lt;code&gt;module_name.cpython-312.pyc&lt;/code&gt;. &lt;code&gt;# 312&lt;/code&gt; is the Python version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subsequent Imports&lt;/strong&gt;: On subsequent imports, Python checks the &lt;code&gt;__pycache__&lt;/code&gt; directory for the compiled bytecode and uses it if the source code hasn't changed, speeding up the import process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;We have &lt;code&gt;byte.py&lt;/code&gt;. When we import code from &lt;code&gt;hello_world.py&lt;/code&gt; after execution of &lt;code&gt;byte.py&lt;/code&gt;, we can see there will be a directory &lt;code&gt;__pycache__&lt;/code&gt; in that particular folder, and we can see &lt;code&gt;.pyc&lt;/code&gt; files:&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;hello_world&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Byte code&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;



&lt;h3&gt;
  
  
  By using py_compile
&lt;/h3&gt;

&lt;p&gt;py_compile module, which allows you to compile Python source files into bytecode files. This is a handy way to speed up script execution for future runs.&lt;/p&gt;

&lt;p&gt;In byte.py&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;py_compile&lt;/span&gt;

&lt;span class="n"&gt;py_compile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;hello_world.py&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The py_compile module compiles hello_world.py into bytecode.&lt;/li&gt;
&lt;li&gt;The resulting bytecode is stored in the &lt;strong&gt;pycache&lt;/strong&gt; directory, creating a file named hello_world.cpython-38.pyc (or similar, depending on your Python version).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Generating Bytecode:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The whole script is executed to generate the bytecode. This means any top-level code (like print("Hello, World!") and print("c")) will run during the compilation process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Resulting Bytecode:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The bytecode contains all functions, classes, and executable statements, which Python uses to speed up future imports of the script.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  dis module
&lt;/h3&gt;

&lt;p&gt;The dis module in Python is used to disassemble bytecode into a more readable form. This can help you understand what your Python code is doing under the hood. It’s especially useful for debugging or learning about the internals of Python.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In  internal.py we have
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Import the dis module which supports the analysis of CPython bytecode
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dis&lt;/span&gt;

&lt;span class="c1"&gt;# Define a simple function to illustrate Python's internals
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# A simple print statement that is more complex than it looks
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Disassemble the greet function to see what's happening under the hood
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;disassemble_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;dis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Call the disassemble function
&lt;/span&gt;&lt;span class="nf"&gt;disassemble_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 5           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello, ')
              4 LOAD_FAST                0 (name)
              6 FORMAT_VALUE             0
              8 BUILD_STRING             2
             10 CALL_FUNCTION            1
             12 POP_TOP
             14 LOAD_CONST               0 (None)
             16 RETURN_VALUE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The program begins by importing the dis module, a powerful tool for analyzing CPython bytecode. CPython is the default implementation of Python and bytecode is an intermediate language for the Python interpreter.&lt;/li&gt;
&lt;li&gt;Next, I’ve defined a simple function called greet. This function takes in a parameter name and prints out a greeting. Though the function itself is quite straightforward, what happens under the hood in Python is more intricate than it might seem on the surface.&lt;/li&gt;
&lt;li&gt;The disassemble_function function makes use of dis.dis() to disassemble the greet function. dis.dis() translates Python functions into the low-level bytecode that Python’s virtual machine actually executes. This bytecode is Python’s interpretation of our greet function and is a step closer to machine code.&lt;/li&gt;
&lt;li&gt;When the script calls disassemble_function(), the console output presents the bytecode of our greet function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s what the bytecode tells us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LOAD_GLOBAL(0): This opcode is used to load the global variable, which, in this case, is the print function.&lt;/li&gt;
&lt;li&gt;LOAD_CONST(1): This loads the constant value ‘Hello, ‘ onto the stack.&lt;/li&gt;
&lt;li&gt;LOAD_FAST(0): This opcode loads the local variable name onto the stack.&lt;/li&gt;
&lt;li&gt;FORMAT_VALUE(0): This formats our name string, preparing it to be inserted into the string that’s about to be built.&lt;/li&gt;
&lt;li&gt;BUILD_STRING(2): This takes the top two values on the stack (‘Hello, ‘ and name) and builds the final string.&lt;/li&gt;
&lt;li&gt;CALL_FUNCTION(1): This line calls the function (global print function that we loaded onto the stack), with the argument count in parentheses (we have one argument, our formatted string).&lt;/li&gt;
&lt;li&gt;POP_TOP: This removes the top of the stack (the result of the previous call, since print returns None).&lt;/li&gt;
&lt;li&gt;LOAD_CONST(0): Loads None.&lt;/li&gt;
&lt;li&gt;RETURN_VALUE: This is the return value of the greet function, which, since there is no explicit return statement, is None.&lt;/li&gt;
&lt;li&gt;In essence, the bytecode shows the individual operations that Python performs to execute our greet function. Understanding these instructions is crucial for developers to understand how Python executes code, optimizes functions, and manages resources—all of this happening seamlessly under the hood when we run our Python code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ain’t that a delightful dive into the Python machine room? Keep coding and keep exploring the depths of this language’s engine room🚀!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Confusion Matrix: A Clear Guide to Understanding It</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Tue, 11 Jun 2024 08:27:04 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/confusion-matrix-a-clear-guide-to-understanding-it-4e0p</link>
      <guid>https://forem.com/yaswanthteja/confusion-matrix-a-clear-guide-to-understanding-it-4e0p</guid>
      <description>&lt;p&gt;Are you struggling to understand the Confusion Matrix? You're not alone. Despite its name, the Confusion Matrix can be quite perplexing for many. However, we're here to simplify it for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Confusion Matrix?
&lt;/h3&gt;

&lt;p&gt;A Confusion Matrix is a crucial tool in machine learning and statistics. It helps you evaluate the performance of a classification algorithm. By breaking down the true positives, true negatives, false positives, and false negatives, you can gain a clear picture of how well your model is performing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is the Confusion Matrix Important?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Performance Measurement: It provides detailed insight into the performance of your classification model.&lt;/li&gt;
&lt;li&gt;Error Analysis: Helps identify where your model is making mistakes, allowing for targeted improvements.&lt;/li&gt;
&lt;li&gt;Model Comparison: Essential for comparing different models to select the best one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why is it called a "Confusion" Matrix?
&lt;/h3&gt;

&lt;p&gt;Because it shows where the model gets "confused" in its predictions.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Interpret a Confusion Matrix?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;True Positive(TP): Positive events are correctly classified as Positive.&lt;/li&gt;
&lt;li&gt;True Negative(TN): Negative events are correctly classified as Negative.&lt;/li&gt;
&lt;li&gt;False Positive(FP): (Type 1 Error) Negative values are incorrectly classified as Positive.&lt;/li&gt;
&lt;li&gt;False Negative(FN): (Type 2 Error) Positive values are incorrectly classified as Negative. 
we discuss these more clearly as we move further.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The performance of the classification model is evaluated using a confusion matrix. I’m sure most of us have gone through this concept several times and still find it confusing. And realized why it is named confusion matrix.&lt;/p&gt;

&lt;p&gt;In supervised learning, if the target variable is categorial, then the classification model has to classify given test data into respective categories. The efficiency of classified data to respective categories is checked using a confusion matrix. For simplicity let’s consider binary classification.&lt;/p&gt;

&lt;p&gt;The results of the classification model can be categorized into four types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;True Positive(TP)&lt;/strong&gt;: Correctly predicted positive observations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cases where the model correctly predicts the class or events as  positive.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Eg1: You think you’ll love the new pizza place, &lt;br&gt;
 and you do!&lt;/p&gt;

&lt;p&gt;Eg2: A patient has got heart disease and the model predicts the same.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;True Negative(TN)&lt;/strong&gt;:  Negative events are correctly classified as Negative.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cases where the model correctly predicts the class or events as negative.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: You think you won't enjoy that new movie, and you don’t.&lt;/p&gt;

&lt;p&gt;Eg: A patient does not have heart disease and the model predicts that the patient is all right.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;False Positive(FP)&lt;/strong&gt;: (Type 1 Error) &lt;br&gt;
Incorrectly predicted positive observations (Type I error).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cases where the model incorrectly predicts the positive class (Type I error).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: You think that weirdly flavored ice cream will be great, but it’s a disaster. &lt;/p&gt;

&lt;p&gt;Eg: A patient does not have heart disease but the model predicts the patient has heart disease.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;False Negative(FN)&lt;/strong&gt; : (Type 2 Error) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Incorrectly predicted negative observations (Type II error).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cases where the model incorrectly predicts the negative class (Type II error).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: You skip that boring-looking book, only to find out later it’s amazing.&lt;/p&gt;

&lt;p&gt;Eg: A patient has heart disease but the model predicts as the patient does not have heart disease.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F97zs0ba0glthlwig7pl3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F97zs0ba0glthlwig7pl3.png" alt=" " width="800" height="586"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Construction of Confusion Matrix
&lt;/h3&gt;

&lt;p&gt;Here are a few steps to write the confusion matrix: Let’s consider two characters XY where X can be True or False and Y can take the value Positive or Negative.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Fill in the confusion matrix for character Y. It is solely based on predicted value, if the prediction is true then it’s Positive otherwise it’s Negative.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fabztk4cv6h4we0yovnyv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fabztk4cv6h4we0yovnyv.png" alt=" " width="472" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Fill in the first character X. Compare the actual with the predicted if both are of the same category say Actual is True and Predicted is True fill T (i.e. True), else fill F (i.e. False).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmr4m7ldmeyps315t08kg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmr4m7ldmeyps315t08kg.png" alt=" " width="472" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it’s time to check the performance of classification using count of TP, FN, FP, and TN.&lt;/p&gt;

&lt;p&gt;Evaluation metrics&lt;/p&gt;

&lt;p&gt;Consider there are nine Positive and nine Negative actual values out of which few are incorrectly classified by the model.&lt;/p&gt;
&lt;h3&gt;
  
  
  Key Metrics Derived from the Confusion Matrix:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Accuracy: (TP + TN) / (TP + TN + FP + FN)&lt;/li&gt;
&lt;li&gt;Precision: TP / (TP + FP)&lt;/li&gt;
&lt;li&gt;Recall: TP / (TP + FN)&lt;/li&gt;
&lt;li&gt;F1 Score: 2 * (Precision * Recall) / (Precision + Recall)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fo2dg6qijb92og0peytgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo2dg6qijb92og0peytgk.png" alt=" " width="720" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recall /True Positive Rate (TPR)/Sensitivity&lt;/strong&gt;: It tells us among actual true events how many are correctly predicted as true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Recall = TP / (TP + FN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fwf6vv9ukqrqz5l0vnfg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwf6vv9ukqrqz5l0vnfg5.png" alt=" " width="720" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Precision&lt;/strong&gt;: Out of all events that are predicted as positive how many are actually positive?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Precision = TP / (TP + FP)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fufkk4u7ivqa45qm5wfej.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fufkk4u7ivqa45qm5wfej.png" alt=" " width="720" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: It tells, out of all events how many are predicted correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Accuracy = ( TP + TN) /(TP + TN + FP + FN )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Accuracy gives the same importance to positive and negative events hence use this when we have a balanced data set. Otherwise, the majority might bias the minority class.&lt;/p&gt;

&lt;p&gt;The higher the value of Recall, Precision, and Accuracy better the model.&lt;/p&gt;

&lt;p&gt;Misclassification rate (Error Rate): How many are wrongly classified w.r.t total number of values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error Rate = (FP + FN) / (TP + TN + FP + FN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;False Positive Rate(FPR)/ Specificity: The number of values that are wrongly classified as positive w.r.t the total number of actual negative values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FPR = FP / (FP + TN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;F Score&lt;/strong&gt;: There is a chance that precision may be low while recall is high or vice versa, in this scenario, we need to consider both recall and precision to evaluate the model. Hence F Score comes into existence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F score = ( 1 + β ^2)* (Precision)* (Recall)/((Precision * β ^2) + Recall)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;β factor provides the weightage to Recall and Precision.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;β = 1: Recall and Precision are balanced&lt;/li&gt;
&lt;li&gt;β &amp;lt; 1: Precision oriented evaluation&lt;/li&gt;
&lt;li&gt;β &amp;gt;1: Recall-oriented evaluation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When β = 1 we call it F1 score:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F1 score = 2 * Recall * Precision /(Recall + Precision)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The higher the F1 score better the model.&lt;/p&gt;

&lt;p&gt;Implementation of the confusion matrix&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score, precision_score,
recall_score, f1_score

actual = [1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
predicted = [1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0]

confusion_matrix = metrics.confusion_matrix(actual, predicted)

# Finding precision and recall

accuracy = accuracy_score(actual, predicted)
print("Accuracy :", accuracy)

precision = precision_score(actual, predicted)
print("Precision :", precision)

recall = recall_score(actual, predicted)
print("Recall :", recall)

F1= f1_score(actual, predicted)
print("F1 Score:", F1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F5puwnkugj46iz7aea7iu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5puwnkugj46iz7aea7iu.png" alt=" " width="293" height="87"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(confusion_matrix,
annot=True,
fmt='g',
xticklabels=['0', '1'],
yticklabels=['0', '1'])
plt.ylabel('Actual',fontsize=13)
plt.xlabel('Predicted',fontsize=13)
plt.title('Confusion Matrix',fontsize=17)
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fjsnt1tnlsn3n3eh3vsqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjsnt1tnlsn3n3eh3vsqr.png" alt=" " width="433" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The trade-off between Recall and Precision:&lt;/p&gt;

&lt;p&gt;In real-time scenarios, where it’s okay to have a false positive than a false negative we choose Recall. Eg: Diagnosis of cancer, if the patient has cancer but is diagnosed as negative then the patient’s life is at stake.&lt;/p&gt;

&lt;p&gt;Where it’s okay to have false negatives but not false positives we use Precision. Eg: Missing to punish a criminal is better than punishing an innocent person.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Understanding the Confusion Matrix is essential for anyone working with classification models. By demystifying its components and applications, we hope to make it less confusing and more useful in your data analysis toolkit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EndNote:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed the article and gained insight into how to construct the confusion matrix and evaluation matrix like Recall, Precision, F score, and Accuracy. Please drop your suggestions or queries in the comment section.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>datascience</category>
      <category>confusionmatrix</category>
    </item>
    <item>
      <title>Alternative to ChatGPT: Introducing Nvidia's Chat with RTX: Revolutionizing the AI Chatbot Experience and Free for Everyone.</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sat, 06 Apr 2024 10:12:54 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/bye-bye-chatgpt-introducing-nvidias-chat-with-rtx-revolutionizing-the-ai-chatbot-experience-and-free-for-everyone-omg</link>
      <guid>https://forem.com/yaswanthteja/bye-bye-chatgpt-introducing-nvidias-chat-with-rtx-revolutionizing-the-ai-chatbot-experience-and-free-for-everyone-omg</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fvvljrijxdbixmq6gbuwj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvvljrijxdbixmq6gbuwj.png" alt=" " width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In today's tech-savvy world, Artificial intelligence (AI) is redefining human interactions with technology by pushing new frontiers. As a pioneer in AI computing, Nvidia has lifted the bar once more with the release of Chat with RTX, a game-changing program that looks to revolutionize how humans interact with AI chatbots.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chat with RTX: What is it?
&lt;/h3&gt;

&lt;p&gt;A cutting-edge software program called Nvidia's Chat with RTX allows customers to run their own AI chatbot on their desktop. Chat with RTX functions both locally and offline, providing users with speed and anonymity, in contrast to conventional chatbot platforms that depend on cloud-based servers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/fc_NSAu41b0" rel="noopener noreferrer"&gt;&lt;img src="https://media2.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%2Fq0tb06ytcdj5epcczvl2.jpg" alt="Blinking LEDs" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Important characteristics:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Local Function: &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of Chat with RTX's most notable features is its local functionality. There is no need for internet connectivity because the entire chatbot operates directly on your PC. This assures data confidentiality and privacy in addition to lightning-fast performance.&lt;/p&gt;

&lt;p&gt;2.YouTube Helper:&lt;/p&gt;

&lt;p&gt;Chat with RTX includes a robust YouTube Helper feature that can read transcripts of videos and offer wise responses to questions posed by users. To ask a question, just copy and paste the URL of a YouTube video into the program. Chat with RTX has you covered whether you're looking for information or clarification on a video.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AI Model Selection:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Users are able to select their favorite AI model from a list of options that includes Ilama and Mistral. Using Mistral 7B,You may be confident of excellent conversational capabilities and accuracy because Mistral 7B is a well-liked option among consumers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dataset Options:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chat with RTX provides users with an array of options for datasets, such as predefined AI model datasets, YouTube video URLs, and local folder directories. Because of its adaptability, users can customize their chatbot experience to meet their own requirements and tastes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Privacy Assurance:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chat with RTX provides comfort to people that value their privacy and data protection. Sensitive data is always protected thanks to the software's local operation on the user's PC and the lack of requirement for an online connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  System Requirements
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftq936zbq0z0prabkmxzs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftq936zbq0z0prabkmxzs.png" alt=" " width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Get Started:
&lt;/h3&gt;

&lt;p&gt;It's simple to get started with Chat with RTX:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;a href="https://nvidia.com/en-us/chatrtx/ai-on-rtx" rel="noopener noreferrer"&gt;https://nvidia.com/en-us/chatrtx/ai-on-rtx&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Download and install the software on your Windows PC.&lt;/li&gt;
&lt;li&gt;Begin to take advantage of the advantages of having your very own AI chatbot at your disposal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;p&gt;AI chatbot technology has advanced significantly with Nvidia's Chat with RTX. Users are enabled to fully utilize AI in their daily lives by the software's unmatched privacy, speed, and customization possibilities. A world of opportunities for meaningful and significant AI engagement is made possible by Chat with RTX, regardless of your interests as a tech enthusiast, content provider, or inquisitive explorer. Why then wait? With Chat with RTX, you can already experience the AI chatbots of the future.&lt;/p&gt;

</description>
      <category>chatgpt</category>
      <category>news</category>
      <category>ai</category>
    </item>
    <item>
      <title>Unlocking Code Clarity: A Guide to Effective Type Hinting in Python</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sun, 25 Feb 2024 10:50:42 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/unlocking-code-clarity-a-guide-to-effective-type-hinting-in-python-3if9</link>
      <guid>https://forem.com/yaswanthteja/unlocking-code-clarity-a-guide-to-effective-type-hinting-in-python-3if9</guid>
      <description>&lt;p&gt;Oh, look, the Python dev finally discovered type hinting! Now they can pretend to be as statically typed as Java, but with extra parentheses and indentation to spare!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fcr71ee7w2vvk8669r4wy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcr71ee7w2vvk8669r4wy.png" alt=" " width="320" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ha! While JavaScript devs are still lost in the dark ages of type ambiguity, still trying to figure out their types while we Python devs are over here adding type hints for fun.&lt;/p&gt;

&lt;p&gt;I'm joking, of course!&lt;/p&gt;

&lt;p&gt;In the ever-evolving landscape of Python programming, one concept that has gained significant traction in recent years is type hinting. type hinting has emerged as a pivotal concept, enhancing code clarity and maintainability. &lt;/p&gt;

&lt;p&gt;In this blog post, we'll delve into the world of type hinting, exploring what it is, and why it matters, Join us as we explore the significance of type hinting and its effective application in Python projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Type Hinting?
&lt;/h2&gt;

&lt;p&gt;In the dynamic world of Python programming.&lt;/p&gt;

&lt;p&gt;Type hinting is a feature introduced in Python 3.5 in Python that allows you to add type information to your code. While Python is a dynamically typed language, meaning you don't have to explicitly declare the type of a variable, type hinting provides a way to indicate the expected types for variables, function arguments, and return values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flecfz4u6pnnoqubw86po.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flecfz4u6pnnoqubw86po.png" alt=" " width="741" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While type hints are entirely optional and do not affect the runtime behavior of Python code, they offer several benefits in terms of code readability, documentation, and static analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python is a Dynamically Typed language
&lt;/h3&gt;

&lt;p&gt;Another staple of dynamic languages is that you don't need to declare a variable before using it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7t9bw9psdgpx9xm28ni6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7t9bw9psdgpx9xm28ni6.png" alt=" " width="644" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can just assign whatever you want to whatever variable you want.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Is this a good thing or a bad thing?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The dynamism of Python gives programmers a lot of power!&lt;/p&gt;

&lt;p&gt;But, with great power, comes great responsibility!&lt;/p&gt;

&lt;p&gt;And many bugs!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;But what does that look like?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us start by looking at the function &lt;code&gt;multiply&lt;/code&gt; from the beginning with and without type hints.&lt;/p&gt;

&lt;p&gt;The bottom-right adds type hints:&lt;/p&gt;

&lt;p&gt;👉 &lt;code&gt;: int&lt;/code&gt; and &lt;code&gt;: str&lt;/code&gt; to the parameters; and&lt;br&gt;
 👉 &lt;code&gt;→ str&lt;/code&gt; to the return value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0t686z867wpdglq5glm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0t686z867wpdglq5glm6.png" alt=" " width="638" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type hints are an OPTIONAL indication:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉 &lt;code&gt;factor: int&lt;/code&gt; says “I expect this to be an integer”;&lt;br&gt;
 👉 &lt;code&gt;string: str&lt;/code&gt; says “I expect this to be a string”; and&lt;br&gt;
 👉 &lt;code&gt;→ str&lt;/code&gt; says “I expect the function to return a string”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F66m9lrltezc6kkiuhqwu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F66m9lrltezc6kkiuhqwu.png" alt=" " width="638" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Type hinting can make your code more readable, help catch potential errors early in development, and improve collaboration, especially in larger codebases. One popular tool for static type checking in Python is &lt;strong&gt;mypy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's a brief overview of how you can use type hinting and &lt;strong&gt;mypy&lt;/strong&gt;:&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Type Hinting Basics:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Variable Type Hinting:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Function Argument and Return Type Hinting:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

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




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Type Hints for Collections:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Dict&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sum&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;count&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

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




&lt;/li&gt;

&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Using &lt;code&gt;mypy&lt;/code&gt; for Static Type Checking:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Installation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;mypy

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

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Running &lt;code&gt;mypy&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Save your Python script with type hints (e.g., &lt;strong&gt;&lt;code&gt;example.py&lt;/code&gt;&lt;/strong&gt;) and run:&lt;/p&gt;

&lt;p&gt;If there are no type errors, &lt;strong&gt;&lt;code&gt;mypy&lt;/code&gt;&lt;/strong&gt; will not produce any output. &lt;/p&gt;

&lt;p&gt;If there are issues, &lt;strong&gt;&lt;code&gt;mypy&lt;/code&gt;&lt;/strong&gt; will provide error messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# example.py
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&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="nb"&gt;int&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="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&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;y&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;7&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Type error: Argument 2 to "add_numbers" has incompatible type "str"; expected "int"
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;strong&gt;&lt;code&gt;mypy&lt;/code&gt;&lt;/strong&gt; would catch the type error, indicating that the second argument to &lt;strong&gt;&lt;code&gt;add_numbers&lt;/code&gt;&lt;/strong&gt; is expected to be an &lt;strong&gt;&lt;code&gt;int&lt;/code&gt;&lt;/strong&gt;, but a &lt;strong&gt;&lt;code&gt;str&lt;/code&gt;&lt;/strong&gt; was provided.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;&lt;code&gt;mypy&lt;/code&gt;&lt;/strong&gt; is optional, and Python will still execute the code even if there are type errors. However, incorporating type hinting and static type checking can be a valuable practice for improving code quality and catching potential issues early in development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type hints act as high-level documentation.
&lt;/h2&gt;

&lt;p&gt;Looking at a variable and knowing its type is better than knowing nothing about it.&lt;/p&gt;

&lt;p&gt;Type hints will also warn you when you misuse a variable or a function, which is incredibly useful in complex programs.&lt;/p&gt;

&lt;p&gt;These are just 2 advantages.&lt;br&gt;
If you want to learn about type hinting in Python 🐍, you're in for an interesting ride.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You should also DEFINITELY take a look at:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉 the module &lt;code&gt;typing&lt;/code&gt; from the standard library; and&lt;br&gt;
 👉 the tool &lt;code&gt;mypy&lt;/code&gt;, will check the types in your code.&lt;/p&gt;

&lt;p&gt;Here's a small typed program:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fheriq3frd5ld9nig8i8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fheriq3frd5ld9nig8i8t.png" alt=" " width="674" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In conclusion, type hinting is a valuable addition to the Python developer's toolkit, empowering them to write clearer, more expressive code while leveraging the full potential of Python's dynamic nature. Whether you're working on a small script or a large-scale application, incorporating type hints can elevate the quality and maintainability of your Python code.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>yaswanthteja</category>
    </item>
    <item>
      <title>Install VS Code in an Android Phone?</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sun, 03 Sep 2023 16:48:02 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/install-vs-code-in-an-android-phone-3bn5</link>
      <guid>https://forem.com/yaswanthteja/install-vs-code-in-an-android-phone-3bn5</guid>
      <description>&lt;p&gt;If you  wants to code on your Android device using VS Code, this might be the best setup for you.&lt;/p&gt;

&lt;p&gt;Follow the steps below:&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - Install termux
&lt;/h2&gt;

&lt;p&gt;In order to install VS code, you will have to install the Termux app by scrolling down and downloading apk &lt;a href="https://f-droid.org/en/packages/com.termux/" rel="noopener noreferrer"&gt;using this link from f-droid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvwhlp1brlgrwl9f0boqc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvwhlp1brlgrwl9f0boqc.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The official release available on the play store doesn't seem to get the packages updated for some reason. &lt;/p&gt;

&lt;p&gt;Termux from Play Store won't work. This is what the official Termux Github repository has to say:&lt;/p&gt;

&lt;p&gt;"Termux and its plugins are no longer updated on Google Play Store due to Android 10 issues and have been deprecated. The last version released for Android &amp;gt;= 7 was v0.101. It is highly recommended to not install Termux apps from Play Store anymore."&lt;/p&gt;

&lt;p&gt;Open the downloaded apk by clicking "Open"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fv3nqafasr40fgssglq29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fv3nqafasr40fgssglq29.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Install the apk file. If you are prompted to go to settings and enable installation of Apps from unknown sources, do that!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7emx22ydeoge8hlpwaei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7emx22ydeoge8hlpwaei.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the Termux app and you will see a screen like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fn6iu2773uax8sskou7zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn6iu2773uax8sskou7zj.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 - Install Ubuntu using Termux
&lt;/h2&gt;

&lt;p&gt;Enter the following command on Termux to update the package repository&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Foker3y510lb9ccxp5tpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foker3y510lb9ccxp5tpw.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When prompted, press 'y' and then press enter. You will have to press y followed by enter every time you are prompted for it.&lt;/p&gt;

&lt;p&gt;Now let's upgrade the packages using the below command in Termux&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pkg upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's install proot-distro 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;pkg install proot-distro
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's list all the distros we can install using proot using the following command (This command is optional)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;proot-distro list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see a screen like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7behqupglezy5c6t9rjz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7behqupglezy5c6t9rjz.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now fire this command and Ubuntu will start to install on your Android phone&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;proot-distro install ubuntu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ubuntu will start installing and you will see a screen like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frmtu74i1js8n8fpwrioh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frmtu74i1js8n8fpwrioh.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, start Ubuntu by firing 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;proot-distro login ubuntu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will now see a root@ubuntu prompt in the terminal like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F50i7vcwmb7v5mnap9p1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F50i7vcwmb7v5mnap9p1p.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3 - Downloading code server
&lt;/h2&gt;

&lt;p&gt;While on Ubuntu run 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;apt update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and following command to upgrade the package repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press 'y' and enter whenever prompted.&lt;/p&gt;

&lt;p&gt;Now install wget 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;apt install wget
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again press 'y' and enter whenever prompted to confirm the installation. You now have wget installed!&lt;/p&gt;

&lt;p&gt;We will now download the latest release of the code server from Github 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;wget https://github.com/coder/code-server/releases/download/v4.16.1/code-server-4.16.1-linux-arm64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fn8yfgt7fej9dzkvoinuh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn8yfgt7fej9dzkvoinuh.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Extract the tarball 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;tar -xvf ./code-server-4.16.1-linux-arm64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tarball will start extracting. Wait for the tarball extraction to finish&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fcmzmax5000rr79mwrauy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcmzmax5000rr79mwrauy.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter the /bin folder of your code-server installation on Ubuntu (running on your phone)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd code-server-4.16.1-linux-arm64
cd bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4 - Set up a password and start using VS Code
&lt;/h2&gt;

&lt;p&gt;Setup a password for your VS Code instance 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;export PASSWORD="password"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: I am using a very weak password for demonstration purposes as I will use this code-server locally on my phone. Consider using strong passwords for your serious projects&lt;/p&gt;

&lt;p&gt;Launch the code server 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;./code-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fsm1wu2ho6wmwnclpmu2x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsm1wu2ho6wmwnclpmu2x.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now go to your browser. I am using Google Chrome for Android and go to localhost:8080&lt;/p&gt;

&lt;p&gt;You will finally see a screen like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbyanfkogig4zuc0iu9ni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbyanfkogig4zuc0iu9ni.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;br&gt;
After entering the password you will see a welcome screen like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fq2rxf0s6f0277w9xbhj0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fq2rxf0s6f0277w9xbhj0.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;br&gt;
You can now start coding in VS Code on your Android Devices&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjlnrk2cd6wp4ogm397gj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjlnrk2cd6wp4ogm397gj.png" alt=" " width="800" height="1777"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope that worked for your phone. Please share your experiences, ask questions, or suggest improvements in the comments section. Happy Hacking!&lt;/p&gt;

&lt;h3&gt;
  
  
  Alternative
&lt;/h3&gt;

&lt;p&gt;You can use vscode directly by visiting &lt;a href="https://vscode.dev/" rel="noopener noreferrer"&gt;vscode.dev&lt;/a&gt; in your browser.&lt;/p&gt;

&lt;p&gt;Caveats:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the update command doesn't work on your Termux or you get some other network errors, try to change the default repo by using the command below:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;termux-change-repo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the repository to Mirror by Grimle and try the pkg update command again. It should work!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Whenever prompted, press 'y' and then press enter. You will have to press y followed by enter every time you are prompted for it.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>beginners</category>
      <category>vscode</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>A Beginner’s Guide To Machine Learning</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Mon, 27 Mar 2023 08:32:22 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/a-beginners-guide-to-machine-learning-3i6</link>
      <guid>https://forem.com/yaswanthteja/a-beginners-guide-to-machine-learning-3i6</guid>
      <description>&lt;p&gt;*&lt;em&gt;Machine Learning *&lt;/em&gt;(ML) is a powerful subset of Artificial Intelligence (AI) that enables machines to learn from data and make predictions or decisions without being explicitly programmed. It has been a game changer in many fields, from finance and healthcare to creative industries and marketing.&lt;/p&gt;

&lt;p&gt;If you're new to ML and want to get started, follow these steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Understand the basics&lt;/strong&gt;: Before diving into ML, it's important to have a solid understanding of statistics, linear algebra, and programming. You don't need to be an expert, but having a basic understanding of these topics will help you grasp the fundamentals of ML.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Choose your language&lt;/strong&gt;: There are many programming languages used in ML, including Python, R, and Java. Python is the most popular language for ML, thanks to its simplicity and versatility. It has many libraries specifically designed for ML, such as scikit-learn, TensorFlow, and Keras.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Choose your platform&lt;/strong&gt;: There are many platforms available for ML, including Azure Machine Learning, Amazon SageMaker, and Google Cloud ML Engine. These platforms provide pre-built ML models and tools to help you build your own models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Gather your data&lt;/strong&gt;: ML models require data to learn from. Make sure you have a sufficient amount of clean, structured data to work with. You can use public datasets or collect your own data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.Choose your model&lt;/strong&gt;: There are many types of ML models, including regression, classification, and clustering. Choose the model that best fits your needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.Train your model&lt;/strong&gt;: Once you have chosen your model, it's time to train it using your data. This involves feeding your data into the model and adjusting the parameters to optimize performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.Test your model&lt;/strong&gt;: After training your model, test it using a separate set of data to evaluate its accuracy and performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.Deploy your model&lt;/strong&gt;: Once your model has been trained and tested, deploy it to a production environment where it can be used in real-world applications.&lt;/p&gt;

&lt;p&gt;The basic idea behind machine learning is to build algorithms that can receive input data and use statistical analysis to predict an output value within an acceptable range.&lt;/p&gt;

&lt;p&gt;At a high level, machine learning algorithms are designed to learn from data. They do this by finding patterns in the data that they can use to make predictions or decisions. For example, a machine learning algorithm might be trained on a dataset of housing prices and be able to predict the sale price of a new house based on factors such as the size of the house, the number of bedrooms, and the location.&lt;/p&gt;

&lt;p&gt;There are many different types of machine learning algorithms, each with its own strengths and weaknesses. Some common types of algorithms include:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Supervised learning algorithms: These algorithms are trained on a labeled dataset, where the correct output is provided for each example in the training data. The algorithm learns from the training data and is able to make predictions on new data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unsupervised learning algorithms: These algorithms are trained on an unlabeled dataset, where the correct output is not provided. The algorithm must find patterns in the data on its own and use them to make predictions or cluster the data into groups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reinforcement learning algorithms: These algorithms learn by interacting with their environment and receiving feedback in the form of rewards or punishments. The algorithm learns to maximize the reward and avoid punishment in order to make the best decisions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Machine learning algorithms are commonly used in a wide range of applications, such as image and speech recognition, natural language processing, fraud detection, and recommendation engines.&lt;/p&gt;

&lt;p&gt;One of the key challenges in building effective machine learning models is finding the right balance between underfitting and overfitting. Underfitting occurs when the model is too simple and is not able to capture the underlying patterns in the data, leading to poor performance on both the training data and new data. Overfitting occurs when the model is too complex and is able to memorize the training data, but is not able to generalize well to new data.&lt;/p&gt;

&lt;p&gt;To avoid overfitting, machine learning models typically use regularization techniques that constrain the model in some way and prevent it from becoming too complex. Common regularization techniques include adding a penalty for large weights in the model, using early stopping to prevent the model from training for too long, and using cross-validation to evaluate the model’s performance on multiple subsets of the training data.&lt;/p&gt;

&lt;p&gt;Remember, ML is a vast and complex field, so don't be discouraged if you encounter challenges along the way. With patience and persistence, you can become proficient in ML and unlock its many benefits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Overall, machine learning is a powerful tool that allows us to build intelligent systems that can learn from data and make predictions or decisions. By using machine learning algorithms, we can build systems that are able to adapt and improve over time, making them more effective at solving real-world problems.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Python Virtual Environments</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sun, 05 Feb 2023 14:37:17 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/python-virtual-environments-2mi6</link>
      <guid>https://forem.com/yaswanthteja/python-virtual-environments-2mi6</guid>
      <description>&lt;h3&gt;
  
  
  An Introduction
&lt;/h3&gt;

&lt;p&gt;Python is a widely used programming language that is well known for its simplicity, versatility and robustness. One of the key benefits of using Python is that it provides a vast number of libraries and packages for various applications, including web development, scientific computing, data analysis, machine learning, etc. However, managing multiple Python projects and keeping them isolated from each other can be challenging. This is where Python virtual environments come into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Python Virtual Environments?
&lt;/h3&gt;

&lt;p&gt;A Python virtual environment is an isolated environment for a Python project, which helps in managing dependencies and packages specific to that project. In other words, virtual environments allow you to create a separate environment for each of your Python projects, ensuring that the dependencies and packages of one project do not interfere with the other. This is particularly useful when you are working on multiple projects that have conflicting dependencies and packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create a Virtual Environment in Python?
&lt;/h3&gt;

&lt;p&gt;Creating a virtual environment in Python is very easy. The first step is to install the virtual environment package, which is called venv. You can install it 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;pip install virtualenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the package is installed, you can create a virtual environment using the following command:&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;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;venv&lt;/span&gt; &lt;span class="n"&gt;myenv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, myenv is the name of the virtual environment that you have created. You can replace it with any other name of your choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Activating the Virtual Environment
&lt;/h3&gt;

&lt;p&gt;After creating the virtual environment, you need to activate it. This can be done by running 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;source myenv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can replace myenv with the name of the virtual environment that you created.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deactivating the Virtual Environment
&lt;/h3&gt;

&lt;p&gt;When you are done working with your virtual environment, you can deactivate it by running the following command:&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;deactivate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing Packages in the Virtual Environment
&lt;/h3&gt;

&lt;p&gt;Once the virtual environment is created and activated, you can install any packages that you need for your project using the following command:&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;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;package_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, package_name is the name of the package that you want to install.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exporting the Virtual Environment
&lt;/h3&gt;

&lt;p&gt;Sometimes, you might need to share your virtual environment with others or move it to another system. To do this, you can export the virtual environment using the following command:&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;pip&lt;/span&gt; &lt;span class="n"&gt;freeze&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a file named requirements.txt that contains a list of all the packages and their versions that are installed in your virtual environment. You can then share this file with others or move it to another system, and recreate the virtual environment using the following command:&lt;/p&gt;

&lt;p&gt;Copy code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, Python virtual environments are a crucial tool for Python developers. They help in keeping the dependencies and packages of each project isolated from each other, ensuring that the projects do not interfere with each other. This makes it easy to manage multiple projects and ensures that the projects are running smoothly and efficiently. With this article, we hope that you have a better understanding of Python virtual environments and how they can be used to manage your Python projects.&lt;/p&gt;

</description>
      <category>devrel</category>
      <category>announcement</category>
      <category>community</category>
      <category>devto</category>
    </item>
    <item>
      <title>A complete guide for Markdown files</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Fri, 13 Jan 2023 12:37:08 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/a-complete-guide-for-markdown-files-9me</link>
      <guid>https://forem.com/yaswanthteja/a-complete-guide-for-markdown-files-9me</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fyi22h5mcnroelw4nf29c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyi22h5mcnroelw4nf29c.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Markdown?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;According to Wikipedia:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Markdown is a lightweight markup language for creating formatted text using a plain-text editor. &lt;code&gt;John Gruber&lt;/code&gt; and &lt;code&gt;Aaron Swartz&lt;/code&gt; created Markdown in 2004 as a markup language that is appealing to human readers in its source code form. Markdown is widely used in blogging, instant messaging, online forums, collaborative software, documentation pages, and readme files.&lt;/p&gt;

&lt;p&gt;A markdown file has &lt;code&gt;.md&lt;/code&gt; extension and we can write and design text or useful information by following the basic syntaxes. It’s very popular and easy to use.&lt;/p&gt;

&lt;p&gt;HTML tags are also supported in markdown.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Case
&lt;/h3&gt;

&lt;p&gt;Markdown syntax is used in various popular places nowadays. You can also make websites using markdown files. This can be done using &lt;a href="https://docsify.js.org/#/?id=docsify" rel="noopener noreferrer"&gt;docsify&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some popular platforms that use Markdown syntaxes and files are as follows -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub&lt;/li&gt;
&lt;li&gt;Hashnode&lt;/li&gt;
&lt;li&gt;Dev.to&lt;/li&gt;
&lt;li&gt;Discord and many other platforms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Basic Syntaxes
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Headings
&lt;/h3&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# H1 - Heading 1
## H2 - Heading 2
### H3 - Heading 3
#### H4 - Heading 4
##### H5 - Heading 5
###### H6 - Heading 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Output:&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  H1 - Heading 1
&lt;/h1&gt;

&lt;h2&gt;
  
  
  H2 - Heading 2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  H3 - Heading 3
&lt;/h3&gt;

&lt;h4&gt;
  
  
  H4 - Heading 4
&lt;/h4&gt;

&lt;h5&gt;
  
  
  H5 - Heading 5
&lt;/h5&gt;

&lt;h6&gt;
  
  
  H6 - Heading 6
&lt;/h6&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;h1&gt;
  
  
  Text
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strikethrough Text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;Strikethrough uses two tildes. ~~Scratch this.~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
Strikethrough uses two tildes. &lt;del&gt;Scratch this.&lt;/del&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Blockquote Text&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gt"&gt;&amp;gt; “I'm unpredictable, I never know where I'm going until I get there, I'm so random, I'm always growing, learning, changing, I'm never the same person twice. But one thing you can be sure of about me; is I will always do exactly what I want to do.”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I'm unpredictable, I never know where I'm going until I get there, I'm so random, I'm always growing, learning, changing, I'm never the same person twice. But one thing you can be sure of about me; is I will always do exactly what I want to do.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Bold&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gs"&gt;**DO NOT UNDERESTIMATE THE POWER OF A PROGRAMMER.**&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;DO NOT UNDERESTIMATE THE POWER OF A PROGRAMMER.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Italic&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="ge"&gt;*It is Written in Italics*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It is Written in Italics&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Bold and Italic&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gs"&gt;***You Can Combine Bold and Italics**&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;You Can Combine Bold and Italics&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Highlight&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To highlight a text, wrap it in == from both sides. For example, the following markdown:&lt;br&gt;
&lt;code&gt;Syntax Example:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;I love ==markdown==
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I love ==markdown==&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Footnote&lt;/em&gt;&lt;/strong&gt;
&lt;code&gt;Syntax:&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;Here's a sentence with a footnote. [^1]

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;^1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="sx"&gt;This&lt;/span&gt; is the footnote.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Emojis&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;:mango: :lemon: :man: :car:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
🥭 🍋 👨 🚗&lt;/p&gt;
&lt;h1&gt;
  
  
  Links
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Embedded Links Into Text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;Github&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://github.com/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;br&gt;
&lt;a href="https://github.com/" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;![&lt;/span&gt;&lt;span class="nv"&gt;alt text&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;imagelink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsitcma9oveam6s4am1ug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsitcma9oveam6s4am1ug.png" alt=" " width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;&lt;em&gt;Embed YouTube Video&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;![Alt Text&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;Thumbnail&lt;/span&gt; Image)](YOUTUBE VIDEO LINK)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output&lt;/code&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=Rwj6bHrmT5M" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/Thumbnail%2520Image" alt="Alt Text" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;&lt;em&gt;Embed GIF&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Syntax:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;![&lt;/span&gt;&lt;span class="nv"&gt;alt-text&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;gif&lt;/span&gt; link)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fyaswanthteja%2FResources%2Fblob%2Fmain%2Fdino1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fyaswanthteja%2FResources%2Fblob%2Fmain%2Fdino1.gif" alt="alt-text" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;&lt;em&gt;Horizontal Line&lt;/em&gt;&lt;/strong&gt;
&lt;code&gt;Syntax:&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;---
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;This can be written in 2 ways - Inline code and normal code&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax for inline code:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;Inline &lt;span class="sb"&gt;`code`&lt;/span&gt; has &lt;span class="sb"&gt;`back-ticks around`&lt;/span&gt; it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Output:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inline &lt;code&gt;code&lt;/code&gt; has &lt;code&gt;back-ticks around&lt;/code&gt; it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax for normal code:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Blocks of Code
&lt;/h4&gt;

&lt;pre&gt;


```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```




 
```python
s = "Python syntax highlighting"
print s
```




 
```
No language indicated, so no syntax highlighting. 
But let's throw in a &lt;b&gt;tag&lt;/b&gt;.
```


&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;HTML
&lt;code&gt;Syntax:&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;align=&lt;/span&gt;&lt;span class="s"&gt;"center"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 Yes, you can use allowed raw HTML in mark-down file.
 This is a paragraph aligned in the center.
&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`

&amp;lt;p align="center"&amp;gt;
 Yes, you can use allowed raw HTML in mark-down file.
 This is a paragraph aligned in the center.
&amp;lt;/p&amp;gt;


## Lists

#### Ordered Lists
`Syntax:`


&lt;span class="p"&gt;```&lt;/span&gt;markdown
&lt;span class="p"&gt;1.&lt;/span&gt; First item
&lt;span class="p"&gt;2.&lt;/span&gt; Second item
&lt;span class="p"&gt;3.&lt;/span&gt; Third item
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`

1. First item
2. Second item
3. Third item

#### Unordered Lists
`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;markdown
&lt;span class="p"&gt;-&lt;/span&gt; First item
&lt;span class="p"&gt;-&lt;/span&gt; Second item
&lt;span class="p"&gt;-&lt;/span&gt; Third item
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;


`Output:`

- First item
- Second item
- Third item

- To define a sub list
`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;markdown
&lt;span class="p"&gt;*&lt;/span&gt; data 1
&lt;span class="p"&gt;*&lt;/span&gt; data 2
&lt;span class="p"&gt;    *&lt;/span&gt; data 21
&lt;span class="p"&gt;    *&lt;/span&gt; data 22
&lt;span class="p"&gt;*&lt;/span&gt; data 3
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`
* data 1
* data 2
    * data 21
    * data 22
* data 3

### Task List
`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; [x] Write the press release
&lt;span class="p"&gt;-&lt;/span&gt; [ ] Update the website
&lt;span class="p"&gt;-&lt;/span&gt; [ ] Contact the media
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`

- [x] Write the press release
- [ ] Update the website
- [ ] Contact the media




# Table

`Syntax:`


&lt;span class="p"&gt;```&lt;/span&gt;markdown
| Fruit | Emoji   |
| ----- | ------- |
| Mango | :mango: |
| Lemon | :lemon: |
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`

| Fruit | Emoji   |
| ----- | ------- |
| Mango | :mango: |
| Lemon | :lemon: |



-  ***Table With Alignments***
`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;markdown
| Fruit(left)                 | Emoji(center) |              Taste(right) |
| :-------------------------- | :-----------: | ------------------------: |
| Mango is the king of Fruits |    :mango:    |       Sweet and I love it |
| Lemon is good for health    |    :lemon:    | Sour, mix it in the water |
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`

| Fruit(left)                 | Emoji(center) |              Taste(right) |
| :-------------------------- | :-----------: | ------------------------: |
| Mango is the king of Fruits |    :mango:    |       Sweet and I love it |
| Lemon is good for health    |    :lemon:    | Sour, mix it in the water |


# Mathematical Expressions

1.### Inline expressions:
`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;markdown
$&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;mathematical&lt;/span&gt; &lt;span class="na"&gt;expression&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&amp;gt;$
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`
Replace `&amp;lt;&amp;lt;mathematical expression&amp;gt;&amp;gt;` with your expression.

`Example`

`Syntax:`



&lt;span class="p"&gt;```&lt;/span&gt;markdown
$&lt;span class="se"&gt;\s&lt;/span&gt;qrt{3}+1$
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Output:`


![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpwaw31rmz8x6g65ut1z.png)



2. ***Block Expressions***:

`Syntax:`


&lt;span class="p"&gt;```&lt;/span&gt;markdown
$$&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;mathematical&lt;/span&gt; &lt;span class="na"&gt;expression&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&amp;gt;$$
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`Example`
$$\sqrt{3}+1$$

`output`

![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrj9oxne72ogohqal36t.png)



3. ***Mixed Expressions***:

`Syntax`




&lt;span class="p"&gt;```&lt;/span&gt;markdown
When $a &lt;span class="se"&gt;\n&lt;/span&gt;e 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are 

$$ x = {-b &lt;span class="se"&gt;\p&lt;/span&gt;m &lt;span class="se"&gt;\s&lt;/span&gt;qrt{b^2-4ac} &lt;span class="se"&gt;\o&lt;/span&gt;ver 2a} $$
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;

`output`

![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whw0c5tter4e996h5a6a.png)




- [Mathematical Equations github](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/writing-mathematical-expressions)
- [Mathematical formulas](https://en.wikibooks.org/wiki/LaTeX/Mathematics)


### Conclusion
This is a comprehensive list for you all as your guide. Hope all went well and you enjoyed the article.

Hope this helps you. Thank you for reading, and let’s connect!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>markdown</category>
      <category>markdowncheatsheet</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>8 Week SQL Challenge: Case Study #2 Pizza Runner</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Wed, 12 Oct 2022 16:07:57 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/8-week-sql-challenge-case-study-2-pizza-runner-e0o</link>
      <guid>https://forem.com/yaswanthteja/8-week-sql-challenge-case-study-2-pizza-runner-e0o</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fc9nbh5ifevhwhrbv28p6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fc9nbh5ifevhwhrbv28p6.png" alt=" " width="720" height="724"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Danny was scrolling through his Instagram feed when something really caught his eye — “80s Retro Styling 🎸 and Pizza 🍕 Is The Future!”&lt;/p&gt;

&lt;p&gt;Danny was sold on the idea, but he knew that pizza alone was not going to help him get seed funding to expand his new Pizza Empire — so he had one more genius idea to combine with it — he was going to Uberize it — and so Pizza Runner was launched!&lt;/p&gt;

&lt;p&gt;Danny started by recruiting “runners” to deliver fresh pizza from Pizza Runner Headquarters (otherwise known as Danny’s house) and also maxed out his credit card to pay freelance developers to build a mobile app to accept orders from customers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Relationship
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;customer_orders — Customers’ pizza orders with 1 row each for individual pizza with topping exclusions and extras, and order time.&lt;/li&gt;
&lt;li&gt;runner_orders — Orders assigned to runners documenting the pickup time, distance and duration from Pizza Runner HQ to customer, and cancellation remark.
runners — Runner IDs and registration date&lt;/li&gt;
&lt;li&gt;pizza_names — Pizza IDs and name&lt;/li&gt;
&lt;li&gt;pizza_recipes — Pizza IDs and topping names&lt;/li&gt;
&lt;li&gt;pizza_toppings — Topping IDs and name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffx2iv9iovv772ra0rbsq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffx2iv9iovv772ra0rbsq.png" alt=" " width="720" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Case Study Questions
&lt;/h2&gt;

&lt;p&gt;This case study has LOTS of questions — they are broken up by area of focus including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A. Pizza Metrics&lt;/li&gt;
&lt;li&gt;B. Runner and Customer Experience&lt;/li&gt;
&lt;li&gt;C. Ingredient Optimisation&lt;/li&gt;
&lt;li&gt;D. Pricing and Ratings&lt;/li&gt;
&lt;li&gt;E. Bonus DML Challenges (DML = Data Manipulation Language)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Cleaning and Transformation
&lt;/h2&gt;

&lt;p&gt;Before I start with the solutions, I investigate the data and found that there are some cleaning and transformation to do, specifically on the&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;null&lt;/code&gt; values and data types in the &lt;code&gt;customer_orders&lt;/code&gt; table&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;null&lt;/code&gt; values and data types in the &lt;code&gt;runner_orders&lt;/code&gt; table&lt;/li&gt;
&lt;li&gt;Alter data type in &lt;code&gt;pizza_names&lt;/code&gt; table&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firstly, to clean up &lt;code&gt;exclusions&lt;/code&gt; and &lt;code&gt;extras&lt;/code&gt; in the &lt;code&gt;customer_orders&lt;/code&gt; — we create TEMP TABLE &lt;code&gt;#customer_orders&lt;/code&gt; and use CASE WHEN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT order_id, customer_id, pizza_id, 
  CASE 
    WHEN exclusions IS null OR exclusions LIKE 'null' THEN ' '
    ELSE exclusions
    END AS exclusions,
  CASE 
    WHEN extras IS NULL or extras LIKE 'null' THEN ' '
    ELSE extras 
    END AS extras, 
  order_time
INTO #customer_orders -- create TEMP TABLE
FROM customer_orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we clean the &lt;code&gt;runner_orders&lt;/code&gt; table with CASE WHEN and TRIM and create TEMP TABLE &lt;code&gt;#runner_orders&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In summary,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pickup_time — Remove nulls and replace with ‘ ‘&lt;/li&gt;
&lt;li&gt;distance — Remove ‘km’ and nulls&lt;/li&gt;
&lt;li&gt;duration — Remove ‘minutes’ and nulls&lt;/li&gt;
&lt;li&gt;cancellation — Remove NULL and null and replace with ‘ ‘
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT order_id, runner_id,
  CASE 
    WHEN pickup_time LIKE 'null' THEN ' '
    ELSE pickup_time 
    END AS pickup_time,
  CASE 
    WHEN distance LIKE 'null' THEN ' '
    WHEN distance LIKE '%km' THEN TRIM('km' from distance) 
    ELSE distance END AS distance,
  CASE 
    WHEN duration LIKE 'null' THEN ' ' 
    WHEN duration LIKE '%mins' THEN TRIM('mins' from duration) 
    WHEN duration LIKE '%minute' THEN TRIM('minute' from duration)        
    WHEN duration LIKE '%minutes' THEN TRIM('minutes' from duration)       
    ELSE duration END AS duration,
  CASE 
    WHEN cancellation IS NULL or cancellation LIKE 'null' THEN ''
    ELSE cancellation END AS cancellation
INTO #runner_orders
FROM runner_orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we alter the date according to its correct data type.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pickup_time to DATETIME type&lt;/li&gt;
&lt;li&gt;distance to FLOAT type&lt;/li&gt;
&lt;li&gt;duration to INT type
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE #runner_orders
ALTER COLUMN pickup_time DATETIME,
ALTER COLUMN distance FLOAT, 
ALTER COLUMN duration INT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the data has been cleaned and transformed, let’s move on solving the questions! 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h2&gt;
  
  
  A. Pizza Metrics
&lt;/h2&gt;

&lt;p&gt;How many pizzas were ordered?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(*) AS pizza_order_count
FROM #customer_orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fpz7h1i8ur960upon6euv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpz7h1i8ur960upon6euv.png" alt=" " width="150" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total pizzas ordered are 14.&lt;/li&gt;
&lt;li&gt;How many unique customer orders were made?
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(DISTINCT order_id) AS unique_order_count
FROM #customer_orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F8bo6kl730kujtpe12xib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8bo6kl730kujtpe12xib.png" alt=" " width="157" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are 10 unique customer orders made.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;How many successful orders were delivered by each runner?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT runner_id, COUNT(order_id) AS successful_orders
FROM #runner_orders
WHERE distance != 0
GROUP BY runner_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F0sx6gonyvmi488uro8x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0sx6gonyvmi488uro8x0.png" alt=" " width="207" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runner 1 has 4 successful delivered orders.&lt;/li&gt;
&lt;li&gt;Runner 2 has 3 successful delivered orders.&lt;/li&gt;
&lt;li&gt;Runner 3 has 1 successful delivered order.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;How many of each type of pizza was delivered?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT p.pizza_name, COUNT(c.pizza_id) AS delivered_pizza_count
FROM #customer_orders AS c
JOIN #runner_orders AS r
 ON c.order_id = r.order_id
JOIN pizza_names AS p
 ON c.pizza_id = p.pizza_id
WHERE r.distance != 0
GROUP BY p.pizza_name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fqy448dqymn68422b8nsn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqy448dqymn68422b8nsn.png" alt=" " width="232" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are 9 delivered Meatlovers pizzas.&lt;/li&gt;
&lt;li&gt;There are 3 delivered Vegetarian pizzas.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;How many Vegetarian and Meatlovers were ordered by each customer?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT c.customer_id, p.pizza_name, COUNT(p.pizza_name) AS order_count
FROM #customer_orders AS c
JOIN pizza_names AS p
 ON c.pizza_id= p.pizza_id
GROUP BY c.customer_id, p.pizza_name
ORDER BY c.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fpty63ebqis779x6uqq2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpty63ebqis779x6uqq2p.png" alt=" " width="252" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer 101 ordered 2 Meatlovers pizzas and 1 Vegetarian pizza.&lt;/li&gt;
&lt;li&gt;Customer 102 ordered 2 Meatlovers pizzas and 2 Vegetarian pizzas.&lt;/li&gt;
&lt;li&gt;Customer 103 ordered 3 Meatlovers pizzas and 1 Vegetarian pizza.&lt;/li&gt;
&lt;li&gt;Customer 104 ordered 1 Meatlovers pizza.&lt;/li&gt;
&lt;li&gt;Customer 105 ordered 1 Vegetarian pizza.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the maximum number of pizzas delivered in a single order?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH pizza_count_cte AS
(
 SELECT c.order_id, COUNT(c.pizza_id) AS pizza_per_order
 FROM #customer_orders AS c
 JOIN #runner_orders AS r
  ON c.order_id = r.order_id
 WHERE r.distance != 0
 GROUP BY c.order_id
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Frvta0bufaoedx4uk1mc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frvta0bufaoedx4uk1mc1.png" alt=" " width="192" height="205"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MAX(pizza_per_order) AS pizza_count
FROM pizza_count_cte;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fj8so20x1xywd3s92ck6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj8so20x1xywd3s92ck6h.png" alt=" " width="141" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maximum number of pizza delivered in a single order is 3 pizzas.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;For each customer, how many delivered pizzas had at least 1 change and how many had no changes?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT c.customer_id,
 SUM(CASE 
  WHEN c.exclusions &amp;lt;&amp;gt; ' ' OR c.extras &amp;lt;&amp;gt; ' ' THEN 1
  ELSE 0
  END) AS at_least_1_change,
 SUM(CASE 
  WHEN c.exclusions = ' ' AND c.extras = ' ' THEN 1 
  ELSE 0
  END) AS no_change
FROM #customer_orders AS c
JOIN #runner_orders AS r
 ON c.order_id = r.order_id
WHERE r.distance != 0
GROUP BY c.customer_id
ORDER BY c.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fozekrf87eqjujngufmrc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fozekrf87eqjujngufmrc.png" alt=" " width="290" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer 101 and 102 likes his/her pizzas per the original recipe.&lt;/li&gt;
&lt;li&gt;Customer 103, 104 and 105 have their own preference for pizza topping and requested at least 1 change (extra or exclusion topping) on their pizza.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;How many pizzas were delivered that had both exclusions and extras?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT  
 SUM(CASE
  WHEN exclusions IS NOT NULL AND extras IS NOT NULL THEN 1
  ELSE 0
  END) AS pizza_count_w_exclusions_extras
FROM #customer_orders AS c
JOIN #runner_orders AS r
 ON c.order_id = r.order_id
WHERE r.distance &amp;gt;= 1 
 AND exclusions &amp;lt;&amp;gt; ' ' 
 AND extras &amp;lt;&amp;gt; ' ';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F28guxu8m9de018ktrmrc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F28guxu8m9de018ktrmrc.png" alt=" " width="218" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only 1 pizza delivered that had both extra and exclusion topping. That’s one fussy customer!&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the total volume of pizzas ordered for each hour of the day?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT DATEPART(HOUR, [order_time]) AS hour_of_day, 
 COUNT(order_id) AS pizza_count
FROM #customer_orders
GROUP BY DATEPART(HOUR, [order_time]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ffrusu0aum97vth5xp7jm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffrusu0aum97vth5xp7jm.png" alt=" " width="191" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highest volume of pizza ordered is at 13 (1:00 pm), 18 (6:00 pm) and 21 (9:00 pm).&lt;/li&gt;
&lt;li&gt;Lowest volume of pizza ordered is at 11 (11:00 am), 19 (7:00 pm) and 23 (11:00 pm).&lt;/li&gt;
&lt;li&gt;What was the volume of orders for each day of the week?
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT FORMAT(DATEADD(DAY, 2, order_time),'dddd') AS day_of_week, 
-- add 2 to adjust 1st day of the week as Monday
 COUNT(order_id) AS total_pizzas_ordered
FROM #customer_orders
GROUP BY FORMAT(DATEADD(DAY, 2, order_time),'dddd');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fm020h27tt5ndh5ajau3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm020h27tt5ndh5ajau3o.png" alt=" " width="231" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are 5 pizzas ordered on Friday and Monday.&lt;/li&gt;
&lt;li&gt;There are 3 pizzas ordered on Saturday.&lt;/li&gt;
&lt;li&gt;There is 1 pizza ordered on Sunday.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  B. Runner and Customer Experience
&lt;/h2&gt;

&lt;p&gt;How many runners signed up for each 1 week period? (i.e. week starts 2021-01-01)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT DATEPART(WEEK, registration_date) AS registration_week,
 COUNT(runner_id) AS runner_signup
FROM runners
GROUP BY DATEPART(WEEK, registration_date);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fu12k6hlworu7ut08h2cy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu12k6hlworu7ut08h2cy.png" alt=" " width="228" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On Week 1 of Jan 2021, 2 new runners signed up.&lt;/li&gt;
&lt;li&gt;On Week 2 and 3 of Jan 2021, 1 new runner signed up.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the average time in minutes it took for each runner to arrive at the Pizza Runner HQ to pickup the order?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH time_taken_cte AS
(
 SELECT c.order_id, c.order_time, r.pickup_time, 
  DATEDIFF(MINUTE, c.order_time, r.pickup_time) AS pickup_minutes
 FROM #customer_orders AS c
 JOIN #runner_orders AS r
  ON c.order_id = r.order_id
 WHERE r.distance != 0
 GROUP BY c.order_id, c.order_time, r.pickup_time
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fx0wgv3vic1fk09oz1i2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx0wgv3vic1fk09oz1i2b.png" alt=" " width="458" height="201"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT AVG(pickup_minutes) AS avg_pickup_minutes
FROM time_taken_cte
WHERE pickup_minutes &amp;gt; 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fv68ymu082uh5xp623688.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fv68ymu082uh5xp623688.png" alt=" " width="154" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The average time taken in minutes by runners to arrive at Pizza Runner HQ to pick up the order is 15 minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Is there any relationship between the number of pizzas and how long the order takes to prepare?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH prep_time_cte AS
(
 SELECT c.order_id, COUNT(c.order_id) AS pizza_order, 
  c.order_time, r.pickup_time, 
  DATEDIFF(MINUTE, c.order_time, r.pickup_time) AS prep_time_minutes
 FROM #customer_orders AS c
 JOIN #runner_orders AS r
  ON c.order_id = r.order_id
 WHERE r.distance != 0
 GROUP BY c.order_id, c.order_time, r.pickup_time
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fhh4cvpytqlebeft2vdaz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhh4cvpytqlebeft2vdaz.png" alt=" " width="538" height="200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT pizza_order, AVG(prep_time_minutes) AS avg_prep_time_minutes
FROM prep_time_cte
WHERE prep_time_minutes &amp;gt; 1
GROUP BY pizza_order;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F8uiuawikly4egrggwpvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8uiuawikly4egrggwpvu.png" alt=" " width="237" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On average, a single pizza order takes 12 minutes to prepare.&lt;/li&gt;
&lt;li&gt;An order with 3 pizzas takes 30 minutes at an average of 10 minutes per pizza.&lt;/li&gt;
&lt;li&gt;It takes 16 minutes to prepare an order with 2 pizzas which is 8 minutes per pizza — making 2 pizzas in a single order the ultimate efficiency rate.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the average distance travelled for each customer?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT c.customer_id, AVG(r.distance) AS avg_distance
FROM #customer_orders AS c
JOIN #runner_orders AS r
 ON c.order_id = r.order_id
WHERE r.duration != 0
GROUP BY c.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fm0tffmdwv3grk6vxelf3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm0tffmdwv3grk6vxelf3.png" alt=" " width="223" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;(Assuming that distance is calculated from Pizza Runner HQ to customer’s place)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer 104 stays the nearest to Pizza Runner HQ at average distance of 10km, whereas Customer 105 stays the furthest at 25km.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the difference between the longest and shortest delivery times for all orders?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Firstly, let’s see all the durations for the orders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT order_id, duration
FROM #runner_orders
WHERE duration not like ' ';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fm4n0nbcp1m8eff90uoif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm4n0nbcp1m8eff90uoif.png" alt=" " width="590" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, we find the difference by deducting the shortest (MIN) from the longest (MAX) delivery times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    MAX(duration::NUMERIC) - MIN(duration::NUMERIC) AS delivery_time_difference
FROM #runner_orders
WHERE duration not like '% %'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fq0oyogd2yje23nqwh6y8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fq0oyogd2yje23nqwh6y8.png" alt=" " width="390" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The difference between longest (40 minutes) and shortest (10 minutes) delivery time for all orders is 30 minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the average speed for each runner for each delivery and do you notice any trend for these values?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT r.runner_id, c.customer_id, c.order_id, 
 COUNT(c.order_id) AS pizza_count, 
 r.distance, (r.duration / 60) AS duration_hr , 
 ROUND((r.distance/r.duration * 60), 2) AS avg_speed
FROM #runner_orders AS r
JOIN #customer_orders AS c
 ON r.order_id = c.order_id
WHERE distance != 0
GROUP BY r.runner_id, c.customer_id, c.order_id, r.distance, r.duration
ORDER BY c.order_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ftwjs3ie1g9em0dkm9wwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftwjs3ie1g9em0dkm9wwl.png" alt=" " width="494" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;(Average speed = Distance in km / Duration in hour)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runner 1’s average speed runs from 37.5km/h to 60km/h.&lt;/li&gt;
&lt;li&gt;Runner 2’s average speed runs from 35.1km/h to 93.6km/h. Danny should investigate Runner 2 as the average speed has a 300% fluctuation rate!&lt;/li&gt;
&lt;li&gt;Runner 3’s average speed is 40km/h&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What is the successful delivery percentage for each runner?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT runner_id, 
 ROUND(100 * SUM
  (CASE WHEN distance = 0 THEN 0
  ELSE 1
  END) / COUNT(*), 0) AS success_perc
FROM #runner_orders
GROUP BY runner_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fvejpler2qs7z8rtgizcq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvejpler2qs7z8rtgizcq.png" alt=" " width="189" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runner 1 has 100% successful delivery.&lt;/li&gt;
&lt;li&gt;Runner 2 has 75% successful delivery.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Runner 3 has 50% successful delivery&lt;br&gt;
(It’s not right to attribute successful delivery to runners as order cancellations are out of the runner’s control.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I will continue with Part A, B and C soon!&lt;/p&gt;
&lt;h2&gt;
  
  
  C. Ingredient Optimisation
&lt;/h2&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What are the standard ingredients for each pizza?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What was the most commonly added extra?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What was the most common exclusion?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generate an order item for each record in the customers_orders table in the format of one of the following:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Meat Lovers&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Meat Lovers&lt;/code&gt; - &lt;code&gt;Exclude Beef&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Meat Lovers - Extra Bacon&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Meat Lovers - Exclude Cheese, Bacon - Extra Mushroom, Peppers&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Generate an alphabetically ordered comma separated ingredient 
list for each pizza order from the customer_orders table and add a 2x in front of any relevant ingredients&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example: &lt;code&gt;"Meat Lovers: 2xBacon, Beef, ... , Salami"&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is the total quantity of each ingredient used in all delivered pizzas sorted by most frequent first?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  D. Pricing and Ratings
&lt;/h3&gt;

&lt;p&gt;If a Meat Lovers pizza costs $12 and Vegetarian costs $10 and there were no charges for changes — how much money has Pizza Runner made so far if there are no delivery fees?&lt;br&gt;
What if there was an additional $1 charge for any pizza extras?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add cheese is $1 extra&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The Pizza Runner team now wants to add an additional ratings system that allows customers to rate their runner, how would you design an additional table for this new dataset — generate a schema for this new table and insert your own data for ratings for each successful customer order between 1 to 5.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using your newly generated table — can you join all of the information together to form a table which has the following information for successful deliveries?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;customer_id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;order_id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;runner_id&lt;/code&gt;
-&lt;code&gt;rating&lt;/code&gt;
-&lt;code&gt;order_time&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pickup_time&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Time between order and pickup&lt;/li&gt;
&lt;li&gt;Delivery duration&lt;/li&gt;
&lt;li&gt;Average speed&lt;/li&gt;
&lt;li&gt;Total number of pizzas&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;If a Meat Lovers pizza was $12 and Vegetarian $10 fixed prices with no cost for extras and each runner is paid $0.30 per kilometre travelled — how much money does Pizza Runner have left over after these deliveries?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;E. Bonus Questions&lt;br&gt;
If Danny wants to expand his range of pizzas — how would this impact the existing data design? Write an INSERT statement to demonstrate what would happen if a new Supreme pizza with all the toppings was added to the Pizza Runner menu?&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>mysql</category>
      <category>database</category>
      <category>sqlchallenges</category>
    </item>
    <item>
      <title>8 Week SQL Challenge: Case Study #1 Danny’s Diner</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Tue, 11 Oct 2022 17:19:23 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/8-week-sql-challenge-case-study-1-dannys-diner-420g</link>
      <guid>https://forem.com/yaswanthteja/8-week-sql-challenge-case-study-1-dannys-diner-420g</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fb6sk307mr0kbz92aazqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fb6sk307mr0kbz92aazqk.png" alt=" " width="720" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Danny seriously loves Japanese food so in the beginning of 2021, he decides to embark upon a risky venture and opens up a cute little restaurant that sells his 3 favourite foods: sushi, curry and ramen.&lt;/p&gt;

&lt;p&gt;Danny’s Diner is in need of your assistance to help the restaurant stay afloat — the restaurant has captured some very basic data from their few months of operation but have no idea how to use &lt;br&gt;
their data to help them run the business.&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Danny wants to use the data to answer a few simple questions about his customers, especially about their&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;visiting patterns,&lt;/li&gt;
&lt;li&gt;how much money they’ve spent, and&lt;/li&gt;
&lt;li&gt;which menu items are their favourite.
Having this deeper connection with his customers will help him deliver a better and more personalised experience for his loyal customers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;He plans on using these insights to help him decide whether he should expand the existing customer loyalty program — additionally he needs help to generate some basic datasets so his team can easily inspect the data without needing to use SQL.&lt;/p&gt;

&lt;p&gt;The data set contains the following 3 tables which you may refer to the relationship diagram below to understand the connection&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sales&lt;/li&gt;
&lt;li&gt;members&lt;/li&gt;
&lt;li&gt;menu&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Table Relationship
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F26zr5ktg5gcxff007k2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F26zr5ktg5gcxff007k2c.png" alt=" " width="595" height="343"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Case Study Questions
&lt;/h2&gt;

&lt;p&gt;1.What is the total amount each customer spent at the restaurant?&lt;br&gt;
2.How many days has each customer visited the restaurant?&lt;br&gt;
3.What was the first item from the menu purchased by each customer?&lt;br&gt;
4.What is the most purchased item on the menu and how many times was it purchased by all customers?&lt;br&gt;
5.Which item was the most popular for each customer?&lt;br&gt;
6.Which item was purchased first by the customer after they became a member?&lt;br&gt;
7.Which item was purchased just before the customer became a member?&lt;br&gt;
8.What is the total items and amount spent for each member before they became a member?&lt;br&gt;
9.If each $1 spent equates to 10 points and sushi has a 2x points multiplier — how many points would each customer have?&lt;br&gt;
10In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi — how many points do customer A and B have at the end of January?&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;I’m using Microsoft SQL Server and these are the functions used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Aggregate functions — SUM, MIN, MAX&lt;/li&gt;
&lt;li&gt;Numerical functions — TOP&lt;/li&gt;
&lt;li&gt;Joins — Inner join, left join&lt;/li&gt;
&lt;li&gt;Temp tables (CTE)&lt;/li&gt;
&lt;li&gt;Windows function&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What is the total amount each customer spent at the restaurant?
We use the SUM and GROUP BY functions to find out total spent for each customer and JOIN function because customer_id is from sales table and price is from menu table.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, SUM(price) AS total_sales
FROM dbo.sales AS s
JOIN dbo.menu AS m
ON s.product_id = m.product_id
GROUP BY customer_id; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.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%2F5eczzf2fx728wscz6lda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5eczzf2fx728wscz6lda.png" alt=" " width="176" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A spent $76.&lt;/li&gt;
&lt;li&gt;Customer B spent $74.&lt;/li&gt;
&lt;li&gt;Customer C spent $36.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;How many days has each customer visited the restaurant?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use DISTINCT and wrap with COUNT function to find out number of days customer visited the restaurant.&lt;/p&gt;

&lt;p&gt;If we do not use DISTINCT for order_date, the number of days may be repeated. For example, if customer A visited the restaurant twice on ‘2021–01–07’, then number of days may have counted as 2 instead of 1 day.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customer_id, COUNT(DISTINCT(order_date)) AS visit_count
FROM dbo.sales
GROUP BY customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F9suj5o3p99ckyzhn57rk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9suj5o3p99ckyzhn57rk.png" alt=" " width="177" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A visited 4 times.&lt;/li&gt;
&lt;li&gt;Customer B visited 6 times.&lt;/li&gt;
&lt;li&gt;Customer C visited 2 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What was the first item from the menu purchased by each customer?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, we have to create a CTE using WITH function. In the summary CTE, we use DENSE_RANK and OVER(PARTITION BY ORDER BY) to create a new column rank based on order_date.&lt;/p&gt;

&lt;p&gt;I chose to use DENSE_RANK instead of ROW_NUMBER or RANK as the order_date is not time stamped hence, we do not know which item is ordered first if 2 or more items are ordered on the same day.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH ordered_sales_cte AS
(
 SELECT customer_id, order_date, product_name,
  DENSE_RANK() OVER(PARTITION BY s.customer_id
  ORDER BY s.order_date) AS rank
 FROM dbo.sales AS s
 JOIN dbo.menu AS m
  ON s.product_id = m.product_id
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fy5ywrp3hndnt1bldjvmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fy5ywrp3hndnt1bldjvmk.png" alt=" " width="307" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Subsequently, we GROUP BY the columns to show rank = 1 only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customer_id, product_name
FROM ordered_sales_cte
WHERE rank = 1
GROUP BY customer_id, product_name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2From1ubtyolv28ppu3gmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2From1ubtyolv28ppu3gmv.png" alt=" " width="193" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A’s first order are curry and sushi.&lt;/li&gt;
&lt;li&gt;Customer B’s first order is curry.&lt;/li&gt;
&lt;li&gt;Customer C’s first order is ramen.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What is the most purchased item on the menu and how many times was it purchased by all customers?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT TOP 1 (COUNT(s.product_id)) AS most_purchased, product_name
FROM dbo.sales AS s
JOIN dbo.menu AS m
 ON s.product_id = m.product_id
GROUP BY s.product_id, product_name
ORDER BY most_purchased DESC;SC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fx4pnqvaqomvj5b6k6irw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx4pnqvaqomvj5b6k6irw.png" alt=" " width="222" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most purchased item on the menu is ramen. Yummy!&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Which item was the most popular for each customer?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Again, we create a CTE to rank the number of orders for each product by DESC order for each customer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH fav_item_cte AS
(
 SELECT s.customer_id, m.product_name, 
  COUNT(m.product_id) AS order_count,
  DENSE_RANK() OVER(PARTITION BY s.customer_id
  ORDER BY COUNT(s.customer_id) DESC) AS rank
FROM dbo.menu AS m
JOIN dbo.sales AS s
 ON m.product_id = s.product_id
GROUP BY s.customer_id, m.product_name
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F9io1yjzelnxgo2rzros3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9io1yjzelnxgo2rzros3.png" alt=" " width="306" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, we generate results where rank of product = 1 only as the most popular product for individual customer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customer_id, product_name, order_count
FROM fav_item_cte 
WHERE rank = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fd1qu0skbo4inbj491edr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fd1qu0skbo4inbj491edr.png" alt=" " width="273" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A and C’s favourite item is ramen.&lt;/li&gt;
&lt;li&gt;Customer B enjoys all items in the menu. He/she is a true foodie.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Which item was purchased first by the customer after they became a member?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yeap, you can guess it! We’re creating another CTE.&lt;/p&gt;

&lt;p&gt;In this CTE, we filter order_date to be on or after their join_date and then rank the product_id by the order_date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH member_sales_cte AS 
(
 SELECT s.customer_id, m.join_date, s.order_date,   s.product_id,
         DENSE_RANK() OVER(PARTITION BY s.customer_id
  ORDER BY s.order_date) AS rank
     FROM sales AS s
 JOIN members AS m
  ON s.customer_id = m.customer_id
 WHERE s.order_date &amp;gt;= m.join_date
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fj5rwn92g6ayykv95pnxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj5rwn92g6ayykv95pnxg.png" alt=" " width="353" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we filter the table by rank = 1 to show first item purchased by customer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, s.order_date, m2.product_name 
FROM member_sales_cte AS s
JOIN menu AS m2
 ON s.product_id = m2.product_id
WHERE rank = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F3hzutg4d8dme6kyh610o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3hzutg4d8dme6kyh610o.png" alt=" " width="268" height="92"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;After Customer A became a member, his/her first order is curry, whereas it’s sushi for Customer B.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Which item was purchased just before the customer became a member?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Basically this is a reversed of Question #6. Create a CTE in order&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create new column rank by partitioning customer_id by DESC order_date to find out the order_date just before the customer became member&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filter order_date before join_date.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH prior_member_purchased_cte AS 
(
 SELECT s.customer_id, m.join_date, s.order_date, s.product_id,
         DENSE_RANK() OVER(PARTITION BY s.customer_id
         ORDER BY s.order_date DESC) AS rank
 FROM sales AS s
 JOIN members AS m
  ON s.customer_id = m.customer_id
 WHERE s.order_date &amp;lt; m.join_date
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fa2t6mhizknntcgm8disg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fa2t6mhizknntcgm8disg.png" alt=" " width="357" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, pull table to show the last item ordered by customer before becoming member.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, s.order_date, m2.product_name 
FROM prior_member_purchased_cte AS s
JOIN menu AS m2
 ON s.product_id = m2.product_id
WHERE rank = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fug1ahdd62jtjp6ih4pdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fug1ahdd62jtjp6ih4pdw.png" alt=" " width="269" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A’s order before he/she became member is sushi and curry and Customer B’s order is sushi. That must have been a real good sushi!&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What is the total items and amount spent for each member before they became a member?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, filter order_date before their join_date. Then, COUNT unique product_id and SUM the prices total spent before becoming member.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, COUNT(DISTINCT s.product_id) AS unique_menu_item, SUM(mm.price) AS total_sales
FROM sales AS s
JOIN members AS m
 ON s.customer_id = m.customer_id
JOIN menu AS mm
 ON s.product_id = mm.product_id
WHERE s.order_date &amp;lt; m.join_date
GROUP BY s.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F9ol01h0uo49vgowjdhde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9ol01h0uo49vgowjdhde.png" alt=" " width="283" height="83"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer: Before becoming members,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer A spent $ 25 on 2 items.&lt;/li&gt;
&lt;li&gt;Customer B spent $40 on 2 items.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;If each $1 spent equates to 10 points and sushi has a 2x points multiplier — how many points would each customer have?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s breakdown the question.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each $1 spent = 10 points.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But, sushi (product_id 1) gets 2x points, meaning each $1 spent = 20 points&lt;br&gt;
So, we use CASE WHEN to create conditional statements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If product_id = 1, then every $1 price multiply by 20 points&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All other product_id that is not 1, multiply $1 by 10 points&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, you can see the table below with new column, points.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH price_points AS
 (
 SELECT *, 
 CASE
  WHEN product_id = 1 THEN price * 20
  ELSE price * 10
  END AS points
 FROM menu
 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ff00lcqo99be09lffbpjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ff00lcqo99be09lffbpjs.png" alt=" " width="277" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the table above, we SUM the price, match it to the product_id and SUM the total_points.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, SUM(p.points) AS total_points
FROM price_points_cte AS p
JOIN sales AS s
 ON p.product_id = s.product_id
GROUP BY s.customer_id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F2kpkn9o3tvwdq86pydo1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2kpkn9o3tvwdq86pydo1.png" alt=" " width="190" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total points for Customer A, B and C are 860, 940 and 360.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi — how many points do customer A and B have at the end of January?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Again, we breakdown the question.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find out customer’s validity date (which is 6 days after join_date and inclusive of join_date) and last day of Jan 2021 (‘2021–01–21’).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH dates_cte AS 
(
 SELECT *, 
  DATEADD(DAY, 6, join_date) AS valid_date, 
  EOMONTH('2021-01-31') AS last_date
 FROM members AS m
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F4avt8d0s8f1ivjvyrsoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4avt8d0s8f1ivjvyrsoh.png" alt=" " width="324" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, use CASE WHEN to allocate points by dates and product_name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT d.customer_id, s.order_date, d.join_date, 
 d.valid_date, d.last_date, m.product_name, m.price,
 SUM(CASE
  WHEN m.product_name = 'sushi' THEN 2 * 10 * m.price
  WHEN s.order_date BETWEEN d.join_date AND d.valid_date THEN 2 * 10 * m.price
  ELSE 10 * m.price
  END) AS points
FROM dates_cte AS d
JOIN sales AS s
 ON d.customer_id = s.customer_id
JOIN menu AS m
 ON s.product_id = m.product_id
WHERE s.order_date &amp;lt; d.last_date
GROUP BY d.customer_id, s.order_date, d.join_date, d.valid_date, d.last_date, m.product_name, m.price

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fkiy24450y4ov2fznxret.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkiy24450y4ov2fznxret.png" alt=" " width="558" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our assumptions are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Day -X to Day 1 (customer becomes member (join_date), each $1 spent is 10 points and for sushi, each $1 spent is 20 points.&lt;/li&gt;
&lt;li&gt;Day 1 (join_date) to Day 7 (valid_date), each $1 spent for all items is 20 points.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Day 8 to last day of Jan 2021 (last_date), each $1 spent is 10 points and sushi is 2x points.&lt;br&gt;
Answer:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customer A has 1,370points.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customer B has 820 points.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bonus Questions
&lt;/h2&gt;

&lt;p&gt;Join All The Things&lt;br&gt;
Recreate the table with: customer_id, order_date, product_name, price, member (Y/N)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT s.customer_id, s.order_date, m.product_name, m.price,
CASE
 WHEN mm.join_date &amp;gt; s.order_date THEN 'N'
 WHEN mm.join_date &amp;lt;= s.order_date THEN 'Y'
 ELSE 'N'
 END AS member
FROM sales AS s
LEFT JOIN menu AS m
 ON s.product_id = m.product_id
LEFT JOIN members AS mm
 ON s.customer_id = mm.customer_id;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fgcrou8d4yz6l52565kon.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgcrou8d4yz6l52565kon.png" alt=" " width="358" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rank All The Things
&lt;/h3&gt;

&lt;p&gt;Danny also requires further information about the ranking of customer products, but he purposely does not need the ranking for non-member purchases so he expects null ranking values for the records when customers are not yet part of the loyalty program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH summary_cte AS 
(
 SELECT s.customer_id, s.order_date, m.product_name, m.price,
  CASE
  WHEN mm.join_date &amp;gt; s.order_date THEN 'N'
  WHEN mm.join_date &amp;lt;= s.order_date THEN 'Y'
  ELSE 'N' END AS member
 FROM sales AS s
 LEFT JOIN menu AS m
  ON s.product_id = m.product_id
 LEFT JOIN members AS mm
  ON s.customer_id = mm.customer_id
)
SELECT *, CASE
 WHEN member = 'N' then NULL
 ELSE
  RANK () OVER(PARTITION BY customer_id, member
  ORDER BY order_date) END AS ranking
FROM summary_cte;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fe3nj2tvbmhs404vttmw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fe3nj2tvbmhs404vttmw7.png" alt=" " width="412" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Insights
&lt;/h3&gt;

&lt;p&gt;From the analysis, we discover a few interesting insights that would be certainly useful for Danny.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer B is the most frequent visitor with 6 visits in Jan 2021.&lt;/li&gt;
&lt;li&gt;Danny’s Diner’s most popular item is ramen, followed by curry and sushi.&lt;/li&gt;
&lt;li&gt;Customer A and C loves ramen whereas Customer B seems to enjoy sushi, curry and ramen equally. Who knows, I might be Customer B!&lt;/li&gt;
&lt;li&gt;Customer A is the 1st member of Danny’s Diner and his first order is curry. Gotta fulfill his curry cravings!&lt;/li&gt;
&lt;li&gt;The last item ordered by Customers A and B before they became members are sushi and curry. Does it mean both of these items are the deciding factor? It must be really delicious for them to sign up as members!&lt;/li&gt;
&lt;li&gt;Before they became members, both Customers A and B spent $25 and $40.&lt;/li&gt;
&lt;li&gt;Throughout Jan 2021, their points for Customer A: 860, Customer B: 940 and Customer C: 360.&lt;/li&gt;
&lt;li&gt;Assuming that members can earn 2x a week from the day they became a member with bonus 2x points for sushi, Customer A has 660 points and Customer B has 340 by the end of Jan 2021.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you Danny Ma for the excellent case study! You can find it &lt;a href="https://8weeksqlchallenge.com/case-study-1/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and try it yourself.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>mysql</category>
      <category>database</category>
      <category>sqlchallenges</category>
    </item>
    <item>
      <title>Explanation of the most important machine learning models</title>
      <dc:creator>Yaswanth Teja</dc:creator>
      <pubDate>Sun, 25 Sep 2022 14:32:28 +0000</pubDate>
      <link>https://forem.com/yaswanthteja/explanation-of-the-most-important-machine-learning-models-5c2a</link>
      <guid>https://forem.com/yaswanthteja/explanation-of-the-most-important-machine-learning-models-5c2a</guid>
      <description>&lt;p&gt;In this blog, I will briefly explain some of the most commonly asked machine learning models in interviews. I will also list important parameters related to each model and a source to find a detailed explanation of the same topic, so you can dig deeper if and when required.&lt;/p&gt;

&lt;p&gt;Machine learning models can be broadly categorized into two categories supervised and unsupervised learning. Further in supervised learning, we have two broad categories regression and classification. The following sections explain each of them briefly to give you the necessary insights.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fon4ayynlr32ya7a0v7n1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fon4ayynlr32ya7a0v7n1.png" alt=" " width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: I am providing models, which I believe are the most common ones and should be prepared before giving any data science interview. However this list is subjective.&lt;/p&gt;

&lt;h1&gt;
  
  
  Supervised learning
&lt;/h1&gt;

&lt;p&gt;In supervised learning, the data that you use for training the models is “labeled”. This means the output for each input is known. For example, if your model is trying to predict house prices, you might have variables like size of the house, number of floors, etc. When your data is labeled, it means you would also have a variable that contains the house price.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fujmi7pkpuarxi84y4loi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fujmi7pkpuarxi84y4loi.png" alt=" " width="720" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example was for regression. Let’s have a close look at regression and classification now.&lt;/p&gt;

&lt;h1&gt;
  
  
  Classification
&lt;/h1&gt;

&lt;p&gt;In classification, the output of the model is discrete. For example, consider dog vs cat image classification, where we predict whether the image contains the family of dogs or cats. The class (which is the output of the model) will be discrete here i.e. either dog or cat. Now, we will look through the models which are commonly used for classification.&lt;/p&gt;

&lt;h1&gt;
  
  
  Logistic regression
&lt;/h1&gt;

&lt;p&gt;Don’t get confused; it has the word “regression” in the name, but it is used for classification. Logistic regression uses an equation to create a curve with your data and then uses this curve to predict the outcome of a new observation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fs192n5iurvc9kj6t0j2m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs192n5iurvc9kj6t0j2m.png" alt=" " width="720" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In essence, a logistic equation is created so that the output values can only be between 0 and 1.&lt;/p&gt;

&lt;h1&gt;
  
  
  Support Vector Machine
&lt;/h1&gt;

&lt;p&gt;Support Vector Machines (SVM) form a boundary between data points for classification. For example, in the case of 2 dimensions, SVM will create a boundary such that the majority of data points of one class fall on one side of the boundary, and most of the other class falls on the other side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6cijdwmcnd2iy4hor3ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6cijdwmcnd2iy4hor3ss.png" alt=" " width="621" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the goal in SVM is to find the boundary which maximizes the margin (described in the above image).&lt;/p&gt;

&lt;p&gt;Important Parameter/Concepts — Kernel, C, Gamma, Margin&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvd61oifwvtxuej8qdqeg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvd61oifwvtxuej8qdqeg.png" alt=" " width="621" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Decision Tree
&lt;/h1&gt;

&lt;p&gt;In the decision tree, you basically ask questions about your observation and follow the tree down until you reach an outcome, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fuh6kp7wvs2zodl30mj01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuh6kp7wvs2zodl30mj01.png" alt=" " width="720" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, each square is called a node, and more number of nodes here will cause more overfitting of the model on the dataset.&lt;/p&gt;

&lt;p&gt;Important Parameter/Concepts — Node, Leaf Node, Entropy, Information Gain&lt;/p&gt;

&lt;h1&gt;
  
  
  Random Forest
&lt;/h1&gt;

&lt;p&gt;It is an ensemble learning technique that uses multiple decision trees to give the final output. Random forests create multiple decision trees based on &lt;a href="https://machinelearningmastery.com/a-gentle-introduction-to-the-bootstrap-method/" rel="noopener noreferrer"&gt;bootstrapped datasets&lt;/a&gt; of the original dataset and randomly select subsets of the variable at each step of decision trees. During inference, we get outputs from all the decision trees, and finally, we select the output which has maximum votes. Random forests are generally preferred over decision trees as they prevent overfitting.&lt;/p&gt;

&lt;p&gt;Important Parameter/Concepts — Number of decision trees, Size of bootstrapped data, Number of random forest feature, and everything else mentioned in decision tree’s section.&lt;/p&gt;

&lt;h1&gt;
  
  
  Naive Bayes
&lt;/h1&gt;

&lt;p&gt;This model requires a strong foundation in probability. Its working is based on the Bayes theorem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0xn5gl330fkbzrippt19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0xn5gl330fkbzrippt19.png" alt=" " width="181" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, we are trying to find out “What is the probability of a particular output (y) given an input (X)”. We assume that each input variable are independent of each other, so P(X|y) can be written as&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzo8otyag8ie0gppw0ja5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzo8otyag8ie0gppw0ja5.png" alt=" " width="293" height="19"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further, P(X) (present in the denominator) will remain the same for all possible output (0, 1). So we calculate P(y|X) for all the possible output, and the output with the highest probability will be the final prediction of the model.&lt;/p&gt;

&lt;h1&gt;
  
  
  Neural Network
&lt;/h1&gt;

&lt;p&gt;One of the essential machine learning models nowadays. It is called a neural network because it is modeled after how the neurons in our brains work. The neural network finds patterns in the dataset, which sometimes even humans can not recognize. They are very powerful and can be of great use, especially when dealing with images, text, and audio.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffdihawxf5cgvvp7wgzvb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffdihawxf5cgvvp7wgzvb.png" alt=" " width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image, the first two circles represent the input layer, the middle column of 5 circles represents the hidden layer, and the last circle in yellow is the output layer. Each node in the hidden layers represents both a linear function and an activation function.&lt;/p&gt;

&lt;p&gt;Important Parameter/Concepts — Learning rate, Loss function, backpropagation, activation function.&lt;/p&gt;

&lt;h1&gt;
  
  
  Regression
&lt;/h1&gt;

&lt;p&gt;In regression, the output of the model is continuous. For example, consider house price prediction, where we predict the price of a house based on size and number of floors. This house price will be a continuos variable, not discrete.&lt;/p&gt;

&lt;h1&gt;
  
  
  Linear regression
&lt;/h1&gt;

&lt;p&gt;It is similar to logistic regression. Here we try to find a line that best fits the data. This best-fit line is then used to make predictions about new data points.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fobdjzvaki3ti657kbqmx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fobdjzvaki3ti657kbqmx.png" alt=" " width="438" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The linear regression is nothing but an equation of line or plane. This equation contains a coefficient for each input variable, reflecting each input variable's sensitivity towards the output.&lt;/p&gt;

&lt;p&gt;Important Parameter/Concepts — Regression coefficient&lt;/p&gt;

&lt;p&gt;Other important models which can be used for regression tasks are decision tree, random forest, neural network, etc. Since I have already covered them above, I will skip them now.&lt;/p&gt;

&lt;h1&gt;
  
  
  Unsupervised Learning
&lt;/h1&gt;

&lt;p&gt;In unsupervised learning, the data that you use for training models is “Unlabeled”. This means you only have input. In unsupervised learning, we try to find patterns only based on input data. Two main methods used in unsupervised learning include clustering and dimensionality reduction.&lt;/p&gt;

&lt;h1&gt;
  
  
  Clustering
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6h6ebiz07iu52qkuto0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6h6ebiz07iu52qkuto0u.png" alt=" " width="320" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clustering involves the grouping or clustering of data points based on some criteria. It is used for customer segmentation, fraud detection, document classification, etc.&lt;/p&gt;

&lt;p&gt;Some common clustering techniques include k-means clustering, hierarchical clustering, mean shift clustering, and density-based clustering. While each technique uses different criteria in finding clusters, they all aim to achieve the same thing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dimensionality reduction
&lt;/h1&gt;

&lt;p&gt;It involves reducing the number of input variables under consideration by finding/extracting a set of more important variables. There are two ways to reduce the input features by feature elimination or by feature extraction.&lt;/p&gt;

&lt;p&gt;One of the most famous methods for dimensionality reduction is Principal Component Analysis (PCA) which is based on feature extraction.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I hope this attempt to summarize all the important machine learning models comes in handy for you all. If you think there is any scope of improvement or you want an explanation about any other model, please let me know. If you would like to keep yourself updated with more topics about machine learning,&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
