<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sonal Shrivastav</title>
    <description>The latest articles on DEV Community by Sonal Shrivastav (@sonalshrivastav).</description>
    <link>https://dev.to/sonalshrivastav</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4086257%2F5dd72a42-2c60-41e9-9965-325182138400.png</url>
      <title>DEV Community: Sonal Shrivastav</title>
      <link>https://dev.to/sonalshrivastav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sonalshrivastav"/>
    <language>en</language>
    <item>
      <title>Building an End-to-End Bike Rental Demand Forecasting Pipeline with Machine Learning and FastAPI</title>
      <dc:creator>Sonal Shrivastav</dc:creator>
      <pubDate>Thu, 20 Aug 2026 08:17:32 +0000</pubDate>
      <link>https://dev.to/sonalshrivastav/building-an-end-to-end-bike-rental-demand-forecasting-pipeline-with-machine-learning-and-fastapi-2gdj</link>
      <guid>https://dev.to/sonalshrivastav/building-an-end-to-end-bike-rental-demand-forecasting-pipeline-with-machine-learning-and-fastapi-2gdj</guid>
      <description>&lt;p&gt;Accurate demand prediction is critical for urban mobility networks. If a bike-sharing network miscalculates, stations run empty or overflow, leading to lost revenue and frustrated commuters. &lt;/p&gt;

&lt;p&gt;In my latest portfolio project, I built an end-to-end Machine Learning regression pipeline in Python that forecasts hourly bike rental demand with an &lt;strong&gt;R² score of 0.9546&lt;/strong&gt; (MAE: 22.81). &lt;/p&gt;

&lt;p&gt;Here is a breakdown of how I designed, built, and productionized this pipeline.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Dataset &amp;amp; Preventing Target Leakage
&lt;/h2&gt;

&lt;p&gt;Working with over 17,000+ hourly records, the raw dataset included columns for &lt;code&gt;casual&lt;/code&gt; and &lt;code&gt;registered&lt;/code&gt; users, which together sum up to the target variable (&lt;code&gt;cnt&lt;/code&gt;). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Trap:&lt;/strong&gt; If you leave these component counts in your feature set during training, the model essentially "cheats" by seeing components of the answer, leading to inflated, unrealistic metrics (target leakage).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Fix:&lt;/strong&gt; I explicitly reconstructed the target column (&lt;code&gt;cnt = casual + registered&lt;/code&gt;) and &lt;strong&gt;dropped&lt;/strong&gt; the component columns prior to splitting the data to ensure zero evaluation contamination.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Feature Engineering &amp;amp; Preprocessing
&lt;/h2&gt;

&lt;p&gt;To capture non-linear commuter trends (like morning rush hours or weekend dips), I engineered custom domain flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;is_rush_hour&lt;/code&gt;: Flagged peak transit windows.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;temp_diff&lt;/code&gt;: Captured temperature variations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;is_weekend&lt;/code&gt;: Differentiated weekday commuter habits from weekend leisure riding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Categorical features were handled using one-hot encoding, and missing values were systematically imputed (Median for numerical variables, Mode for categorical ones).&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Model Benchmarking &amp;amp; Hyperparameter Tuning
&lt;/h2&gt;

&lt;p&gt;I evaluated several regression algorithms, comparing Decision Trees, Random Forests, and Gradient Boosting. &lt;/p&gt;

&lt;p&gt;To squeeze out maximum performance, I leveraged &lt;strong&gt;5-fold RandomizedSearchCV&lt;/strong&gt; to optimize the hyperparameters of the Gradient Boosting Regressor, successfully achieving a robust &lt;strong&gt;R² of 0.9546&lt;/strong&gt; and an &lt;strong&gt;MAE of 22.81&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Production Deployment with FastAPI
&lt;/h2&gt;

&lt;p&gt;A model sitting in a Jupyter notebook doesn't solve real-world problems. To make this pipeline production-ready:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I serialized the trained model and scaler using &lt;strong&gt;Joblib&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;I built a lightweight &lt;strong&gt;FastAPI&lt;/strong&gt; prediction endpoint (&lt;code&gt;/predict&lt;/code&gt;) to handle real-time inference requests.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🔗 Resources &amp;amp; Walkthrough
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/sonalshri12/Bike-Demand-Regression-Pipeline" rel="noopener noreferrer"&gt;Bike Demand Regression Pipeline&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;7-Min Video Demo:&lt;/strong&gt; &lt;a href="https://youtu.be/yOxDHdvUOTE" rel="noopener noreferrer"&gt;Watch on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this breakdown helpful or have questions about handling target leakage in regression tasks, let me know in the comments!&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>python</category>
      <category>machinelearning</category>
      <category>fastapi</category>
    </item>
  </channel>
</rss>
