<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vatsal Lakhmani on Medium]]></title>
        <description><![CDATA[Stories by Vatsal Lakhmani on Medium]]></description>
        <link>https://medium.com/@vatsallakhmani1?source=rss-0027c2fd826c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aLExnL0S1-1jQ0owzSTWXg@2x.jpeg</url>
            <title>Stories by Vatsal Lakhmani on Medium</title>
            <link>https://medium.com/@vatsallakhmani1?source=rss-0027c2fd826c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:33:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vatsallakhmani1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PCA, The Legendary Algorithm That Sees Data Differently]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/pca-the-legendary-algorithm-that-sees-data-differently-b757dcb687ad?source=rss-0027c2fd826c------2"><img src="https://cdn-images-1.medium.com/max/600/1*NMpkvp12K1DQPc3LFaOC7A.png" width="600"></a></p><p class="medium-feed-snippet">Understand how machines uncover hidden structure in high-dimensional data.</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/pca-the-legendary-algorithm-that-sees-data-differently-b757dcb687ad?source=rss-0027c2fd826c------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/pca-the-legendary-algorithm-that-sees-data-differently-b757dcb687ad?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b757dcb687ad</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 30 Mar 2026 20:35:08 GMT</pubDate>
            <atom:updated>2026-03-30T20:35:08.330Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Building MockMentor: AI Interview Coach That Reads Your Resume]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science-collective/building-mockmentor-ai-interview-coach-that-reads-your-resume-1f892a0a71f0?source=rss-0027c2fd826c------2"><img src="https://cdn-images-1.medium.com/max/1152/1*ObztpWh8x01mvGrP1xM6xg.png" width="1152"></a></p><p class="medium-feed-snippet">How I built a resume-aware mock interview platform using LangChain, Google Gemini, Streamlit, and MLflow</p><p class="medium-feed-link"><a href="https://medium.com/data-science-collective/building-mockmentor-ai-interview-coach-that-reads-your-resume-1f892a0a71f0?source=rss-0027c2fd826c------2">Continue reading on Data Science Collective »</a></p></div>]]></description>
            <link>https://medium.com/data-science-collective/building-mockmentor-ai-interview-coach-that-reads-your-resume-1f892a0a71f0?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f892a0a71f0</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 22 Dec 2025 14:08:50 GMT</pubDate>
            <atom:updated>2026-05-21T18:40:26.286Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Turning Medical Knowledge Into AI Conversations: My RAG Chatbot Journey]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/turning-medical-knowledge-into-ai-conversations-my-rag-chatbot-journey-29a11e0c37e5?source=rss-0027c2fd826c------2"><img src="https://cdn-images-1.medium.com/max/657/1*TCe3_dcCHPJktjwg-Y4_-g.png" width="657"></a></p><p class="medium-feed-snippet">A practical walkthrough of ingestion, vector DBs, LLMs, and deploying a real medical assistant app.</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/turning-medical-knowledge-into-ai-conversations-my-rag-chatbot-journey-29a11e0c37e5?source=rss-0027c2fd826c------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/turning-medical-knowledge-into-ai-conversations-my-rag-chatbot-journey-29a11e0c37e5?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/29a11e0c37e5</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 08 Dec 2025 03:29:00 GMT</pubDate>
            <atom:updated>2025-12-08T03:29:00.854Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[5 SQL Questions 90% of Candidates Can’t Answer (But You Should)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/5-sql-questions-90-of-candidates-cant-answer-but-you-should-803a3f5fa870?source=rss-0027c2fd826c------2"><img src="https://cdn-images-1.medium.com/max/675/1*FMHMjn8EBSiFBa6R4FL3-Q.png" width="675"></a></p><p class="medium-feed-snippet">Stop studying textbook queries. Start thinking like a data detective.</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/5-sql-questions-90-of-candidates-cant-answer-but-you-should-803a3f5fa870?source=rss-0027c2fd826c------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/5-sql-questions-90-of-candidates-cant-answer-but-you-should-803a3f5fa870?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/803a3f5fa870</guid>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Wed, 27 Aug 2025 14:34:40 GMT</pubDate>
            <atom:updated>2025-08-27T14:34:40.769Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[8 Pandas Functions You’re Not Using (But Should)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science-collective/8-pandas-functions-youre-not-using-but-should-76310ec8c33c?source=rss-0027c2fd826c------2"><img src="https://cdn-images-1.medium.com/max/1024/1*5mg8NRcIBOmaMdQOiclw8A.png" width="1024"></a></p><p class="medium-feed-snippet">The functions that will save your sanity</p><p class="medium-feed-link"><a href="https://medium.com/data-science-collective/8-pandas-functions-youre-not-using-but-should-76310ec8c33c?source=rss-0027c2fd826c------2">Continue reading on Data Science Collective »</a></p></div>]]></description>
            <link>https://medium.com/data-science-collective/8-pandas-functions-youre-not-using-but-should-76310ec8c33c?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/76310ec8c33c</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 03:46:27 GMT</pubDate>
            <atom:updated>2025-08-18T03:46:27.933Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Your Gateway to Machine Learning: Linear Regression Explained]]></title>
            <link>https://python.plainenglish.io/your-gateway-to-machine-learning-linear-regression-explained-e3a1e86f622a?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3a1e86f622a</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Sun, 10 Aug 2025 13:50:58 GMT</pubDate>
            <atom:updated>2025-08-14T00:18:17.998Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Learn how to predict values and visualize trends with hands-on examples.</blockquote><p><strong>Let’s play a game!</strong></p><p>I’ll give you a set of values, and your task is to <strong>predict the missing ones</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*cy1rkPIdifH3KjLHU5Mfdw.png" /></figure><p>Say you’re given a table where each key has a corresponding value. <br>Now, you need to guess the value for <strong>key</strong> <strong>7</strong>.</p><p>After looking at the data, you notice a pattern: each value is simply <strong>4 times</strong> its key. So, for <strong>key 7</strong>, the value is <strong>28</strong>. <strong>Easy, right?</strong></p><p>That’s because we humans are naturally good at spotting patterns. We intuitively figured out that the relationship is: <strong>value = key × 4</strong></p><p>But unfortunately, our computers — <strong>despite being super smart</strong>, can’t recognize this pattern on their own, not without help.<br>We have to teach them how to learn from data.<br>And that’s exactly what <strong><em>Machine Learning </em></strong>is about: making our machines learn with the help of data and then predicting values for new, unseen data.</p><p>Over time, many algorithms have been developed to help machines learn patterns from data. <br>One of the <strong>simplest </strong>and <strong>oldest </strong>algorithm is <strong>Linear Regression, </strong>which is the main focus of this blog.</p><h3>Understanding “Regression” and “Linear”</h3><p>Now that we’ve mentioned <strong>Linear Regression</strong>, you might be wondering: what does <strong>“regression”</strong> mean, and what’s <strong>“linear”</strong> about this algorithm?</p><p>Okay, so algorithms are often grouped based on the type of prediction we want our model to make: whether that’s <strong>predicting a numerical value</strong> or <strong>categorizing data into specific groups</strong>.</p><p>Broadly, they fall into <strong>two main categories</strong>.</p><ol><li><strong>Regression:</strong> Here, the <strong>goal is to predict a numerical, continuous value</strong>. For example, predicting a company’s stock price after learning from its past data.</li><li><strong>Classification:</strong> Here, the <strong>goal is to assign data to a specific category</strong>. For example, predicting whether an image is of a cat or a dog.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*uLURmG0EygitRqnviRHyJw.png" /></figure><p>So, I hope this answers the first question: <strong>“What does regression mean?”</strong></p><p>Now, let’s answer what <strong>“linear”</strong> means in this algorithm. <br>In <strong>Linear Regression</strong>, we start with the <strong>assumption that our data is linearly distributed</strong>, in other words, the data points roughly follow a <strong>straight-line pattern.<br></strong>Therefore <strong>Linear Regression</strong> is a <strong>machine learning algorithm</strong> which tries to <strong>find the equation of a line</strong> which <strong>passes closest to all the data points</strong> <strong>in the given sample</strong>. Once we have the equation of line then we can use it to <strong>predict numerical values for a new test data</strong>.</p><h3>Visualizing the Problem</h3><p>Let’s make this concrete with a real example. <br>Imagine you’re trying to <strong>predict house prices based on their size</strong>. <br>You collect data from your neighborhood:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*EVW0NktDZJhbhzwOFBW0cg.png" /></figure><p>If you plot this data on a graph with <strong>house size on the x-axis</strong> and <strong>price on the y-axis</strong>, you’ll notice the <strong>points form a straight line</strong> (look at the below graph for your reference.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hbkqKI-kOXh5VW--0LfmiA.png" /></figure><p>This is exactly the kind of <strong>linear relationship</strong> that Linear Regression excels at capturing.<br>But here’s the thing, real-world data is rarely this perfect. You might have some houses that are slightly <strong>overpriced</strong> or <strong>underpriced</strong> due to factors like <strong>location, condition,</strong> or <strong>market fluctuations</strong>. So your data points might look scattered around an imaginary straight line.<br>Real world data will be having some noise and would look somewhat like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*9wN4BKnBpilBbMk-cznIDg.png" /></figure><p><strong>This is where Linear Regression comes to the rescue.</strong> It finds the <strong>“best-fit line”</strong> that passes as close as possible to all your data points, even when they’re not perfectly aligned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*SKYIidM6w7yZqdrz9yXCbQ.png" /></figure><h3>The Mathematical Foundation</h3><p>Now you might be thinking: “<strong><em>Great, Linear Regression finds the best-fit line for our data… but how exactly does it figure that out?</em></strong>”</p><p>Remember our simple example from the beginning where <strong>value = key × 4</strong>? That’s actually a linear equation! <br>In mathematical terms, we can write it as: <strong>y = 4x </strong>where <strong>x represents the key </strong>and<strong> y represents values</strong>.</p><p>More generally, any straight line can be represented by the equation:<br><strong>y = mx + b<br></strong>Where:<br> . <strong>y</strong> is our target variable (what we want to predict)<br> . <strong>x</strong> is our input variable (what we use to make predictions)<br> . <strong>m</strong> is the slope of the line<br> . <strong>b</strong> is the y-intercept (where the line crosses the y-axis)</p><p>In our house price example:<br> . <strong>y</strong> would be the house price<br> . <strong>x</strong> would be the house size<br> . <strong>m</strong> tells us how much the price increases for each additional square foot<br> . <strong>b</strong> might represent a base price that every house has regardless of size</p><h3>Finding the Best Line Through Error Minimization</h3><p>Now comes the interesting part: how does the algorithm actually find the best possible line among infinite possibilities?</p><p>The key concept here is <strong>error minimization</strong>. For any line we draw, some data points will be above the line and some below it. The distance between each actual data point and our predicted line represents an <strong>error</strong> or <strong>residual</strong>.</p><p>Let’s see this with an example.</p><p>I want to compute <strong>total error</strong> for the below data points:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*ekGV7PUfY_gMKSX3pQ5K5Q.png" /></figure><p>Suppose we have a best-fit line for the data points and we’ve calculated the residuals for each data point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*EBtMiZBgZlR8_xPOAc8CLA.png" /></figure><p>If we simply add the residuals for each data point, the <strong>total error</strong> becomes:</p><p><strong>TOTAL ERROR = 4 + (-3) + (-3) = (-2) units.</strong></p><p>Is this okay? <strong>Absolutely not.</strong><br>Here, the positive and negative errors cancel each other out, which hides the actual size of the errors. What we really want is to <strong>measure and minimize the overall magnitude of the errors</strong>, without letting positive and negative values cancel each other.</p><p>To avoid the problem of positive and negative errors canceling each other out, <strong>we square each error before adding them together</strong>.</p><p>This way:</p><ul><li><strong>All errors become positive</strong></li><li><strong>Larger errors are penalized more heavily than smaller ones</strong></li></ul><p>Once squared, we take the average of these values to get the <strong>Mean Squared Error (MSE)</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*R8fq_u71JwUiJgENtsUZ6g.png" /></figure><p>Where:</p><ul><li><strong>yᵢ = actual value</strong></li><li><strong>ŷᵢ = predicted value from the line</strong></li><li><strong>n = total number of data points</strong></li></ul><p>The <strong>predicted value ŷᵢ </strong>is calculated using the formula:<br><strong>ŷᵢ = mxᵢ + b<br></strong>where <strong>m is the slope of the line</strong> and <strong>b is the y-intercept of the line</strong>.</p><p>The <strong>MSE</strong> is our cost function, it tells us how far off our predictions are from the actual values. <br><strong>The smaller the MSE, the better our line fits the data.</strong></p><h3>The Algorithm in Action</h3><p>So our main goal is to find the<strong> optimal values for slope (m)</strong> and<strong> intercept (b)</strong> which minimizes the value of <strong>Mean Squared Error (MSE)</strong>.</p><h4>The algorithm follows these steps:</h4><ol><li><strong>Start with a random line</strong> (random values for m and b)</li><li><strong>Calculate how far off</strong> each prediction is from the actual values</li><li><strong>Adjust the line slightly</strong> to reduce these errors</li><li><strong>Repeat </strong>until you find the line with minimum total error</li></ol><p>Think of it like this: Imagine you’re trying to adjust the angle of a garden hose to water plants in a straight line. You start with a random angle, see which plants you missed, then adjust the angle slightly to hit more plants. You keep tweaking until you’ve found the perfect angle that waters all plants as evenly as possible.</p><p><strong>Important Note:</strong> Linear Regression works best when your data is actually linearly distributed. If your data follows a curved pattern or has complex relationships, Linear Regression might not be the best choice.</p><h3>Hands-On Implementation: Building Your First Linear Regression Model</h3><p>Now that we understand the theory, let’s see <strong>Linear Regression in action</strong> with some practical. 🚀</p><p>We’re going to create a <strong>mini project</strong> that demonstrates <strong>Simple Linear Regression</strong> in action. <br>Our mission: <strong>predict employee salaries based on their years of experience</strong>.</p><p>I’ve chosen a small, clean dataset that’s perfect for learning: no messy data cleaning required, just pure focus on understanding how Linear Regression works in practice.</p><h4>The Dataset: Employee Salaries</h4><p>Our dataset contains employee records with just two columns:</p><ul><li><strong>Years of Experience</strong> — Our input variable (x)</li><li><strong>Salary</strong> — Our target variable (y)</li></ul><p><em>You can download the dataset from </em><a href="https://github.com/watzal/SimpleLinearRegression/blob/main/data.csv"><strong><em>this link</em></strong></a><strong><em> </em></strong><em>to follow along.</em></p><h4>Setting Up Our Environment</h4><p>First, let’s import the essential libraries we’ll need:</p><pre># Import all the necessary libraries<br>import numpy as np                # For numerical operations and linear algebra<br>import pandas as pd               # For data manipulation and analysis<br>import matplotlib.pyplot as plt   # For data visualization<br>from sklearn.linear_model import LinearRegression  # For building our Linear Regression model</pre><p><strong>Quick note:</strong> We could absolutely build <strong>Linear Regression from scratch</strong> (and that’s a great exercise for later!), but for this blog we’ll use <strong>Scikit-learn’s</strong> implementation so we can focus on understanding the concepts rather than getting lost in mathematical implementations.</p><h4>Loading and Exploring Our Data</h4><p>Let’s load our dataset and take a peek at what we’re working with:</p><pre># Read the CSV file into a pandas DataFrame<br>data = pd.read_csv(&#39;data.csv&#39;)</pre><p>Our data looks something like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*sbEKStspLbFPg5eqRzKJ-Q.png" /></figure><p>You might notice an extra unnamed index column in your dataset. Let’s clean that up:</p><pre># Drop unnecessary columns if any (e.g., unnamed index column)<br>data.drop(columns=[&#39;Unnamed: 0&#39;], inplace=True)</pre><h4>Visualizing the Data</h4><p><strong>Before building any model, it’s crucial to visualize your data</strong>. This helps us understand if a linear relationship actually exists:</p><pre># Visualize the data<br>plt.scatter(data[&#39;YearsExperience&#39;], data[&#39;Salary&#39;], color=&#39;blue&#39;, marker=&#39;o&#39;)<br>plt.title(&#39;Years of Experience vs Salary&#39;)<br>plt.xlabel(&#39;Years of Experience&#39;)<br>plt.ylabel(&#39;Salary&#39;)<br>plt.show()</pre><p><strong>What do you see?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*nyKGmIWrwu9iNiLHCFlVQA.png" /></figure><p>The data points should form a roughly straight line pattern, <strong>confirming that Linear Regression is a good choice for this problem.</strong></p><h4>Preparing Data for the Model</h4><pre># Prepare data for the model<br>X = data[[&#39;YearsExperience&#39;]]  # Input features (must be 2D array for sklearn)<br>y = data[&#39;Salary&#39;]             # Target variable (1D array is fine)<br><br>print(&quot;Input shape (X):&quot;, X.shape)<br>print(&quot;Target shape (y):&quot;, y.shape)</pre><p><strong>Important:</strong> Notice that X is a 2D array (DataFrame with double brackets) while y is a 1D array (Series with single brackets). This is exactly what Scikit-learn expects.</p><h4>Creating and Training Our Model</h4><p>Here’s where the magic happens : <strong>training our Linear Regression model</strong>:</p><pre># Create and train the model<br>model = LinearRegression()      # Create Linear Regression model<br>model.fit(X, y)     # Train the model</pre><p>In that single <strong>fit()</strong> call, the algorithm is:</p><ul><li><strong>Finding the optimal slope and intercept</strong></li><li><strong>Minimizing the Mean Squared Error</strong></li><li><strong>Learning the best-fit line through our data</strong></li></ul><p>That’s it! In just two lines, we’ve trained a machine learning model.</p><h4>Making Predictions and Visualizing Results</h4><p>Now let’s see how well our model learned the pattern:</p><pre># Make predictions<br>predictions = model.predict(X)<br><br># Visualize the best-fit line<br>plt.scatter(X, y, color=&#39;blue&#39;, label=&#39;Actual Data&#39;)<br>plt.plot(X, predictions, color=&#39;red&#39;, linewidth=2, label=&#39;Best Fit Line&#39;)<br>plt.xlabel(&#39;Years of Experience&#39;)<br>plt.ylabel(&#39;Salary&#39;)<br>plt.title(&#39;Linear Regression - Best Fit Line&#39;)<br>plt.legend()<br>plt.show()</pre><p><strong>Beautiful!</strong> You should see a red line that passes very close to most of the blue data points. This line represents our model’s understanding of the relationship between experience and salary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*9UfCOT4gg96G1ApOyA6HRA.png" /></figure><h4>Understanding Our Model’s Parameters</h4><p>We can also find the slope and the y intercept of our best fit line.</p><pre># Print the model parameters<br>print(&quot;Slope (m):&quot;, model.coef_[0])<br>print(&quot;Intercept (b):&quot;, model.intercept_)</pre><h4>Making Predictions for New Data</h4><p>Let’s suppose that we have to calculate salary for an employee having 5 years of experience.</p><pre># Predict salary for a new experience value<br>new_exp = np.array([[5]])  # Example: 5 years of experience<br>pred_salary = model.predict(new_exp)<br>print(f&quot;Predicted Salary for {new_exp[0][0]} years of experience: {pred_salary[0]:.2f}&quot;)</pre><p>We get an output of <strong>72,098.02</strong>, meaning an employee with <strong>5 years of experience</strong> can expect a salary of around <strong>72k</strong>. In the same way, we can use our model to predict salaries for multiple new experience values, giving us a powerful tool for forecasting.</p><h3>Wrapping Up</h3><p>🎉 <strong>Congratulations!</strong> You’ve just built and understood your first machine learning model from start to finish.</p><p>In this journey, you:</p><ul><li>Learned <strong>what Linear Regression is</strong> and when to use it</li><li>Understood <strong>how the best-fit line is found</strong> using error minimization</li><li>Explored <strong>why Mean Squared Error (MSE)</strong> is used instead of plain error sums</li><li>Implemented a working model with <strong>Scikit-learn</strong> in just a few lines of code</li><li>Made predictions for <strong>new, unseen data</strong></li></ul><p>This might seem simple now, but it’s the foundation for many powerful models you’ll encounter later. Mastering this concept means you’ve taken a solid step into the world of data science and machine learning.</p><p>💡 <strong>Next step</strong>: In the upcoming blog, we’ll strip away <strong>Scikit-learn’s</strong> magic and code <strong>Linear Regression from scratch</strong>, so you can watch the math come alive, step-by-step. Once you understand the mechanics, you’ll be able to tackle more complex algorithms with confidence.</p><h3>Bonus</h3><ul><li><a href="https://github.com/watzal/SimpleLinearRegression"><strong>GitHub Link for the project</strong></a></li><li><a href="https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html"><strong>Linear Regression documentation</strong></a></li></ul><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? <strong>We don’t receive any funding, we do this to support the community. ❤️</strong></p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a>, <a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. You can also subscribe to our <a href="https://newsletter.plainenglish.io/"><strong>weekly newsletter</strong></a>.</p><p>And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3a1e86f622a" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/your-gateway-to-machine-learning-linear-regression-explained-e3a1e86f622a">Your Gateway to Machine Learning: Linear Regression Explained</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dockerizing Your First Machine Learning Project]]></title>
            <link>https://ai.plainenglish.io/dockerizing-your-first-machine-learning-project-c732327800fa?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c732327800fa</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[fastapi]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Fri, 04 Jul 2025 22:04:36 GMT</pubDate>
            <atom:updated>2025-08-14T11:36:22.726Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>No more “it works on my machine” — make your ML project run anywhere with Docker.</blockquote><p>Imagine you’re cooking your favorite pack of ramen noodles. But this time, the company doesn’t include the tastemaker (the magic spice mix). Instead, they just give you a long recipe to make it yourself.<br>You try your best to follow it, but there’s a catch, there’s no guarantee it’ll taste the same. Maybe you added a little too much garlic. Maybe someone else didn’t have the exact chili powder. Suddenly, your “favorite” ramen tastes different for everyone.<br>That’s why the company normally includes the tastemaker inside the packet, so no matter who cooks it, it tastes exactly the same.<br>This is exactly what Docker does for your machine learning models, apps, or software. It <strong><em>packages everything — code, dependencies, environment</em> </strong>— so it runs the same everywhere, every time. <strong>No guesswork, no “it works on my machine” issues.</strong></p><p>In software development, this “tastemaker” is your application environment — <strong>things like the programming language version, libraries, dependencies, and system settings.</strong> When these aren’t bundled properly, the end result (your application) can behave very differently on different machines.</p><p>So how does this play out in real life?<br>Let’s say you’re building an application using an old version of Python — say Python 2.0. Some of the features you used have been deprecated in newer versions.<br>You upload your code to GitHub.<br>Now, the testing team comes in. They clone your repository and try to run the code on their machines — but they’re using the <strong>latest version of Python</strong>.<br><strong>Boom.</strong> The code breaks. <strong>Functions don’t exist anymore, syntax throws errors, and now everyone’s frustrated.</strong><br>This kind of <strong>“works on my machine”</strong> problem happens all the time — not just with Python, but with different operating systems, library versions, and dependencies.<br>Docker solves this by allowing you to create a <strong>container</strong> that has everything your code needs: <strong>the same Python version, the same libraries, the same OS environment — all packed together.</strong> Anyone can run the container and get the exact same behavior, whether they’re on Windows, Mac, Linux, or the cloud.</p><h4>So, what exactly is Docker?</h4><p>Docker is a platform that helps developers build, share, and run applications using something called <em>containers</em>.<br>A container is like a lightweight, portable box that holds your application along with everything it needs to run — the code, Python version, libraries, system tools — all bundled together. This makes sure your app works the same on any machine, every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*iI15MCRy5CrPcdm8zNaN5g.png" /></figure><h4>Why do we need Docker?</h4><ol><li><strong>Consistency across environments</strong>: Applications often behave differently in development, testing, and production environments due to variations in configurations, dependencies, and infrastructure, so <strong>Docker encapsulates all the necessary components, ensuring the applications runs consistently across the environments.</strong></li><li><strong>Scalability</strong>: Imagine you’re trying to buy concert tickets for your favorite music band. You’re on the website, excited and ready — but so are <strong>thousands of other fans</strong>, all at the same time.<br>If the server can’t handle that sudden spike in traffic, it might crash — and boom, no tickets for anyone. Not a great user experience.<br>This is where <strong>scalability</strong> comes in. You need a way to <strong>handle more users when demand spikes</strong>, and scale back down when things are quiet.<br>Now, suppose your website is <strong>Dockerized</strong> — meaning the code, configuration, and environment are all packaged into a container.<br>With Docker, you can quickly <strong>spin up multiple instances</strong> (i.e., replicas) of your application container to handle the extra load. <strong>It’s like opening more counters at a busy box office.<br>More users? Just add more containers.<br>Less traffic? Scale back down.</strong><br>Docker makes this kind of <strong>on-demand scaling fast, efficient, and cost-effective</strong>, especially when combined with orchestration tools like Kubernetes or Docker Swarm.</li><li><strong>Isolation</strong>: Running multiple applications on the same machine can often lead to conflicts — like dependency clashes, version mismatches, or one app consuming too much CPU or memory and affecting others. Docker solves this problem by creating isolated environments for each application, ensuring they don’t interfere with one another. Each container has its own file system, libraries, dependencies, and system tools, completely separate from the host and from other containers. This isolation guarantees stable performance, cleaner development, and the ability to run multiple apps side by side without headaches.</li></ol><h4>How exactly Docker is used?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YWTdFLSG7-OeU5QhoIv_gw.png" /></figure><p>Take a moment to carefully study the image above. If you truly understand what it’s showing, you’ll have a solid grasp of how Docker works across all types of applications.</p><p>Let’s get started with the technicalities:</p><h4><strong>Docker/Container Engine</strong></h4><p>It is the core component of the Docker platform, responsible for creating, running, and managing Docker containers. It serves as the runtime that powers Docker’s containerization capabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*gSjhvVgURUzCN8I0Fkj9uQ.png" /><figcaption>Components of a Docker Engine</figcaption></figure><p>The Docker Engine is made up of <strong>three main components</strong>:</p><ul><li><strong>Docker Daemon (Server)</strong>: The Docker daemon is the background service that runs on the host machine. It manages Docker objects such as images, containers, networks, and volumes. It listens for Docker API requests and processes them, handling container lifecycle operations like starting, stopping, building, etc.</li><li><strong>REST API: </strong>The Docker <strong>REST API</strong> acts as the bridge between the Docker CLI (what users type) and the Docker Daemon (the engine doing the work). It allows remote communication between the user interface and the background service.</li><li><strong>Docker CLI (Command Line Interface): </strong>This is the tool you use in your terminal to interact with Docker — using commands like docker run, docker build, or docker ps. The CLI sends these commands to the Docker Daemon via the REST API.</li></ul><h4><strong>Docker Image</strong></h4><p>A <strong>Docker image</strong> is a <strong>lightweight, standalone, and executable package</strong> that contains everything needed to run a piece of software: including the code, runtime, libraries, environment variables, and configuration files.<br>Think of it as a <strong>blueprint</strong> or <strong>snapshot</strong> of your application and its environment.<br>Docker containers are created from these images. When you run a Docker image, it becomes a <strong>container</strong> — an actual running instance of the image.</p><p>Components of a Docker Image:</p><ul><li><strong>Base Image: </strong>This is the foundational layer, it could be a minimal OS like alpine, ubuntu, or even a language runtime like python, node, etc. It provides the basic file system and essential tools.</li><li><strong>Application Source Code: </strong>This is your actual source code of the application that you are building.</li><li><strong>Dependencies: </strong>List of libraries, packages, or modules that your application needs to run.</li><li><strong>Metadata: </strong>Information about the image, such as environment variables, labels, and exposed ports.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KwvosrBR7CFc2gC4IOpOtg.png" /><figcaption>Docker Image lifecycle</figcaption></figure><h4>Docker File</h4><p>A <strong>Dockerfile</strong> is a text file containing a series of instructions that <strong>define</strong> <strong>how to build a Docker image</strong>. Each instruction creates a new <strong>layer</strong>, enabling efficient image building and reuse of unchanged layers. Docker files help automate the creation of images, ensuring consistency, repeatability, and version control across environments.</p><p>This is what a Docker File looks like (Don’t worry — it’s simpler than it seems. We’ll learn how to write one step by step in the practical section.):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*kUjiROU5uENzvLejYHn2OQ.png" /><figcaption>Sample Docker file</figcaption></figure><h4>Docker Container</h4><p>A <strong>Docker container</strong> is a lightweight, portable, and isolated environment that runs your application along with everything it needs, including the code, libraries, and system tools. It ensures the application behaves the same regardless of where it is running, whether on a developer’s laptop, a testing server, or in production. Containers are created from <strong>Docker images</strong>, which are read-only templates that include all the required components. When you run an image, it becomes a container — a live, working instance of your application in a consistent and controlled setup.</p><h4>Docker Registry</h4><p>A <strong>Docker registry</strong> is a service that stores and distributes Docker images. It acts as a central repository where users can push, pull, and manage images. <strong>Docker Hub</strong> is the most well-known public registry, but organizations can also set up <strong>private registries</strong> to securely store and manage images for internal use. <strong>Just like a GitHub repository stores your code, a Docker registry stores your Docker images.</strong><br>The main key component of the Docker Registry is the Repository, which is a collection of related Docker images, typically different versions of the same application. Each repository can hold multiple tags, representing<br>different versions of an image (For example, myapp:1.0,<br>myapp:2.0, and myapp:latest are tags for different versions of the myapp image).</p><p>Some common examples of Docker Registry are: <br>1) <strong>Docker Hub</strong> (The default public registry provided by Docker, which hosts a vast number of public images and also supports private repositories.)<br>2) <strong>Private registries (</strong>Custom registries set up by organizations to securely store and manage their own Docker images.)<br>3) <strong>Third-Party Registries </strong>(Cloud platforms like Amazon Elastic Container Registry (ECR), Google Container Registry (GCR), Azure Container Registry (ACR) for seamless deployment and management of images within cloud infrastructure.)</p><h4><strong>Now let’s put all of this into action.</strong></h4><p>I trained a <strong>Decision Tree Classifier model </strong>on the good old <strong>Iris dataset</strong> (yes, the classic one we all love to reuse) and exposed a /predict endpoint using <strong>FastAPI</strong>.</p><p>Building the model and its API isn’t the main focus of this blog, but I’ll share the <a href="https://github.com/watzal/IrisModel">GitHub link to the full project</a> if you’d like to check it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*ckoIaoBg6wX8ZFJEa6lJIw.png" /><figcaption>Project folder without the Docker file</figcaption></figure><p>This is our current project folder.</p><p><strong>Note:</strong> Make sure to install <strong>Docker Desktop</strong> on your machine and create an account. You’ll also need an account on <a href="https://hub.docker.com/"><strong>Docker Hub</strong></a> to push your images to the registry.</p><p><strong>Step 1: </strong>In the same project folder, create a file named Dockerfile (no extension). After this, your project should look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*1_DKHpIeVQ0DcsP7p7cXSw.png" /><figcaption>Project folder with the Docker file</figcaption></figure><p>Here’s what your Dockerfile should contain:</p><pre># Use Python 3.11 base image<br>FROM python:3.11-slim<br><br># Set working directory<br>WORKDIR /app<br><br># Copy requirements and install dependencies<br>COPY requirements.txt .<br>RUN pip install --no-cache-dir -r requirements.txt<br><br># Copy rest of application code<br>COPY . .<br><br># Expose the application port<br>EXPOSE 8000<br><br># Command to start FastAPI application<br>CMD [&quot;uvicorn&quot;, &quot;app:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]</pre><p>Let’s understand the Docker File line-by-line:</p><ul><li><strong>FROM python:3.11-slim : </strong>This instruction tells Docker <strong>which base image to use</strong> as the starting point for your container. In simple terms you are telling Docker to start building this container using a minimal version of Python 3.11.</li><li><strong>WORKDIR /app: </strong>This is an instruction in a Dockerfile that sets the <strong>working directory</strong> inside the Docker container. It tells Docker: “From this point onward, all file operations (like COPY, RUN, CMD, etc.) should be done inside the /app folder in the container.&quot;</li><li><strong>COPY requirements.txt . : </strong>This will copy the requirements.txt file (Text file that lists the packages required for a Python project) from our project folder and paste it to the current working directory, therefore the new path for requirements.txt will be <strong>/app/requirements.txt</strong></li><li><strong>RUN pip install — no-cache-dir -r requirements.txt: </strong>This will download all the packages that are necessary for the application to execute successfully.</li><li><strong>COPY . .</strong> : This instruction will copy all the application code to the current working directory.</li><li><strong>EXPOSE 8000 : </strong>This instruction <strong>informs Docker</strong> that your application runs on port <strong>8000</strong> inside the container.</li><li><strong>CMD [“uvicorn”, “app:app”, “ — host”, “0.0.0.0”, “ — port”, “8000”]: <br></strong>CMD: Defines the <strong>default command</strong> to run when the container starts.<br>This runs a <strong>FastAPI application</strong> using <strong>Uvicorn</strong> inside the Docker container.</li></ul><p>Now once you are done with building the Docker file for our application, we will proceed towards building our Docker Image.</p><p><strong>Step 2:</strong> Build a Docker Image.<br>Open your terminal, navigate to the project directory, and run:</p><pre>docker build -t your-dockerhub-username/iris-api . </pre><p>Replace your-dockerhub-username with your actual Docker Hub username.</p><p><strong>Step 3:</strong> At this point, your Docker image has been created locally. Now let’s upload it to <strong>Docker Hub</strong>, so it’s accessible from anywhere — like a cloud-based GitHub for container images.</p><p>First, make sure you’re logged in to Docker Hub via the terminal:</p><pre>docker login</pre><p>It will prompt you to enter your Docker Hub <strong>username</strong> and <strong>password</strong>.</p><p>Now push your image to your Docker Hub repository:</p><pre>docker push your-dockerhub-username/iris-api</pre><p>Make sure to replace your-dockerhub-username with your actual Docker Hub username.</p><p>If everything goes well, your image will be available on your Docker Hub profile.</p><p><strong>Step 4: </strong>Now that your image has been successfully pushed to Docker Hub, your application is officially dockerized 🎉.</p><p>To run the application, open <strong>Docker Desktop</strong>, launch its terminal , and run it using:</p><pre>docker run -p 8000:8000 your-dockerhub-username/iris-api</pre><p>Replace your-dockerhub-username with your actual Docker Hub username.</p><p>This command does the following:</p><ul><li>-p 8000:8000 maps your <strong>local machine&#39;s port 8000</strong> to the <strong>container’s port 8000.</strong></li><li>It spins up a <strong>container instance</strong> from your image.</li></ul><p>Now, open your browser and go to <strong>http://localhost:8000/docs.</strong><br>This is the <strong>FastAPI Swagger UI</strong>, where you can interact with your /predict endpoint directly.</p><h4>Conclusion</h4><p>And that’s a wrap!<br><strong>We started with noodles, tossed in some FastAPI, and ended up with a fully dockerized machine learning application</strong>. From understanding what Docker is, to learning <em>why</em> it matters in the ML lifecycle, to actually packaging and running your own container — you’ve done it all.</p><p>If you’ve followed along, congrats — you now know how to:</p><ul><li>Understand the purpose and components of Docker</li><li>Write your own Dockerfile</li><li>Build and push Docker images</li><li>Run containers and expose ML APIs with ease</li></ul><p><strong>Happy Dockering!</strong> 🐳<br>Let me know if you found this helpful or if you’d like a follow-up blog on deploying containers to the cloud.</p><h4>Bonus</h4><ul><li><a href="https://github.com/watzal/IrisModel">GitHub Link for the project</a></li><li><a href="https://docs.docker.com/">Docker documentation</a></li></ul><h3>A message from our Founder</h3><p><strong>Hey, </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>Sunil</strong></a><strong> here.</strong> I wanted to take a moment to thank you for reading until the end and for being a part of this community.</p><p>Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? <strong>We don’t receive any funding, we do this to support the community. ❤️</strong></p><p>If you want to show some love, please take a moment to <strong>follow me on </strong><a href="https://linkedin.com/in/sunilsandhu"><strong>LinkedIn</strong></a><strong>, </strong><a href="https://tiktok.com/@messyfounder"><strong>TikTok</strong></a>, <a href="https://instagram.com/sunilsandhu"><strong>Instagram</strong></a>. You can also subscribe to our <a href="https://newsletter.plainenglish.io/"><strong>weekly newsletter</strong></a>.</p><p>And before you go, don’t forget to <strong>clap</strong> and <strong>follow</strong> the writer️!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c732327800fa" width="1" height="1" alt=""><hr><p><a href="https://ai.plainenglish.io/dockerizing-your-first-machine-learning-project-c732327800fa">Dockerizing Your First Machine Learning Project</a> was originally published in <a href="https://ai.plainenglish.io/">Artificial Intelligence in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pydantic : The Data Validation Powerhouse  in Python]]></title>
            <link>https://medium.com/@vatsallakhmani1/pydantic-the-data-validation-powerhouse-in-python-6acc3bf78dc5?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/6acc3bf78dc5</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[pydantic]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 30 Jun 2025 17:50:10 GMT</pubDate>
            <atom:updated>2025-06-30T17:53:24.505Z</atom:updated>
            <content:encoded><![CDATA[<h3>Pydantic : The Data Validation Powerhouse 💪 in Python</h3><blockquote><strong>Pydantic</strong>: your data’s strict but friendly bodyguard — validating, cleaning, and keeping bugs out .</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*YD3ReKsw7jaFUA4ebxAnBw.png" /></figure><p>As we all know that Python is a <strong>dynamically typed</strong> language — meaning a variable can hold different types of values at different times.</p><pre>a = 3<br>a = &#39;Hello&#39;</pre><p>First, the variable a holds an integer, and then it switches to a string —<strong> no complaints from Python!</strong></p><p>This kind of flexibility is great for quick scripts or experimentation — but is it really a good practice for <strong>industry-level code</strong>?</p><p>In large-scale applications, this dynamic behavior can become a <strong>double-edged sword ⚔️</strong>. Without strict type enforcement, bugs sneak in silently, data becomes unpredictable, and debugging turns into a nightmare. 😵‍💫</p><p>That’s where <strong>Pydantic</strong> steps in — bringing structure, validation, and peace of mind to your Python code without sacrificing its elegance.</p><h4>🧠 So, What is Pydantic?</h4><p><strong>Pydantic</strong> is a powerful Python library that uses type hints to define clear, type‑safe models and automatically validate and parse data into clean, correct, well‑structured Python objects with minimal boilerplate — saving you tons of time.<br>Whether you’re building APIs, handling user input, or reading from a config file, Pydantic acts like a smart gatekeeper for your data 🛡️.</p><p><strong>Let’s consider a real scenario 🤔:<br></strong>Suppose you’ve been hired by a school, and your task is to insert student records into the database. You want to make sure every student’s data is valid. <br>The schema of the student database should have the following fields with the given conditions:</p><ul><li><strong>Name: </strong>Name should <strong>not be empty</strong> and must be a <strong>string </strong>with maximum length 50<strong>.</strong></li><li><strong>Email: </strong>Must be a <strong>valid</strong> Email Address.</li><li><strong>DOB (Date of Birth): </strong>Must be a <strong>valid date</strong> in YYYY-MM-DD format.</li><li><strong>Age: </strong>Should be a <strong>derived / computed field</strong> from <strong>DOB</strong>.</li><li><strong>Weight (assuming the unit to be in kgs): </strong>Must be a <strong>float</strong> and <strong>greater than 0</strong>.</li><li><strong>Height (assuming the unit to be in meters): </strong>Must be a <strong>float</strong> and <strong>greater than 0</strong>.</li><li><strong>BMI: </strong>Should be a <strong>derived / computed field</strong> from <strong>Weight &amp; Height</strong>.</li><li><strong>Contact Details (nested object):<br>1</strong>. <strong>Phone</strong>: Must be a valid <strong>10-digit number<br>2. Address: </strong>Must be a <strong>non-empty string.</strong></li></ul><p>Now technically, I could validate each field manually like this:</p><pre>if type(name) == str and len(name) &gt; 0:<br>    print(&quot;Valid name&quot;)</pre><p>And for an email field:</p><pre>if &quot;@&quot; in email and &quot;.&quot; in email:<br>    print(&quot;Looks like a valid email&quot;)</pre><p>But here’s the problem — this approach quickly becomes messy, repetitive, and hard to maintain. Every time you add a new field or use the same logic elsewhere, you’ll end up repeating the same checks.</p><p>❌ <strong>This violates the DRY principle</strong> (Don’t Repeat Yourself), which is a core concept in clean, maintainable code.</p><p>Instead of scattering validations throughout your codebase, wouldn’t it be great if you could <strong>define all your rules once</strong> and have them automatically enforced everywhere?<br>✨ That’s exactly what <strong>Pydantic</strong> helps you do — centralized, reusable, and automatic data validation with zero clutter.</p><p><strong>Let’s code</strong> 🧑‍💻:</p><p>🧪 <strong>Let’s Build the Student Model (with Pydantic)<br></strong>Now, it’s time to replace our messy manual checks with something elegant — a Pydantic model!<br> Start with a simple version and build it step by step.</p><pre>from pydantic import BaseModel, EmailStr, Field<br>from typing import Annotated<br>from datetime import date</pre><p>Then introduce the basic <strong>Student </strong>model:</p><pre>#  Main model to store student data<br>class Student(BaseModel):<br>    name: Annotated[str, Field(min_length = 1, max_length=50, description = &#39;Name of the Student&#39;)]<br>    email: EmailStr<br>    dob: date<br>    # Weight in kgs, must be a float and greater than 0<br>    weight: Annotated[float, Field(gt=0, description = &#39;Weight of the Student&#39;)]<br>    # Height in m, must be a float and greater than 0<br>    height: Annotated[float, Field(gt=0, description = &#39;Height of the Student&#39;)]<br>    <br>    # ContactDetails is another class which inherits from the BaseModel (present in the below code block)<br>    contact_details: ContactDetails<br><br>    # Computed fields (not given by user, we calculate them)<br>    # We have assigned default values to them.<br>    # age is of integer type and bmi is of float type.<br>    age: int = 0 <br>    bmi: float = 0.0<br>    <br><br>    # Code to calculate Age<br>    @computed_field<br>    @property<br>    def age(self) -&gt; int:<br>        today = date.today()<br>        # Calculate age from DOB<br>        age = today.year - self.dob.year - (<br>            (today.month, today.day) &lt; (self.dob.month, self.dob.day)<br>        )<br>        # Check if DOB is in the future<br>        if self.dob &gt; today:<br>            raise ValueError(&quot;Date of birth cannot be in the future&quot;)<br>        return age<br>    <br>    # Code to calculate BMI<br>    @computed_field<br>    @property<br>    def bmi(self) -&gt; float:<br>        bmi = round(self.weight/(self.height**2),2)<br>        return bmi</pre><p>Then we have the <strong>ContactDetails </strong>model:</p><pre># A nested model to store contact information<br>class ContactDetails(BaseModel):<br>    # Phone number must be a string and contains exactly 10 digits<br>    phone: Annotated[str, Field(min_length=10, max_length=10)]<br><br>    # Address must be a non-empty string<br>    address: Annotated[str, Field(min_length=1)]<br><br>    # Extra validation: phone must contain only digits<br>    # mode = &#39;after&#39; means that the below logic will run after the other fields are validated.<br>    # cls is the class ContactDetails.<br>    # model is the full, already-validated object created from the input data.<br>    @model_validator(mode=&quot;after&quot;)<br>    def validate_phone(cls, model):<br>        if not model.phone.isdigit():<br>            raise ValueError(&quot;Phone number must contain only digits&quot;)<br>        return model</pre><ul><li><strong>BaseModel</strong> gives us automatic validation.</li><li><strong>EmailStr</strong> ensures email is correct.</li><li><strong>Field() </strong>lets us set extra rules (like max length).</li><li><strong>Annotated[…] </strong>is a way to attach runtime metadata to types without changing how type checkers interpret them.</li><li><strong>@model-validator</strong> is a <strong>special function decorator</strong> in Pydantic v2 that lets you <strong>run your own custom logic</strong> <em>after</em> (or <em>before</em>) Pydantic validates all the individual fields.</li></ul><h4><strong>🚀 How to Use This Model ?</strong></h4><pre># Sample input data (like what you&#39;d get from a form or API)<br>student_data = {<br>    &quot;name&quot;: &quot;Riya Sharma&quot;,<br>    &quot;email&quot;: &quot;riya.sharma@example.com&quot;,<br>    &quot;dob&quot;: &quot;2006-08-10&quot;,<br>    &quot;weight&quot;: 55.2,<br>    &quot;height&quot;: 1.625,<br>    &quot;contact_details&quot;: {<br>        &quot;phone&quot;: &quot;9876543210&quot;,<br>        &quot;address&quot;: &quot;123, Green Park, Delhi&quot;<br>    }<br>}<br><br># Create the Student model and validate everything<br>student = Student(**student_data)<br><br><br>&#39;&#39;&#39;<br><br>Logic to insert the Student data in the Database after successful validation.<br><br>&#39;&#39;&#39; <br><br><br># Print the validated and computed fields<br>print(&quot;✅ Student Data Saved Successfully!&quot;)<br>print(&quot;Name:&quot;, student.name)<br>print(&quot;Email:&quot;, student.email)<br>print(&quot;Age:&quot;, student.age)<br>print(&quot;BMI:&quot;, student.bmi)<br>print(&quot;Phone:&quot;, student.contact_details.phone)<br>print(&quot;Address:&quot;, student.contact_details.address)</pre><p>🧠<strong> What Just Happened?</strong></p><ul><li>We gave raw data as a dictionary.</li><li>Student(**student_data) validated the entire input.</li><li>Pydantic automatically:<br>- Checked name, email, DOB format, weight, height, phone.<br>- Calculated age and BMI<br>- Gave us a clean Python object we can safely work with</li></ul><h4>🔁<strong> What Happens When Data is Wrong?</strong></h4><p>Pydantic doesn’t just validate good data — it <strong>actively protects you</strong> from bad data too. Let’s try giving it some incorrect inputs and see what happens.</p><pre>from pydantic import ValidationError<br><br>bad_data = {<br>    &quot;name&quot;: &quot;&quot;,  # empty name ❌<br>    &quot;email&quot;: &quot;not-an-email&quot;,  # invalid email ❌<br>    &quot;dob&quot;: &quot;2030-01-01&quot;,  # date in future ❌<br>    &quot;weight&quot;: -10,  # invalid weight ❌<br>    &quot;height&quot;: 0,  # invalid height ❌<br>    &quot;contact_details&quot;: {<br>        &quot;phone&quot;: &quot;abc123&quot;,  # not 10 digits ❌<br>        &quot;address&quot;: &quot;&quot;  # empty address ❌<br>    }<br>}<br><br>try:<br>    student = Student(**bad_data)<br>except ValidationError as e:<br>    print(e)</pre><p>🧠 <strong>What happens?<br></strong>You get a <strong>detailed error report</strong> that tells you exactly what went wrong — field by field.</p><pre>1 validation error for Student<br>email<br>  value is not a valid email address (type=value_error.email)<br>weight<br>  ensure this value is greater than 0 (type=value_error.number.not_gt; limit_value=0)<br>...</pre><p>✨ Therefore, you don’t need to manually write if-else statements — Pydantic acts like your <strong>automatic data gatekeeper</strong>.</p><h4><strong>📦 Where Would You Use Pydantic in Real Projects?</strong></h4><p>You might be wondering: <em>“Is this just for demos?”</em> <strong>Not at all</strong>.</p><p>Pydantic is used in <strong>real-world production systems</strong>, including:</p><ul><li>🧬 <strong>APIs</strong> — especially with <strong>FastAPI</strong> (which is built entirely on top of Pydantic!)</li><li>🗂️ <strong>Data cleaning</strong> — before inserting into a database or sending over a network</li><li>🧾 <strong>Config file parsing</strong> — read from .json, .env, .yaml, and validate all values</li><li>🧪 <strong>Test data generation</strong> — create fake, validated test records with tools like pydantic-factories</li></ul><blockquote>🔥 <strong>Pydantic</strong> is used by companies like <strong>Microsoft, Netflix, and Uber</strong> in their <strong>backend Python services</strong>.</blockquote><h4><strong>🎯 Final Thoughts: Clean Data, Clean Code</strong></h4><p>Data is the backbone of every application — and bad data leads to bad decisions, broken features, and long debugging sessions.</p><p><strong>Pydantic</strong> offers a clean, declarative way to:</p><ul><li>Validate your input</li><li>Document your schema</li><li>Automatically catch errors early etc.</li></ul><p>All without giving up the <strong>flexibility and friendliness of Python</strong> 🐍.</p><p>So the next time you start a new project, remember:</p><p><strong><em>“Don’t trust your data — validate it with Pydantic.”</em></strong><em> ✅</em></p><h4>🔗 Bonus: Explore More</h4><ul><li>📘 <a href="https://docs.pydantic.dev/latest/">Official Pydantic Docs</a></li><li>⚡<a href="https://fastapi.tiangolo.com/tutorial/body/"> FastAPI + Pydantic Demo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6acc3bf78dc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Python’s requests Library – Part 1: HTTP for Humans]]></title>
            <link>https://medium.com/@vatsallakhmani1/mastering-pythons-requests-library-part-1-http-for-humans-d5375ea944ee?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5375ea944ee</guid>
            <category><![CDATA[requests-library]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[http-request]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Sat, 03 May 2025 07:50:19 GMT</pubDate>
            <atom:updated>2025-05-03T07:50:19.729Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A beginner-friendly guide to making web requests the easy way — using Python’s most loved HTTP library.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*WQXyKr4_6cabVooLy2-Xnw.png" /></figure><p>When working with Python, one of the most common tasks is interacting with the web — whether you’re fetching data from an API, submitting a form, or downloading a file. Thankfully, Python’s requests library makes these tasks incredibly simple. In this post, we’ll explore how to use requests to send HTTP requests in the most Pythonic way possible—with clear examples you can start using right away.</p><p>Before we dive into the details of the requests library, it’s important to understand the basics of how websites and web services communicate. At the heart of this interaction is <strong>HTTP (HyperText Transfer Protocol)</strong>—the foundation of data exchange on the web. We&#39;ll briefly explore how HTTP works, the different types of <strong>HTTP methods</strong> like GET and POST, and what those <strong>status codes</strong> (like 200 or 404) actually mean. This will give you the context you need to use requests effectively and confidently.</p><h3>🧭 What is HTTP?</h3><p><strong>HTTP (HyperText Transfer Protocol)</strong> is the protocol used by browsers and servers to communicate over the web. When you visit a website or call an API, your browser (or code) sends a <strong>request</strong>, and the server sends back a <strong>response</strong> — usually containing data like HTML, JSON, or images.</p><p>Think of it like placing an order at a restaurant:</p><ul><li>You (the client) ask for something (request).</li><li>The server (kitchen) prepares and returns it (response).</li></ul><h3>✋ Common HTTP Methods</h3><p>HTTP defines several <strong>methods</strong> for how a client can interact with a server. The most commonly used ones are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*f-ZAtVDDedEO_XVVI_DOkw.png" /></figure><h3>📬 What Are HTTP Status Codes?</h3><p>When a server responds to a request, it sends a <strong>status code</strong> along with the response to tell you how things went. Here are some common ones:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*0jJLn2p7PRrRoICEKQ1ptg.png" /></figure><ol><li><strong>1xx (Informational)</strong>: The request was received, and the server is processing it.</li><li><strong>2xx (Successful)</strong>: The request was successfully processed.</li><li><strong>3xx (Redirection):</strong> The resource has been moved, and further action is required.</li><li><strong>4xx (Client Error)</strong>: The request was invalid or cannot be processed by the server.</li><li><strong>5xx (Server Error)</strong>: The server failed to fulfill a valid request.</li></ol><p>Knowing these status codes helps you debug issues and handle responses more intelligently in your code.</p><p>Now that we’ve covered the basics of HTTP and status codes, it’s time to dive into the heart of our topic: the <strong>requests library</strong>.</p><h3>🚀 What is requests?</h3><p>Python’s requests library is a simple, human-friendly HTTP library. The requests library is a popular Python tool that makes it easy to send HTTP requests—like <strong>GET and POST</strong>—to communicate with websites and APIs. It’s designed to be user-friendly, so you can work with web data using plain Python code without needing to worry about the complicated details of networking.</p><h3>🖥️ Where Do These Requests Go?</h3><p>Websites are hosted on <strong>servers</strong> — powerful computers that store website files and respond to user requests. When you use Python’s requests library, you&#39;re essentially acting like a browser: sending a request to the server, and waiting for a <strong>response</strong>.</p><p>Here’s how it works step by step:</p><ol><li>You use requests.get() (or another method) in your Python code.</li><li>That sends an <strong>HTTP request</strong> to the website’s server.</li><li>The server processes the request and sends back a <strong>response</strong>, which might include data like HTML, JSON, or an image.</li><li>You can then access that response directly in your Python script.</li></ol><p>So, in simple terms: <strong>Your Python script (client) talks to a website (server) using HTTP — and </strong><strong>requests makes that conversation easy.</strong></p><h3>🛠️ Getting Hands-On with the requests Library: A Practical Guide</h3><p>Now that we’ve covered the basics of how HTTP works and how requests travel between clients and servers, let’s jump into the practical side of things! We’ll start by making a simple <strong>GET request</strong> using Python’s requests library to fetch some data from a website.</p><h4>Making a Simple GET Request</h4><p>A <strong>GET request</strong> is used to retrieve data from a server. It’s one of the most common HTTP methods and is super easy to use with the requests library.</p><p>Here’s how you can make your first GET request to fetch data from a website:</p><h4>Syntax for Sending a GET Request with requests Library:</h4><pre>import requests  # Import the requests library<br><br># Send a GET request to a URL<br>response = requests.get(&#39;URL&#39;)<br><br># Access the response status code<br>response.status_code  # Status code (e.g., 200, 404)<br><br># Access the response content (raw data)<br>response.text  # Raw HTML or JSON content from the server<br><br># Optionally, convert the response to JSON (if applicable)<br>response.json()  # Converts response to a JSON object if it&#39;s in JSON format</pre><h4>Example:</h4><pre>import requests  # Import the requests library<br><br># Send a GET request to GitHub API<br>response = requests.get(&#39;https://api.github.com&#39;)<br><br># Print the status code of the response<br>print(&quot;Status Code:&quot;, response.status_code)  # Output: 200<br><br># Print the raw response text (HTML or JSON)<br>print(&quot;Response Text:&quot;, response.text)  # Output: Raw JSON content</pre><h4>Sending Data with a POST Request</h4><p>A <strong>POST request</strong> is used to send data to a server, often to submit a form or create a new resource (e.g., registering a user or submitting feedback).</p><h4>Syntax for Sending a POST Request:</h4><pre>import requests  # Import the requests library<br><br># Define the data to send (as a dictionary)<br>data = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<br><br># Send a POST request to a URL with the data<br>response = requests.post(&#39;URL&#39;, data=data)<br><br># Access the response status code<br>response.status_code<br><br># Access the response content<br>response.text<br><br># If the response is JSON, parse it<br>response.json()</pre><h4>Example:</h4><pre>import requests<br><br># Define the form data<br>payload = {<br>    &#39;username&#39;: &#39;johndoe&#39;,<br>    &#39;password&#39;: &#39;secret123&#39;<br>}<br><br># Send the POST request to a test API<br>response = requests.post(&#39;https://httpbin.org/post&#39;, data=payload)<br><br># Print the status code<br>print(&quot;Status Code:&quot;, response.status_code)<br><br># Print the response JSON (it will echo back what you sent)<br>print(&quot;Response JSON:&quot;, response.json())</pre><h4>Updating Data with a PUT Request</h4><p>A <strong>PUT request</strong> is used to <strong>update existing data</strong> on the server. It’s commonly used in REST APIs to modify a resource by replacing it with new data.</p><h4><strong>Syntax for Sending a PUT Request:</strong></h4><pre>import requests  # Import the requests library<br><br># Define the data to update (as a dictionary)<br>data = {&#39;key1&#39;: &#39;new_value1&#39;, &#39;key2&#39;: &#39;new_value2&#39;}<br><br># Send a PUT request to a URL with the data<br>response = requests.put(&#39;URL&#39;, data=data)<br><br># Access the response status code<br>response.status_code<br><br># Access the response content<br>response.text<br><br># Parse JSON if the response is in JSON format<br>response.json()</pre><h4><strong>Example: Updating Data with PUT</strong></h4><pre>import requests<br><br># Define the updated data<br>payload = {<br>    &#39;username&#39;: &#39;johndoe&#39;,<br>    &#39;email&#39;: &#39;john.doe@example.com&#39;<br>}<br><br># Send a PUT request to a test API<br>response = requests.put(&#39;https://httpbin.org/put&#39;, data=payload)<br><br># Print the status code<br>print(&quot;Status Code:&quot;, response.status_code)<br><br># Print the response JSON (it will echo back the updated data)<br>print(&quot;Response JSON:&quot;, response.json())</pre><h4>Deleting Data with a DELETE Request</h4><p>A <strong>DELETE request</strong> is used to <strong>remove data</strong> from the server. In REST APIs, it’s typically used to delete a specific resource like a user, a post, or an item.</p><h4><strong>Syntax for Sending a DELETE Request:</strong></h4><pre>import requests  # Import the requests library<br><br># Send a DELETE request to a URL<br>response = requests.delete(&#39;URL&#39;)<br><br># Access the response status code<br>response.status_code<br><br># Access the response content<br>response.text<br><br># Parse JSON if available<br>response.json()</pre><h4>Example:</h4><pre>import requests<br><br># Send the DELETE request<br>response = requests.delete(&#39;https://httpbin.org/delete&#39;)<br><br># Print the status code<br>print(&quot;Status Code:&quot;, response.status_code)<br><br># Print the response JSON (echoed by httpbin)<br>print(&quot;Response JSON:&quot;, response.json())</pre><h4>Conclusion</h4><p>In this first part of our journey into Python’s requests library, we covered the essentials—understanding how the web works, the role of HTTP, and how to use GET, POST, PUT, and DELETE requests in a clean and Pythonic way. These building blocks are the foundation for working with any web service or API.</p><p>By now, you should feel confident making basic web requests and interpreting responses from a server. But this is just the beginning!</p><p>In the next part, we’ll dive deeper into <strong>real-world scenarios</strong>, including:</p><ul><li>Working with APIs that require authentication,</li><li>Handling JSON data,</li><li>Adding headers and query parameters,</li><li>Dealing with errors and timeouts.</li></ul><p>Stay tuned — and happy coding! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5375ea944ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vectors & Matrices for Data Science — Part 1: The Basics You Must Know]]></title>
            <link>https://medium.com/@vatsallakhmani1/vectors-matrices-for-data-science-part-1-the-basics-you-must-know-bd8fe0ac8382?source=rss-0027c2fd826c------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd8fe0ac8382</guid>
            <category><![CDATA[vector]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[matrice]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[linear-algebra]]></category>
            <dc:creator><![CDATA[Vatsal Lakhmani]]></dc:creator>
            <pubDate>Mon, 21 Apr 2025 19:41:24 GMT</pubDate>
            <atom:updated>2025-04-21T19:41:24.133Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vectors &amp; Matrices for Data Science — Part 1: The Basics You Must Know</h3><blockquote><strong>Prerequisite</strong>: You should have some basic knowledge of <strong>Python and Numpy (</strong>fundamental library for scientific computing in Python).</blockquote><p>Whether you’re building a machine learning model, analyzing customer behavior, or simply working with rows of numbers in a spreadsheet — <strong>linear algebra is happening behind the scenes</strong>. At the heart of it lie four fundamental building blocks that represent data in most data science and machine learning workflows:</p><h4>The Four Basic Data Structures in Linear Algebra:</h4><ol><li><strong>Scalars</strong> — a single number, it represents <strong>magnitude</strong> only, with no direction or structure.</li><li><strong>Vectors</strong> — vector is an <strong>ordered collection of numbers</strong> — you can think of it as a <strong>list</strong>, a <strong>row</strong> of data, or even a <strong>point in space</strong>. It’s the next step up from a scalar. While a scalar represents <strong>just one value</strong>, a <strong>vector groups related values together.</strong></li><li><strong>Matrices (</strong>plural form of matrix<strong>)</strong> — <strong>matrix</strong> is a <strong>two-dimensional</strong> array of numbers — basically a table with <strong>rows</strong> and <strong>columns</strong>. While a <strong>vector</strong> is a single row or column of numbers (1D), a <strong>matrix</strong> is a collection of <strong>multiple vectors</strong> stacked together. You can think of it as a <strong>grid</strong> where data lives in <strong>rows and columns</strong>.</li><li><strong>Tensors (</strong>matrix in a higher dimension<strong>)</strong> — <strong>tensor</strong> is a general container for data that can have any number of dimensions. It extends the concepts of scalars (single value), vectors (1D), and matrices (2D) into higher dimensions.</li></ol><p>Now lets have a look how we represent these data structures using Python:</p><pre>import numpy as np #This code line is used to import the NumPy library (NumPy is a powerful Python library for numerical computing and is essential for data science.)<br><br># 1) Scalars<br><br># A scalar is just a single numerical value<br>scalar = np.array(5)<br><br># Print the scalar value<br>print(&quot;Scalar:&quot;, scalar)<br><br># Shape is empty since it&#39;s 0-dimensional<br>print(&quot;Shape:&quot;, scalar.shape)<br><br># Output:<br># Scalar: 5<br># Shape: ()<br><br># 2) Vectors<br><br># A vector is a 1D array – like a list of numbers<br>vector = np.array([5, 10, 15])<br><br># Print the vector values<br>print(&quot;Vector:&quot;, vector)<br><br># Shape shows only one dimension with 3 elements<br>print(&quot;Shape:&quot;, vector.shape)<br><br># Output:<br># Vector: [ 5 10 15]<br># Shape: (3,)<br><br># 3) Matrices<br><br># A matrix is a 2D array – like a table of rows and columns<br>matrix = np.array([<br>    [1, 2, 3],  # Row 1<br>    [4, 5, 6]   # Row 2<br>])<br><br># Print the matrix<br>print(&quot;Matrix:\n&quot;, matrix)<br><br># Shape indicates 2 rows and 3 columns<br>print(&quot;Shape:&quot;, matrix.shape)<br><br># Output:<br># Matrix:<br># [[1 2 3]<br>#  [4 5 6]]<br># Shape: (2, 3)<br><br># 4) Tensors<br><br># A 3D tensor – imagine stacking 2 matrices on top of each other<br>tensor = np.array([<br>    [[1, 2], [3, 4], [5, 6]],      # First matrix (3x2)<br>    [[7, 8], [9, 10], [11, 12]]    # Second matrix (3x2)<br>])<br><br># Print the tensor<br>print(&quot;Tensor:\n&quot;, tensor)<br><br># Shape shows 2 matrices, each with 3 rows and 2 columns<br>print(&quot;Shape:&quot;, tensor.shape)<br><br># Output:<br># Tensor:<br># [[[ 1  2]<br>#   [ 3  4]<br>#   [ 5  6]]<br>#<br>#  [[ 7  8]<br>#   [ 9 10]<br>#   [11 12]]]<br># Shape: (2, 3, 2)</pre><p>These structures are how we <strong>store, manipulate, and learn from data</strong> in almost every AI system. So, if you’re aiming to understand how machine learning really works — or want to write more efficient code using libraries like <strong>NumPy, TensorFlow, or PyTorch — these concepts are non-negotiable</strong>.</p><p><strong>For this blog, we will mainly focus on vectors and their respective operations as they</strong> are foundational to many key concepts in data science, such as linear regression, gradient descent, natural language processing, and more. A solid understanding of vectors will make it easier to grasp more advanced topics like matrices, tensors, and deep learning in the future.</p><h3>Vectors</h3><p>In data science, a <strong>vector</strong> is typically used to represent a collection of numerical features or attributes of a data point. It is a <strong>1-dimensional array</strong> that holds values such as height, weight, age, income, or pixel intensities — depending on the context.</p><p>For example, a single row in a dataset (one observation or data sample) is often treated as a vector:</p><p><strong>X </strong>= [[x₁, x₂,… ,xₙ]]</p><p>where <strong>X</strong> is the feature vector, ([x₁, x₂,… ,xₙ])​ are the <strong>features</strong> or <strong>variables </strong>and <strong>n is the number of features</strong> in the dataset.</p><h4>Types of Vectors</h4><p>Vectors come in different types depending on how they are used, what they represent, and the operations performed on them. Below are some commonly encountered types of vectors in <strong>mathematics</strong> and <strong>data science</strong>:</p><ol><li><strong>Row vector</strong>: A <strong>row vector</strong> is a 1-dimensional array with elements arranged <strong>horizontally</strong>. It has a single row and multiple columns.</li><li><strong>Column vector: </strong>A <strong>column vector</strong> is a one-dimensional array of numbers arranged <strong>vertically</strong>. It has a single column and multiple rows.</li><li><strong>Zero vector: </strong>A vector in which <strong>all elements are zero</strong>.</li><li><strong>Unit vector</strong>: A vector with a <strong>magnitude of 1</strong>. It represents <strong>only direction</strong>, not scale. (Calculating magnitude of vector will be covered in the below section, so don’t worry)</li></ol><p>This is how we represent the above vectors in Python:</p><pre>import numpy as np<br>row_vector = np.array([[2, 4, 6]])  # 1 row, 3 columns<br>column_vector = np.array([[2], [4], [6]])  # 3 rows, 1 column<br>zero_vector = np.zeros(3) # creates a 1d array with 3 elements as zeros<br>unit_vector = np.array([1,0,0]) # creates a 1d array having magnitude 1</pre><h4>Magnitude of a Vector</h4><p>The <strong>magnitude</strong> or <strong>length</strong> of a vector tells us how far the vector extends in space — just like the distance from the origin to the point the vector represents. In mathematical terms, this is called the <strong>norm</strong> of a vector.</p><p>There are different ways to measure the length of a vector. The most common ones in data science and machine learning are:</p><ol><li><strong>L2 Norm (Euclidean Norm): </strong>The<strong> L2 norm </strong>is the most familiar — it’s the same as the straight-line (Euclidean) distance from the origin to the point.</li></ol><p>Suppose we have a vector <strong>X</strong> as [x₁, x₂,… ,xₙ], its <strong>L2 Norm</strong> will be calculated by ‖ <strong>X</strong> ‖₂ = √{x₁² + x₂² + …. + xₙ²}.</p><p>This is how we calculate the L2 Norm in Python:</p><pre>import numpy as np<br><br>v = np.array([3, 4])<br><br>l2_norm = np.linalg.norm(v)  # Calculates Euclidean norm<br>print(&quot;L2 Norm (Magnitude):&quot;, l2_norm)<br><br>#Output: L2 Norm (Magnitude): 5.0</pre><p>2. <strong>L1 Norm (Manhattan Norm): </strong>The <strong>L1 norm</strong> is the sum of the absolute values of the vector’s components. It’s called the <strong>Manhattan distance</strong> because it’s like navigating city blocks in a grid.</p><p>Suppose we have a vector <strong>X</strong> as [x₁, x₂,… ,xₙ], its <strong>L1 Norm</strong> will be calculated by ‖ <strong>X </strong>‖₁ = |x₁| + |x₂| + …+ |xₙ|.</p><p>This is how we calculate the L1 Norm in Python:</p><pre>import numpy as np<br><br>v = np.array([-3, 4])<br><br>l1_norm = np.linalg.norm(v, ord=1)  # Calculates Manhattan norm<br>print(&quot;L1 Norm:&quot;, l1_norm)<br><br>#Output: L1 Norm (Magnitude): 7.0</pre><h4>Distance between 2 Vectors</h4><p>The <strong>distance between two vectors</strong> tells us how far apart they are in space — this is fundamental in <strong>machine learning</strong>, especially in tasks like <strong>clustering</strong>, <strong>recommendation systems</strong>, and <strong>nearest neighbor algorithms</strong>.</p><p>There are different ways to measure the distance between 2 vectors. The most common ones in data science and machine learning are:</p><ol><li><strong>L2 Distance (Euclidean Distance): </strong>This is the most common way to measure distance — the straight-line distance between two points (vectors).</li></ol><p>Let’s consider two vectors:</p><p>𝐯 = [v₁, v₂ ,…., vₙ], 𝐰 = [w₁, w₂ ,…., wₙ]</p><p>These vectors can represent anything — for example, two users with feature values like age, income, or preferences.</p><p>The <strong>Euclidean distance</strong> is the “straight-line” distance between two vectors in n-dimensional space. It is calculated using the Pythagorean theorem and is the most common distance measure in geometry and machine learning.</p><p>{L2 Distance} = ‖ 𝐯 − 𝐰 ‖₂ = √{(v₁ − w₁)² + (v₂ − w₂)² + ….. + (vₙ − wₙ)²}</p><p>Implementation of <strong>L2 distance in Python</strong>:</p><pre>import numpy as np<br><br>v = np.array([3, 4])   # First vector<br>w = np.array([1, 1])   # Second vector<br><br># Euclidean distance<br>l2_distance = np.linalg.norm(v - w)<br>print(&quot;L2 Distance:&quot;, l2_distance)<br><br>#Output: L2 Distance: 3.605551275463989</pre><p>2. <strong>L1 Distance (Manhattan Distance): </strong>The <strong>Manhattan distance</strong>, or <strong>L1 norm</strong>, calculates the distance by summing the absolute differences of each corresponding component. Think of this as the number of blocks you’d walk in a city grid to get from one point to another.</p><p>Let’s consider two vectors:</p><p>𝐯 = [v₁, v₂ ,…., vₙ], 𝐰 = [w₁, w₂ ,…., wₙ]</p><p><strong>L1 Distance is calculated using:</strong></p><p>{L1 Distance} = ‖ 𝐯 − 𝐰 ‖₁ = |v₁ − w₁| + |v₂ − w₂| + …. + |vₙ − wₙ|</p><p>Implementation of <strong>L1 distance in Python:</strong></p><pre>import numpy as np<br><br>v = np.array([3, 4])   # First vector<br>w = np.array([1, 1])   # Second vector<br><br># Manhattan distance<br>l1_distance = np.linalg.norm(v - w, ord=1)<br>print(&quot;L1 Distance:&quot;, l1_distance)<br><br>#Output: L1 Distance: 5<br></pre><h4>Scalar Operations on a Vector</h4><p>In data science, vectors often represent data points, and scalar operations help us manipulate these vectors easily. A <strong>scalar</strong> is simply a single number (like 2, -5, or 0.75), and when we apply scalar operations to vectors, we&#39;re applying a mathematical operation between each element of the vector and the scalar.</p><p>Let’s say we have a vector: 𝐯 = [v₁, v₂ ,…., vₙ] and a scalar value (<strong>α)</strong></p><p>Here’s how scalar operations work:</p><p><strong>Scalar Addition: </strong>Each element of the vector is added with the scalar.</p><p><strong>v+α </strong>= [v₁+α, v₂+α ,…, vₙ+α]</p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>alpha = 5<br><br># Scalar addition<br>result = v + alpha<br>print(&quot;v + alpha =&quot;, result)<br><br>#Output: v + alpha = [6 7 8]</pre><p><strong>Scalar Subtraction: </strong>Each element of the vector is subtracted with the scalar.</p><p><strong>v-α </strong>= [v₁-α, v₂-α ,…, vₙ-α]</p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>alpha = 5<br><br>result = v - alpha<br>print(&quot;v - alpha =&quot;, result)<br><br>#Output: v - alpha = [-4 -3 -2]</pre><p><strong>Scalar Multiplication: </strong>Each element is multiplied by the scalar. This is very common in scaling data.</p><p><strong>v.α </strong>= [v₁.α, v₂.α ,…, vₙ.α]</p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>alpha = 5<br><br>result = alpha * v<br>print(&quot;alpha * v =&quot;, result)<br><br>#Output: alpha * v = [5 10 15]</pre><p><strong>Scalar Division: </strong>Each element is divided by the scalar.</p><p><strong>v/α </strong>= [v₁/α, v₂/α ,…, vₙ/α]</p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>alpha = 5<br><br>result = v / alpha<br>print(&quot;v / alpha =&quot;, result)<br><br>#Output: v / alpha = [0.2 0.4 0.6]</pre><h4>Vector-to-Vector Operations</h4><p>In data science, we often compare or combine <strong>two vectors</strong> element-wise. These operations are used everywhere — from computing similarities, transforming features, or performing calculations in neural networks.</p><p>Let’s take two vectors of equal length:</p><p>𝐯 = [v₁, v₂ ,…., vₙ], 𝐰 = [w₁, w₂ ,…., wₙ]</p><p><strong>Element-wise Addition: </strong>Add corresponding elements from both vectors:</p><p><strong>v + w = </strong>[v₁+w₁, v₂+w₂, …. , vₙ+wₙ]</p><p><strong>Python code:</strong></p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>w = np.array([4, 5, 6])<br><br># Element-wise addition<br>result = v + w<br>print(&quot;v + w =&quot;, result)<br><br>#Output: v + w = [5 7 9]</pre><p><strong>Element-wise Subtraction: </strong>Subtract<strong> </strong>corresponding elements from both vectors:</p><p><strong>v-w = </strong>[v₁-w₁, v₂-w₂, …. , vₙ-wₙ]</p><p><strong>Python code:</strong></p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>w = np.array([4, 5, 6])<br><br>result = v - w<br>print(&quot;v - w =&quot;, result)<br><br>#Output: v - w = [-3 -3 -3]</pre><p><strong>Element-wise Multiplication: </strong>Multiply corresponding elements from both vectors:</p><p><strong>v.w = </strong>[v₁.w₁, v₂.w₂, …. , vₙ.wₙ]</p><p><strong>Python code:</strong></p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>w = np.array([4, 5, 6])<br><br>result = v * w<br>print(&quot;v * w =&quot;, result)<br><br>#Output: v * w = [4 10 18]</pre><p><strong>Element-wise Division: </strong>Divide corresponding elements from both vectors:</p><p><strong>v/w = </strong>[v₁/w₁, v₂/w₂, …. , vₙ/wₙ]</p><p><strong>Python code:</strong></p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>w = np.array([4, 5, 6])<br><br>result = v / w<br>print(&quot;v / w =&quot;, result)<br><br>#Output: v / w = [0.25 0.4 0.5]</pre><h4>Note:</h4><p>All these operations require both vectors to have the <strong>same shape (length)</strong>. Otherwise, Python will raise a shape mismatch error.</p><h4>Dot Product of Vectors</h4><p>The <strong>dot product</strong> (also called the <strong>inner product</strong>) is one of the most fundamental operations in linear algebra and data science. It’s used heavily in <strong>machine learning</strong>, <strong>recommendation systems</strong>, <strong>similarity calculations</strong>, and <strong>neural networks</strong>.</p><p>If you have two vectors of the same length:</p><p>𝐯 = [v₁, v₂ ,…., vₙ], 𝐰 = [w₁, w₂ ,…., wₙ]</p><p>Their <strong>dot product</strong> is defined as:</p><p><strong>v.w = </strong>v₁.w₁ + v₂.w₂ + …. + vₙ.wₙ</p><p>This results in a <strong>single scalar value</strong>.</p><pre>import numpy as np<br><br>v = np.array([1, 2, 3])<br>w = np.array([4, 5, 6])<br><br># Dot product using np.dot<br>result = np.dot(v, w)<br>print(&quot;Dot product =&quot;, result)<br><br>#Output: Dot product = 32<br>#(Explanation: 1×4+2×5+3×6 = 4+10+18 = 32)</pre><p><strong>Geometric Interpretation</strong></p><p>The dot product can also be defined in terms of <strong>vector magnitudes and the angle between them</strong>:</p><p><strong>v⋅w=∥v∥⋅∥w∥⋅cos(θ)</strong></p><p>Where:</p><ul><li>∥v∥ is the magnitude (length) of vector <strong>v</strong>, ∥w∥ is the magnitude (length) of vector <strong>w</strong>,</li><li>θ (theta) is the angle between the two vectors.</li></ul><p>If θ = 0 degrees , vectors are in the same direction → dot product is <strong>maximum</strong>.</p><p>If θ = 90 degrees, vectors are perpendicular → dot product is <strong>zero</strong>.</p><p>In this first part of our journey through vectors and matrices, we explored the fundamental concepts of <strong>scalars</strong>, <strong>vectors</strong>, and their operations. Vectors, in particular, serve as the building blocks for many data science techniques, from machine learning to computer vision. We covered essential vector operations like addition, subtraction, multiplication, division, and the dot product, which are crucial for transforming and analyzing data.</p><p>In the next part of this series, we’ll dive deeper into <strong>matrices</strong> and their significance in data science, as well as more advanced operations and techniques.</p><p>Stay tuned for the next section, where we unlock the power of <strong>matrices</strong> and learn how they are used to represent data in higher dimensions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd8fe0ac8382" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>