<?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: Abhijeet Pratap Singh</title>
    <description>The latest articles on DEV Community by Abhijeet Pratap Singh (@abhijeet_pratapsingh_868).</description>
    <link>https://dev.to/abhijeet_pratapsingh_868</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%2F4010075%2Fdec69484-cdf0-4fdd-b429-1ea9c59c392a.jpg</url>
      <title>DEV Community: Abhijeet Pratap Singh</title>
      <link>https://dev.to/abhijeet_pratapsingh_868</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhijeet_pratapsingh_868"/>
    <language>en</language>
    <item>
      <title>Hierarchical Clustering</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Sun, 05 Jul 2026 05:39:19 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/hierarchical-clustering-1okp</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/hierarchical-clustering-1okp</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Many clustering algorithms split data into a fixed number of groups.&lt;/p&gt;

&lt;p&gt;Once a data point belongs to a cluster, that's the end of the story.&lt;/p&gt;

&lt;p&gt;But real-world data is rarely that simple.&lt;/p&gt;

&lt;p&gt;Customers, products, organizations, and even biological species naturally form &lt;strong&gt;nested relationships&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;A startup grows into a mid-sized company.&lt;/p&gt;

&lt;p&gt;Several mid-sized companies belong to the same enterprise.&lt;/p&gt;

&lt;p&gt;That enterprise belongs to an industry.&lt;/p&gt;

&lt;p&gt;These relationships exist at multiple levels.&lt;/p&gt;

&lt;p&gt;Hierarchical Clustering is designed to discover this hierarchy.&lt;/p&gt;

&lt;p&gt;Instead of producing one flat grouping, it builds an entire tree that shows how clusters gradually merge together.&lt;/p&gt;

&lt;p&gt;This lets you analyze your data at different levels of detail.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're organizing hundreds of old family photographs.&lt;/p&gt;

&lt;p&gt;You don't immediately decide there should be exactly five albums.&lt;/p&gt;

&lt;p&gt;Instead, you start by finding the two most similar photos.&lt;/p&gt;

&lt;p&gt;Perhaps they're from the same birthday party.&lt;/p&gt;

&lt;p&gt;You group them together.&lt;/p&gt;

&lt;p&gt;Next, you find another similar pair.&lt;/p&gt;

&lt;p&gt;Eventually those small groups begin joining together.&lt;/p&gt;

&lt;p&gt;Birthday albums merge into childhood albums.&lt;/p&gt;

&lt;p&gt;Childhood albums merge into family collections.&lt;/p&gt;

&lt;p&gt;Eventually every photo belongs to one giant family archive.&lt;/p&gt;

&lt;p&gt;Now imagine drawing a horizontal line across that family tree.&lt;/p&gt;

&lt;p&gt;Wherever you cut the tree determines how many groups you end up with.&lt;/p&gt;

&lt;p&gt;Cut lower → many small groups.&lt;/p&gt;

&lt;p&gt;Cut higher → fewer large groups.&lt;/p&gt;

&lt;p&gt;That's exactly how Hierarchical Clustering works.&lt;/p&gt;

&lt;p&gt;It builds the entire hierarchy first.&lt;/p&gt;

&lt;p&gt;You decide later how many clusters you actually want.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;The most common version is &lt;strong&gt;Agglomerative Hierarchical Clustering&lt;/strong&gt;, which follows a bottom-up approach.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — Start with Individual Clusters
&lt;/h2&gt;

&lt;p&gt;Initially,&lt;/p&gt;

&lt;p&gt;every single observation forms its own cluster.&lt;/p&gt;

&lt;p&gt;If you have 500 samples,&lt;/p&gt;

&lt;p&gt;you begin with 500 clusters.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Compute Pairwise Distances
&lt;/h2&gt;

&lt;p&gt;The algorithm calculates the distance between every pair of clusters.&lt;/p&gt;

&lt;p&gt;For individual points, this is usually Euclidean Distance.&lt;/p&gt;

&lt;p&gt;This produces a complete distance matrix.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3 — Merge the Closest Clusters
&lt;/h2&gt;

&lt;p&gt;The two closest clusters are merged together.&lt;/p&gt;

&lt;p&gt;After merging,&lt;/p&gt;

&lt;p&gt;the distance matrix is updated.&lt;/p&gt;

&lt;p&gt;The algorithm repeats this process until every observation belongs to one single cluster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Build the Dendrogram
&lt;/h2&gt;

&lt;p&gt;Every merge is recorded.&lt;/p&gt;

&lt;p&gt;The result is a tree called a &lt;strong&gt;Dendrogram&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The height of each branch represents the distance at which clusters were merged.&lt;/p&gt;

&lt;p&gt;The taller the merge,&lt;/p&gt;

&lt;p&gt;the less similar those clusters were.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Linkage Methods
&lt;/h1&gt;

&lt;p&gt;One important question remains:&lt;/p&gt;

&lt;p&gt;How do we measure the distance between two clusters?&lt;/p&gt;

&lt;p&gt;This depends on the &lt;strong&gt;Linkage Method&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Single Linkage
&lt;/h2&gt;

&lt;p&gt;Measures the distance between the two closest points.&lt;/p&gt;

&lt;p&gt;It tends to create long chain-like clusters.&lt;/p&gt;

&lt;p&gt;Useful for detecting irregular shapes.&lt;/p&gt;

&lt;p&gt;Prone to chaining.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complete Linkage
&lt;/h2&gt;

&lt;p&gt;Measures the distance between the two farthest points.&lt;/p&gt;

&lt;p&gt;Produces compact clusters.&lt;/p&gt;

&lt;p&gt;More resistant to noise.&lt;/p&gt;




&lt;h2&gt;
  
  
  Average Linkage
&lt;/h2&gt;

&lt;p&gt;Uses the average distance between all pairs of points.&lt;/p&gt;

&lt;p&gt;Provides a balance between Single and Complete Linkage.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ward's Linkage
&lt;/h2&gt;

&lt;p&gt;Rather than measuring distance directly,&lt;/p&gt;

&lt;p&gt;Ward's method chooses the merge that causes the &lt;strong&gt;smallest increase in within-cluster variance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This usually produces balanced, compact clusters.&lt;/p&gt;

&lt;p&gt;It is the most commonly used linkage in machine learning.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. What Is the Algorithm Optimizing?
&lt;/h1&gt;

&lt;p&gt;Unlike K-Means,&lt;/p&gt;

&lt;p&gt;Hierarchical Clustering doesn't optimize a global objective function.&lt;/p&gt;

&lt;p&gt;Instead,&lt;/p&gt;

&lt;p&gt;it greedily builds the cluster hierarchy based on local merge decisions.&lt;/p&gt;

&lt;p&gt;The quality of the resulting tree is often evaluated using the &lt;strong&gt;Cophenetic Correlation Coefficient&lt;/strong&gt;, which measures how well the dendrogram preserves the original pairwise distances.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. When Should You Use Hierarchical Clustering?
&lt;/h1&gt;

&lt;p&gt;Hierarchical Clustering works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The natural hierarchy matters.&lt;/li&gt;
&lt;li&gt;You don't know the correct number of clusters beforehand.&lt;/li&gt;
&lt;li&gt;The dataset is relatively small.&lt;/li&gt;
&lt;li&gt;Understanding relationships is more important than prediction speed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Biological taxonomy&lt;/li&gt;
&lt;li&gt;Document organization&lt;/li&gt;
&lt;li&gt;Gene expression analysis&lt;/li&gt;
&lt;li&gt;Product categorization&lt;/li&gt;
&lt;li&gt;Organizational structure analysis&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  7. Advantages
&lt;/h1&gt;

&lt;p&gt;Hierarchical Clustering offers several important benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need to specify the number of clusters beforehand.&lt;/li&gt;
&lt;li&gt;Produces a complete hierarchy of relationships.&lt;/li&gt;
&lt;li&gt;Easy to visualize using a dendrogram.&lt;/li&gt;
&lt;li&gt;Works with many different distance metrics.&lt;/li&gt;
&lt;li&gt;Flexible through different linkage methods.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Like every algorithm,&lt;/p&gt;

&lt;p&gt;Hierarchical Clustering has its limitations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Poor Scalability
&lt;/h2&gt;

&lt;p&gt;This algorithm must compute and store every pairwise distance.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O(N³)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Memory Complexity:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O(N²)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As datasets grow,&lt;/p&gt;

&lt;p&gt;memory usage increases rapidly.&lt;/p&gt;

&lt;p&gt;For hundreds of thousands of observations,&lt;/p&gt;

&lt;p&gt;Hierarchical Clustering becomes impractical.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sensitive to Noise
&lt;/h2&gt;

&lt;p&gt;A few noisy observations can significantly change the structure of the tree,&lt;/p&gt;

&lt;p&gt;especially with Single Linkage.&lt;/p&gt;




&lt;h2&gt;
  
  
  Greedy Decisions
&lt;/h2&gt;

&lt;p&gt;Once two clusters are merged,&lt;/p&gt;

&lt;p&gt;that decision can never be undone.&lt;/p&gt;

&lt;p&gt;If an early merge is incorrect,&lt;/p&gt;

&lt;p&gt;the algorithm carries that mistake throughout the rest of the hierarchy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Linkage Selection Matters
&lt;/h2&gt;

&lt;p&gt;Different linkage methods can produce completely different dendrograms on the same dataset.&lt;/p&gt;

&lt;p&gt;Choosing the wrong linkage may hide the true underlying structure.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;scipy.cluster.hierarchy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;linkage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fcluster&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;

&lt;span class="c1"&gt;# Create sample dataset
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;vstack&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;],(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;],(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seat_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Support_Tickets&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Standardize
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;X_scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Build hierarchy using Ward linkage
&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;linkage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_scaled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ward&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Cut the tree into 3 clusters
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fcluster&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;criterion&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;maxclust&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;value_counts&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Cluster Means&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;groupby&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  10. How to Evaluate Hierarchical Clustering
&lt;/h1&gt;

&lt;p&gt;Since Hierarchical Clustering is unsupervised,&lt;/p&gt;

&lt;p&gt;there is no prediction accuracy.&lt;/p&gt;

&lt;p&gt;Instead, we evaluate the quality of the hierarchy.&lt;/p&gt;




&lt;h3&gt;
  
  
  Dendrogram
&lt;/h3&gt;

&lt;p&gt;The dendrogram is the primary evaluation tool.&lt;/p&gt;

&lt;p&gt;Large vertical jumps indicate natural places to cut the tree into clusters.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cophenetic Correlation Coefficient
&lt;/h3&gt;

&lt;p&gt;Measures how faithfully the dendrogram preserves the original pairwise distances.&lt;/p&gt;

&lt;p&gt;Higher values indicate a better hierarchy.&lt;/p&gt;




&lt;h3&gt;
  
  
  Silhouette Score
&lt;/h3&gt;

&lt;p&gt;Once a cut is chosen,&lt;/p&gt;

&lt;p&gt;Silhouette Score can measure how well-separated the resulting clusters are.&lt;/p&gt;

&lt;p&gt;Higher values indicate cleaner clusters.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Some practical lessons you'll quickly discover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always standardize features before clustering.&lt;/li&gt;
&lt;li&gt;Ward Linkage generally produces the most stable business clusters.&lt;/li&gt;
&lt;li&gt;Hierarchical Clustering is excellent for exploratory analysis but rarely used on massive production datasets.&lt;/li&gt;
&lt;li&gt;Dendrograms become unreadable once the dataset grows beyond a few thousand observations.&lt;/li&gt;
&lt;li&gt;For very large datasets, K-Means or DBSCAN are usually better choices.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  12. Hierarchical Clustering vs K-Means
&lt;/h1&gt;

&lt;p&gt;Although both perform clustering, they work very differently.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hierarchical Clustering&lt;/th&gt;
&lt;th&gt;K-Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Builds a hierarchy&lt;/td&gt;
&lt;td&gt;Produces flat clusters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No need to specify K initially&lt;/td&gt;
&lt;td&gt;Must choose K beforehand&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Creates a dendrogram&lt;/td&gt;
&lt;td&gt;Creates centroids&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slower on large datasets&lt;/td&gt;
&lt;td&gt;Much faster&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Better for relationship discovery&lt;/td&gt;
&lt;td&gt;Better for large-scale segmentation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  13. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Hierarchical Clustering is an &lt;strong&gt;unsupervised clustering algorithm&lt;/strong&gt; that builds a nested hierarchy of groups.&lt;/li&gt;
&lt;li&gt;It starts with every observation as its own cluster and repeatedly merges the closest clusters until only one remains.&lt;/li&gt;
&lt;li&gt;The resulting dendrogram allows you to choose the number of clusters after training rather than before.&lt;/li&gt;
&lt;li&gt;Different linkage methods produce different cluster structures, with Ward Linkage being the most common.&lt;/li&gt;
&lt;li&gt;The algorithm works well for small to medium datasets where understanding relationships is more important than computational efficiency.&lt;/li&gt;
&lt;li&gt;Its biggest limitation is scalability, as both memory and computation grow rapidly with dataset size.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Principal Component Analysis (PCA)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Sun, 05 Jul 2026 05:35:24 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/principal-component-analysis-pca-p75</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/principal-component-analysis-pca-p75</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;As datasets grow, they often collect dozens—or even hundreds—of features.&lt;/p&gt;

&lt;p&gt;The problem is that many of these features carry almost the same information.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Page Views&lt;/li&gt;
&lt;li&gt;Session Duration&lt;/li&gt;
&lt;li&gt;Click Count&lt;/li&gt;
&lt;li&gt;Active Minutes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These metrics are often highly correlated.&lt;/p&gt;

&lt;p&gt;Feeding all of them into a machine learning model increases computation, introduces multicollinearity, and often adds very little new information.&lt;/p&gt;

&lt;p&gt;Principal Component Analysis (PCA) solves this problem by compressing the dataset into a much smaller set of new variables while preserving as much information as possible.&lt;/p&gt;

&lt;p&gt;Instead of removing features, PCA combines them into new synthetic features called &lt;strong&gt;Principal Components&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The goal isn't to lose information.&lt;/p&gt;

&lt;p&gt;It's to remove redundancy.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're taking a photograph of a 3D airplane model.&lt;/p&gt;

&lt;p&gt;If you photograph it from the front, you mostly see a thin vertical shape.&lt;/p&gt;

&lt;p&gt;You lose almost all of the airplane's structure.&lt;/p&gt;

&lt;p&gt;Now rotate the airplane.&lt;/p&gt;

&lt;p&gt;Take another picture from above.&lt;/p&gt;

&lt;p&gt;Suddenly you capture the wings, the body, and the overall shape.&lt;/p&gt;

&lt;p&gt;One photograph contains much more useful information than the other.&lt;/p&gt;

&lt;p&gt;PCA does exactly this mathematically.&lt;/p&gt;

&lt;p&gt;Instead of changing the data,&lt;/p&gt;

&lt;p&gt;it rotates the coordinate system.&lt;/p&gt;

&lt;p&gt;It looks for the viewing angle that captures the largest spread in the data.&lt;/p&gt;

&lt;p&gt;That becomes the &lt;strong&gt;First Principal Component (PC1).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then it finds another completely independent direction that captures the next largest amount of variation.&lt;/p&gt;

&lt;p&gt;That becomes &lt;strong&gt;PC2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It keeps repeating this until every important direction has been discovered.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;PCA transforms correlated variables into a new set of uncorrelated variables called &lt;strong&gt;Principal Components&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — Standardize the Data
&lt;/h2&gt;

&lt;p&gt;Since PCA measures variance, every feature must first be placed on the same scale.&lt;/p&gt;

&lt;p&gt;Without scaling,&lt;/p&gt;

&lt;p&gt;features with larger numerical values dominate the analysis.&lt;/p&gt;

&lt;p&gt;This is why &lt;strong&gt;StandardScaler&lt;/strong&gt; is almost always used before PCA.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Center the Data
&lt;/h2&gt;

&lt;p&gt;The mean of every feature is subtracted.&lt;/p&gt;

&lt;p&gt;This shifts the dataset so every feature has a mean of zero.&lt;/p&gt;

&lt;p&gt;Centering ensures PCA measures variation around the center of the data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3 — Compute the Covariance Matrix
&lt;/h2&gt;

&lt;p&gt;PCA now measures how every feature varies relative to every other feature.&lt;/p&gt;

&lt;p&gt;This relationship is captured in the covariance matrix.&lt;/p&gt;

&lt;p&gt;Large covariance values indicate strong relationships between features.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Compute Eigenvectors and Eigenvalues
&lt;/h2&gt;

&lt;p&gt;Next, PCA performs an eigendecomposition (or more commonly, Singular Value Decomposition).&lt;/p&gt;

&lt;p&gt;The relationship is defined as:&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;v&lt;/strong&gt; = Eigenvector (direction)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;λ&lt;/strong&gt; = Eigenvalue (amount of variance captured)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of it this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eigenvectors tell you &lt;strong&gt;where to look&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Eigenvalues tell you &lt;strong&gt;how much information exists in that direction.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 5 — Create Principal Components
&lt;/h2&gt;

&lt;p&gt;The eigenvectors are sorted from highest to lowest eigenvalue.&lt;/p&gt;

&lt;p&gt;The first component captures the largest amount of variation.&lt;/p&gt;

&lt;p&gt;The second captures the next largest amount.&lt;/p&gt;

&lt;p&gt;Each component is always perpendicular (orthogonal) to the previous one.&lt;/p&gt;

&lt;p&gt;This guarantees that every Principal Component is completely uncorrelated with the others.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. What Is PCA Optimizing?
&lt;/h1&gt;

&lt;p&gt;PCA searches for the direction that captures the greatest possible variance.&lt;/p&gt;

&lt;p&gt;Mathematically, it solves:&lt;/p&gt;

&lt;p&gt;The objective is simple:&lt;/p&gt;

&lt;p&gt;Capture the maximum information using the fewest dimensions.&lt;/p&gt;

&lt;p&gt;Another way to think about it is that PCA minimizes the amount of information lost when projecting high-dimensional data into a lower-dimensional space.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Explained Variance
&lt;/h1&gt;

&lt;p&gt;Every Principal Component explains part of the dataset's total variance.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Variance Explained&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PC1&lt;/td&gt;
&lt;td&gt;58%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PC2&lt;/td&gt;
&lt;td&gt;24%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PC3&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PC4&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remaining&lt;/td&gt;
&lt;td&gt;3%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If the first two components explain &lt;strong&gt;82%&lt;/strong&gt; of the variance,&lt;/p&gt;

&lt;p&gt;you may safely reduce dozens of original features down to just two components.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. When Should You Use PCA?
&lt;/h1&gt;

&lt;p&gt;PCA works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are many correlated numerical features.&lt;/li&gt;
&lt;li&gt;Dimensionality reduction is needed.&lt;/li&gt;
&lt;li&gt;Models train slowly because of many variables.&lt;/li&gt;
&lt;li&gt;Visualization of high-dimensional data is required.&lt;/li&gt;
&lt;li&gt;Multicollinearity is hurting downstream models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feature reduction&lt;/li&gt;
&lt;li&gt;Data visualization&lt;/li&gt;
&lt;li&gt;Image compression&lt;/li&gt;
&lt;li&gt;Face recognition&lt;/li&gt;
&lt;li&gt;Recommendation systems&lt;/li&gt;
&lt;li&gt;Bioinformatics&lt;/li&gt;
&lt;li&gt;Financial modeling&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  7. Advantages
&lt;/h1&gt;

&lt;p&gt;PCA offers several important benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces dimensionality.&lt;/li&gt;
&lt;li&gt;Removes multicollinearity.&lt;/li&gt;
&lt;li&gt;Speeds up machine learning models.&lt;/li&gt;
&lt;li&gt;Reduces storage requirements.&lt;/li&gt;
&lt;li&gt;Improves visualization.&lt;/li&gt;
&lt;li&gt;Eliminates redundant information.&lt;/li&gt;
&lt;li&gt;Creates completely uncorrelated features.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Despite its usefulness, PCA has several limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difficult to Interpret
&lt;/h2&gt;

&lt;p&gt;The new Principal Components are mathematical combinations of many features.&lt;/p&gt;

&lt;p&gt;Instead of saying&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Session Duration caused the prediction,"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;you now have&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Principal Component 2 contributed most."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is much harder to explain to business stakeholders.&lt;/p&gt;




&lt;h2&gt;
  
  
  Assumes Linear Relationships
&lt;/h2&gt;

&lt;p&gt;PCA only discovers linear directions.&lt;/p&gt;

&lt;p&gt;It cannot capture curved or highly non-linear structures.&lt;/p&gt;

&lt;p&gt;Methods like Kernel PCA, t-SNE, or UMAP perform better on non-linear data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sensitive to Scaling
&lt;/h2&gt;

&lt;p&gt;Without feature scaling,&lt;/p&gt;

&lt;p&gt;variables with larger units dominate the variance calculations.&lt;/p&gt;

&lt;p&gt;This produces misleading components.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sensitive to Outliers
&lt;/h2&gt;

&lt;p&gt;A few extreme observations can dramatically change the direction of the Principal Components because variance depends on squared distances.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.decomposition&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PCA&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;

&lt;span class="c1"&gt;# Generate correlated features
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Feature_A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Feature_B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Session_Time&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Page_Views&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;# Standardize
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;X_scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Apply PCA
&lt;/span&gt;&lt;span class="n"&gt;pca&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PCA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_components&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;X_pca&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pca&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_scaled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Original Features:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Principal Components:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X_pca&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Explained Variance Ratio&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pca&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;explained_variance_ratio_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Total Variance Retained&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pca&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;explained_variance_ratio_&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  10. How to Evaluate PCA
&lt;/h1&gt;

&lt;p&gt;Unlike supervised learning, PCA has no prediction accuracy.&lt;/p&gt;

&lt;p&gt;Instead, we evaluate how much information is retained.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explained Variance Ratio
&lt;/h3&gt;

&lt;p&gt;Shows how much variance each Principal Component captures.&lt;/p&gt;

&lt;p&gt;Higher values indicate more informative components.&lt;/p&gt;




&lt;h3&gt;
  
  
  Cumulative Explained Variance
&lt;/h3&gt;

&lt;p&gt;Measures the total variance preserved after selecting multiple components.&lt;/p&gt;

&lt;p&gt;Many practitioners retain &lt;strong&gt;90–95%&lt;/strong&gt; of the total variance.&lt;/p&gt;




&lt;h3&gt;
  
  
  Scree Plot
&lt;/h3&gt;

&lt;p&gt;A Scree Plot graphs the explained variance of each component.&lt;/p&gt;

&lt;p&gt;The "elbow" helps determine how many components should be kept.&lt;/p&gt;




&lt;h3&gt;
  
  
  Reconstruction Error
&lt;/h3&gt;

&lt;p&gt;Measures how much information is lost when reconstructing the original dataset from the reduced components.&lt;/p&gt;

&lt;p&gt;Lower reconstruction error indicates better compression.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Some practical lessons you'll quickly discover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always standardize numerical features before PCA.&lt;/li&gt;
&lt;li&gt;PCA is often used as a preprocessing step before Logistic Regression, SVMs, and Neural Networks.&lt;/li&gt;
&lt;li&gt;Tree-based models (Decision Trees, Random Forests, XGBoost) usually don't benefit much from PCA because they naturally handle correlated features.&lt;/li&gt;
&lt;li&gt;PCA is excellent for visualization—reducing hundreds of dimensions down to two or three makes complex datasets much easier to explore.&lt;/li&gt;
&lt;li&gt;The first few components often capture the vast majority of useful information.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  12. PCA vs Feature Selection
&lt;/h1&gt;

&lt;p&gt;These are often confused, but they solve different problems.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature Selection&lt;/th&gt;
&lt;th&gt;PCA&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Keeps original features&lt;/td&gt;
&lt;td&gt;Creates entirely new features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Easy to interpret&lt;/td&gt;
&lt;td&gt;Hard to interpret&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Removes unnecessary columns&lt;/td&gt;
&lt;td&gt;Combines existing columns&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Human-readable&lt;/td&gt;
&lt;td&gt;Mathematical representation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  13. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;PCA is an &lt;strong&gt;unsupervised dimensionality reduction algorithm&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It compresses many correlated features into a smaller set of uncorrelated Principal Components.&lt;/li&gt;
&lt;li&gt;The algorithm works by finding the directions that capture the maximum variance in the data.&lt;/li&gt;
&lt;li&gt;Principal Components are orthogonal, eliminating multicollinearity.&lt;/li&gt;
&lt;li&gt;Feature scaling is essential before applying PCA.&lt;/li&gt;
&lt;li&gt;PCA improves computational efficiency and reduces redundancy but sacrifices interpretability because the resulting components are mathematical combinations of the original features.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>DBSCAN (Density-Based Spatial Clustering of Applications with Noise)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Fri, 03 Jul 2026 14:42:20 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/dbscan-density-based-spatial-clustering-of-applications-with-noise-4a4k</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/dbscan-density-based-spatial-clustering-of-applications-with-noise-4a4k</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Many clustering algorithms assume that clusters are round, evenly sized, and well separated.&lt;/p&gt;

&lt;p&gt;Unfortunately, real-world data rarely behaves that way.&lt;/p&gt;

&lt;p&gt;Customer behavior, GPS locations, fraud patterns, network traffic, and sensor readings often form irregular shapes with scattered outliers.&lt;/p&gt;

&lt;p&gt;Traditional algorithms like K-Means struggle because they force every data point into a cluster—even obvious anomalies.&lt;/p&gt;

&lt;p&gt;DBSCAN solves this problem by identifying &lt;strong&gt;dense regions of data&lt;/strong&gt; while automatically labeling isolated observations as &lt;strong&gt;noise&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of asking,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Which centroid is closest?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;DBSCAN asks,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Is this point surrounded by enough nearby neighbors to belong to a meaningful group?"&lt;/em&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're standing at a crowded music festival.&lt;/p&gt;

&lt;p&gt;People naturally form groups of friends.&lt;/p&gt;

&lt;p&gt;Some groups are large.&lt;/p&gt;

&lt;p&gt;Some are small.&lt;/p&gt;

&lt;p&gt;Some form circles.&lt;/p&gt;

&lt;p&gt;Others stretch into long lines.&lt;/p&gt;

&lt;p&gt;You walk up to one person and ask:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"How many people are standing within 5 feet of you?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If enough people are nearby, you decide they're part of a real group.&lt;/p&gt;

&lt;p&gt;Now you repeat the same question for each nearby person.&lt;/p&gt;

&lt;p&gt;If they also have enough neighbors, the group expands.&lt;/p&gt;

&lt;p&gt;Eventually you've discovered the entire crowd.&lt;/p&gt;

&lt;p&gt;Meanwhile, a few people standing alone near the food trucks never connect to anyone.&lt;/p&gt;

&lt;p&gt;They aren't forced into a group.&lt;/p&gt;

&lt;p&gt;They're simply classified as &lt;strong&gt;noise&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's exactly how DBSCAN builds clusters.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;DBSCAN groups data based on &lt;strong&gt;local point density&lt;/strong&gt; instead of distance to a centroid.&lt;/p&gt;

&lt;p&gt;Two parameters control everything.&lt;/p&gt;




&lt;h2&gt;
  
  
  Epsilon (ε)
&lt;/h2&gt;

&lt;p&gt;Epsilon defines the maximum distance considered to be "nearby."&lt;/p&gt;

&lt;p&gt;Every point looks inside this radius to find its neighbors.&lt;/p&gt;




&lt;h2&gt;
  
  
  Minimum Points (MinPts)
&lt;/h2&gt;

&lt;p&gt;This is the minimum number of neighbors required for a region to be considered dense.&lt;/p&gt;

&lt;p&gt;If a point has enough nearby neighbors, it becomes a &lt;strong&gt;Core Point&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Three Types of Points
&lt;/h1&gt;

&lt;p&gt;Every observation belongs to one of three categories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Point
&lt;/h3&gt;

&lt;p&gt;A point with at least &lt;strong&gt;MinPts&lt;/strong&gt; neighbors inside its ε radius.&lt;/p&gt;

&lt;p&gt;These points form the backbone of every cluster.&lt;/p&gt;




&lt;h3&gt;
  
  
  Border Point
&lt;/h3&gt;

&lt;p&gt;A point that doesn't have enough neighbors itself but lies inside the neighborhood of a Core Point.&lt;/p&gt;

&lt;p&gt;It belongs to the cluster but doesn't expand it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Noise Point
&lt;/h3&gt;

&lt;p&gt;A point that isn't connected to any dense region.&lt;/p&gt;

&lt;p&gt;Noise points receive a cluster label of &lt;strong&gt;-1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike K-Means, DBSCAN doesn't force these observations into artificial groups.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Cluster Expansion
&lt;/h1&gt;

&lt;p&gt;Once DBSCAN discovers a Core Point, it begins expanding the cluster.&lt;/p&gt;

&lt;p&gt;It repeatedly visits neighboring Core Points and merges their neighborhoods together.&lt;/p&gt;

&lt;p&gt;This process continues until no more connected Core Points remain.&lt;/p&gt;

&lt;p&gt;Instead of growing outward from a centroid, the cluster naturally follows the density of the data.&lt;/p&gt;

&lt;p&gt;This allows DBSCAN to discover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Curved clusters&lt;/li&gt;
&lt;li&gt;Long clusters&lt;/li&gt;
&lt;li&gt;Irregular clusters&lt;/li&gt;
&lt;li&gt;Nested structures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;without making any assumptions about shape.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Mathematical View
&lt;/h1&gt;

&lt;p&gt;The neighborhood around a point is defined as:&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ε&lt;/strong&gt; is the search radius&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt; is the dataset&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the neighborhood contains at least &lt;strong&gt;MinPts&lt;/strong&gt; observations, the point becomes a Core Point.&lt;/p&gt;

&lt;p&gt;Unlike K-Means, DBSCAN &lt;strong&gt;does not optimize a global objective function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead, it performs a local density search until every point has been classified.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. When Should You Use DBSCAN?
&lt;/h1&gt;

&lt;p&gt;DBSCAN performs exceptionally well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clusters have irregular shapes.&lt;/li&gt;
&lt;li&gt;The number of clusters is unknown.&lt;/li&gt;
&lt;li&gt;Outlier detection is important.&lt;/li&gt;
&lt;li&gt;Noise naturally exists in the data.&lt;/li&gt;
&lt;li&gt;Geographic or spatial data is involved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GPS location clustering&lt;/li&gt;
&lt;li&gt;Fraud detection&lt;/li&gt;
&lt;li&gt;Network intrusion detection&lt;/li&gt;
&lt;li&gt;Customer behavior analysis&lt;/li&gt;
&lt;li&gt;Image segmentation&lt;/li&gt;
&lt;li&gt;Anomaly detection&lt;/li&gt;
&lt;li&gt;Earthquake analysis&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. Advantages
&lt;/h1&gt;

&lt;p&gt;DBSCAN has several advantages over centroid-based clustering.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need to specify the number of clusters beforehand.&lt;/li&gt;
&lt;li&gt;Automatically detects outliers.&lt;/li&gt;
&lt;li&gt;Handles arbitrary cluster shapes.&lt;/li&gt;
&lt;li&gt;Works well with noisy datasets.&lt;/li&gt;
&lt;li&gt;Naturally separates isolated observations.&lt;/li&gt;
&lt;li&gt;Finds clusters based on density rather than geometry.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  9. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Although powerful, DBSCAN has limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sensitive to Epsilon
&lt;/h2&gt;

&lt;p&gt;Choosing ε incorrectly can dramatically change the results.&lt;/p&gt;

&lt;p&gt;A small ε produces many tiny clusters and excessive noise.&lt;/p&gt;

&lt;p&gt;A large ε merges unrelated clusters together.&lt;/p&gt;




&lt;h2&gt;
  
  
  Different Cluster Densities
&lt;/h2&gt;

&lt;p&gt;DBSCAN assumes all clusters have roughly similar densities.&lt;/p&gt;

&lt;p&gt;If one cluster is extremely dense and another is sparse, a single ε value cannot fit both.&lt;/p&gt;




&lt;h2&gt;
  
  
  High-Dimensional Data
&lt;/h2&gt;

&lt;p&gt;As dimensionality increases, distances become less meaningful.&lt;/p&gt;

&lt;p&gt;This is known as the &lt;strong&gt;Curse of Dimensionality&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;DBSCAN performs much better on low-dimensional datasets.&lt;/p&gt;




&lt;h2&gt;
  
  
  Large Datasets
&lt;/h2&gt;

&lt;p&gt;Finding neighbors for every point becomes computationally expensive on massive datasets unless efficient spatial indexing structures (such as KD-Trees or Ball Trees) are used.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.cluster&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;DBSCAN&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample activity data
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;dense_cluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;noise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;480&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;vstack&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;dense_cluster&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;noise&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Actions_Per_Minute&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Active_Minutes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Feature scaling
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;X_scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Train DBSCAN
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DBSCAN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;eps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;min_samples&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_scaled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;value_counts&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Noise Points&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;head&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  11. How to Evaluate the Model
&lt;/h1&gt;

&lt;p&gt;Since DBSCAN is unsupervised, there are no labels to calculate accuracy.&lt;/p&gt;

&lt;p&gt;Instead, we use clustering metrics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Silhouette Score
&lt;/h3&gt;

&lt;p&gt;Measures how well clusters are separated.&lt;/p&gt;

&lt;p&gt;Higher values indicate better-defined clusters.&lt;/p&gt;




&lt;h3&gt;
  
  
  Davies-Bouldin Index
&lt;/h3&gt;

&lt;p&gt;Measures cluster similarity.&lt;/p&gt;

&lt;p&gt;Lower values indicate better clustering.&lt;/p&gt;




&lt;h3&gt;
  
  
  Noise Percentage
&lt;/h3&gt;

&lt;p&gt;One unique metric for DBSCAN.&lt;/p&gt;

&lt;p&gt;It measures the proportion of observations labeled as &lt;strong&gt;-1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Too much noise usually means ε is too small.&lt;/p&gt;

&lt;p&gt;Too little noise may indicate ε is too large.&lt;/p&gt;




&lt;h3&gt;
  
  
  Visual Inspection
&lt;/h3&gt;

&lt;p&gt;For 2D and 3D datasets, plotting the clusters often provides valuable insight into whether the discovered structure matches reality.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Some practical lessons you'll quickly learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always standardize numerical features before running DBSCAN.&lt;/li&gt;
&lt;li&gt;Choosing ε is usually the hardest part of the algorithm.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;k-distance graph&lt;/strong&gt; is commonly used to estimate a good ε value.&lt;/li&gt;
&lt;li&gt;DBSCAN excels at anomaly detection because it naturally isolates unusual observations.&lt;/li&gt;
&lt;li&gt;It performs far better than K-Means when clusters have irregular shapes.&lt;/li&gt;
&lt;li&gt;For datasets with widely varying densities, algorithms like &lt;strong&gt;HDBSCAN&lt;/strong&gt; generally produce better results.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  13. K-Means vs DBSCAN
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;K-Means&lt;/th&gt;
&lt;th&gt;DBSCAN&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requires the number of clusters beforehand&lt;/td&gt;
&lt;td&gt;Automatically discovers clusters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses centroids&lt;/td&gt;
&lt;td&gt;Uses local density&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assumes spherical clusters&lt;/td&gt;
&lt;td&gt;Handles arbitrary shapes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Forces every point into a cluster&lt;/td&gt;
&lt;td&gt;Detects and removes noise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sensitive to outliers&lt;/td&gt;
&lt;td&gt;Naturally handles outliers&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  14. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;DBSCAN is a &lt;strong&gt;density-based clustering algorithm&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It discovers clusters by connecting dense regions instead of measuring distance to centroids.&lt;/li&gt;
&lt;li&gt;Every point is classified as either a &lt;strong&gt;Core Point&lt;/strong&gt;, &lt;strong&gt;Border Point&lt;/strong&gt;, or &lt;strong&gt;Noise Point&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It automatically detects outliers instead of forcing every observation into a cluster.&lt;/li&gt;
&lt;li&gt;Unlike K-Means, it does &lt;strong&gt;not&lt;/strong&gt; require specifying the number of clusters beforehand.&lt;/li&gt;
&lt;li&gt;It performs exceptionally well on irregularly shaped clusters and noisy datasets but struggles when cluster densities vary significantly.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>K-Means Clustering (Unsupervised Learning)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Thu, 02 Jul 2026 22:32:48 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/k-means-clustering-unsupervised-learning-1lj0</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/k-means-clustering-unsupervised-learning-1lj0</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;In many real-world problems, we don't have labeled data.&lt;/p&gt;

&lt;p&gt;We may have thousands of customers, products, or transactions, but no information about which ones belong together.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which customers behave similarly?&lt;/li&gt;
&lt;li&gt;Which products attract similar buyers?&lt;/li&gt;
&lt;li&gt;Which users are likely to become power users?&lt;/li&gt;
&lt;li&gt;Which stores have similar purchasing patterns?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;K-Means Clustering solves this problem by automatically grouping similar data points together based on their characteristics.&lt;/p&gt;

&lt;p&gt;Unlike supervised learning, there are &lt;strong&gt;no labels&lt;/strong&gt; telling the algorithm what the correct answer is.&lt;/p&gt;

&lt;p&gt;Its job is simply to discover hidden patterns within the data.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine dropping hundreds of marbles onto a large table.&lt;/p&gt;

&lt;p&gt;Your task is to separate them into &lt;strong&gt;three piles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nobody tells you which marble belongs where.&lt;/p&gt;

&lt;p&gt;You begin by placing three random markers on the table.&lt;/p&gt;

&lt;p&gt;These markers are called &lt;strong&gt;Centroids&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now every marble walks to the nearest marker.&lt;/p&gt;

&lt;p&gt;Once every marble has chosen a marker, you move each marker to the exact center of its group.&lt;/p&gt;

&lt;p&gt;Because the markers moved, some marbles are now closer to a different marker.&lt;/p&gt;

&lt;p&gt;They switch groups.&lt;/p&gt;

&lt;p&gt;Again, the markers move to the center.&lt;/p&gt;

&lt;p&gt;This process repeats until the markers stop moving.&lt;/p&gt;

&lt;p&gt;Eventually, each marker sits at the center of a natural group of marbles.&lt;/p&gt;

&lt;p&gt;That's exactly how K-Means works.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;K-Means follows a simple iterative process.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — Choose K
&lt;/h2&gt;

&lt;p&gt;The first thing you must decide is the number of clusters.&lt;/p&gt;

&lt;p&gt;This value is called &lt;strong&gt;K&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;K = 2&lt;/li&gt;
&lt;li&gt;K = 5&lt;/li&gt;
&lt;li&gt;K = 10&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike supervised learning, the algorithm cannot determine this automatically.&lt;/p&gt;

&lt;p&gt;You choose it before training begins.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Initialize Centroids
&lt;/h2&gt;

&lt;p&gt;The algorithm randomly places &lt;strong&gt;K centroids&lt;/strong&gt; inside the feature space.&lt;/p&gt;

&lt;p&gt;These are simply starting points.&lt;/p&gt;

&lt;p&gt;Modern implementations usually use &lt;strong&gt;K-Means++&lt;/strong&gt;, which chooses better initial locations to improve convergence.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3 — Assign Points to the Nearest Centroid
&lt;/h2&gt;

&lt;p&gt;Every data point calculates its distance to every centroid.&lt;/p&gt;

&lt;p&gt;The point joins whichever centroid is closest.&lt;/p&gt;

&lt;p&gt;Mathematically, the assignment is based on minimizing Euclidean distance.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;xᵢ&lt;/strong&gt; = data point&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;μⱼ&lt;/strong&gt; = centroid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each point belongs to exactly one cluster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Update the Centroids
&lt;/h2&gt;

&lt;p&gt;Once all points have been assigned, each centroid moves to the average position of the points inside its cluster.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sⱼ&lt;/strong&gt; = all points assigned to cluster &lt;em&gt;j&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The centroid is literally the mathematical center of that cluster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5 — Repeat
&lt;/h2&gt;

&lt;p&gt;The algorithm repeats two operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assign points&lt;/li&gt;
&lt;li&gt;Move centroids&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;until the centroids stop moving significantly.&lt;/p&gt;

&lt;p&gt;At that point, the clusters are considered stable.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. What Is K-Means Optimizing?
&lt;/h1&gt;

&lt;p&gt;K-Means tries to make every cluster as compact as possible.&lt;/p&gt;

&lt;p&gt;It minimizes the &lt;strong&gt;Within-Cluster Sum of Squares (WCSS)&lt;/strong&gt;, also called &lt;strong&gt;Inertia&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Lower WCSS means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Points are closer to their centroid.&lt;/li&gt;
&lt;li&gt;Clusters are tighter.&lt;/li&gt;
&lt;li&gt;Similar observations stay together.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The algorithm stops when further improvements become very small.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Choosing the Right Value of K
&lt;/h1&gt;

&lt;p&gt;One of the biggest challenges with K-Means is selecting the number of clusters.&lt;/p&gt;

&lt;p&gt;The algorithm doesn't know how many groups actually exist.&lt;/p&gt;

&lt;p&gt;Two common methods are used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Elbow Method
&lt;/h3&gt;

&lt;p&gt;Train the model using different values of K.&lt;/p&gt;

&lt;p&gt;Plot WCSS against K.&lt;/p&gt;

&lt;p&gt;Look for the point where improvement starts slowing down.&lt;/p&gt;

&lt;p&gt;That bend is called the &lt;strong&gt;Elbow&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Silhouette Score
&lt;/h3&gt;

&lt;p&gt;Measures how well-separated the clusters are.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Close to &lt;strong&gt;1&lt;/strong&gt; → Excellent clusters&lt;/li&gt;
&lt;li&gt;Around &lt;strong&gt;0&lt;/strong&gt; → Overlapping clusters&lt;/li&gt;
&lt;li&gt;Below &lt;strong&gt;0&lt;/strong&gt; → Poor clustering&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  6. When Should You Use K-Means?
&lt;/h1&gt;

&lt;p&gt;K-Means works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data contains natural groups.&lt;/li&gt;
&lt;li&gt;Features are numerical.&lt;/li&gt;
&lt;li&gt;Clusters are roughly spherical.&lt;/li&gt;
&lt;li&gt;Cluster sizes are similar.&lt;/li&gt;
&lt;li&gt;The dataset is relatively clean.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Product recommendation&lt;/li&gt;
&lt;li&gt;User behavior analysis&lt;/li&gt;
&lt;li&gt;Market segmentation&lt;/li&gt;
&lt;li&gt;Image compression&lt;/li&gt;
&lt;li&gt;Document clustering&lt;/li&gt;
&lt;li&gt;Sales territory planning&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  7. Advantages
&lt;/h1&gt;

&lt;p&gt;K-Means is popular because it is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple to understand.&lt;/li&gt;
&lt;li&gt;Fast on large datasets.&lt;/li&gt;
&lt;li&gt;Easy to implement.&lt;/li&gt;
&lt;li&gt;Highly scalable.&lt;/li&gt;
&lt;li&gt;Computationally efficient.&lt;/li&gt;
&lt;li&gt;Works well for many business segmentation problems.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;K-Means also has important limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sensitive to Feature Scaling
&lt;/h2&gt;

&lt;p&gt;Distance calculations are everything.&lt;/p&gt;

&lt;p&gt;If one feature has much larger values than another, it dominates the clustering.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Salary → 100,000&lt;/li&gt;
&lt;li&gt;Age → 25&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without scaling, salary completely overwhelms age.&lt;/p&gt;

&lt;p&gt;This is why &lt;strong&gt;StandardScaler&lt;/strong&gt; is almost always used before K-Means.&lt;/p&gt;




&lt;h2&gt;
  
  
  Poor for Non-Spherical Clusters
&lt;/h2&gt;

&lt;p&gt;K-Means assumes clusters are roughly circular.&lt;/p&gt;

&lt;p&gt;It struggles with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Crescent shapes&lt;/li&gt;
&lt;li&gt;Spiral data&lt;/li&gt;
&lt;li&gt;Nested clusters&lt;/li&gt;
&lt;li&gt;Elongated groups&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Algorithms like DBSCAN or Hierarchical Clustering handle these cases much better.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sensitive to Outliers
&lt;/h2&gt;

&lt;p&gt;A single extreme point can pull the centroid far away from the true center.&lt;/p&gt;

&lt;p&gt;This affects the entire cluster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Requires K in Advance
&lt;/h2&gt;

&lt;p&gt;You must decide the number of clusters before training.&lt;/p&gt;

&lt;p&gt;Choosing the wrong value often produces poor segmentation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Local Optimum
&lt;/h2&gt;

&lt;p&gt;Because centroids start randomly, different runs can produce different results.&lt;/p&gt;

&lt;p&gt;This is why modern implementations use &lt;strong&gt;K-Means++&lt;/strong&gt; and multiple random initializations (&lt;code&gt;n_init&lt;/code&gt;).&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.cluster&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;KMeans&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample CRM data
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;vstack&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2500&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seat_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Monthly_Billing&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Scale features
&lt;/span&gt;&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;X_scaled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Train K-Means
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KMeans&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;n_clusters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;k-means++&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;n_init&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit_predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_scaled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;head&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Cluster Centers&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cluster_centers_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  10. How to Evaluate the Model
&lt;/h1&gt;

&lt;p&gt;Since K-Means has no labels, traditional accuracy metrics cannot be used.&lt;/p&gt;

&lt;p&gt;Instead, we use clustering metrics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inertia (WCSS)
&lt;/h3&gt;

&lt;p&gt;Measures how compact each cluster is.&lt;/p&gt;

&lt;p&gt;Lower values indicate tighter clusters.&lt;/p&gt;




&lt;h3&gt;
  
  
  Silhouette Score
&lt;/h3&gt;

&lt;p&gt;Measures both cohesion and separation.&lt;/p&gt;

&lt;p&gt;Values close to &lt;strong&gt;1&lt;/strong&gt; indicate well-separated clusters.&lt;/p&gt;




&lt;h3&gt;
  
  
  Davies-Bouldin Index
&lt;/h3&gt;

&lt;p&gt;Measures cluster similarity.&lt;/p&gt;

&lt;p&gt;Lower values indicate better clustering.&lt;/p&gt;




&lt;h3&gt;
  
  
  Calinski-Harabasz Index
&lt;/h3&gt;

&lt;p&gt;Measures the ratio of separation between clusters to compactness within clusters.&lt;/p&gt;

&lt;p&gt;Higher values generally indicate better-defined clusters.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Here are a few things you'll notice in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always scale numerical features before using K-Means.&lt;/li&gt;
&lt;li&gt;Remove obvious outliers whenever possible.&lt;/li&gt;
&lt;li&gt;Try several values of &lt;strong&gt;K&lt;/strong&gt; instead of assuming one is correct.&lt;/li&gt;
&lt;li&gt;Use K-Means as an exploratory tool rather than expecting perfect segmentation.&lt;/li&gt;
&lt;li&gt;K-Means is extremely fast, making it a great first clustering algorithm for large datasets.&lt;/li&gt;
&lt;li&gt;For irregular cluster shapes, consider DBSCAN or Hierarchical Clustering instead.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  12. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;K-Means is an &lt;strong&gt;unsupervised learning algorithm&lt;/strong&gt; used for clustering.&lt;/li&gt;
&lt;li&gt;It groups similar observations based on Euclidean distance.&lt;/li&gt;
&lt;li&gt;The algorithm repeatedly assigns points to the nearest centroid and updates centroid locations until convergence.&lt;/li&gt;
&lt;li&gt;Its objective is to minimize the Within-Cluster Sum of Squares (WCSS).&lt;/li&gt;
&lt;li&gt;Feature scaling is essential because distance calculations drive the algorithm.&lt;/li&gt;
&lt;li&gt;Choosing the correct number of clusters is one of the most important parts of using K-Means effectively.&lt;/li&gt;
&lt;li&gt;It performs best on compact, well-separated, spherical clusters and is widely used for customer segmentation and exploratory data analysis.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Random Forest (Supervised Learning)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Thu, 02 Jul 2026 22:31:25 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/random-forest-supervised-learning-2j4a</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/random-forest-supervised-learning-2j4a</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Decision Trees are simple, easy to understand, and work well on non-linear data.&lt;/p&gt;

&lt;p&gt;The problem is that a single Decision Tree is &lt;strong&gt;very unstable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A small change in the training data can produce a completely different tree. Left unchecked, it can also memorize the training data instead of learning patterns, leading to overfitting.&lt;/p&gt;

&lt;p&gt;Random Forest solves this problem by combining &lt;strong&gt;many Decision Trees&lt;/strong&gt; instead of relying on just one.&lt;/p&gt;

&lt;p&gt;Each tree learns from a slightly different version of the data, and their predictions are combined to produce a more reliable final answer.&lt;/p&gt;

&lt;p&gt;Instead of trusting one opinion, Random Forest trusts the wisdom of many independent trees.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're trying to guess the weight of a prize-winning cow at a county fair.&lt;/p&gt;

&lt;p&gt;If you ask just one person, their estimate could be far off.&lt;/p&gt;

&lt;p&gt;Maybe they're experienced.&lt;/p&gt;

&lt;p&gt;Maybe they're guessing.&lt;/p&gt;

&lt;p&gt;Now imagine asking &lt;strong&gt;200 different people&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each person gets slightly different information about the cow.&lt;/p&gt;

&lt;p&gt;Some see its height.&lt;/p&gt;

&lt;p&gt;Some see its age.&lt;/p&gt;

&lt;p&gt;Others see its feeding history.&lt;/p&gt;

&lt;p&gt;Everyone makes an independent estimate.&lt;/p&gt;

&lt;p&gt;When you average all those guesses together, the random mistakes tend to cancel each other out.&lt;/p&gt;

&lt;p&gt;The final estimate is usually much closer to the truth than any single guess.&lt;/p&gt;

&lt;p&gt;That's exactly how Random Forest works.&lt;/p&gt;

&lt;p&gt;Each Decision Tree acts like one independent opinion.&lt;/p&gt;

&lt;p&gt;The forest combines them into one stronger prediction.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;Random Forest builds hundreds (or sometimes thousands) of Decision Trees.&lt;/p&gt;

&lt;p&gt;Every tree is trained differently.&lt;/p&gt;

&lt;p&gt;This diversity is what makes the model so powerful.&lt;/p&gt;

&lt;p&gt;There are three main steps.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Bootstrap Sampling (Bagging)
&lt;/h1&gt;

&lt;p&gt;Instead of giving every tree the exact same training data, Random Forest creates a new dataset for each tree.&lt;/p&gt;

&lt;p&gt;It does this by randomly sampling rows &lt;strong&gt;with replacement&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;Bootstrap Sampling&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because sampling is done with replacement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some rows appear multiple times.&lt;/li&gt;
&lt;li&gt;Some rows aren't selected at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those unused rows are called &lt;strong&gt;Out-of-Bag (OOB) samples&lt;/strong&gt; and can be used to estimate model performance without needing a separate validation dataset.&lt;/p&gt;

&lt;p&gt;Each tree therefore learns from a slightly different view of the data.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Random Feature Selection
&lt;/h1&gt;

&lt;p&gt;When a Decision Tree chooses the next split, it normally considers every feature.&lt;/p&gt;

&lt;p&gt;Random Forest intentionally prevents this.&lt;/p&gt;

&lt;p&gt;At each split, the tree only looks at a random subset of features.&lt;/p&gt;

&lt;p&gt;For classification problems, a common choice is:&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;M&lt;/strong&gt; = total number of features&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;m&lt;/strong&gt; = randomly selected subset used at that split&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This forces different trees to explore different patterns instead of always relying on the strongest feature.&lt;/p&gt;

&lt;p&gt;As a result, the trees become less correlated, which improves the overall model.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Combining Predictions
&lt;/h1&gt;

&lt;p&gt;Once every tree has made its prediction, Random Forest combines them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Classification
&lt;/h3&gt;

&lt;p&gt;Each tree casts one vote.&lt;/p&gt;

&lt;p&gt;The class with the most votes becomes the final prediction.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tree 1 → Fraud
Tree 2 → Legitimate
Tree 3 → Fraud
Tree 4 → Fraud
Tree 5 → Legitimate

Final Prediction → Fraud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Regression
&lt;/h3&gt;

&lt;p&gt;For regression problems, the predictions are averaged.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;B&lt;/strong&gt; = number of trees&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;f(x)&lt;/strong&gt; = prediction from each tree&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The average prediction is usually much more stable than using a single Decision Tree.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. Why Does Random Forest Work So Well?
&lt;/h1&gt;

&lt;p&gt;The strength of Random Forest comes from two ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every tree makes different mistakes.&lt;/li&gt;
&lt;li&gt;Averaging many different opinions reduces overall error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compared to a single Decision Tree, Random Forest has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lower variance&lt;/li&gt;
&lt;li&gt;Better generalization&lt;/li&gt;
&lt;li&gt;Less overfitting&lt;/li&gt;
&lt;li&gt;More stable predictions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why it's often considered one of the strongest "plug-and-play" machine learning algorithms.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. When Should You Use Random Forest?
&lt;/h1&gt;

&lt;p&gt;Random Forest works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The data has non-linear relationships.&lt;/li&gt;
&lt;li&gt;There are many input features.&lt;/li&gt;
&lt;li&gt;You need strong predictive performance.&lt;/li&gt;
&lt;li&gt;Feature interactions are complex.&lt;/li&gt;
&lt;li&gt;You don't want extensive preprocessing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer churn prediction&lt;/li&gt;
&lt;li&gt;Fraud detection&lt;/li&gt;
&lt;li&gt;Credit risk analysis&lt;/li&gt;
&lt;li&gt;Medical diagnosis&lt;/li&gt;
&lt;li&gt;Product recommendation&lt;/li&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Equipment failure prediction&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  9. Advantages
&lt;/h1&gt;

&lt;p&gt;Random Forest has several practical advantages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handles both classification and regression.&lt;/li&gt;
&lt;li&gt;Automatically captures non-linear relationships.&lt;/li&gt;
&lt;li&gt;Less prone to overfitting than a single Decision Tree.&lt;/li&gt;
&lt;li&gt;Works well with noisy datasets.&lt;/li&gt;
&lt;li&gt;No feature scaling required.&lt;/li&gt;
&lt;li&gt;Handles high-dimensional data effectively.&lt;/li&gt;
&lt;li&gt;Provides feature importance scores.&lt;/li&gt;
&lt;li&gt;Usually performs well with minimal parameter tuning.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  10. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Despite its strengths, Random Forest isn't perfect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Poor Extrapolation
&lt;/h2&gt;

&lt;p&gt;Random Forest cannot predict values outside the range of its training data.&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;if the largest recorded house price is $2 million,&lt;/p&gt;

&lt;p&gt;the model won't confidently predict $5 million.&lt;/p&gt;

&lt;p&gt;It only learns from what it has already seen.&lt;/p&gt;




&lt;h2&gt;
  
  
  Slower Predictions
&lt;/h2&gt;

&lt;p&gt;A single Decision Tree makes one prediction.&lt;/p&gt;

&lt;p&gt;Random Forest may need to evaluate hundreds of trees before producing an answer.&lt;/p&gt;

&lt;p&gt;This increases prediction latency.&lt;/p&gt;




&lt;h2&gt;
  
  
  Less Interpretable
&lt;/h2&gt;

&lt;p&gt;One Decision Tree can be visualized and explained.&lt;/p&gt;

&lt;p&gt;A forest of 500 trees cannot.&lt;/p&gt;

&lt;p&gt;You gain accuracy but lose interpretability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Large Memory Usage
&lt;/h2&gt;

&lt;p&gt;Training hundreds of deep trees consumes significantly more memory than a single tree.&lt;/p&gt;




&lt;h3&gt;
  
  
  11. Python Implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.ensemble&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomForestClassifier&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;accuracy_score&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample data
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Usage_A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Usage_B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ticket_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seats&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Tenure&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Business rule
&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Usage_A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seats&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ticket_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Train Random Forest
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RandomForestClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;n_estimators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_features&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sqrt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Predictions
&lt;/span&gt;&lt;span class="n"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Accuracy:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;predictions&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Feature importance
&lt;/span&gt;&lt;span class="n"&gt;feature_importance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;feature_importances_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ascending&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Feature Importance&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature_importance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  12. How to Evaluate the Model
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Accuracy
&lt;/h3&gt;

&lt;p&gt;Percentage of correct predictions.&lt;/p&gt;

&lt;p&gt;Useful when classes are balanced.&lt;/p&gt;




&lt;h3&gt;
  
  
  Precision
&lt;/h3&gt;

&lt;p&gt;Measures how many predicted positives were actually correct.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recall
&lt;/h3&gt;

&lt;p&gt;Measures how many actual positives were identified.&lt;/p&gt;




&lt;h3&gt;
  
  
  F1 Score
&lt;/h3&gt;

&lt;p&gt;Balances Precision and Recall.&lt;/p&gt;

&lt;p&gt;Useful for imbalanced datasets.&lt;/p&gt;




&lt;h3&gt;
  
  
  ROC-AUC
&lt;/h3&gt;

&lt;p&gt;Measures how well the forest separates different classes.&lt;/p&gt;

&lt;p&gt;Higher values indicate better classification performance.&lt;/p&gt;




&lt;h3&gt;
  
  
  Out-of-Bag (OOB) Score
&lt;/h3&gt;

&lt;p&gt;One unique advantage of Random Forest.&lt;/p&gt;

&lt;p&gt;Instead of creating a separate validation dataset, the model evaluates itself using the Out-of-Bag samples that weren't included when training each tree.&lt;/p&gt;

&lt;p&gt;A high OOB score usually indicates good generalization.&lt;/p&gt;




&lt;h3&gt;
  
  
  Feature Importance
&lt;/h3&gt;

&lt;p&gt;Random Forest automatically estimates how useful each feature was during training.&lt;/p&gt;

&lt;p&gt;This makes it easier to understand which variables drive predictions.&lt;/p&gt;




&lt;h1&gt;
  
  
  13. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Here are a few things you'll notice in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Random Forest is often one of the best baseline models for tabular data.&lt;/li&gt;
&lt;li&gt;It usually performs well even without extensive feature engineering.&lt;/li&gt;
&lt;li&gt;More trees generally improve stability, but they also increase training time and memory usage.&lt;/li&gt;
&lt;li&gt;Feature importance is useful, but remember it shows correlation, not causation.&lt;/li&gt;
&lt;li&gt;Random Forest is much harder to interpret than a single Decision Tree.&lt;/li&gt;
&lt;li&gt;If you need even higher accuracy, algorithms like Gradient Boosting, XGBoost, LightGBM, or CatBoost often outperform Random Forest, although they require more tuning.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  14. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Random Forest is an ensemble of many Decision Trees.&lt;/li&gt;
&lt;li&gt;It uses Bootstrap Sampling and Random Feature Selection to create diverse trees.&lt;/li&gt;
&lt;li&gt;Final predictions are made using majority voting (classification) or averaging (regression).&lt;/li&gt;
&lt;li&gt;Great at handling non-linear relationships and noisy data.&lt;/li&gt;
&lt;li&gt;Less prone to overfitting than a single Decision Tree.&lt;/li&gt;
&lt;li&gt;Requires little preprocessing and no feature scaling.&lt;/li&gt;
&lt;li&gt;One of the strongest and most reliable machine learning algorithms for structured tabular datasets.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Decision Trees (Supervised Learning)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Wed, 01 Jul 2026 21:48:10 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/decision-trees-supervised-learning-2h3b</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/decision-trees-supervised-learning-2h3b</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Many real-world problems don't follow a straight-line relationship.&lt;/p&gt;

&lt;p&gt;People don't make decisions by gradually increasing or decreasing something. Instead, they often make decisions based on &lt;strong&gt;conditions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Will this customer upgrade?&lt;/li&gt;
&lt;li&gt;Is this transaction fraudulent?&lt;/li&gt;
&lt;li&gt;Should this loan be approved?&lt;/li&gt;
&lt;li&gt;Will this machine fail?&lt;/li&gt;
&lt;li&gt;Is this email spam?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The answer usually depends on a series of &lt;strong&gt;if-else rules&lt;/strong&gt;, not a mathematical equation.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If monthly spending is greater than $500 &lt;strong&gt;and&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Login frequency is less than twice a week &lt;strong&gt;and&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Support tickets are increasing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;then the customer is likely to churn.&lt;/p&gt;

&lt;p&gt;Decision Trees are designed to discover these kinds of rules automatically.&lt;/p&gt;

&lt;p&gt;Instead of fitting a line like Linear or Logistic Regression, they keep asking questions that split the data into smaller and more similar groups.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're playing &lt;strong&gt;20 Questions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You're trying to guess whether a customer will upgrade their subscription.&lt;/p&gt;

&lt;p&gt;Instead of making one big guess, you ask simple Yes/No questions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does the customer have more than 20 seats?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If yes...&lt;/p&gt;

&lt;p&gt;Ask another question.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Are API calls greater than 500 per day?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If yes...&lt;/p&gt;

&lt;p&gt;Ask another question.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Has the account been active in the last week?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Eventually, you reach a point where almost every customer in that group behaves the same way.&lt;/p&gt;

&lt;p&gt;That final group becomes a &lt;strong&gt;Leaf Node&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever a new customer arrives, you simply walk them through the same set of questions until they reach a leaf.&lt;/p&gt;

&lt;p&gt;The prediction is based on the majority of training examples that ended up there.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. How the Algorithm Works
&lt;/h1&gt;

&lt;p&gt;Decision Trees are built one split at a time.&lt;/p&gt;

&lt;p&gt;At every node, the algorithm asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Which question separates the data the best?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It tries every feature.&lt;/p&gt;

&lt;p&gt;Then every possible split point.&lt;/p&gt;

&lt;p&gt;The split that creates the cleanest separation is chosen.&lt;/p&gt;

&lt;p&gt;This process repeats until the stopping criteria are met.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Measuring Node Purity
&lt;/h1&gt;

&lt;p&gt;To decide whether a split is good, the algorithm measures how "mixed" the classes are inside each node.&lt;/p&gt;

&lt;p&gt;One of the most common metrics is &lt;strong&gt;Gini Impurity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pᵢ&lt;/strong&gt; = probability of class &lt;em&gt;i&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt; = total number of classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interpretation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gini = 0&lt;/strong&gt; → Every sample belongs to one class (perfectly pure)&lt;/li&gt;
&lt;li&gt;Higher values → Classes are mixed together&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to make every leaf node as pure as possible.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Information Gain
&lt;/h1&gt;

&lt;p&gt;Every possible split is evaluated.&lt;/p&gt;

&lt;p&gt;The algorithm calculates how much impurity decreases after making that split.&lt;/p&gt;

&lt;p&gt;This decrease is called &lt;strong&gt;Information Gain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The split with the &lt;strong&gt;highest Information Gain&lt;/strong&gt; becomes the next branch in the tree.&lt;/p&gt;

&lt;p&gt;Then the entire process repeats recursively for each child node.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. When Does the Tree Stop Growing?
&lt;/h1&gt;

&lt;p&gt;If left alone, a Decision Tree keeps splitting until every training example has its own leaf.&lt;/p&gt;

&lt;p&gt;That almost always leads to overfitting.&lt;/p&gt;

&lt;p&gt;To prevent this, we usually limit tree growth using parameters like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;max_depth&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min_samples_split&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min_samples_leaf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_leaf_nodes&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These regularization settings help the tree generalize to unseen data instead of memorizing the training set.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. When Should You Use Decision Trees?
&lt;/h1&gt;

&lt;p&gt;Decision Trees work well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relationships are non-linear.&lt;/li&gt;
&lt;li&gt;Data contains many conditional rules.&lt;/li&gt;
&lt;li&gt;Features are a mix of numerical and categorical values.&lt;/li&gt;
&lt;li&gt;Interpretability is important.&lt;/li&gt;
&lt;li&gt;You don't want extensive preprocessing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer churn prediction&lt;/li&gt;
&lt;li&gt;Credit approval&lt;/li&gt;
&lt;li&gt;Fraud detection&lt;/li&gt;
&lt;li&gt;Medical diagnosis&lt;/li&gt;
&lt;li&gt;Product recommendation&lt;/li&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Risk assessment&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. Advantages
&lt;/h1&gt;

&lt;p&gt;Decision Trees have several practical benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No feature scaling required.&lt;/li&gt;
&lt;li&gt;Handles numerical and categorical data.&lt;/li&gt;
&lt;li&gt;Learns non-linear relationships automatically.&lt;/li&gt;
&lt;li&gt;Easy to visualize and explain.&lt;/li&gt;
&lt;li&gt;Captures feature interactions naturally.&lt;/li&gt;
&lt;li&gt;Works well even with missing values (depending on implementation).&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  9. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Decision Trees are powerful, but they have some important weaknesses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overfitting
&lt;/h2&gt;

&lt;p&gt;The biggest problem.&lt;/p&gt;

&lt;p&gt;If the tree grows without limits, it starts memorizing the training data instead of learning real patterns.&lt;/p&gt;

&lt;p&gt;This usually results in poor performance on new data.&lt;/p&gt;




&lt;h2&gt;
  
  
  High Variance
&lt;/h2&gt;

&lt;p&gt;Decision Trees are unstable.&lt;/p&gt;

&lt;p&gt;A small change in the training data can completely change the structure of the tree.&lt;/p&gt;

&lt;p&gt;Two trees trained on almost identical datasets may look very different.&lt;/p&gt;




&lt;h2&gt;
  
  
  Greedy Decisions
&lt;/h2&gt;

&lt;p&gt;The algorithm always chooses the best split &lt;strong&gt;right now&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It never looks ahead.&lt;/p&gt;

&lt;p&gt;That means an early decision can prevent the tree from finding a better overall structure later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bias Toward Features with Many Split Points
&lt;/h2&gt;

&lt;p&gt;Continuous numerical features often have many possible split locations.&lt;/p&gt;

&lt;p&gt;Without proper controls, the algorithm may favor these features even when they aren't the most meaningful.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.tree&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;DecisionTreeClassifier&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.tree&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;export_text&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;accuracy_score&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample data
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;seat_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;api_calls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Business rule
&lt;/span&gt;&lt;span class="n"&gt;upgraded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seat_count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_calls&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seat_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;seat_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API_Calls&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;api_calls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Upgraded&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;upgraded&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seat_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API_Calls&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Upgraded&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Train Decision Tree
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DecisionTreeClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;max_depth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Predictions
&lt;/span&gt;&lt;span class="n"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Accuracy:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;predictions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Decision Rules&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;export_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;feature_names&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Seat_Count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API_Calls&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  11. How to Evaluate the Model
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Accuracy
&lt;/h3&gt;

&lt;p&gt;Measures the percentage of correct predictions.&lt;/p&gt;

&lt;p&gt;Useful when classes are balanced.&lt;/p&gt;




&lt;h3&gt;
  
  
  Precision
&lt;/h3&gt;

&lt;p&gt;How many predicted positives were actually positive.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recall
&lt;/h3&gt;

&lt;p&gt;How many actual positive cases were correctly identified.&lt;/p&gt;




&lt;h3&gt;
  
  
  F1 Score
&lt;/h3&gt;

&lt;p&gt;Balances Precision and Recall.&lt;/p&gt;

&lt;p&gt;Useful for imbalanced datasets.&lt;/p&gt;




&lt;h3&gt;
  
  
  Tree Depth
&lt;/h3&gt;

&lt;p&gt;A deeper tree isn't always better.&lt;/p&gt;

&lt;p&gt;Very deep trees usually indicate overfitting.&lt;/p&gt;




&lt;h3&gt;
  
  
  Feature Importance
&lt;/h3&gt;

&lt;p&gt;Decision Trees automatically estimate how useful each feature was during training.&lt;/p&gt;

&lt;p&gt;This helps explain which variables influenced predictions the most.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Here are a few things you'll notice in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decision Trees are one of the easiest ML models to explain to non-technical teams.&lt;/li&gt;
&lt;li&gt;They require very little preprocessing.&lt;/li&gt;
&lt;li&gt;Always limit tree growth using &lt;code&gt;max_depth&lt;/code&gt; or &lt;code&gt;min_samples_leaf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A single Decision Tree rarely gives the best performance.&lt;/li&gt;
&lt;li&gt;Most production systems use ensembles like Random Forest or Gradient Boosting because they reduce overfitting and improve accuracy.&lt;/li&gt;
&lt;li&gt;Think of a Decision Tree as the building block for many of today's strongest machine learning algorithms.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  13. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Decision Trees solve classification and regression problems using a series of if-else rules.&lt;/li&gt;
&lt;li&gt;They automatically discover non-linear relationships in data.&lt;/li&gt;
&lt;li&gt;The algorithm chooses splits that maximize Information Gain and reduce impurity.&lt;/li&gt;
&lt;li&gt;Easy to understand, visualize, and explain.&lt;/li&gt;
&lt;li&gt;Requires little preprocessing and no feature scaling.&lt;/li&gt;
&lt;li&gt;Can overfit easily if not regularized.&lt;/li&gt;
&lt;li&gt;Forms the foundation of Random Forests, Extra Trees, XGBoost, LightGBM, and many other ensemble methods.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Logistic Regression (Supervised Family)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Wed, 01 Jul 2026 21:31:00 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/logistic-regression-supervised-family-1om4</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/logistic-regression-supervised-family-1om4</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Logistic Regression is used when the outcome is &lt;strong&gt;a category rather than a number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Most commonly, it's used for &lt;strong&gt;binary classification&lt;/strong&gt;, where the answer is either &lt;strong&gt;Yes or No&lt;/strong&gt;, &lt;strong&gt;True or False&lt;/strong&gt;, or &lt;strong&gt;1 or 0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Typical business problems include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Will a customer churn?&lt;/li&gt;
&lt;li&gt;Is this transaction fraudulent?&lt;/li&gt;
&lt;li&gt;Will a customer click an ad?&lt;/li&gt;
&lt;li&gt;Will a loan default?&lt;/li&gt;
&lt;li&gt;Is an email spam?&lt;/li&gt;
&lt;li&gt;Will a machine fail in the next 24 hours?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike Linear Regression, we're not trying to predict a continuous value.&lt;/p&gt;

&lt;p&gt;Instead, we're predicting the &lt;strong&gt;probability&lt;/strong&gt; that an event belongs to a particular class.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;A customer may have an &lt;strong&gt;82% probability of churning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The business can then decide whether that probability is high enough to trigger an intervention.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine you're trying to predict whether a customer will cancel their subscription.&lt;/p&gt;

&lt;p&gt;Suppose the only feature you have is how many times they opened your app this month.&lt;/p&gt;

&lt;p&gt;If you use a straight line like Linear Regression, the predictions quickly become unrealistic.&lt;/p&gt;

&lt;p&gt;A very active customer might end up with a &lt;strong&gt;-20% chance of churn&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A completely inactive customer could end up with &lt;strong&gt;140%&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Probabilities obviously can't work like that.&lt;/p&gt;

&lt;p&gt;To fix this, Logistic Regression takes the linear equation and passes it through a mathematical function called the &lt;strong&gt;Sigmoid Function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of producing a straight line, it creates an &lt;strong&gt;S-shaped curve&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;No matter how large or small the input becomes, the output always stays between &lt;strong&gt;0 and 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That makes it perfect for probability estimation.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. The Mathematical Model
&lt;/h1&gt;

&lt;p&gt;The model first calculates a linear score.&lt;/p&gt;

&lt;p&gt;Instead of using that score directly, it passes it through the Sigmoid function.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;z&lt;/strong&gt; = linear score&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;p̂&lt;/strong&gt; = predicted probability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final output is always between &lt;strong&gt;0 and 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.08  → Very unlikely
0.32  → Low risk
0.65  → Moderate risk
0.94  → Very high probability
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Businesses can then choose a decision threshold.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Probability ≥ 0.50 → Predict Churn&lt;/li&gt;
&lt;li&gt;Probability &amp;lt; 0.50 → Predict Renewal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That threshold doesn't have to be 0.5.&lt;/p&gt;

&lt;p&gt;Fraud detection systems often use much lower thresholds to catch more suspicious transactions.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. What Is the Model Optimizing?
&lt;/h1&gt;

&lt;p&gt;Linear Regression minimizes squared error.&lt;/p&gt;

&lt;p&gt;That doesn't work well for classification.&lt;/p&gt;

&lt;p&gt;Instead, Logistic Regression minimizes &lt;strong&gt;Log Loss&lt;/strong&gt; (also called Binary Cross Entropy).&lt;/p&gt;

&lt;p&gt;Log Loss heavily penalizes predictions that are both &lt;strong&gt;wrong and confident&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Actual class = Fraud&lt;/p&gt;

&lt;p&gt;Prediction = 0.99 Legitimate&lt;/p&gt;

&lt;p&gt;This receives a much larger penalty than predicting 0.55.&lt;/p&gt;

&lt;p&gt;That's exactly what we want.&lt;/p&gt;

&lt;p&gt;A model should never be extremely confident when it's wrong.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. How the Model Learns
&lt;/h1&gt;

&lt;p&gt;Unlike Linear Regression, there isn't a direct mathematical formula that instantly finds the best coefficients.&lt;/p&gt;

&lt;p&gt;Instead, Logistic Regression learns gradually.&lt;/p&gt;

&lt;p&gt;It starts with random weights.&lt;/p&gt;

&lt;p&gt;It makes predictions.&lt;/p&gt;

&lt;p&gt;Measures the error.&lt;/p&gt;

&lt;p&gt;Then adjusts the coefficients a little.&lt;/p&gt;

&lt;p&gt;This repeats thousands of times until the Log Loss stops improving.&lt;/p&gt;

&lt;p&gt;Gradient Descent is one of the most common optimization methods used during this process.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Decision Boundary
&lt;/h1&gt;

&lt;p&gt;Eventually, the model needs to convert probabilities into class labels.&lt;/p&gt;

&lt;p&gt;This is done using a &lt;strong&gt;decision threshold&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Predicted Probability = 0.81

Threshold = 0.50

Prediction = Churn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing the threshold changes how conservative the model becomes.&lt;/p&gt;

&lt;p&gt;Lower thresholds increase recall.&lt;/p&gt;

&lt;p&gt;Higher thresholds increase precision.&lt;/p&gt;

&lt;p&gt;Choosing the right threshold depends on the business problem.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. When Should You Use Logistic Regression?
&lt;/h1&gt;

&lt;p&gt;Logistic Regression works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The target is binary.&lt;/li&gt;
&lt;li&gt;The classes are reasonably separable.&lt;/li&gt;
&lt;li&gt;You need probability estimates.&lt;/li&gt;
&lt;li&gt;You want a fast, interpretable model.&lt;/li&gt;
&lt;li&gt;The relationship between features and the log-odds is roughly linear.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer churn prediction&lt;/li&gt;
&lt;li&gt;Fraud detection&lt;/li&gt;
&lt;li&gt;Medical diagnosis&lt;/li&gt;
&lt;li&gt;Email spam detection&lt;/li&gt;
&lt;li&gt;Credit approval&lt;/li&gt;
&lt;li&gt;Employee attrition prediction&lt;/li&gt;
&lt;li&gt;Marketing campaign response prediction&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  8. Core Assumptions
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Independent Observations
&lt;/h2&gt;

&lt;p&gt;Each training example should be independent.&lt;/p&gt;




&lt;h2&gt;
  
  
  Linear Relationship in Log-Odds
&lt;/h2&gt;

&lt;p&gt;The features should have a roughly linear relationship with the &lt;strong&gt;log-odds&lt;/strong&gt;, not necessarily with the probability itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  No High Multicollinearity
&lt;/h2&gt;

&lt;p&gt;Features shouldn't contain nearly identical information.&lt;/p&gt;

&lt;p&gt;Highly correlated variables make the coefficients unstable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Limited Influence of Extreme Outliers
&lt;/h2&gt;

&lt;p&gt;Extreme feature values can heavily influence the learned coefficients.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Logistic Regression isn't designed for every classification problem.&lt;/p&gt;

&lt;p&gt;It struggles when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class boundaries are highly non-linear.&lt;/li&gt;
&lt;li&gt;Features interact in complex ways.&lt;/li&gt;
&lt;li&gt;Classes overlap heavily.&lt;/li&gt;
&lt;li&gt;There are many irrelevant features.&lt;/li&gt;
&lt;li&gt;One feature perfectly separates both classes (Perfect Separation).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Suppose every customer with more than five support tickets always churns.&lt;/p&gt;

&lt;p&gt;The coefficient for that feature can grow toward infinity, making the model unstable.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.linear_model&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LogisticRegression&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;classification_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;roc_auc_score&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample customer activity
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app_opens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concatenate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;churned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concatenate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ones&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;App_Opens&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;app_opens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Churned&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;churned&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;App_Opens&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Churned&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Train model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LogisticRegression&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;solver&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lbfgs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Predictions
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Probability&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)[:,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Intercept : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intercept_&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Coefficient : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coef_&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;classification_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ROC AUC:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Probability&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  11. How to Evaluate the Model
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Accuracy
&lt;/h3&gt;

&lt;p&gt;The percentage of correct predictions.&lt;/p&gt;

&lt;p&gt;Works well only when classes are balanced.&lt;/p&gt;




&lt;h3&gt;
  
  
  Precision
&lt;/h3&gt;

&lt;p&gt;Out of everything predicted as positive,&lt;/p&gt;

&lt;p&gt;how many were actually positive?&lt;/p&gt;

&lt;p&gt;Useful when false positives are expensive.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recall
&lt;/h3&gt;

&lt;p&gt;Out of all actual positive cases,&lt;/p&gt;

&lt;p&gt;how many did the model find?&lt;/p&gt;

&lt;p&gt;Useful when missing a positive case is costly.&lt;/p&gt;




&lt;h3&gt;
  
  
  F1 Score
&lt;/h3&gt;

&lt;p&gt;Balances Precision and Recall.&lt;/p&gt;

&lt;p&gt;A good overall metric for imbalanced datasets.&lt;/p&gt;




&lt;h3&gt;
  
  
  ROC-AUC
&lt;/h3&gt;

&lt;p&gt;Measures how well the model separates the two classes across every possible threshold.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1.0&lt;/strong&gt; → Perfect classifier&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0.5&lt;/strong&gt; → Random guessing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Higher is better.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Some practical lessons you'll run into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always look at predicted probabilities, not just class labels.&lt;/li&gt;
&lt;li&gt;Adjust the decision threshold based on business needs instead of blindly using 0.5.&lt;/li&gt;
&lt;li&gt;Scale numerical features when using gradient-based optimization.&lt;/li&gt;
&lt;li&gt;Logistic Regression is often the strongest baseline classifier before trying tree-based models.&lt;/li&gt;
&lt;li&gt;Highly imbalanced datasets usually need class weighting or resampling techniques.&lt;/li&gt;
&lt;li&gt;Don't rely on accuracy alone—Precision, Recall, F1 Score, and ROC-AUC usually tell a much better story.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  13. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Logistic Regression predicts probabilities for binary classification problems.&lt;/li&gt;
&lt;li&gt;It converts a linear model into probabilities using the Sigmoid function.&lt;/li&gt;
&lt;li&gt;It learns by minimizing Log Loss instead of squared error.&lt;/li&gt;
&lt;li&gt;Fast to train, easy to interpret, and widely used in production.&lt;/li&gt;
&lt;li&gt;Produces probability scores rather than just Yes/No predictions.&lt;/li&gt;
&lt;li&gt;Works best when class boundaries are reasonably linear.&lt;/li&gt;
&lt;li&gt;A great baseline classifier before moving to Decision Trees, Random Forests, XGBoost, or Neural Networks.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Linear Regression (Supervised Learning)</title>
      <dc:creator>Abhijeet Pratap Singh</dc:creator>
      <pubDate>Tue, 30 Jun 2026 20:37:03 +0000</pubDate>
      <link>https://dev.to/abhijeet_pratapsingh_868/linear-regression-supervisedlearning-4e93</link>
      <guid>https://dev.to/abhijeet_pratapsingh_868/linear-regression-supervisedlearning-4e93</guid>
      <description>&lt;h1&gt;
  
  
  1. The Problem It Solves
&lt;/h1&gt;

&lt;p&gt;Linear Regression is one of the simplest and most widely used machine learning algorithms for predicting &lt;strong&gt;continuous numeric values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever your target is a number rather than a category, Linear Regression is usually the first model worth trying.&lt;/p&gt;

&lt;p&gt;Some common examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predicting monthly cloud infrastructure costs&lt;/li&gt;
&lt;li&gt;Estimating customer lifetime value (CLV)&lt;/li&gt;
&lt;li&gt;Forecasting next month's sales&lt;/li&gt;
&lt;li&gt;Predicting electricity consumption&lt;/li&gt;
&lt;li&gt;Estimating delivery times&lt;/li&gt;
&lt;li&gt;Predicting marketing leads based on ad spend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is simple.&lt;/p&gt;

&lt;p&gt;Given a set of input features, the model learns the relationship between them and predicts a numeric output.&lt;/p&gt;

&lt;p&gt;For example, suppose a SaaS company wants to estimate a customer's next monthly usage bill.&lt;/p&gt;

&lt;p&gt;The inputs could be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Active seats&lt;/li&gt;
&lt;li&gt;API requests&lt;/li&gt;
&lt;li&gt;Storage usage&lt;/li&gt;
&lt;li&gt;Historical consumption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The output would be a single number:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Predicted Monthly Bill&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Core Intuition
&lt;/h1&gt;

&lt;p&gt;Imagine plotting every house in a city.&lt;/p&gt;

&lt;p&gt;The horizontal axis represents the size of the house.&lt;/p&gt;

&lt;p&gt;The vertical axis represents its selling price.&lt;/p&gt;

&lt;p&gt;Every house becomes a point on the graph.&lt;/p&gt;

&lt;p&gt;The points won't line up perfectly. They'll be scattered everywhere.&lt;/p&gt;

&lt;p&gt;Now imagine placing a long ruler across those points.&lt;/p&gt;

&lt;p&gt;You slowly rotate it and move it up or down until it passes through the center of the data as closely as possible.&lt;/p&gt;

&lt;p&gt;That's exactly what Linear Regression is trying to do.&lt;/p&gt;

&lt;p&gt;The model adjusts only two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Intercept&lt;/strong&gt; — where the line starts on the Y-axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slope&lt;/strong&gt; — how steep the line is.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Its goal is to find the line that produces the smallest overall prediction error.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. The Mathematical Model
&lt;/h1&gt;

&lt;p&gt;Linear Regression assumes that the relationship between the input variables (&lt;strong&gt;X&lt;/strong&gt;) and the target (&lt;strong&gt;y&lt;/strong&gt;) can be represented using a straight line.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ŷ&lt;/strong&gt; = predicted value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;β₀&lt;/strong&gt; = intercept&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;β₁ ... βₙ&lt;/strong&gt; = feature coefficients&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x₁ ... xₙ&lt;/strong&gt; = input features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every coefficient tells us how much the prediction changes when that feature increases by one unit.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Suppose the learned equation becomes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Predicted Leads = 50 + 0.08 × Marketing Spend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That means every extra &lt;strong&gt;$1&lt;/strong&gt; spent on marketing increases the expected leads by &lt;strong&gt;0.08&lt;/strong&gt;, assuming everything else stays the same.&lt;/p&gt;

&lt;p&gt;This interpretability is one of the biggest reasons Linear Regression is still widely used in business.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. What Is the Model Optimizing?
&lt;/h1&gt;

&lt;p&gt;Not every line fits the data equally well.&lt;/p&gt;

&lt;p&gt;Some lines pass too high.&lt;/p&gt;

&lt;p&gt;Others pass too low.&lt;/p&gt;

&lt;p&gt;Linear Regression measures the difference between the actual value and the predicted value.&lt;/p&gt;

&lt;p&gt;These differences are called &lt;strong&gt;Residual Errors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of simply adding those errors together (which would cancel positive and negative values), the model squares every error before adding them.&lt;/p&gt;

&lt;p&gt;This gives us the &lt;strong&gt;Sum of Squared Residuals (SSR)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The smaller this value becomes, the better the fitted line.&lt;/p&gt;

&lt;p&gt;The entire training process is simply trying to minimize this error.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. How the Model Learns
&lt;/h1&gt;

&lt;p&gt;There are two common ways to calculate the coefficients.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1 — Normal Equation
&lt;/h2&gt;

&lt;p&gt;For smaller datasets, Linear Regression has a direct mathematical solution.&lt;/p&gt;

&lt;p&gt;Instead of learning gradually, it computes the best coefficients in one step.&lt;/p&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exact solution&lt;/li&gt;
&lt;li&gt;No learning rate&lt;/li&gt;
&lt;li&gt;No iterations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Computationally expensive for very large datasets&lt;/li&gt;
&lt;li&gt;Requires matrix inversion&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Method 2 — Gradient Descent
&lt;/h2&gt;

&lt;p&gt;For larger datasets, calculating the exact solution becomes expensive.&lt;/p&gt;

&lt;p&gt;Instead, the model starts with random coefficients.&lt;/p&gt;

&lt;p&gt;It then repeatedly measures the prediction error and slightly adjusts the coefficients in the direction that reduces the loss.&lt;/p&gt;

&lt;p&gt;Each update moves the model closer to the minimum error.&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;α&lt;/strong&gt; = learning rate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;∂J/∂β&lt;/strong&gt; = gradient of the loss function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The process repeats until the error stops improving.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. When Should You Use Linear Regression?
&lt;/h1&gt;

&lt;p&gt;Linear Regression works well when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The target is continuous.&lt;/li&gt;
&lt;li&gt;The relationship is approximately linear.&lt;/li&gt;
&lt;li&gt;You need an interpretable model.&lt;/li&gt;
&lt;li&gt;Training speed matters.&lt;/li&gt;
&lt;li&gt;You need a strong baseline before trying more advanced algorithms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Revenue prediction&lt;/li&gt;
&lt;li&gt;Cost estimation&lt;/li&gt;
&lt;li&gt;Demand forecasting&lt;/li&gt;
&lt;li&gt;Capacity planning&lt;/li&gt;
&lt;li&gt;Financial modeling&lt;/li&gt;
&lt;li&gt;Energy consumption forecasting&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  7. Core Assumptions
&lt;/h1&gt;

&lt;p&gt;Linear Regression relies on several assumptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linearity
&lt;/h2&gt;

&lt;p&gt;The relationship between inputs and output should roughly follow a straight line.&lt;/p&gt;




&lt;h2&gt;
  
  
  Independence
&lt;/h2&gt;

&lt;p&gt;Observations should not influence one another.&lt;/p&gt;




&lt;h2&gt;
  
  
  Homoscedasticity
&lt;/h2&gt;

&lt;p&gt;Residual errors should have roughly constant variance across all prediction levels.&lt;/p&gt;




&lt;h2&gt;
  
  
  No Multicollinearity
&lt;/h2&gt;

&lt;p&gt;Input variables should not be highly correlated with each other.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Using both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Age in Years&lt;/li&gt;
&lt;li&gt;Birth Year&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;creates redundant information and makes coefficients unstable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Normally Distributed Residuals (mainly for statistical inference)
&lt;/h2&gt;

&lt;p&gt;Residual errors should be approximately normally distributed if confidence intervals or hypothesis testing are important.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. When It Starts Breaking Down
&lt;/h1&gt;

&lt;p&gt;Linear Regression is powerful, but only under the right conditions.&lt;/p&gt;

&lt;p&gt;It struggles when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The relationship is curved rather than linear.&lt;/li&gt;
&lt;li&gt;A few extreme outliers dominate the data.&lt;/li&gt;
&lt;li&gt;Important variables are missing.&lt;/li&gt;
&lt;li&gt;Input features are highly correlated.&lt;/li&gt;
&lt;li&gt;The variance changes dramatically across different prediction ranges.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common example is stock prices.&lt;/p&gt;

&lt;p&gt;Markets rarely move in a straight line, so Linear Regression usually performs poorly without additional feature engineering.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Python Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.linear_model&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LinearRegression&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r2_score&lt;/span&gt;

&lt;span class="c1"&gt;# Generate sample data
&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;marketing_spend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;leads_generated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="mf"&gt;0.08&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;marketing_spend&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Marketing_Spend&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;marketing_spend&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Leads_Generated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;leads_generated&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Marketing_Spend&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Leads_Generated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Train model
&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LinearRegression&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Predictions
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Predicted_Leads&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Evaluation
&lt;/span&gt;&lt;span class="n"&gt;rmse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Predicted_Leads&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;r2_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Predicted_Leads&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Intercept : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intercept_&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Coefficient : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coef_&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RMSE : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;rmse&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R² Score : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r2&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  10. How to Evaluate the Model
&lt;/h1&gt;

&lt;h3&gt;
  
  
  RMSE (Root Mean Squared Error)
&lt;/h3&gt;

&lt;p&gt;Measures the average prediction error.&lt;/p&gt;

&lt;p&gt;Lower is better.&lt;/p&gt;




&lt;h3&gt;
  
  
  R² Score
&lt;/h3&gt;

&lt;p&gt;Measures how much variance the model explains.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1.0&lt;/strong&gt; → Perfect predictions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0.8&lt;/strong&gt; → Explains 80% of the variance&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0.0&lt;/strong&gt; → No better than predicting the average&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  11. Real-World Engineering Notes
&lt;/h1&gt;

&lt;p&gt;Some lessons you'll quickly learn in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear Regression should almost always be your first baseline model.&lt;/li&gt;
&lt;li&gt;Feature engineering usually improves accuracy more than changing algorithms.&lt;/li&gt;
&lt;li&gt;Always inspect residual plots before trusting the predictions.&lt;/li&gt;
&lt;li&gt;Remove or investigate extreme outliers before training.&lt;/li&gt;
&lt;li&gt;Scale isn't required for ordinary Linear Regression, but becomes important when using Gradient Descent or regularized variants like Ridge and Lasso.&lt;/li&gt;
&lt;li&gt;Just because the R² score is high doesn't mean the assumptions are satisfied.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  12. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;One of the simplest and most interpretable machine learning algorithms.&lt;/li&gt;
&lt;li&gt;Predicts continuous numeric values using a linear relationship.&lt;/li&gt;
&lt;li&gt;Finds the best-fitting line by minimizing squared prediction errors.&lt;/li&gt;
&lt;li&gt;Extremely fast to train and easy to explain to business stakeholders.&lt;/li&gt;
&lt;li&gt;Works best when relationships are approximately linear.&lt;/li&gt;
&lt;li&gt;Struggles with non-linear patterns, outliers, and multicollinearity.&lt;/li&gt;
&lt;li&gt;A great baseline model before moving to more advanced algorithms like Decision Trees, Random Forests, or Gradient Boosting.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
