<?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: Ephantus Macharia</title>
    <description>The latest articles on DEV Community by Ephantus Macharia (@ephantus_macharia_).</description>
    <link>https://dev.to/ephantus_macharia_</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%2F3818259%2F2a0ce59f-1b47-4c2b-a228-f6b44f45d932.jpg</url>
      <title>DEV Community: Ephantus Macharia</title>
      <link>https://dev.to/ephantus_macharia_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ephantus_macharia_"/>
    <language>en</language>
    <item>
      <title>Statistics for Data Science</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Tue, 22 Sep 2026 14:03:46 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/statistics-for-data-science-2905</link>
      <guid>https://dev.to/ephantus_macharia_/statistics-for-data-science-2905</guid>
      <description>&lt;p&gt;Data science is often presented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python + Machine Learning + Data = Data Science
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is something underneath all three:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Statistics.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Machine learning can build a prediction. Python can process the data. A dashboard can make the result look beautiful.&lt;/p&gt;

&lt;p&gt;But statistics helps you answer the most important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Can I trust what the data is telling me?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Start With Descriptive Statistics
&lt;/h2&gt;

&lt;p&gt;Before predicting anything, understand what you already have.&lt;/p&gt;

&lt;p&gt;Imagine these exam scores:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;45, 50, 52, 55, 60, 62, 90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can summarize them using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mean&lt;/strong&gt; → average&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Median&lt;/strong&gt; → middle value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mode&lt;/strong&gt; → most frequent value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Range&lt;/strong&gt; → maximum − minimum&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variance&lt;/strong&gt; → how spread out the data is&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standard deviation&lt;/strong&gt; → typical distance from the mean&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;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="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;45&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;52&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&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;Mean:&lt;/span&gt;&lt;span class="sh"&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="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&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;Median:&lt;/span&gt;&lt;span class="sh"&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="nf"&gt;median&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&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;Standard deviation:&lt;/span&gt;&lt;span class="sh"&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="nf"&gt;std&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why does this matter?
&lt;/h3&gt;

&lt;p&gt;The mean alone can be misleading.&lt;/p&gt;

&lt;p&gt;That &lt;code&gt;90&lt;/code&gt; is pulling the average upward.&lt;/p&gt;

&lt;p&gt;Statistics helps you notice the &lt;strong&gt;story hidden behind the average&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Distribution: How Is Your Data Behaving?
&lt;/h1&gt;

&lt;p&gt;Data doesn't always behave nicely.&lt;/p&gt;

&lt;p&gt;It can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Normal       →    
Right-skewed →     
Left-skewed  →     
Uniform      →     
Bimodal      →     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding distributions helps you decide how to analyze your data.&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 python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bins&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;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Frequency&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Distribution of Exam Scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Strong pointer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Always visualize your data before trusting a statistical summary.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A graph can reveal patterns that a table of numbers hides.&lt;/p&gt;




&lt;h1&gt;
  
  
  Probability: Measuring Uncertainty
&lt;/h1&gt;

&lt;p&gt;Data science rarely deals with certainty.&lt;/p&gt;

&lt;p&gt;Instead, we ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How likely is this?"&lt;/p&gt;
&lt;/blockquote&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;Probability of rain = 70%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or in machine learning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Probability customer will churn = 0.82
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Probability gives us a mathematical language for &lt;strong&gt;uncertainty&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And uncertainty is everywhere in data science.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Hypothesis Testing
&lt;/h1&gt;

&lt;p&gt;Suppose a company introduces a new website design.&lt;/p&gt;

&lt;p&gt;The average conversion rate increases from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5.1% → 5.8%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is that a real improvement?&lt;/p&gt;

&lt;p&gt;Or could it simply be random variation?&lt;/p&gt;

&lt;p&gt;This is where hypothesis testing comes in.&lt;/p&gt;

&lt;p&gt;We formulate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H₀ → No meaningful difference
H₁ → There is a difference
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we use an appropriate statistical test.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;t-test&lt;/li&gt;
&lt;li&gt;ANOVA&lt;/li&gt;
&lt;li&gt;Chi-square&lt;/li&gt;
&lt;li&gt;Mann–Whitney U&lt;/li&gt;
&lt;li&gt;Wilcoxon&lt;/li&gt;
&lt;li&gt;Pearson correlation&lt;/li&gt;
&lt;li&gt;Spearman correlation&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  P-Values: Don't Worship 0.05
&lt;/h1&gt;

&lt;p&gt;You will often hear:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p &amp;lt; 0.05
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But statistics is more than checking whether a number crossed &lt;code&gt;0.05&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A p-value helps quantify how compatible the observed data are with the null hypothesis under the assumptions of the statistical test.&lt;/p&gt;

&lt;p&gt;And remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A small p-value does not automatically mean the effect is important.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Always consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Statistical significance
        +
Effect size
        +
Confidence interval
        +
Real-world importance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Confidence Intervals
&lt;/h1&gt;

&lt;p&gt;Suppose your analysis estimates that the average delivery time is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42 minutes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of reporting only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42 minutes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you might report an interval such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;95% CI: 39–45 minutes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interval communicates uncertainty around the estimate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Think of it as:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Here is our estimate, and here is the uncertainty surrounding it."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  . Correlation: When Variables Move Together
&lt;/h1&gt;

&lt;p&gt;Suppose we discover:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hours studied ↑
        ↓
Exam scores ↑
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There may be a positive relationship.&lt;/p&gt;

&lt;p&gt;Python makes this easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;scipy.stats&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pearsonr&lt;/span&gt;

&lt;span class="n"&gt;correlation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pearsonr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&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;correlation&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;p_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But remember the golden rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Correlation does not prove causation.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ice cream sales and swimming accidents might increase during summer.&lt;/p&gt;

&lt;p&gt;That doesn't mean ice cream causes swimming accidents.&lt;/p&gt;

&lt;p&gt;A third factor—&lt;strong&gt;temperature&lt;/strong&gt;—may influence both.&lt;/p&gt;




&lt;h1&gt;
  
  
  Sampling: You Usually Don't Need Everyone
&lt;/h1&gt;

&lt;p&gt;Imagine a country has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;50 million people
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you want to understand consumer preferences.&lt;/p&gt;

&lt;p&gt;You don't necessarily need to ask all 50 million.&lt;/p&gt;

&lt;p&gt;You can study a &lt;strong&gt;sample&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Population
     ↓
   Sample
     ↓
Analysis
     ↓
Inference about population
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is a catch:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A bad sample can produce a confidently wrong conclusion.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sampling bias is therefore a major concern in data science.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Statistics Meets Machine Learning
&lt;/h1&gt;

&lt;p&gt;Statistics isn't separate from machine learning.&lt;/p&gt;

&lt;p&gt;They overlap everywhere.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Statistics&lt;/th&gt;
&lt;th&gt;Data Science / ML&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Probability&lt;/td&gt;
&lt;td&gt;Classification probabilities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Distributions&lt;/td&gt;
&lt;td&gt;Model assumptions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Correlation&lt;/td&gt;
&lt;td&gt;Feature analysis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sampling&lt;/td&gt;
&lt;td&gt;Train/test datasets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Confidence intervals&lt;/td&gt;
&lt;td&gt;Uncertainty&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hypothesis testing&lt;/td&gt;
&lt;td&gt;Experimentation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Regression&lt;/td&gt;
&lt;td&gt;Predictive modeling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Variance&lt;/td&gt;
&lt;td&gt;Model generalization&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is why learning statistics makes machine learning concepts much easier to understand.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Data Scientist's Statistical Mindset
&lt;/h1&gt;

&lt;p&gt;When you receive a dataset, don't immediately open your machine-learning library.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What does each variable mean?
          ↓
What does the distribution look like?
          ↓
Are there outliers?
          ↓
How much variation exists?
          ↓
Is the sample representative?
          ↓
Are variables related?
          ↓
Could this pattern be random?
          ↓
What uncertainty exists?
          ↓
What conclusion can the data actually support?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That mindset is more valuable than memorizing formulas.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Statistics Toolkit
&lt;/h1&gt;

&lt;p&gt;If you're learning data science, build your statistics foundation around these areas:&lt;/p&gt;

&lt;h3&gt;
  
  
  Descriptive Statistics
&lt;/h3&gt;

&lt;p&gt;Mean • Median • Mode • Variance • Standard Deviation&lt;/p&gt;

&lt;h3&gt;
  
  
  Probability
&lt;/h3&gt;

&lt;p&gt;Events • Conditional Probability • Bayes' Theorem&lt;/p&gt;

&lt;h3&gt;
  
  
  Distributions
&lt;/h3&gt;

&lt;p&gt;Normal • Binomial • Poisson • Uniform&lt;/p&gt;

&lt;h3&gt;
  
  
  Inferential Statistics
&lt;/h3&gt;

&lt;p&gt;Sampling • Confidence Intervals • Hypothesis Testing&lt;/p&gt;

&lt;h3&gt;
  
  
  Relationships
&lt;/h3&gt;

&lt;p&gt;Correlation • Covariance • Regression&lt;/p&gt;

&lt;h3&gt;
  
  
  Statistical Tests
&lt;/h3&gt;

&lt;p&gt;t-test • ANOVA • Chi-square • Mann–Whitney • Wilcoxon&lt;/p&gt;

&lt;h3&gt;
  
  
  Experimental Thinking
&lt;/h3&gt;

&lt;p&gt;A/B Testing • Control Groups • Randomization • Bias&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Statistics isn't about making data complicated.&lt;/p&gt;

&lt;p&gt;It's about learning how to &lt;strong&gt;question the numbers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📈 Revenue increased 20%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;don't immediately celebrate.&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;20% compared with what?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🎯 Model accuracy = 95%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;95% on which dataset?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🔗 Correlation = 0.85
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Does correlation actually explain the relationship?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And when you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p &amp;lt; 0.05
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is the effect statistically significant, and does it actually matter?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the difference between &lt;strong&gt;reading numbers&lt;/strong&gt; and &lt;strong&gt;thinking with data&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Statistics doesn't just tell you what happened. It teaches you how confident you should be in the story your data is telling.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>datascience</category>
      <category>statistics</category>
      <category>data</category>
    </item>
    <item>
      <title>Parametric vs Non-Parametric Statistical Tests in Python</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Tue, 22 Sep 2026 12:55:14 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/parametric-vs-non-parametric-statistical-tests-in-python-m7p</link>
      <guid>https://dev.to/ephantus_macharia_/parametric-vs-non-parametric-statistical-tests-in-python-m7p</guid>
      <description>&lt;p&gt;When working with data, knowing the &lt;strong&gt;mean, median, or standard deviation&lt;/strong&gt; is often not enough.&lt;/p&gt;

&lt;p&gt;At some point, you may want to answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the average of two groups significantly different?&lt;/li&gt;
&lt;li&gt;Are two variables related?&lt;/li&gt;
&lt;li&gt;Did an intervention change the results?&lt;/li&gt;
&lt;li&gt;Are three or more groups statistically different?&lt;/li&gt;
&lt;li&gt;Does my sample provide enough evidence to reject the null hypothesis?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where &lt;strong&gt;statistical hypothesis testing&lt;/strong&gt; becomes useful.&lt;/p&gt;

&lt;p&gt;Two major families of statistical tests are:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Parametric tests&lt;/strong&gt; and &lt;strong&gt;non-parametric tests&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding when to use each one is an important skill for anyone working in &lt;strong&gt;data analysis, data science, or machine learning&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Basic Idea
&lt;/h1&gt;

&lt;p&gt;Most hypothesis tests start with two competing statements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null hypothesis — H₀
&lt;/h3&gt;

&lt;p&gt;There is &lt;strong&gt;no statistically significant difference or relationship&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alternative hypothesis — H₁
&lt;/h3&gt;

&lt;p&gt;There &lt;strong&gt;is&lt;/strong&gt; a statistically significant difference or relationship.&lt;/p&gt;

&lt;p&gt;The test produces a &lt;strong&gt;p-value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A common significance level is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;α = 0.05
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simplified interpretation is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p &amp;lt; 0.05  → evidence against H₀
p ≥ 0.05 → insufficient evidence against H₀
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Strong pointer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A p-value is &lt;strong&gt;not&lt;/strong&gt; the probability that the null hypothesis is true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It tells you how compatible your observed data are with the null hypothesis under the assumptions of the test.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Are Parametric Tests?
&lt;/h1&gt;

&lt;p&gt;Parametric tests make assumptions about the underlying population distribution and its parameters.&lt;/p&gt;

&lt;p&gt;Common assumptions can include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Approximately normally distributed data&lt;/li&gt;
&lt;li&gt;Independence of observations&lt;/li&gt;
&lt;li&gt;Continuous measurements&lt;/li&gt;
&lt;li&gt;Equal or appropriately modeled variances in some tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common parametric tests include:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test&lt;/th&gt;
&lt;th&gt;Typical Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;t-test&lt;/td&gt;
&lt;td&gt;Compare means&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ANOVA&lt;/td&gt;
&lt;td&gt;Compare means across 3+ groups&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pearson correlation&lt;/td&gt;
&lt;td&gt;Measure linear association&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Paired t-test&lt;/td&gt;
&lt;td&gt;Compare paired measurements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Parametric tests can be powerful when their assumptions are reasonably satisfied.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Are Non-Parametric Tests?
&lt;/h1&gt;

&lt;p&gt;Non-parametric tests generally make fewer distributional assumptions.&lt;/p&gt;

&lt;p&gt;They are particularly useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data are strongly skewed&lt;/li&gt;
&lt;li&gt;Data contain influential outliers&lt;/li&gt;
&lt;li&gt;The sample is small and normality is questionable&lt;/li&gt;
&lt;li&gt;Data are ordinal/ranked&lt;/li&gt;
&lt;li&gt;Parametric assumptions are not reasonably satisfied&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parametric&lt;/th&gt;
&lt;th&gt;Non-Parametric Alternative&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Independent t-test&lt;/td&gt;
&lt;td&gt;Mann–Whitney U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Paired t-test&lt;/td&gt;
&lt;td&gt;Wilcoxon signed-rank&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One-way ANOVA&lt;/td&gt;
&lt;td&gt;Kruskal–Wallis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pearson correlation&lt;/td&gt;
&lt;td&gt;Spearman correlation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Strong pointer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Non-parametric does not mean "no assumptions."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These tests still have assumptions about things such as independence, measurement structure, and the form of the distributions or relationships being tested.&lt;/p&gt;




&lt;h1&gt;
  
  
  Install the Python Libraries
&lt;/h1&gt;

&lt;p&gt;For most statistical testing in Python, &lt;strong&gt;SciPy&lt;/strong&gt; is a great starting point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;scipy pandas numpy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;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&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Independent t-test
&lt;/h1&gt;

&lt;p&gt;Suppose we want to compare the average exam scores of two independent groups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;group_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;group_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;84&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;86&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ttest_ind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;group_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;group_b&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;t-statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.05&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;Statistically significant difference&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&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;Insufficient evidence of a difference&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;p&gt;The independent t-test is designed to compare the means of two independent groups under its assumptions.&lt;/p&gt;




&lt;h1&gt;
  
  
  Mann–Whitney U Test
&lt;/h1&gt;

&lt;p&gt;What if the data are heavily skewed or ordinal and the assumptions for the t-test aren't appropriate?&lt;/p&gt;

&lt;p&gt;We can consider the &lt;strong&gt;Mann–Whitney U test&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;group_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;77&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;group_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;84&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;86&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mannwhitneyu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;group_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;group_b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;alternative&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;two-sided&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;U-statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test works with the &lt;strong&gt;ranks of observations&lt;/strong&gt; rather than relying on the same distributional assumptions as the t-test.&lt;/p&gt;

&lt;h3&gt;
  
  
  Important
&lt;/h3&gt;

&lt;p&gt;Don't simply say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Mann–Whitney compares medians."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's an oversimplification.&lt;/p&gt;

&lt;p&gt;Its interpretation depends on the distributions of the two groups. Under additional conditions, differences in location can be interpreted more directly.&lt;/p&gt;




&lt;h1&gt;
  
  
  Paired t-test
&lt;/h1&gt;

&lt;p&gt;Sometimes observations come in natural pairs.&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;Student's score BEFORE training
Student's score AFTER training
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same students are measured twice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;68&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;71&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;79&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;76&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ttest_rel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&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;t-statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is different from an independent t-test because the observations are &lt;strong&gt;paired&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wilcoxon Signed-Rank Test
&lt;/h1&gt;

&lt;p&gt;If the paired data do not reasonably satisfy the assumptions of a paired t-test, the &lt;strong&gt;Wilcoxon signed-rank test&lt;/strong&gt; is a common alternative.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wilcoxon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&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;Statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of the relationship like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Paired numerical data
        │
        ├── assumptions reasonable
        │       ↓
        │   Paired t-test
        │
        └── assumptions questionable
                ↓
        Wilcoxon signed-rank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  ANOVA
&lt;/h1&gt;

&lt;p&gt;What if we have &lt;strong&gt;three or more independent groups&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;Method A
Method B
Method C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could use one-way ANOVA.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;method_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;72&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;method_b&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;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;81&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;84&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;method_c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;91&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;89&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;f_oneway&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;method_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;method_b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;method_c&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;F-statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the ANOVA is statistically significant, it tells us that &lt;strong&gt;at least one group differs&lt;/strong&gt;, but it does not tell us which groups differ.&lt;/p&gt;

&lt;p&gt;That requires appropriate &lt;strong&gt;post-hoc testing&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strong pointer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't run multiple t-tests instead of ANOVA just because you have several groups.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Repeated testing can increase the chance of false-positive findings unless you appropriately control for multiple comparisons.&lt;/p&gt;

&lt;h1&gt;
  
  
  Kruskal–Wallis Test
&lt;/h1&gt;

&lt;p&gt;The non-parametric counterpart commonly used for comparing three or more independent groups is the &lt;strong&gt;Kruskal–Wallis test&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kruskal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;method_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;method_b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;method_c&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;H-statistic:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, a significant result tells you that there is evidence of a difference somewhere among the groups.&lt;/p&gt;

&lt;p&gt;You may need appropriate &lt;strong&gt;post-hoc pairwise comparisons&lt;/strong&gt; to determine where the differences occur.&lt;/p&gt;




&lt;h1&gt;
  
  
  Pearson Correlation
&lt;/h1&gt;

&lt;p&gt;Suppose we want to measure the linear relationship between:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hours studied
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exam score
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use Pearson correlation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;hours&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;3&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;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&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="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;74&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;85&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pearsonr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&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;Correlation:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The correlation coefficient ranges from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1 → strong negative linear relationship
 0 → no linear relationship
+1 → strong positive linear relationship
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important
&lt;/h3&gt;

&lt;p&gt;Correlation does &lt;strong&gt;not&lt;/strong&gt; establish causation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Correlation ≠ Causation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Spearman Correlation
&lt;/h1&gt;

&lt;p&gt;When the relationship is better represented by &lt;strong&gt;ranks or monotonic association&lt;/strong&gt;, Spearman's correlation can be useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spearmanr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&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;Spearman correlation:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;statistic&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;p-value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pvalue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A monotonic relationship means that as one variable increases, the other tends to consistently increase or decrease, even if the relationship isn't linear.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Do I Choose the Test?
&lt;/h1&gt;

&lt;p&gt;Here's a practical cheat sheet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 What are you testing?
                         │
        ┌────────────────┼─────────────────┐
        │                │                 │
     2 groups         3+ groups       Relationship
        │                │                 │
        ↓                ↓                 ↓
   ┌─────────┐      ┌──────────┐      ┌──────────┐
   │Independent│     │  ANOVA   │      │ Pearson  │
   │ t-test    │     │          │      │          │
   └─────────┘      └──────────┘      └──────────┘
        │                │                 │
        ↓                ↓                 ↓
   Mann–Whitney     Kruskal–Wallis     Spearman
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For paired observations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Paired measurements
        │
        ├── Parametric assumptions reasonable
        │        ↓
        │    Paired t-test
        │
        └── Assumptions questionable
                 ↓
        Wilcoxon signed-rank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  . Don't Automatically Test for Normality
&lt;/h1&gt;

&lt;p&gt;A common beginner workflow is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Run Shapiro-Wilk
       ↓
p &amp;gt; 0.05?
       ↓
Use t-test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is too simplistic.&lt;/p&gt;

&lt;p&gt;Normality testing should not be the &lt;strong&gt;only&lt;/strong&gt; thing determining your choice.&lt;/p&gt;

&lt;p&gt;Also consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sample size&lt;/li&gt;
&lt;li&gt;Distribution shape&lt;/li&gt;
&lt;li&gt;Outliers&lt;/li&gt;
&lt;li&gt;Independence&lt;/li&gt;
&lt;li&gt;Measurement scale&lt;/li&gt;
&lt;li&gt;Study design&lt;/li&gt;
&lt;li&gt;Equal variance assumptions&lt;/li&gt;
&lt;li&gt;Robustness of the chosen test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visual inspection can help:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;group_a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Frequency&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Distribution of Scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use a Q-Q plot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;probplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;group_a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;norm&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  . Statistical Significance vs Practical Significance
&lt;/h1&gt;

&lt;p&gt;This is one of the most important lessons in statistics.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p = 0.001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That may provide strong evidence against the null hypothesis under the test assumptions.&lt;/p&gt;

&lt;p&gt;But the actual difference might be extremely small.&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;Group A average = 70.01
Group B average = 70.10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A large dataset can make tiny differences statistically significant.&lt;/p&gt;

&lt;p&gt;Therefore, don't report only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p &amp;lt; 0.05
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Effect size&lt;/li&gt;
&lt;li&gt;Confidence intervals&lt;/li&gt;
&lt;li&gt;Magnitude of the difference&lt;/li&gt;
&lt;li&gt;Practical/business importance&lt;/li&gt;
&lt;li&gt;Sample size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Golden rule
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Statistical significance tells you about evidence; practical significance tells you whether the size of the effect matters.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  . A Better Python Workflow
&lt;/h1&gt;

&lt;p&gt;When performing a statistical test, use this workflow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Understand the research/business question
             ↓
2. Identify the variables
             ↓
3. Define the groups or relationship
             ↓
4. Understand the study design
             ↓
5. Check assumptions
             ↓
6. Choose the appropriate test
             ↓
7. Run the test
             ↓
8. Examine the p-value
             ↓
9. Report effect size / confidence interval
             ↓
10. Interpret the result in context
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is much better than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load data → run test → look at p-value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  . Parametric vs Non-Parametric: Quick Reference
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Parametric&lt;/th&gt;
&lt;th&gt;Non-Parametric&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2 independent groups&lt;/td&gt;
&lt;td&gt;Independent t-test&lt;/td&gt;
&lt;td&gt;Mann–Whitney U&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2 paired measurements&lt;/td&gt;
&lt;td&gt;Paired t-test&lt;/td&gt;
&lt;td&gt;Wilcoxon signed-rank&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3+ independent groups&lt;/td&gt;
&lt;td&gt;One-way ANOVA&lt;/td&gt;
&lt;td&gt;Kruskal–Wallis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linear association&lt;/td&gt;
&lt;td&gt;Pearson&lt;/td&gt;
&lt;td&gt;Spearman for rank/monotonic association&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Distribution assumptions&lt;/td&gt;
&lt;td&gt;Generally stronger&lt;/td&gt;
&lt;td&gt;Generally fewer distributional assumptions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works with ordinal data&lt;/td&gt;
&lt;td&gt;Usually not ideal&lt;/td&gt;
&lt;td&gt;Often appropriate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outlier sensitivity&lt;/td&gt;
&lt;td&gt;Often higher&lt;/td&gt;
&lt;td&gt;Often lower, but not immune&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Parametric and non-parametric tests are not competitors where one is universally better.&lt;/p&gt;

&lt;p&gt;They are tools designed for different situations.&lt;/p&gt;

&lt;p&gt;Think of the decision this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             BUSINESS / RESEARCH QUESTION
                         ↓
                    YOUR DATA
                         ↓
                   STUDY DESIGN
                         ↓
                   ASSUMPTIONS
                         ↓
              ┌──────────┴──────────┐
              ↓                     ↓
       Parametric test       Non-parametric
              ↓                     ↓
        t-test / ANOVA       Mann-Whitney /
        / Pearson            Wilcoxon / Kruskal
              └──────────┬──────────┘
                         ↓
                 INTERPRET RESULTS
                         ↓
          Effect size + CI + Context
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal isn't to memorize dozens of statistical tests.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The real data-analysis skill is knowing why a particular test is appropriate for a particular question and dataset.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you understand that, Python becomes the tool that helps you execute the analysis not the thing making the statistical decision for you.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>python</category>
      <category>data</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Star, Snowflake, or Galaxy? A Practical Guide to Data Warehouse Modeling</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Tue, 22 Sep 2026 11:45:43 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/star-snowflake-or-galaxy-a-practical-guide-to-data-warehouse-modeling-572m</link>
      <guid>https://dev.to/ephantus_macharia_/star-snowflake-or-galaxy-a-practical-guide-to-data-warehouse-modeling-572m</guid>
      <description>&lt;p&gt;When building a data warehouse, one of the most important decisions is &lt;strong&gt;how to organize the data for analytics&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You will often encounter three dimensional modeling patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Star Schema&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Snowflake Schema&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Galaxy Schema (Fact Constellation)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first, they can seem like three completely different architectures. They are not.&lt;/p&gt;

&lt;p&gt;They are better understood as different ways of organizing &lt;strong&gt;fact tables and dimension tables&lt;/strong&gt; around analytical requirements.&lt;/p&gt;

&lt;p&gt;The real question isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Which schema is the best?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A better question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Which schema best fits the business processes, query patterns, data relationships, and analytical requirements I have?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This article breaks down all three approaches and provides a practical framework for choosing between them.&lt;/p&gt;




&lt;h2&gt;
  
  
  First: What Is a Data Warehouse Schema?
&lt;/h2&gt;

&lt;p&gt;A data warehouse schema defines how analytical data is organized into tables and relationships.&lt;/p&gt;

&lt;p&gt;Unlike an operational database, where the primary concern may be efficient transaction processing, a data warehouse is designed primarily for &lt;strong&gt;analysis, reporting, aggregation, and decision-making&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A dimensional warehouse generally revolves around two important table types:&lt;/p&gt;

&lt;h3&gt;
  
  
  Fact tables
&lt;/h3&gt;

&lt;p&gt;Fact tables contain measurable business events.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sales
Orders
Payments
Shipments
Inventory movements
Website clicks
Transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A sales fact table might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fact_sales

sale_id
date_id
customer_id
product_id
store_id
quantity
sales_amount
discount_amount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dimension tables
&lt;/h3&gt;

&lt;p&gt;Dimensions provide descriptive context around the facts.&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;dim_customer
dim_product
dim_date
dim_store
dim_employee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A product dimension might contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product_id
product_name
category
subcategory
brand
supplier
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows an analyst to ask questions such as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How much revenue did we generate from electronics in Nairobi during Q2?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The fact table provides the &lt;strong&gt;measurement&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The dimension tables provide the &lt;strong&gt;context&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. The Most Important Concept: Grain
&lt;/h1&gt;

&lt;p&gt;Before choosing a schema, define the &lt;strong&gt;grain&lt;/strong&gt; of your fact table.&lt;/p&gt;

&lt;p&gt;The grain answers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"What does one row in this fact table represent?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&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;One row = one product on one customer order
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One row = one customer transaction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One row = daily inventory balance for one product at one warehouse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This decision is extremely important.&lt;/p&gt;

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

&lt;p&gt;Suppose we have:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;and define its grain as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One row represents one product sold in one transaction.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then safely store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order_id
product_id
customer_id
date_id
quantity
sales_amount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if another developer assumes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One row = one complete order
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the same table could easily be misinterpreted.&lt;/p&gt;

&lt;p&gt;That can lead to incorrect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;revenue calculations&lt;/li&gt;
&lt;li&gt;order counts&lt;/li&gt;
&lt;li&gt;averages&lt;/li&gt;
&lt;li&gt;inventory metrics&lt;/li&gt;
&lt;li&gt;dashboards&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Strong pointer
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Never design the schema before defining the grain.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A well-defined grain is one of the foundations of reliable dimensional modeling.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Star Schema ⭐
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;star schema&lt;/strong&gt; is one of the most common dimensional modeling patterns.&lt;/p&gt;

&lt;p&gt;It consists of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             dim_customer
                   |
                   |
dim_product — fact_sales — dim_date
                   |
                   |
               dim_store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fact table sits at the center.&lt;/p&gt;

&lt;p&gt;Dimension tables surround it.&lt;/p&gt;

&lt;p&gt;This creates a shape that resembles a star.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example Star Schema
&lt;/h2&gt;

&lt;p&gt;Imagine an online store.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fact table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;fact_sales&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;sale_id&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;date_id&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;customer_id&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;product_id&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;store_id&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;quantity&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;sales_amount&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Product dimension
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_product&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;product_id&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;product_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;category&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;subcategory&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;brand&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Customer dimension
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_customer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&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;customer_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;city&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;country&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;customer_segment&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Date dimension
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_date&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;date_id&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;full_date&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;month&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;month_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;quarter&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;year&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dimensions are relatively &lt;strong&gt;denormalized&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, the product dimension can contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product
   ↓
subcategory
   ↓
category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as columns in the same table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product_id | product_name | subcategory | category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;rather than creating separate tables for every level.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Why Use a Star Schema?
&lt;/h1&gt;

&lt;p&gt;The major advantage is &lt;strong&gt;simplicity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose an analyst wants revenue by product category.&lt;/p&gt;

&lt;p&gt;The query can be straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sales_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;revenue&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;fact_sales&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;dim_product&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is only one dimension join.&lt;/p&gt;

&lt;p&gt;This makes the model easier for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data analysts&lt;/li&gt;
&lt;li&gt;BI developers&lt;/li&gt;
&lt;li&gt;Data scientists&lt;/li&gt;
&lt;li&gt;Reporting teams&lt;/li&gt;
&lt;li&gt;Business users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Microsoft's dimensional-modeling guidance describes star schema as a design optimized for analytical workloads involving filtering, grouping, sorting, and summarization.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Advantages of Star Schema
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Simple queries
&lt;/h3&gt;

&lt;p&gt;Fewer joins generally make analytical SQL easier to write and understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easy for BI tools
&lt;/h3&gt;

&lt;p&gt;Business intelligence tools can navigate a relatively simple model more easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easy for analysts
&lt;/h3&gt;

&lt;p&gt;An analyst doesn't need to understand a complicated hierarchy of dimension tables just to answer a simple business question.&lt;/p&gt;

&lt;h3&gt;
  
  
  Good analytical performance
&lt;/h3&gt;

&lt;p&gt;The relatively direct fact-to-dimension relationships can make analytical queries efficient, although actual performance depends heavily on the warehouse engine, data volume, partitioning, clustering, indexing, and query design.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easier maintenance
&lt;/h3&gt;

&lt;p&gt;Adding an attribute can often be as simple as adding a column to a dimension.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Disadvantages of Star Schema
&lt;/h1&gt;

&lt;p&gt;Star schemas intentionally introduce some redundancy.&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;product_id | product | category
-----------|---------|----------
101        | Laptop  | Electronics
102        | Phone   | Electronics
103        | Tablet  | Electronics
104        | Camera  | Electronics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The word:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;is repeated.&lt;/p&gt;

&lt;p&gt;With very large dimensions, this can increase storage and create additional maintenance considerations.&lt;/p&gt;

&lt;p&gt;However, an important modern-data-warehouse lesson is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't automatically normalize everything simply because normalization reduces duplication.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Modern analytical warehouses often make storage relatively inexpensive compared with the human and query complexity introduced by excessive normalization.&lt;/p&gt;

&lt;p&gt;The actual trade-off depends on the platform and workload.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Snowflake Schema ❄️
&lt;/h1&gt;

&lt;p&gt;A snowflake schema starts with the same basic idea as a star schema.&lt;/p&gt;

&lt;p&gt;The difference is that dimensions are &lt;strong&gt;normalized into additional related tables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dim_product

product_id
product_name
subcategory
category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you might have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dim_product
    |
    ↓
dim_subcategory
    |
    ↓
dim_category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 dim_category
                       |
                       |
                dim_subcategory
                       |
                       |
fact_sales → dim_product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dimension has been broken into multiple related tables.&lt;/p&gt;

&lt;p&gt;Microsoft describes this as a "snowflake dimension": a normalized set of tables representing a single business entity.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Snowflake Example
&lt;/h1&gt;

&lt;p&gt;Instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dim_product

product_id
product_name
subcategory
category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we create:&lt;/p&gt;

&lt;h3&gt;
  
  
  Product
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_product&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;product_id&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;product_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;subcategory_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Subcategory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_subcategory&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;subcategory_id&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;subcategory_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;category_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Category
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dim_category&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;category_id&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;category_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the query requires additional joins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sales_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;revenue&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;fact_sales&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;dim_product&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;dim_subcategory&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subcategory_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subcategory_id&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;dim_category&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare that with the star-schema query.&lt;/p&gt;

&lt;p&gt;The snowflake model is more normalized, but the query is more complex.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Why Use a Snowflake Schema?
&lt;/h1&gt;

&lt;p&gt;Snowflaking can make sense when a dimension contains a &lt;strong&gt;genuine hierarchy or reusable structure&lt;/strong&gt; that benefits from normalization.&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;Country
   ↓
State
   ↓
City
   ↓
Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If these relationships are complex, independently maintained, or reused in meaningful ways, splitting them into related tables may be useful.&lt;/p&gt;

&lt;p&gt;Snowflaking can also reduce repeated values in dimensions.&lt;/p&gt;

&lt;p&gt;But don't use it simply because:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Normalization is always better."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is an important misconception.&lt;/p&gt;

&lt;p&gt;A data warehouse has a different purpose from an OLTP system.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Advantages of Snowflake Schema
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Less redundancy
&lt;/h3&gt;

&lt;p&gt;Repeated dimensional attributes can be stored once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explicit hierarchies
&lt;/h3&gt;

&lt;p&gt;Relationships such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Category → Subcategory → Product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be represented explicitly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easier centralized maintenance in some cases
&lt;/h3&gt;

&lt;p&gt;If a shared hierarchy changes frequently, maintaining it in one normalized structure can sometimes be advantageous.&lt;/p&gt;

&lt;h3&gt;
  
  
  Useful for complex dimensions
&lt;/h3&gt;

&lt;p&gt;Snowflaking can be appropriate when dimensions contain complex, reusable relationships.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Disadvantages of Snowflake Schema
&lt;/h1&gt;

&lt;h3&gt;
  
  
  More joins
&lt;/h3&gt;

&lt;p&gt;A simple analytical question may require several tables.&lt;/p&gt;

&lt;h3&gt;
  
  
  More complex SQL
&lt;/h3&gt;

&lt;p&gt;Analysts need to understand the relationships between multiple dimension tables.&lt;/p&gt;

&lt;h3&gt;
  
  
  More complicated BI models
&lt;/h3&gt;

&lt;p&gt;Longer relationship chains can make semantic models harder to understand and can affect filter propagation and usability. Microsoft specifically notes these considerations when snowflake designs are modeled in Power BI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Greater modeling complexity
&lt;/h3&gt;

&lt;p&gt;More tables mean more relationships, testing, documentation, and governance.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Galaxy Schema
&lt;/h1&gt;

&lt;p&gt;Now imagine that your organization doesn't have only one business process.&lt;/p&gt;

&lt;p&gt;You have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sales
Inventory
Shipping
Returns
Payments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each process may require its own fact table.&lt;/p&gt;

&lt;p&gt;Instead of building one giant fact table, we can create multiple fact tables that share common dimensions.&lt;/p&gt;

&lt;p&gt;This is known as a:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Galaxy Schema&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fact Constellation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fact Constellation Schema&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    dim_date
                       |
                       |
       ┌──────── fact_sales ────────┐
       |             |              |
       |             |              |
dim_customer   dim_product     dim_store
       |             |              |
       |             |              |
       └────── fact_returns ────────┘
                       |
                       |
                 fact_inventory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key idea is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Multiple fact tables share common dimensions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is why it is called a constellation: several stars connected through shared dimensions.&lt;/p&gt;

&lt;p&gt;The fact-constellation concept is documented as a model where multiple fact tables share dimension tables.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Example: An E-Commerce Galaxy
&lt;/h1&gt;

&lt;p&gt;Imagine an e-commerce company.&lt;/p&gt;

&lt;p&gt;We have three business processes:&lt;/p&gt;

&lt;h3&gt;
  
  
  Sales
&lt;/h3&gt;



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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;quantity
sales_amount
discount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Returns
&lt;/h3&gt;



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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return_quantity
refund_amount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inventory
&lt;/h3&gt;



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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stock_quantity
inventory_value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three can share:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dim_product
dim_date
dim_store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    dim_date
                       |
          ┌────────────┼────────────┐
          |            |            |
          ↓            ↓            ↓
     fact_sales   fact_returns   fact_inventory
          ↑            ↑            ↑
          |            |            |
          └──────── dim_product ────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives analysts the ability to analyze several business processes using common dimensions.&lt;/p&gt;




&lt;h1&gt;
  
  
  . The Critical Concept: Conformed Dimensions
&lt;/h1&gt;

&lt;p&gt;Galaxy schemas introduce an extremely important data-warehousing concept:&lt;/p&gt;

&lt;h2&gt;
  
  
  Conformed dimensions
&lt;/h2&gt;

&lt;p&gt;A conformed dimension is a dimension that is consistently defined and can be shared across multiple fact tables.&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;dim_date
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;should mean the same thing whether you're analyzing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sales
Inventory
Returns
Shipping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;should have consistent definitions across the business processes that use it.&lt;/p&gt;

&lt;p&gt;This allows meaningful cross-process analysis.&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;Sales revenue
        +
Inventory levels
        +
Product returns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be analyzed using the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product
Date
Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;dimensions.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Star vs Snowflake vs Galaxy
&lt;/h1&gt;

&lt;p&gt;Here's the big picture:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;⭐ Star&lt;/th&gt;
&lt;th&gt;❄️ Snowflake&lt;/th&gt;
&lt;th&gt;🌌 Galaxy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Main idea&lt;/td&gt;
&lt;td&gt;One fact + dimensions&lt;/td&gt;
&lt;td&gt;Normalized dimensions&lt;/td&gt;
&lt;td&gt;Multiple fact tables&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dimensions&lt;/td&gt;
&lt;td&gt;Mostly denormalized&lt;/td&gt;
&lt;td&gt;More normalized&lt;/td&gt;
&lt;td&gt;Shared/conformed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fact tables&lt;/td&gt;
&lt;td&gt;Usually one or several stars&lt;/td&gt;
&lt;td&gt;Usually one or several&lt;/td&gt;
&lt;td&gt;Multiple&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Query complexity&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Medium–High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number of joins&lt;/td&gt;
&lt;td&gt;Fewer&lt;/td&gt;
&lt;td&gt;More&lt;/td&gt;
&lt;td&gt;Depends&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Redundancy&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Depends&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ease of use&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;td&gt;Moderate–Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best for&lt;/td&gt;
&lt;td&gt;Straightforward analytics&lt;/td&gt;
&lt;td&gt;Complex dimensions&lt;/td&gt;
&lt;td&gt;Multiple business processes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Governance needs&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical use&lt;/td&gt;
&lt;td&gt;BI/reporting&lt;/td&gt;
&lt;td&gt;Complex hierarchies&lt;/td&gt;
&lt;td&gt;Enterprise analytics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These are not absolute performance rules. Actual performance depends on the warehouse engine, data size, physical design, workload, and query patterns.&lt;/p&gt;




&lt;h1&gt;
  
  
  . So Which One Should You Choose?
&lt;/h1&gt;

&lt;p&gt;This is where data modeling becomes interesting.&lt;/p&gt;

&lt;p&gt;Don't start with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I need a star schema."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Start with the business requirements.&lt;/p&gt;

&lt;p&gt;Ask these questions.&lt;/p&gt;




&lt;h2&gt;
  
  
  : What business process am I modeling?
&lt;/h2&gt;

&lt;p&gt;If you're modeling:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;a star schema may be sufficient.&lt;/p&gt;

&lt;p&gt;If you're modeling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sales
Inventory
Returns
Shipping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you may eventually need a galaxy/fact constellation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Question 2: How complex are my dimensions?
&lt;/h2&gt;

&lt;p&gt;Suppose you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product
 └── Subcategory
      └── Category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ask yourself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does this hierarchy actually need to be normalized?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If not, keeping it inside:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;may be simpler.&lt;/p&gt;

&lt;p&gt;If the hierarchy is independently managed, reused, or genuinely complex, snowflaking may be justified.&lt;/p&gt;




&lt;p&gt;: Who will query the data?&lt;/p&gt;

&lt;p&gt;This is often overlooked.&lt;/p&gt;

&lt;p&gt;If your users are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Business analysts
BI developers
Data scientists
Managers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;simplicity matters.&lt;/p&gt;

&lt;p&gt;A model like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fact_sales
dim_product
dim_customer
dim_date
dim_store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is easier to understand than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fact_sales
dim_product
dim_subcategory
dim_category
dim_brand
dim_supplier
dim_region
dim_country
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The schema isn't just a database structure.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;It is also a user interface for your data.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  . Question 4: How often do dimensions change?
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product Category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If category information changes frequently and is maintained centrally, normalization may offer advantages.&lt;/p&gt;

&lt;p&gt;But if the dimension is relatively stable, introducing several additional tables may create complexity without enough benefit.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Question 5: Do Multiple Business Processes Share Dimensions?
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sales
Inventory
Returns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;all need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product
Date
Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a strong signal that you should think about a &lt;strong&gt;fact constellation / galaxy architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of duplicating dimensions, use shared conformed dimensions.&lt;/p&gt;




&lt;h1&gt;
  
  
  . A Practical Decision Tree
&lt;/h1&gt;

&lt;p&gt;You can simplify the decision process like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;START
  |
  ↓
What are you modeling?
  |
  ├── One main business process
  |        |
  |        ↓
  |    Start with STAR
  |
  └── Multiple business processes
           |
           ↓
      Shared dimensions?
           |
       ┌───┴───┐
       |       |
      YES      NO
       |       |
       ↓       ↓
    GALAXY   Separate
             dimensional
              models
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Are any dimensions genuinely complex or
strongly hierarchical?
          |
      ┌───┴───┐
      |       |
     YES      NO
      |       |
      ↓       ↓
Consider     Keep
SNOWFLAKE    STAR
selectively
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  . A Better Real-World Approach: Don't Be Dogmatic
&lt;/h1&gt;

&lt;p&gt;One of the biggest mistakes beginners make is thinking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Project = one schema
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In reality, you can combine approaches.&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;                    dim_date
                       |
                       |
                  fact_sales
                 /     |     \
                /      |      \
       dim_product   dim_customer   dim_store
            |
            |
       dim_category
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The overall model is a &lt;strong&gt;star&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dim_product&lt;/code&gt; has been &lt;strong&gt;partially snowflaked&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Multiple fact tables could turn the overall warehouse into a &lt;strong&gt;galaxy&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This hybrid approach is often more practical than forcing an entire warehouse into one pure pattern.&lt;/p&gt;

&lt;p&gt;Modern guidance also recognizes that production warehouses can combine star modeling with selective snowflaking rather than treating the choice as all-or-nothing.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Strong Data Modeling Principles
&lt;/h1&gt;

&lt;p&gt;Regardless of the schema you choose, remember these principles.&lt;/p&gt;

&lt;h3&gt;
  
  
  . Define the grain first
&lt;/h3&gt;

&lt;p&gt;Write this sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"One row in this fact table represents ______."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you cannot complete that sentence clearly, stop modeling.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Identify the business process
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What event are we measuring?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A sale
A shipment
A payment
A return
An inventory snapshot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  . Separate facts from descriptive attributes
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;quantity
revenue
cost
profit
discount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customer
product
location
date
employee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. Think about query patterns
&lt;/h3&gt;

&lt;p&gt;Don't optimize only for theoretical normalization.&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What questions will analysts actually ask?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  . Avoid unnecessary joins
&lt;/h3&gt;

&lt;p&gt;Every additional relationship adds cognitive and technical complexity.&lt;/p&gt;

&lt;p&gt;If an analyst needs five joins to answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What were sales by product category?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;your model may deserve another look.&lt;/p&gt;




&lt;h3&gt;
  
  
  6. Use conformed dimensions
&lt;/h3&gt;

&lt;p&gt;When multiple fact tables share:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date
Customer
Product
Location
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;make sure those dimensions have consistent definitions.&lt;/p&gt;




&lt;h3&gt;
  
  
  . Design for the people using the warehouse
&lt;/h3&gt;

&lt;p&gt;A technically elegant model that nobody understands is not necessarily a successful analytical model.&lt;/p&gt;




&lt;h1&gt;
  
  
  . The Interview Question You Should Be Ready For
&lt;/h1&gt;

&lt;p&gt;You may hear:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"When would you choose a star schema over a snowflake schema?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A strong answer is not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Star schema is faster."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's too simplistic.&lt;/p&gt;

&lt;p&gt;Instead:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"I would generally start with a star schema when the workload is primarily analytical and users benefit from simple fact-to-dimension relationships. I would consider snowflaking when a dimension contains a hierarchy or structure that has a clear reason to be normalized, such as independent maintenance or significant reuse. If multiple business processes need to share dimensions, I would consider a fact constellation or galaxy design. Ultimately, the choice depends on grain, query patterns, dimension complexity, governance, and the capabilities of the warehouse platform."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a much stronger data-engineering answer.&lt;/p&gt;




&lt;h1&gt;
  
  
  . The Golden Rule
&lt;/h1&gt;

&lt;p&gt;If you remember only one thing from this article, remember this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Start simple. Add complexity only when the business or data gives you a reason.&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;A practical starting point for many analytical workloads is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              dim_customer
                   |
                   |
dim_product — fact_sales — dim_date
                   |
                   |
                dim_store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with a clean star.&lt;/p&gt;

&lt;p&gt;Then ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Does this dimension genuinely need normalization?
                    ↓
              Snowflake it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Do I have multiple business processes
sharing common dimensions?
                    ↓
          Build a fact constellation.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal isn't to build the most complicated schema.&lt;/p&gt;

&lt;p&gt;The goal is to build a schema that makes &lt;strong&gt;reliable analysis easier&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  . Final Takeaway
&lt;/h1&gt;

&lt;p&gt;The three schemas can be thought of as a progression in modeling complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;⭐ STAR
Simple dimensional analytics
        ↓
❄️ SNOWFLAKE
More normalized dimensions
        ↓
🌌 GALAXY
Multiple fact tables + shared dimensions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But don't interpret this as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Star → Snowflake → Galaxy = better
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It isn't a ranking.&lt;/p&gt;

&lt;p&gt;Each solves a different modeling problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  ⭐ Star
&lt;/h3&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Keep analytics simple."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  ❄️ Snowflake
&lt;/h3&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"This dimension has a good reason to be normalized."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  🌌 Galaxy
&lt;/h3&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"We have multiple business processes that need shared dimensions."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And above everything else:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Understand the business process, define the grain, understand the users and query patterns, then choose the schema.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the real skill behind data warehouse modeling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Cheat Sheet
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STAR
├── Simple
├── Denormalized dimensions
├── Fewer joins
├── Analyst-friendly
└── Great starting point

SNOWFLAKE
├── Normalized dimensions
├── Explicit hierarchies
├── More joins
├── More complex
└── Use when normalization has a clear benefit

GALAXY
├── Multiple fact tables
├── Shared dimensions
├── Multiple business processes
├── Requires strong governance
└── Useful for broader enterprise analytics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The best data warehouse schema isn't the one with the most tables.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's the one that allows your organization to answer important questions accurately, consistently, and efficiently.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>ai</category>
      <category>data</category>
    </item>
    <item>
      <title>Introduction to Machine Learning: A Beginner's Guide</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Tue, 22 Sep 2026 10:16:59 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/introduction-to-machine-learning-a-beginners-guide-2fh5</link>
      <guid>https://dev.to/ephantus_macharia_/introduction-to-machine-learning-a-beginners-guide-2fh5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Machine learning is about teaching computers to find patterns in data and use those patterns to make predictions or decisions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  So, What Exactly Is Machine Learning?
&lt;/h2&gt;

&lt;p&gt;Let's start with a simple example.&lt;/p&gt;

&lt;p&gt;Imagine you have data showing how many hours students studied and the scores they received in an exam.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hours Studied&lt;/th&gt;
&lt;th&gt;Exam Score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;42&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;61&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;68&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;73&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;84&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;89&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you look at this data, you can probably notice a pattern:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Students who studied more hours generally scored higher.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now imagine giving this data to a computer and asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can you learn this pattern and predict the score of a student who studies for 6.5 hours?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's where machine learning comes in.&lt;/p&gt;

&lt;p&gt;Instead of explicitly programming every possible answer, we give the computer data and an algorithm that can learn from it.&lt;/p&gt;

&lt;p&gt;The computer builds a &lt;strong&gt;model&lt;/strong&gt; that represents the pattern it discovered.&lt;/p&gt;

&lt;p&gt;We can then use that model to make predictions about new data.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Machine Learning Works
&lt;/h2&gt;

&lt;p&gt;At a high level, machine learning follows a process like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       DATA
         ↓
   Clean &amp;amp; Prepare
         ↓
    Train the Model
         ↓
      Evaluate
         ↓
    Make Predictions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important thing to understand is that &lt;strong&gt;the model is not magically intelligent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It learns from examples.&lt;/p&gt;

&lt;p&gt;The quality and relevance of the data we give it can have a huge impact on the results.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Machine Learning Example
&lt;/h2&gt;

&lt;p&gt;Let's return to our student example.&lt;/p&gt;

&lt;p&gt;We have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; Hours studied&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; Exam score&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In machine learning terminology, the input is called a &lt;strong&gt;feature&lt;/strong&gt;, while the value we're trying to predict is called the &lt;strong&gt;target&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature
   ↓
Hours Studied
   ↓
Machine Learning Model
   ↓
Predicted Exam Score
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;6 hours studied
       ↓
     Model
       ↓
Approximately 73 marks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prediction doesn't have to be perfect.&lt;/p&gt;

&lt;p&gt;Machine learning is often about finding useful patterns and making predictions with an acceptable level of accuracy.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧩 The Main Types of Machine Learning
&lt;/h2&gt;

&lt;p&gt;Machine learning is commonly divided into three major categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Supervised Learning&lt;/li&gt;
&lt;li&gt;Unsupervised Learning&lt;/li&gt;
&lt;li&gt;Reinforcement Learning&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at each one.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Supervised Learning
&lt;/h2&gt;

&lt;p&gt;In supervised learning, we train a model using data where the correct answer is already known.&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;Hours Studied → Exam Score
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We already know the exam scores in our training data.&lt;/p&gt;

&lt;p&gt;The model learns the relationship between the input and the known output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common supervised learning problems
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Regression&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used when we're predicting a numerical value.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Predicting house prices&lt;/li&gt;
&lt;li&gt;Predicting sales&lt;/li&gt;
&lt;li&gt;Predicting temperature&lt;/li&gt;
&lt;li&gt;Predicting exam scores&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Classification&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used when we're predicting a category.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Spam or not spam&lt;/li&gt;
&lt;li&gt;Fraud or not fraud&lt;/li&gt;
&lt;li&gt;Pass or fail&lt;/li&gt;
&lt;li&gt;Cat or dog&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some popular supervised learning algorithms include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear Regression&lt;/li&gt;
&lt;li&gt;Logistic Regression&lt;/li&gt;
&lt;li&gt;Decision Trees&lt;/li&gt;
&lt;li&gt;Random Forest&lt;/li&gt;
&lt;li&gt;Support Vector Machines&lt;/li&gt;
&lt;li&gt;K-Nearest Neighbors&lt;/li&gt;
&lt;li&gt;Gradient Boosting&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Unsupervised Learning
&lt;/h2&gt;

&lt;p&gt;What happens when we don't have the answers?&lt;/p&gt;

&lt;p&gt;That's where unsupervised learning comes in.&lt;/p&gt;

&lt;p&gt;Instead of telling the model what the correct answer is, we give it data and ask it to find patterns or structures.&lt;/p&gt;

&lt;p&gt;For example, imagine a supermarket has thousands of customers.&lt;/p&gt;

&lt;p&gt;We might have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Customer
   ↓
Age
Income
Spending
Purchase Frequency
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't tell the algorithm which customers belong together.&lt;/p&gt;

&lt;p&gt;Instead, it can discover groups of customers with similar characteristics.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;clustering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One popular clustering algorithm is &lt;strong&gt;K-Means&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unsupervised learning can be useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Finding groups in data&lt;/li&gt;
&lt;li&gt;Anomaly detection&lt;/li&gt;
&lt;li&gt;Exploring large datasets&lt;/li&gt;
&lt;li&gt;Recommendation systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Reinforcement Learning
&lt;/h2&gt;

&lt;p&gt;Reinforcement learning works a little differently.&lt;/p&gt;

&lt;p&gt;Instead of learning from a dataset with known answers, an agent learns by interacting with an environment.&lt;/p&gt;

&lt;p&gt;Think about teaching a computer to play a game.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Environment
             ↓
           Agent
             ↓
          Action
             ↓
          Reward
             ↓
          Learn
             ↓
        Try Again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good actions can receive positive rewards, while poor actions can receive negative rewards.&lt;/p&gt;

&lt;p&gt;Over time, the agent learns which actions tend to produce better results.&lt;/p&gt;

&lt;p&gt;Reinforcement learning is used in areas such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Robotics&lt;/li&gt;
&lt;li&gt;Games&lt;/li&gt;
&lt;li&gt;Autonomous systems&lt;/li&gt;
&lt;li&gt;Resource optimization&lt;/li&gt;
&lt;li&gt;Decision-making systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  The Machine Learning Workflow
&lt;/h1&gt;

&lt;p&gt;Learning machine learning isn't just about knowing algorithms.&lt;/p&gt;

&lt;p&gt;There is a complete workflow behind a machine learning project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Collect Data
&lt;/h2&gt;

&lt;p&gt;First, we need data.&lt;/p&gt;

&lt;p&gt;This could come from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSV files&lt;/li&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;Sensors&lt;/li&gt;
&lt;li&gt;Websites&lt;/li&gt;
&lt;li&gt;Business systems&lt;/li&gt;
&lt;/ul&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;customer_data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;might contain thousands of customer records.&lt;/p&gt;




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

&lt;p&gt;Real-world data is rarely perfect.&lt;/p&gt;

&lt;p&gt;You might find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Missing values&lt;/li&gt;
&lt;li&gt;Duplicate records&lt;/li&gt;
&lt;li&gt;Incorrect dates&lt;/li&gt;
&lt;li&gt;Outliers&lt;/li&gt;
&lt;li&gt;Spelling inconsistencies&lt;/li&gt;
&lt;li&gt;Wrong data types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where tools like &lt;strong&gt;Pandas&lt;/strong&gt; become extremely useful.&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 python"&gt;&lt;code&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="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="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;customer_data.csv&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;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="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&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;isnull&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before training a model, we need to understand what we're working with.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Prepare the Data
&lt;/h2&gt;

&lt;p&gt;Now we transform the data into something the machine learning algorithm can use.&lt;/p&gt;

&lt;p&gt;This can include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Selecting relevant features&lt;/li&gt;
&lt;li&gt;Encoding categorical variables&lt;/li&gt;
&lt;li&gt;Scaling numerical values&lt;/li&gt;
&lt;li&gt;Handling missing data&lt;/li&gt;
&lt;li&gt;Removing unnecessary columns&lt;/li&gt;
&lt;/ul&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;Gender
Male
Female
Female
Male
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;might need to be converted into numerical values before being passed to some algorithms.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Split the Data
&lt;/h2&gt;

&lt;p&gt;One of the most important concepts for beginners is the &lt;strong&gt;train-test split&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We generally don't want to train and evaluate a model using exactly the same data.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Dataset
                    |
          --------------------
          |                  |
       Training            Testing
        Data                Data
          |                  |
          ↓                  ↓
       Learn from       Evaluate on
          data          unseen data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.model_selection&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;train_test_split&lt;/span&gt;

&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;train_test_split&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="n"&gt;test_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model learns from the training data.&lt;/p&gt;

&lt;p&gt;The test data is then used to see how well the model performs on data it hasn't seen before.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 5: Train the Model
&lt;/h1&gt;

&lt;p&gt;Now comes the exciting part.&lt;/p&gt;

&lt;p&gt;We choose an algorithm and train it.&lt;/p&gt;

&lt;p&gt;For our simple example, we'll use &lt;strong&gt;Linear Regression&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.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="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_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.fit()&lt;/code&gt; method is where the model learns from the training data.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 6: Make Predictions
&lt;/h1&gt;

&lt;p&gt;Once the model has learned, we can use it to make predictions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;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_test&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;predictions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, the model might receive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hours Studied = 6.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and return something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Predicted Score ≈ 75
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact result depends on the data and the model.&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating the Visualization with Python
&lt;/h1&gt;

&lt;p&gt;Here's the Python code used to create the visualization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;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;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;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="c1"&gt;# Create sample data
&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="mi"&gt;1&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;2.5&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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.5&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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&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;9&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;61&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;68&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;73&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;84&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;89&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Create and train the 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;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Make predictions
&lt;/span&gt;&lt;span class="n"&gt;predicted&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;hours&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Visualize
&lt;/span&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scatter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Actual exam scores&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;predicted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;linewidth&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;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Model prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Study Hours vs Exam Score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hours Studied&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Exam Score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need to understand every line immediately.&lt;/p&gt;

&lt;p&gt;At the beginning, focus on the bigger picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create Data
     ↓
Create Model
     ↓
Train Model
     ↓
Make Predictions
     ↓
Visualize Results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The deeper understanding will come as you practice.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Is Overfitting?
&lt;/h1&gt;

&lt;p&gt;Here's one of the first concepts that confused many beginners when learning machine learning:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overfitting.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a student memorizes every question from a practice exam.&lt;/p&gt;

&lt;p&gt;They perform extremely well on that exact exam.&lt;/p&gt;

&lt;p&gt;But when you give them different questions, their performance drops significantly.&lt;/p&gt;

&lt;p&gt;A machine learning model can do something similar.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;overfitted model&lt;/strong&gt; learns the training data too closely, including noise and unusual patterns.&lt;/p&gt;

&lt;p&gt;As a result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Training Performance → Very High
New Data Performance  → Poor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We usually want a model that can &lt;strong&gt;generalize&lt;/strong&gt; well.&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't just memorize the training data. Learn patterns that also work on new data.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  How Do We Know If a Model Is Good?
&lt;/h1&gt;

&lt;p&gt;Training a model is only half the job.&lt;/p&gt;

&lt;p&gt;We also need to evaluate it.&lt;/p&gt;

&lt;p&gt;The evaluation metric depends on the type of problem.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Mean Absolute Error (MAE)&lt;/li&gt;
&lt;li&gt;Mean Squared Error (MSE)&lt;/li&gt;
&lt;li&gt;Root Mean Squared Error (RMSE)&lt;/li&gt;
&lt;li&gt;R² Score&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Accuracy&lt;/li&gt;
&lt;li&gt;Precision&lt;/li&gt;
&lt;li&gt;Recall&lt;/li&gt;
&lt;li&gt;F1 Score&lt;/li&gt;
&lt;li&gt;ROC-AUC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, accuracy can be calculated using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Correct Predictions
------------------- × 100
Total Predictions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But accuracy isn't always enough.&lt;/p&gt;

&lt;p&gt;For some problems, such as fraud detection or medical screening, precision and recall can provide more useful information.&lt;/p&gt;




&lt;h1&gt;
  
  
  Tools You Will See Everywhere
&lt;/h1&gt;

&lt;p&gt;If you're starting your machine learning journey with Python, you'll quickly encounter these libraries:&lt;/p&gt;

&lt;h3&gt;
  
  
  NumPy
&lt;/h3&gt;

&lt;p&gt;Useful for numerical operations and arrays.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pandas
&lt;/h3&gt;

&lt;p&gt;Useful for working with structured data.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Matplotlib
&lt;/h3&gt;

&lt;p&gt;Useful for creating visualizations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Scikit-learn
&lt;/h3&gt;

&lt;p&gt;One of the most useful libraries for learning traditional machine learning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sklearn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Together, these tools give you a strong foundation for many beginner machine learning projects.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Beginner's Machine Learning Roadmap
&lt;/h1&gt;

&lt;p&gt;If you're wondering &lt;strong&gt;"What should I learn next?"&lt;/strong&gt;, don't try to learn everything at once.&lt;/p&gt;

&lt;p&gt;A practical path looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python
  ↓
NumPy &amp;amp; Pandas
  ↓
Data Cleaning
  ↓
Data Visualization
  ↓
Statistics
  ↓
Supervised Learning
  ↓
Unsupervised Learning
  ↓
Model Evaluation
  ↓
Feature Engineering
  ↓
Machine Learning Projects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you're comfortable with these concepts, you can start exploring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deep Learning&lt;/li&gt;
&lt;li&gt;Natural Language Processing&lt;/li&gt;
&lt;li&gt;Computer Vision&lt;/li&gt;
&lt;li&gt;Recommendation Systems&lt;/li&gt;
&lt;li&gt;Time Series&lt;/li&gt;
&lt;li&gt;Large Language Models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there's no need to rush.&lt;/p&gt;

&lt;p&gt;A strong foundation is more valuable than knowing fifty algorithms without understanding what they actually do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Titanic Survival Prediction
&lt;/h3&gt;

&lt;p&gt;Predict whether a passenger survived based on information such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Age&lt;/li&gt;
&lt;li&gt;Sex&lt;/li&gt;
&lt;li&gt;Passenger class&lt;/li&gt;
&lt;li&gt;Fare&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Customer Segmentation
&lt;/h3&gt;

&lt;p&gt;Use K-Means to group customers based on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Income&lt;/li&gt;
&lt;li&gt;Spending&lt;/li&gt;
&lt;li&gt;Purchase frequency&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Spam Detection
&lt;/h3&gt;

&lt;p&gt;Build a classification model that predicts whether a message is spam.&lt;/p&gt;

&lt;p&gt;The goal isn't to create a perfect model.&lt;/p&gt;

&lt;p&gt;The goal is to understand the entire process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Problem
  ↓
Data
  ↓
Cleaning
  ↓
Exploration
  ↓
Features
  ↓
Model
  ↓
Evaluation
  ↓
Improvement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  The Most Important Lesson
&lt;/h1&gt;

&lt;p&gt;Machine learning isn't simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Give Python some data and let it predict the future."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There's much more to it.&lt;/p&gt;

&lt;p&gt;A good machine learning project starts with a &lt;strong&gt;good question&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then you need relevant data.&lt;/p&gt;

&lt;p&gt;Then you explore and clean that data.&lt;/p&gt;

&lt;p&gt;Then you choose an appropriate model.&lt;/p&gt;

&lt;p&gt;Then you evaluate the model.&lt;/p&gt;

&lt;p&gt;And finally, you ask whether the result is actually useful.&lt;/p&gt;

&lt;p&gt;That's why understanding the &lt;strong&gt;process&lt;/strong&gt; is more important than memorizing algorithms.&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusions
&lt;/h1&gt;

&lt;p&gt;Machine learning can look complicated from the outside.&lt;/p&gt;

&lt;p&gt;But when you break it down, the foundation is surprisingly approachable.&lt;/p&gt;

&lt;p&gt;You start with data.&lt;/p&gt;

&lt;p&gt;You look for patterns.&lt;/p&gt;

&lt;p&gt;You train a model.&lt;/p&gt;

&lt;p&gt;You evaluate what it learned.&lt;/p&gt;

&lt;p&gt;And then you use it to make predictions or discover something useful.&lt;/p&gt;

&lt;p&gt;You won't understand everything after reading one article  and that's completely normal.&lt;/p&gt;

&lt;p&gt;The best way to learn machine learning is to take one concept at a time and &lt;strong&gt;build something with it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Start small.&lt;/p&gt;

&lt;p&gt;Write the code.&lt;/p&gt;

&lt;p&gt;Break it.&lt;/p&gt;

&lt;p&gt;Fix it.&lt;/p&gt;

&lt;p&gt;Visualize the results.&lt;/p&gt;

&lt;p&gt;Then move to the next concept.&lt;/p&gt;

&lt;p&gt;That's how the pieces start coming together.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>A Beginner’s Guide to Unsupervised Learning in Machine Learning</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Wed, 16 Sep 2026 06:16:54 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/a-beginners-guide-to-unsupervised-learning-in-machine-learning-1535</link>
      <guid>https://dev.to/ephantus_macharia_/a-beginners-guide-to-unsupervised-learning-in-machine-learning-1535</guid>
      <description>&lt;h2&gt;
  
  
  What Is Unsupervised Learning?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Unsupervised learning&lt;/strong&gt; is a type of machine learning where an algorithm learns patterns from data &lt;strong&gt;without being given labeled answers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine you have information about 1,000 customers.&lt;/p&gt;

&lt;p&gt;You know things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Age&lt;/li&gt;
&lt;li&gt;Annual income&lt;/li&gt;
&lt;li&gt;Amount spent&lt;/li&gt;
&lt;li&gt;Number of purchases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But you don't have a column telling you what type of customer each person is.&lt;/p&gt;

&lt;p&gt;There might not be a column like:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer&lt;/th&gt;
&lt;th&gt;Income&lt;/th&gt;
&lt;th&gt;Spending&lt;/th&gt;
&lt;th&gt;Customer Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;25,000&lt;/td&gt;
&lt;td&gt;15,000&lt;/td&gt;
&lt;td&gt;Budget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;td&gt;45,000&lt;/td&gt;
&lt;td&gt;Regular&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;90,000&lt;/td&gt;
&lt;td&gt;80,000&lt;/td&gt;
&lt;td&gt;Premium&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Instead, you give the data to an algorithm and ask it to &lt;strong&gt;discover patterns or groups&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It might discover:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Group 1 → Low income + low spending
Group 2 → Medium income + medium spending
Group 3 → High income + high spending
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You didn't tell the algorithm that these groups existed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It discovered them from the data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the basic idea behind unsupervised learning.&lt;/p&gt;




&lt;h1&gt;
  
  
  Supervised vs. Unsupervised Learning
&lt;/h1&gt;

&lt;p&gt;One of the easiest ways to understand unsupervised learning is by comparing it with supervised learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Supervised Learning
&lt;/h2&gt;

&lt;p&gt;In supervised learning, we have data with known answers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input Data → Known Labels → Machine Learning Model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;House Size + Bedrooms → House Price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model learns from historical examples where the correct answer is already known.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;House Size&lt;/th&gt;
&lt;th&gt;Bedrooms&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;800 sq ft&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;$100,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,200 sq ft&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;$160,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,800 sq ft&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;$240,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The model can then predict the price of a new house.&lt;/p&gt;




&lt;h2&gt;
  
  
  Unsupervised Learning
&lt;/h2&gt;

&lt;p&gt;With unsupervised learning, there is no target label.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input Data → Algorithm → Hidden Patterns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;Customer Data
      ↓
Machine Learning Algorithm
      ↓
Discover Groups
      ↓
Customer Segments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key difference is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Supervised learning learns from known answers, while unsupervised learning searches for patterns without known answers.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  How Unsupervised Learning Works
&lt;/h1&gt;

&lt;p&gt;A simple way to visualize the process is:&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
    A[Raw Data] --&amp;gt; B[Explore Data]
    B --&amp;gt; C[Choose Features]
    C --&amp;gt; D[Unsupervised Algorithm]
    D --&amp;gt; E[Discover Patterns]
    E --&amp;gt; F[Interpret Results]&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The algorithm isn't necessarily trying to predict a specific answer.&lt;/p&gt;

&lt;p&gt;Instead, it might be trying to answer questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which observations are similar?&lt;/li&gt;
&lt;li&gt;Are there natural groups?&lt;/li&gt;
&lt;li&gt;Which observations are unusual?&lt;/li&gt;
&lt;li&gt;Can we represent this dataset using fewer dimensions?&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Why Do We Need Unsupervised Learning?
&lt;/h1&gt;

&lt;p&gt;Real-world datasets aren't always neatly labeled.&lt;/p&gt;

&lt;p&gt;Sometimes we have thousands or millions of records but don't know what patterns exist inside them.&lt;/p&gt;

&lt;p&gt;Unsupervised learning helps us explore these datasets.&lt;/p&gt;

&lt;p&gt;Some common applications include:&lt;/p&gt;

&lt;h3&gt;
  
  
  Customer Segmentation
&lt;/h3&gt;

&lt;p&gt;Businesses can group customers according to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spending habits&lt;/li&gt;
&lt;li&gt;Income&lt;/li&gt;
&lt;li&gt;Purchase frequency&lt;/li&gt;
&lt;li&gt;Product preferences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This can help businesses create more targeted marketing strategies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recommendation Systems
&lt;/h3&gt;

&lt;p&gt;Unsupervised techniques can help identify products, movies, songs, or other items that are similar.&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;Customer purchases Product A
            ↓
Find similar customers/products
            ↓
Recommend Products B and C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Anomaly Detection
&lt;/h3&gt;

&lt;p&gt;Unsupervised algorithms can identify observations that look very different from normal behavior.&lt;/p&gt;

&lt;p&gt;For example, a bank could analyze transaction behavior and identify unusual transactions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Exploration
&lt;/h3&gt;

&lt;p&gt;Sometimes you don't even know what you're looking for.&lt;/p&gt;

&lt;p&gt;Unsupervised learning can help you explore a dataset and discover hidden structures.&lt;/p&gt;




&lt;h1&gt;
  
  
  Main Types of Unsupervised Learning
&lt;/h1&gt;

&lt;p&gt;There are several important techniques you should understand as a beginner.&lt;/p&gt;

&lt;p&gt;The three I recommend learning first are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Clustering&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dimensionality Reduction&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Anomaly Detection&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Clustering
&lt;/h1&gt;

&lt;p&gt;Clustering is one of the most common applications of unsupervised learning.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Group similar data points together.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine having hundreds of points on a graph.&lt;/p&gt;

&lt;p&gt;Before clustering, you might see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        • • •
      • • • •
        • •

                    • • •
                  • • • •
                    • •

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

&lt;/div&gt;



&lt;p&gt;The algorithm attempts to identify natural groups within those points.&lt;/p&gt;

&lt;p&gt;After clustering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cluster A          Cluster B          Cluster C

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

&lt;/div&gt;



&lt;p&gt;Some popular clustering algorithms include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;K-Means&lt;/li&gt;
&lt;li&gt;Hierarchical Clustering&lt;/li&gt;
&lt;li&gt;DBSCAN&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Dimensionality Reduction
&lt;/h1&gt;

&lt;p&gt;Datasets can contain hundreds or even thousands of features.&lt;/p&gt;

&lt;p&gt;Imagine having:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature 1
Feature 2
Feature 3
Feature 4
...
Feature 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to visualize 100 dimensions isn't practical.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dimensionality reduction&lt;/strong&gt; attempts to reduce the number of features while preserving important information.&lt;/p&gt;

&lt;p&gt;A common technique is &lt;strong&gt;Principal Component Analysis (PCA)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[100 Features] --&amp;gt; B[PCA]
    B --&amp;gt; C[Important Components]
    C --&amp;gt; D[2D or 3D Visualization]&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Other techniques include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PCA&lt;/li&gt;
&lt;li&gt;t-SNE&lt;/li&gt;
&lt;li&gt;UMAP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These techniques are especially useful when exploring complex datasets.&lt;/p&gt;




&lt;h1&gt;
  
  
  Anomaly Detection
&lt;/h1&gt;

&lt;p&gt;Anomaly detection focuses on finding observations that don't look like the rest of the data.&lt;/p&gt;

&lt;p&gt;Imagine most transactions from a customer look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$10
$25
$40
$15
$30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then suddenly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$5,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That transaction might be considered unusual.&lt;/p&gt;

&lt;p&gt;Anomaly detection algorithms can help identify observations like this.&lt;/p&gt;

&lt;p&gt;Some algorithms include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Isolation Forest&lt;/li&gt;
&lt;li&gt;Local Outlier Factor&lt;/li&gt;
&lt;li&gt;One-Class SVM&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  A Practical Example: Customer Segmentation
&lt;/h1&gt;

&lt;p&gt;Let's make things more practical.&lt;/p&gt;

&lt;p&gt;Suppose a company has customer data containing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Annual income&lt;/li&gt;
&lt;li&gt;Annual spending&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our goal is to discover whether customers naturally form different groups.&lt;/p&gt;

&lt;p&gt;We can use &lt;strong&gt;K-Means clustering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our dataset might look something like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer&lt;/th&gt;
&lt;th&gt;Annual Income&lt;/th&gt;
&lt;th&gt;Annual Spending&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;25,000&lt;/td&gt;
&lt;td&gt;15,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;30,000&lt;/td&gt;
&lt;td&gt;20,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;35,000&lt;/td&gt;
&lt;td&gt;18,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;td&gt;45,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;65,000&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;80,000&lt;/td&gt;
&lt;td&gt;75,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can visualize this data using a scatter plot.&lt;/p&gt;

&lt;p&gt;The x-axis represents income, while the y-axis represents spending.&lt;/p&gt;

&lt;p&gt;Once K-Means is applied, the algorithm may discover three different groups.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Does K-Means Work?
&lt;/h1&gt;

&lt;p&gt;K-Means is easier to understand when broken down into steps.&lt;/p&gt;

&lt;p&gt;Suppose we want to create &lt;strong&gt;3 clusters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We tell the algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;n_clusters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The algorithm then roughly follows this process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1  Choose K
&lt;/h3&gt;

&lt;p&gt;We decide how many clusters we want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;K = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2  Initialize Centroids
&lt;/h3&gt;

&lt;p&gt;The algorithm selects initial cluster centers called &lt;strong&gt;centroids&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          X
       Centroid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3  Assign Data Points
&lt;/h3&gt;

&lt;p&gt;Each data point is assigned to the closest centroid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Data Point → Closest Centroid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4  Recalculate Centroids
&lt;/h3&gt;

&lt;p&gt;The algorithm calculates new centers based on the assigned data points.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5  Repeat
&lt;/h3&gt;

&lt;p&gt;The assignment and centroid calculation process continues until the clusters stabilize.&lt;/p&gt;

&lt;p&gt;The basic process looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[Choose K] --&amp;gt; B[Initialize Centroids]
    B --&amp;gt; C[Assign Points to Closest Centroid]
    C --&amp;gt; D[Recalculate Centroids]
    D --&amp;gt; E{Clusters Stable?}
    E --&amp;gt;|No| C
    E --&amp;gt;|Yes| F[Final Clusters]&lt;/code&gt;&lt;/pre&gt;



&lt;h1&gt;
  
  
  Let's Implement K-Means in Python
&lt;/h1&gt;

&lt;p&gt;Now let's put the concept into practice.&lt;/p&gt;

&lt;p&gt;We'll use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pandas&lt;/li&gt;
&lt;li&gt;Matplotlib&lt;/li&gt;
&lt;li&gt;Scikit-learn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install them with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pandas matplotlib scikit-learn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then import the libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="kn"&gt;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Let's assume we have a file called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customer_data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can load it using Pandas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&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="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;customer_data.csv&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;data&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;h2&gt;
  
  
  Select Our Features
&lt;/h2&gt;

&lt;p&gt;For this example, we'll use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Annual Income&lt;/li&gt;
&lt;li&gt;Annual Spending
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&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;Annual Income&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;Annual Spending&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the features we want the algorithm to use when creating clusters.&lt;/p&gt;




&lt;h1&gt;
  
  
  Create the K-Means Model
&lt;/h1&gt;

&lt;p&gt;Let's create three clusters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;kmeans&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then train the model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;kmeans&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can obtain the cluster assigned to each customer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;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;kmeans&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;labels_&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now each customer has a cluster number.&lt;/p&gt;

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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer&lt;/th&gt;
&lt;th&gt;Income&lt;/th&gt;
&lt;th&gt;Spending&lt;/th&gt;
&lt;th&gt;Cluster&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;25,000&lt;/td&gt;
&lt;td&gt;15,000&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;30,000&lt;/td&gt;
&lt;td&gt;20,000&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;td&gt;45,000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;65,000&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;80,000&lt;/td&gt;
&lt;td&gt;75,000&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Cluster 0, 1, and 2 don't automatically mean "bad", "average", and "good".&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They are simply labels assigned by the algorithm.&lt;/p&gt;

&lt;p&gt;We need to interpret what each cluster represents.&lt;/p&gt;




&lt;h1&gt;
  
  
  Visualizing Our Clusters
&lt;/h1&gt;

&lt;p&gt;Visualization is one of the most useful parts of machine learning because it allows us to &lt;strong&gt;see what the algorithm discovered&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can create a scatter plot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scatter&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Annual Income&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&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Annual Spending&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cluster&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;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Annual Income&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Annual Spending&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Customer Segmentation Using 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;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting visualization allows us to see how customers have been grouped.&lt;/p&gt;

&lt;p&gt;A simplified representation looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Annual Spending
       ↑
       |
  ● ● ●              Cluster 3
 ● ● ● ●
       |
              ● ● ●
            ● ● ●        Cluster 2
       |
 ● ● ●
● ● ●                  Cluster 1
       |
       +------------------------→ Annual Income
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The visualization makes the concept much easier to understand:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Customers that are closer together tend to belong to the same cluster.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Understanding the Distance
&lt;/h1&gt;

&lt;p&gt;K-Means relies heavily on the concept of &lt;strong&gt;distance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A common distance measure is &lt;strong&gt;Euclidean distance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For two points:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = (x₁, y₁)
B = (x₂, y₂)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Euclidean distance is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;distance = √((x₂ - x₁)² + (y₂ - y₁)²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need to memorize the mathematics immediately.&lt;/p&gt;

&lt;p&gt;The important idea is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;K-Means uses distance to determine which data points are closest to each centroid.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  How Do We Choose the Number of Clusters?
&lt;/h1&gt;

&lt;p&gt;This is one of the most important questions when working with K-Means.&lt;/p&gt;

&lt;p&gt;We can't always simply guess:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;n_clusters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So how do we choose K?&lt;/p&gt;

&lt;p&gt;One popular approach is the &lt;strong&gt;Elbow Method&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Elbow Method
&lt;/h1&gt;

&lt;p&gt;The basic idea is to test different values of K and measure how well the clusters fit the data.&lt;/p&gt;

&lt;p&gt;We can calculate the inertia for different values of K:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;inertia&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;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="n"&gt;k&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;inertia&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inertia_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then visualize the results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;inertia&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;o&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Number of Clusters&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Inertia&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Elbow Method&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;a href="https://miro.medium.com/0*aY163H0kOrBO46S-.png" rel="noopener noreferrer"&gt;https://miro.medium.com/0*aY163H0kOrBO46S-.png&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;You may see a graph where the improvement becomes much smaller after a certain point.&lt;/p&gt;

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

&lt;p&gt;The elbow can help us choose a reasonable number of clusters.&lt;/p&gt;

&lt;p&gt;However, it isn't a magic rule.&lt;/p&gt;

&lt;p&gt;Domain knowledge and other evaluation techniques can also be important.&lt;/p&gt;

&lt;h1&gt;
  
  
  Important Things to Understand
&lt;/h1&gt;

&lt;p&gt;K-Means is powerful, but it isn't perfect.&lt;/p&gt;

&lt;p&gt;There are several things beginners should be aware of.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. You Need to Choose K
&lt;/h2&gt;

&lt;p&gt;K-Means requires you to specify the number of clusters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="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;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Choosing the wrong K can produce misleading groups.&lt;/p&gt;




&lt;h2&gt;
  
  
  2 Feature Scaling Can Matter
&lt;/h2&gt;

&lt;p&gt;Suppose one feature ranges from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1–10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while another ranges from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1–1,000,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The larger-scale feature can dominate distance calculations.&lt;/p&gt;

&lt;p&gt;This is why feature scaling is often important.&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 python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&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;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;kmeans&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_scaled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Clusters Need Interpretation
&lt;/h2&gt;

&lt;p&gt;The algorithm gives you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cluster 0
Cluster 1
Cluster 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the numbers themselves don't explain what the groups mean.&lt;/p&gt;

&lt;p&gt;As a data analyst, you still need to investigate the characteristics of each cluster.&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;Cluster 0
→ Lower income
→ Lower spending

Cluster 1
→ Medium income
→ Medium spending

Cluster 2
→ Higher income
→ Higher spending
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where &lt;strong&gt;data analysis and domain knowledge&lt;/strong&gt; become extremely important.&lt;/p&gt;

&lt;h1&gt;
  
  
  Real-World Applications
&lt;/h1&gt;

&lt;p&gt;Unsupervised learning appears in many different industries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Banking
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fraud detection&lt;/li&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Transaction analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Retail
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Product grouping&lt;/li&gt;
&lt;li&gt;Shopping behavior analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Entertainment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Music recommendations&lt;/li&gt;
&lt;li&gt;Movie recommendations&lt;/li&gt;
&lt;li&gt;User behavior analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Healthcare
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Patient grouping&lt;/li&gt;
&lt;li&gt;Identifying unusual observations&lt;/li&gt;
&lt;li&gt;Exploring medical datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cybersecurity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Detecting unusual network activity&lt;/li&gt;
&lt;li&gt;Identifying suspicious behavior&lt;/li&gt;
&lt;li&gt;Finding unusual patterns&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Beginner Project Idea
&lt;/h1&gt;

&lt;p&gt;If you're learning unsupervised learning, one of the best projects you can build is a:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Customer Segmentation Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Your workflow could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
    A[Find Dataset] --&amp;gt; B[Clean Data]
    B --&amp;gt; C[Explore Data]
    C --&amp;gt; D[Select Features]
    D --&amp;gt; E[Scale Features]
    E --&amp;gt; F[Apply K-Means]
    F --&amp;gt; G[Choose K]
    G --&amp;gt; H[Visualize Clusters]
    H --&amp;gt; I[Interpret Results]
    I --&amp;gt; J[Write Business Insights]&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You could investigate questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who are our high-value customers?&lt;/li&gt;
&lt;li&gt;Which customers have low spending?&lt;/li&gt;
&lt;li&gt;Are there natural groups of customers?&lt;/li&gt;
&lt;li&gt;Which customer segment should receive a marketing campaign?&lt;/li&gt;
&lt;li&gt;How does income relate to spending?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This transforms machine learning from &lt;strong&gt;just writing Python code&lt;/strong&gt; into actual &lt;strong&gt;data-driven problem solving&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Unsupervised learning is a powerful part of machine learning because it allows us to discover patterns, relationships, and structures hidden within data even when we don't have predefined labels.&lt;br&gt;
The most important lesson is that machine learning isn't always about predicting a known outcome. Sometimes, the goal is simply to understand the data and discover what it is telling us.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Nobody Tells You This, But Statistics Is the Thing Actually Doing the Work in "Data Science"</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Thu, 10 Sep 2026 11:39:15 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/nobody-tells-you-this-but-statistics-is-the-thing-actually-doing-the-work-in-data-science-5bnm</link>
      <guid>https://dev.to/ephantus_macharia_/nobody-tells-you-this-but-statistics-is-the-thing-actually-doing-the-work-in-data-science-5bnm</guid>
      <description>&lt;p&gt;A few months ago I was messing around with a spreadsheet, trying to figure out the average price my dad's paintings were selling for on his website. Simple enough, right? Average the column, done. Except some months he sold one painting for 200,000 shillings and nothing else for weeks. The "average" said his business was doing great. The reality was one lucky sale was carrying the whole story.&lt;/p&gt;

&lt;p&gt;That's the moment statistics stopped being a boring word from a textbook and started being the thing standing between me and looking like an idiot in front of actual data.&lt;/p&gt;

&lt;p&gt;Here's what I've come to believe , data science gets all the glamour  the Python, the models, the "AI did this" headlines  but statistics is the part nobody photographs. It's the kitchen, not the dining room. And if your kitchen is a mess, it doesn't matter how nice the table looks.&lt;/p&gt;

&lt;h2&gt;
  
  
  A model is just a guess with better manners
&lt;/h2&gt;

&lt;p&gt;People love to say machine learning "learns patterns." Sure. But underneath nearly every algorithm  linear regression, logistic regression, even the loss functions inside a neural network  there's a statistical idea doing the heavy lifting: variance, probability, distributions, correlation. The model isn't magic. It's statistics wearing a hoodie and calling itself AI.&lt;/p&gt;

&lt;p&gt;When I built out sample data for a tracker recently, I had to think about what a "realistic" spread of sale prices and dates would actually look like not just random numbers, but numbers shaped the way real-world data is shaped: lumpy, skewed, occasionally weird. That's a statistical decision before it's ever a coding decision.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dataset lies to you in very polite ways
&lt;/h2&gt;

&lt;p&gt;Outliers. Missing values. Sampling bias.  These aren't side topics you cover in week 3 of a course and forget. They're the reason two analysts can look at the exact same CSV file and walk away with opposite conclusions.&lt;/p&gt;

&lt;p&gt;I learned this the hard way working through a housing dataset for Nairobi some neighborhoods had ten times more listings than others, just because that's where the data happened to get collected, not because that's where all the housing actually is. Without understanding sampling and bias, I would've confidently reported a "trend" that was really just a data collection accident.&lt;/p&gt;

&lt;p&gt;Statistics is the discipline of asking "wait, is this real, or is this just how the data was gathered?" before you ask Python to plot it.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Significant" doesn't mean what you think it means
&lt;/h2&gt;

&lt;p&gt;This one quietly ruins a lot of beginner analyses. You run a test, you get a result, the result looks different from what you expected, and you announce a finding. But "different" and "statistically significant" are not the same sentence. A 3% bump in sales could be a real shift in customer behavior or it could be noise that would disappear if you reran the week.&lt;/p&gt;

&lt;p&gt;Without a grounding in hypothesis testing, p-values, and confidence intervals, data science turns into expensive guessing with extra charts.&lt;/p&gt;

&lt;h2&gt;
  
  
  For the small-business side of things, this is survival, not theory
&lt;/h2&gt;

&lt;p&gt;I'm involved with a farm business that runs two sides  supplying groceries in town, and turning potatoes into fries on the production side. The inventory numbers, the cashflow, the leave days employees take, the spoilage rate on produce  none of that becomes useful until you can say things like "this branch's costs vary more month to month than that one" or "this dip looks seasonal, not a real decline."&lt;/p&gt;

&lt;p&gt;That's not deep learning. That's basic statistical reasoning, applied to a real business, by someone without a fancy title. And it matters just as much  arguably more  than anything you'd build for a Kaggle competition.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is data science, actually?
&lt;/h2&gt;

&lt;p&gt;Strip away the buzzwords and data science is basically: collect data → understand its shape and limitations (statistics) → build something that uses that understanding to predict or explain (modeling) → check whether your conclusion could be a fluke (more statistics) → communicate it without lying by accident.&lt;/p&gt;

&lt;p&gt;Three out of those five steps are statistics with a different outfit on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The honest takeaway
&lt;/h2&gt;

&lt;p&gt;If you're new to this field and trying to decide where to spend your limited hours  more frameworks, more libraries, more flashy visualizations, or actually sitting with distributions, variance, and what a confidence interval is really telling you  pick the boring one. The libraries change every two years. The statistical reasoning underneath them doesn't.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Getting Started with Python: A Structured Guide for New Beginners.</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Tue, 19 May 2026 08:40:43 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/getting-started-with-python-a-structured-guide-for-new-beginners-5ded</link>
      <guid>https://dev.to/ephantus_macharia_/getting-started-with-python-a-structured-guide-for-new-beginners-5ded</guid>
      <description>&lt;p&gt;Python has consistently ranked among the world's most popular programming languages, and for good reason. Its clean syntax, extensive ecosystem, and broad applicability across domains  from web development and data science to automation and artificial intelligence make it an exceptionally strong first language for aspiring developers.&lt;/p&gt;

&lt;p&gt;However, getting started can feel overwhelming. The sheer volume of tutorials, courses, and conflicting advice online often leaves beginners unsure of where to focus their energy. This article cuts through that noise by providing a &lt;strong&gt;deliberate, structured learning path&lt;/strong&gt; covering the foundational concepts every Python developer needs, presented in the order that makes the most sense for building lasting understanding.&lt;/p&gt;

&lt;p&gt;Each section includes practical code examples you can run immediately. Whether you are exploring programming for the first time or transitioning from another discipline, this guide is designed to give you a clear and confident starting point.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Install Python
&lt;/h2&gt;

&lt;p&gt;Before anything else, get Python on your machine. Head to &lt;a href="https://python.org" rel="noopener noreferrer"&gt;python.org&lt;/a&gt;, download the latest stable version, and install it. Then write your very first program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Your very first Python program
&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;Hello, World! I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m learning Python 🐍&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;p&gt;Run it. See those words appear on your screen. That's your rite of passage in programming — welcome aboard!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt; with the Python extension. It gives you syntax highlighting, error hints, and a run button right in the editor.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. Variables and Data Types 📦
&lt;/h2&gt;

&lt;p&gt;Think of variables as labelled boxes that hold information. Python has four basic types you'll use constantly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# String — text
&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amara&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# Integer — whole number
&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;

&lt;span class="c1"&gt;# Float — decimal number
&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.72&lt;/span&gt;

&lt;span class="c1"&gt;# Boolean — True or False
&lt;/span&gt;&lt;span class="n"&gt;is_student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&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;Hi, I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; and I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Output: Hi, I'm Amara and I'm 24 years old.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 Python figures out the type automatically no need to declare &lt;code&gt;int x = 5&lt;/code&gt; like in Java or C. This is called &lt;strong&gt;dynamic typing&lt;/strong&gt;, and it makes Python very beginner-friendly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. Control Flow — Making Decisions
&lt;/h2&gt;

&lt;p&gt;Programs need to make decisions. That's where &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; come in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&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;🏆 Distinction!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&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;✅ You passed!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&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;📚 Keep studying, you&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ve got this.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Output: ✅ You passed!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that Python uses &lt;strong&gt;indentation&lt;/strong&gt; (spaces) to define code blocks — no curly braces &lt;code&gt;{}&lt;/code&gt; needed. This forces clean, readable code from day one.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Loops — Doing Things Repeatedly
&lt;/h2&gt;

&lt;p&gt;Instead of writing the same line 10 times, loops do the repetition for you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# for loop — great for going through a list
&lt;/span&gt;&lt;span class="n"&gt;fruits&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;mango&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;banana&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;avocado&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fruit&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fruits&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;I love &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;! &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# while loop — runs as long as a condition is True
&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;Count: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use a &lt;code&gt;for&lt;/code&gt; loop when you know how many times to repeat. Use a &lt;code&gt;while&lt;/code&gt; loop when you're waiting for a condition to change.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Functions — Reusable Blocks of Code 🔧
&lt;/h2&gt;

&lt;p&gt;Functions let you write code once and use it many times. This is one of the most important ideas in all of programming.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;English&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Swahili&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;Karibu, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;! 🇰🇪&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&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;Welcome, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;! 👋&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Brian&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amara&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Swahili&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Output: Welcome, Brian! 👋
# Output: Karibu, Amara! 🇰🇪
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; If you find yourself copy-pasting the same code more than twice, it belongs in a function.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  6. Lists and Dictionaries
&lt;/h2&gt;

&lt;p&gt;Python has powerful built-in ways to organize data. Two you'll use constantly:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lists&lt;/strong&gt; — ordered, changeable collections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tasks&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;learn Python&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;build a project&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;get a job&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;celebrate 🎉&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;tasks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;   &lt;span class="c1"&gt;# learn Python
&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;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# 4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dictionaries&lt;/strong&gt; — store data as key-value pairs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user&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;name&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;Juma&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;Nairobi&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;user&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# Nairobi
&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;age&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="mi"&gt;29&lt;/span&gt;     &lt;span class="c1"&gt;# update a value
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dictionaries are incredibly useful — you'll see them everywhere in real Python projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Working with Files
&lt;/h2&gt;

&lt;p&gt;Real programs read and write data. Python makes file handling simple and safe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Writing to a file
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python is awesome!&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="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m going to build great things.&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="c1"&gt;# Reading from a file
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;with&lt;/code&gt; keyword automatically closes the file when the block ends — preventing data corruption or memory leaks. Always use it!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. Modules and the Standard Library
&lt;/h2&gt;

&lt;p&gt;Python ships with a huge collection of ready-made tools. No need to reinvent the wheel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;math&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;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;keep going&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;you&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;re doing great&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;almost there!&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Today is:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;today&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;√144 =&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;144&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# 12.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you're comfortable with the basics, explore popular third-party packages using &lt;code&gt;pip install&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;requests&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fetch data from the web&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pandas&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Data analysis and spreadsheets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;flask&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Build simple web apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pygame&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Build games&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;beautifulsoup4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Scrape websites&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🗺️ Your 7-Week Learning Roadmap
&lt;/h2&gt;

&lt;p&gt;Don't rush — spend real time on each step before moving forward.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Week&lt;/th&gt;
&lt;th&gt;Topic&lt;/th&gt;
&lt;th&gt;Focus Areas&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Week 1&lt;/td&gt;
&lt;td&gt;The Basics&lt;/td&gt;
&lt;td&gt;Variables, types, &lt;code&gt;print()&lt;/code&gt;, &lt;code&gt;input()&lt;/code&gt;, operators&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Week 2&lt;/td&gt;
&lt;td&gt;Control Flow&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;if/elif/else&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt; loops, &lt;code&gt;while&lt;/code&gt; loops&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Week 3&lt;/td&gt;
&lt;td&gt;Functions&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;def&lt;/code&gt;, &lt;code&gt;return&lt;/code&gt;, parameters, scope&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Week 4&lt;/td&gt;
&lt;td&gt;Data Structures&lt;/td&gt;
&lt;td&gt;Lists, dicts, tuples, sets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Week 5–6&lt;/td&gt;
&lt;td&gt;Files &amp;amp; Modules&lt;/td&gt;
&lt;td&gt;File I/O, stdlib, &lt;code&gt;pip&lt;/code&gt; packages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Week 7+&lt;/td&gt;
&lt;td&gt;Build Something!&lt;/td&gt;
&lt;td&gt;CLI tool, quiz app, data script — anything!&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Common Beginner Mistakes to Avoid
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Forgetting indentation&lt;/strong&gt; — Python will throw an &lt;code&gt;Indentation Error&lt;/code&gt;. Always use 4 spaces (or your editor will handle it).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confusing &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;==&lt;/code&gt;&lt;/strong&gt; — &lt;code&gt;=&lt;/code&gt; assigns a value, &lt;code&gt;==&lt;/code&gt; compares two values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trying to learn everything before building&lt;/strong&gt; — you don't need to. Start building early, even if it's messy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring error messages&lt;/strong&gt; — read them carefully. Python's error messages are actually very helpful!&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where to Learn More 📖
&lt;/h2&gt;

&lt;p&gt;Here are some free, high-quality resources to keep you going:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://docs.python.org/3/" rel="noopener noreferrer"&gt;Python Official Docs&lt;/a&gt; — The source of truth&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://realpython.com/" rel="noopener noreferrer"&gt;Real Python&lt;/a&gt; — Excellent tutorials and articles&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://cs50.harvard.edu/python/" rel="noopener noreferrer"&gt;CS50P on edX&lt;/a&gt; — Harvard's free Python course&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://automatetheboringstuff.com/" rel="noopener noreferrer"&gt;Automate the Boring Stuff&lt;/a&gt; — Free book, very practical&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;The most important thing is &lt;strong&gt;write code every single day&lt;/strong&gt;, even if it's just 15 minutes. Reading tutorials is not the same as building things. Break stuff, fix it, Google the error messages, and repeat.&lt;/p&gt;

&lt;p&gt;You don't need to know everything before you start building. Start with something small  a number guessing game, a to-do list, a weather script  and grow from there.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Subqueries vs CTEs in SQL: Master Nested Queries and Write Cleaner, Smarter Code</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Thu, 30 Apr 2026 13:41:06 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/subqueries-vs-ctes-in-sql-master-nested-queries-and-write-cleaner-smarter-code-8i0</link>
      <guid>https://dev.to/ephantus_macharia_/subqueries-vs-ctes-in-sql-master-nested-queries-and-write-cleaner-smarter-code-8i0</guid>
      <description>&lt;p&gt;If you've been writing SQL for a while, you've hit this wall  your query works, but it's a mess of nested parentheses and you can barely read it yourself. That's the moment subqueries and CTEs become your best friends.&lt;/p&gt;

&lt;p&gt;Both tools let you break complex logic into manageable steps. &lt;/p&gt;




&lt;h2&gt;
  
  
  Subqueries
&lt;/h2&gt;

&lt;p&gt;A subquery is a query inside another query. The inner query runs first, and its result is used by the outer query.&lt;/p&gt;

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

&lt;p&gt;Say you want to find all employees earning above the company average:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inner query calculates the average first let's say &lt;code&gt;58,000&lt;/code&gt;  then the outer query filters for everyone above that. Simple and effective.&lt;/p&gt;




&lt;h3&gt;
  
  
  Subquery in the FROM Clause
&lt;/h3&gt;

&lt;p&gt;You can also use a subquery as a temporary table by placing it in the &lt;code&gt;FROM&lt;/code&gt; clause:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;dept_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;dept_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;dept_summary&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;60000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inner query builds a summary table per department. The outer query then filters it. You can't filter on an aggregate alias directly in &lt;code&gt;WHERE&lt;/code&gt;, so this pattern is really handy.&lt;/p&gt;




&lt;h3&gt;
  
  
  Correlated Subquery
&lt;/h3&gt;

&lt;p&gt;A correlated subquery references the outer query  it runs once for every row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each employee, the inner query finds the highest salary in &lt;em&gt;their&lt;/em&gt; department. This gives you the top earner from every department in one query.&lt;/p&gt;




&lt;h3&gt;
  
  
  Using IN with a Subquery
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Employees who have made at least one sale&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the opposite employees who have &lt;em&gt;never&lt;/em&gt; made a sale:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Always filter out NULLs when using &lt;code&gt;NOT IN&lt;/code&gt;. If the subquery returns even one NULL, you'll get zero results  a silent bug that's easy to miss.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  CTEs (Common Table Expressions)
&lt;/h2&gt;

&lt;p&gt;A CTE lets you name a subquery and place it at the top of your statement using &lt;code&gt;WITH&lt;/code&gt;. Same result, but much easier to read and maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rewriting Our First Example as a CTE
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;company_avg&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;company_avg&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;company_avg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;avg_salary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same logic, but now the average calculation has a name. Anyone reading this query immediately knows what &lt;code&gt;company_avg&lt;/code&gt; means.&lt;/p&gt;




&lt;h3&gt;
  
  
  Chaining Multiple CTEs
&lt;/h3&gt;

&lt;p&gt;This is where CTEs really shine  you can stack them, each building on the previous:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; 
&lt;span class="n"&gt;dept_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="k"&gt;COUNT&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;headcount&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;dept_averages&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_salary&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;headcount&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dept_totals&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;top_departments&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dept_averages&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;65000&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;top_departments&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;avg_salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it top to bottom  calculate totals, derive averages, filter the top ones.&lt;/p&gt;




&lt;h3&gt;
  
  
  Reusing a CTE
&lt;/h3&gt;

&lt;p&gt;One thing subqueries can't do cleanly  a CTE can be referenced multiple times in the same query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;high_earners&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'Count'&lt;/span&gt;         &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;high_earners&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'Total Payroll'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;high_earners&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'Average Salary'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="n"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;high_earners&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One definition, three uses. With a subquery you'd repeat the same block three times.&lt;/p&gt;




&lt;h3&gt;
  
  
  Recursive CTE  For Hierarchical Data
&lt;/h3&gt;

&lt;p&gt;CTEs have one trick subqueries simply cannot do  recursion. Perfect for org charts, category trees, or any parent-child relationship:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;org_chart&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Start: the CEO (no manager above them)&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;

    &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;

    &lt;span class="c1"&gt;-- Recurse: find everyone who reports to someone already in the CTE&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;oc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;org_chart&lt;/span&gt; &lt;span class="n"&gt;oc&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;oc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;org_chart&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sarah (CEO)&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Diana&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;The query keeps joining until no more reports are found. No loops, no procedural code just SQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Subqueries&lt;/strong&gt; are great for quick, inline logic filtering with &lt;code&gt;IN&lt;/code&gt;, comparing against an aggregate, or building a derived table&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CTEs&lt;/strong&gt; shine when your logic is multi-step, needs to be reused, or involves recursion&lt;/li&gt;
&lt;li&gt;Both are tools for breaking complex problems into steps  picking one is about readability and context, not right vs wrong&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>database</category>
      <category>sql</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to Publish a Power BI Report and Embed It on a Website</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Wed, 29 Apr 2026 10:46:52 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/how-to-publish-a-power-bi-report-and-embed-it-on-a-website-109m</link>
      <guid>https://dev.to/ephantus_macharia_/how-to-publish-a-power-bi-report-and-embed-it-on-a-website-109m</guid>
      <description>&lt;h3&gt;A Step-by-Step Guide Using the Electronic Sales Data Dashboard&lt;/h3&gt;

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

&lt;p&gt;Microsoft Power BI is a leading business intelligence platform that transforms raw data into rich, interactive dashboards. Once you've built a report  like the &lt;strong&gt;Electronic Sales Data Dashboard&lt;/strong&gt; used in this guide the next step is sharing it with stakeholders by publishing it to Power BI Service and embedding it on a website for broader access.&lt;/p&gt;

&lt;p&gt;This guide walks through every stage of that process, with steps tailored directly to the &lt;strong&gt;Electronic Sales Data Dashboard (.pbix)&lt;/strong&gt;, which contains three report pages:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Page&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Dashboard&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;KPI cards, bar charts, line chart, pie chart, slicers, and a product table&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Profit Margin (City, Product)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Column and bar charts breaking down profit margin by city and product&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Geographical Sales Analysis&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Interactive maps showing sales by city and country&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt; Step 1
 Create a Workspace in Power BI Service&lt;/h2&gt;

&lt;p&gt;Before publishing, you need a &lt;strong&gt;workspace&lt;/strong&gt;  a collaborative container in the cloud where your reports and datasets live.&lt;/p&gt;

&lt;h3&gt;How to do it:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open your browser and go to &lt;a href="https://app.powerbi.com" rel="noopener noreferrer"&gt;https://app.powerbi.com&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Sign in with your Microsoft 365 or Power BI account.&lt;/li&gt;
&lt;li&gt;In the left navigation panel, click &lt;strong&gt;Workspaces&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;+ New workspace&lt;/strong&gt; at the top right.&lt;/li&gt;
&lt;li&gt;In the panel that slides open:&lt;ul&gt;
&lt;li&gt;Enter a &lt;strong&gt;Name&lt;/strong&gt; (e.g., &lt;code&gt;Electronics Sales Analytics&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Optionally add a &lt;strong&gt;Description&lt;/strong&gt; (e.g., &lt;code&gt;Sales performance reports for electronic products&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Select a &lt;strong&gt;License mode&lt;/strong&gt; — choose &lt;em&gt;Pro&lt;/em&gt; or &lt;em&gt;Premium per user&lt;/em&gt; if you need to share with others outside your organisation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Step 2&lt;/h2&gt;

&lt;h1&gt;Upload and Publish the Report from Power BI Desktop&lt;/h1&gt;

&lt;p&gt;With the workspace ready, publish the &lt;code&gt;.pbix&lt;/code&gt; file from &lt;strong&gt;Power BI Desktop&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;How to do it:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open the file &lt;code&gt;Electronic_Salesdata_Dashboard.pbix&lt;/code&gt; in &lt;strong&gt;Power BI Desktop&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;strong&gt;Home&lt;/strong&gt; ribbon, click the &lt;strong&gt;Publish&lt;/strong&gt; button (cloud icon).&lt;/li&gt;
&lt;li&gt;A dialog box will appear — &lt;strong&gt;Select a destination&lt;/strong&gt;:&lt;ul&gt;
&lt;li&gt;Choose the workspace you just created (e.g., &lt;code&gt;Electronics Sales Analytics&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Select&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Power BI Desktop will upload the report and data model to the cloud.&lt;/li&gt;
&lt;li&gt;Once complete, a success message appears with a link: &lt;strong&gt;Open 'Electronic_Salesdata_Dashboard' in Power BI&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click the link to verify all three pages — &lt;strong&gt;Dashboard&lt;/strong&gt;, &lt;strong&gt;Profit Margin (City, Product)&lt;/strong&gt;, and &lt;strong&gt;Geographical Sales Analysis&lt;/strong&gt; — are rendering correctly in the browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Step 3&lt;/h3&gt;

&lt;p&gt;Generate the Embed Code&lt;/p&gt;

&lt;p&gt;Once the report is live in Power BI Service, you can generate an iframe embed code.&lt;/p&gt;

&lt;h3&gt;How to do it:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open the report in Power BI Service at &lt;code&gt;app.powerbi.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;File&lt;/strong&gt; in the top menu bar.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Embed report&lt;/strong&gt; → &lt;strong&gt;Publish to web (public)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A warning dialog will appear confirming the report will be &lt;strong&gt;publicly accessible&lt;/strong&gt; — click &lt;strong&gt;Create embed code&lt;/strong&gt; to proceed.&lt;/li&gt;
&lt;li&gt;The next dialog presents two things:&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;shareable link&lt;/strong&gt; (for direct URL sharing)&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;HTML iframe snippet&lt;/strong&gt; ready to paste into any webpage&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Example embed snippet generated:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;iframe
  title="Electronic Sales Data Dashboard"
  width="1140"
  height="541.25"
  src="https://app.powerbi.com/reportEmbed?reportId=YOUR_REPORT_ID&amp;amp;autoAuth=true&amp;amp;ctid=YOUR_TENANT_ID"
  frameborder="0"
  allowFullScreen="true"&amp;gt;
&amp;lt;/iframe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Step 4&lt;/h2&gt;

&lt;p&gt; Embed the Report on Your Website&lt;/p&gt;

&lt;p&gt;Paste the iframe into your HTML. Below are two approaches — a fixed-size embed and a fully responsive one.&lt;/p&gt;

&lt;h3&gt;Option A — Fixed-size embed (simplest)&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;iframe
  title="Electronic Sales Data Dashboard"
  width="1140"
  height="541"
  src="https://app.powerbi.com/reportEmbed?reportId=YOUR_REPORT_ID&amp;amp;autoAuth=true&amp;amp;ctid=YOUR_TENANT_ID"
  frameborder="0"
  allowFullScreen="true"&amp;gt;
&amp;lt;/iframe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Option B — Responsive  embed (recommended)&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!-- Responsive Power BI embed wrapper --&amp;gt;
&amp;lt;div style="position: relative; padding-top: 56.25%; overflow: hidden;"&amp;gt;
  &amp;lt;iframe
    title="Electronic Sales Data Dashboard"
    src="https://app.powerbi.com/reportEmbed?reportId=YOUR_REPORT_ID&amp;amp;autoAuth=true&amp;amp;ctid=YOUR_TENANT_ID"
    style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
    frameborder="0"
    allowFullScreen="true"&amp;gt;
  &amp;lt;/iframe&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Embedding a Specific Page&lt;/h3&gt;

&lt;p&gt;To load a specific report page on page load, append the &lt;code&gt;pageName&lt;/code&gt; parameter to the &lt;code&gt;src&lt;/code&gt; URL:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Report Page&lt;/th&gt;
&lt;th&gt;URL Parameter to Append&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dashboard&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;pageName=ReportSection&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Profit Margin (City, Product)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;pageName=ReportSection1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Geographical Sales Analysis&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;pageName=ReportSection2&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;pre&gt;&lt;code&gt;src="https://app.powerbi.com/reportEmbed?reportId=YOUR_REPORT_ID&amp;amp;pageName=ReportSection2"
&lt;/code&gt;&lt;/pre&gt;




&lt;h2&gt;Report Pages Overview&lt;/h2&gt;

&lt;h3&gt;Page 1 _ Dashboard&lt;/h3&gt;

&lt;p&gt;The main dashboard contains the following visuals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3 KPI Cards&lt;/strong&gt; — Total Sales, Total Profit, Profit Margin&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bar chart&lt;/strong&gt; — Sales by Product Name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bar chart&lt;/strong&gt; — Sales by Region&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bar chart&lt;/strong&gt; — Sales by Product Category&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Line chart&lt;/strong&gt; — Sales over time (by Quarter and Month)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pie chart&lt;/strong&gt; — Sales by Region and Country&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 Slicers&lt;/strong&gt; — Filter by Region and by Customer Name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Table&lt;/strong&gt; — Product Name list&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Page 2_ Profit Margin (City, Product)&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Column chart&lt;/strong&gt; — Total Sales by Country&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clustered column chart&lt;/strong&gt; — Profit Margin by City&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bar chart&lt;/strong&gt; — Profit Margin by Product Name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 Slicers&lt;/strong&gt; — Filter by Product Name and by City&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Page 3 _ Geographical Sales Analysis&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bubble map&lt;/strong&gt; — Sales Amount sized by city&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bubble map&lt;/strong&gt; — Sales Amount sized by country&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;Key Insights &amp;amp; Best Practices&lt;/h2&gt;

&lt;h3&gt;1. Use Premium Capacity for Scale&lt;/h3&gt;

&lt;p&gt;Power BI Premium capacity lets you embed reports without requiring every viewer to have a Pro licence. This is essential for public-facing websites where visitor volumes are unknown.&lt;/p&gt;

&lt;h3&gt;2. Apply Row-Level Security (RLS)&lt;/h3&gt;

&lt;p&gt;Before publishing this dashboard to a public or semi-public audience, define RLS roles in Power BI Desktop (Modelling → Manage roles) to restrict which rows of the &lt;code&gt;fact&lt;/code&gt;, &lt;code&gt;Customer&lt;/code&gt;, or &lt;code&gt;Location&lt;/code&gt; tables individual users can see.&lt;/p&gt;

&lt;h3&gt;3. Schedule Data Refresh&lt;/h3&gt;

&lt;p&gt;The sales data in this dashboard is static until refreshed. Set up a &lt;strong&gt;data gateway&lt;/strong&gt; and configure a &lt;strong&gt;scheduled refresh&lt;/strong&gt; (daily or weekly) in the workspace settings so the embedded report always reflects current figures.&lt;/p&gt;

&lt;h3&gt;4. Make the Embed Responsive&lt;/h3&gt;

&lt;p&gt;Always use the padding-top wrapper approach (Option B above) so the dashboard scales cleanly on mobile, tablet, and desktop screens without horizontal scrollbars.&lt;/p&gt;

&lt;h3&gt;5. Control Which Page Loads First&lt;/h3&gt;

&lt;p&gt;Use the &lt;code&gt;pageName&lt;/code&gt; URL parameter to decide whether visitors land on the summary Dashboard, the Profit Margin drill-down, or the Geographical map view  depending on your audience.&lt;/p&gt;

&lt;h3&gt;6. Monitor Usage&lt;/h3&gt;

&lt;p&gt;Use the &lt;strong&gt;Usage Metrics&lt;/strong&gt; report in the workspace to track how many users view the embedded report, which pages they visit most, and peak viewing times.&lt;/p&gt;

&lt;h3&gt;7. Secure Sensitive Sales Data&lt;/h3&gt;

&lt;p&gt;For any scenario involving authenticated users or confidential sales figures, replace "Publish to web (public)" with the &lt;strong&gt;Power BI Embedded (Azure)&lt;/strong&gt; approach. This uses a service principal and generates tokens server-side, keeping data protected behind authentication.&lt;/p&gt;




&lt;h2&gt;Summary&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Create a workspace in Power BI Service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Publish &lt;code&gt;Electronic_Salesdata_Dashboard.pbix&lt;/code&gt; from Power BI Desktop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Generate an iframe embed code via File → Embed report → Publish to web&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Paste the responsive frame snippet into your website HTML&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With these four steps, your Electronic Sales Data Dashboard  complete with KPI cards, regional sales charts, profit margin analysis, and geographic maps  is live and fully interactive on your website.&lt;/p&gt;

</description>
      <category>powerfuldevs</category>
      <category>data</category>
      <category>beginners</category>
      <category>basic</category>
    </item>
    <item>
      <title>Understanding Data Modelling in Power BI: Joins, Relationships, and Schemas.</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Mon, 30 Mar 2026 18:39:36 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/understanding-data-modelling-in-power-bi-joins-relationships-and-schemas-254o</link>
      <guid>https://dev.to/ephantus_macharia_/understanding-data-modelling-in-power-bi-joins-relationships-and-schemas-254o</guid>
      <description>&lt;p&gt;Data modelling is the process of defining how your tables are structured and how they relate to one another. Instead of dumping everything into one giant flat table, you organise your data into multiple purpose-built tables and link them together. Power BI then uses those links to filter, aggregate, and display data correctly across your entire report.&lt;/p&gt;

&lt;h1&gt;
  
  
  Joins
&lt;/h1&gt;

&lt;p&gt;How Tables Connect at the Query Level&lt;br&gt;
A join is how you combine rows from two tables based on a shared column. In Power BI, joins happen inside Power Query  before the data even hits your model.&lt;/p&gt;

&lt;p&gt;There are four main join types:&lt;/p&gt;
&lt;h1&gt;
  
  
  Inner Join
&lt;/h1&gt;

&lt;p&gt;Returns only rows that have a match in both tables. If a farmer exists in your Sales table but not in your Customers table, that row is dropped.&lt;/p&gt;

&lt;p&gt;You only use when you want complete, matched records.&lt;/p&gt;
&lt;h1&gt;
  
  
  Left Outer Join
&lt;/h1&gt;

&lt;p&gt;Returns all rows from the left table, plus any matches from the right. Unmatched rows from the right come back as nulls.&lt;/p&gt;

&lt;p&gt;you  use it when you want to keep all records from your primary table regardless of whether a match exists.&lt;/p&gt;
&lt;h1&gt;
  
  
  Right Outer Join
&lt;/h1&gt;

&lt;p&gt;The mirror of a left join  keeps all rows from the right table and fills nulls where the left has no match.&lt;/p&gt;
&lt;h1&gt;
  
  
  Full Outer Join
&lt;/h1&gt;

&lt;p&gt;Returns all rows from both tables. Nulls appear wherever there's no match on either side.&lt;/p&gt;

&lt;p&gt;Use when  you want a complete picture and  you are willing to handle the nulls.&lt;/p&gt;

&lt;p&gt;In Power Query, you access these through Home → Merge Queries, then pick your join type from the dropdown.&lt;/p&gt;
&lt;h1&gt;
  
  
  Relationships
&lt;/h1&gt;

&lt;p&gt;How Tables Connect in the Model&lt;br&gt;
Once your tables are loaded, relationships are how Power BI understands the links between them inside the data model. Unlike joins (which physically merge rows), relationships are virtual  they let Power BI filter one table through another without duplicating data.&lt;/p&gt;

&lt;p&gt;You define relationships in the Model view, and they work automatically whenever you use fields from multiple tables in a visual.&lt;/p&gt;

&lt;p&gt;The Three Relationship Types&lt;/p&gt;

&lt;p&gt;One-to-Many (1:*) —The most common type. One row in Table A matches multiple rows in Table B. Example: one County matches many farmers. One Product matches many sales transactions.&lt;/p&gt;

&lt;p&gt;One-to-One (1:1)  Each row in Table A matches exactly one row in Table B. Example: one Employee record maps to one HR Profile. Rare in practice, often a sign you could just merge the tables.&lt;/p&gt;

&lt;p&gt;Many-to-Many (:)  Multiple rows in Table A match multiple rows in Table B. Example: one Order can contain many Products, and one Product can appear in many Orders. Power BI supports this natively, but it can create ambiguous filter paths best handled with a bridge table in between.&lt;/p&gt;
&lt;h1&gt;
  
  
  Filter Direction
&lt;/h1&gt;

&lt;p&gt;Every relationship has a filter direction  it controls which way filters flow between tables.&lt;/p&gt;
&lt;h1&gt;
  
  
  Single direction
&lt;/h1&gt;

&lt;p&gt;filters flow one way only (from the "one" side to the "many" side). This is the safe default.&lt;br&gt;
Both directioners flow both ways, Powerful, but can cause unexpected results in complex models. Use sparingly.&lt;br&gt;
How Joins and Relationships Are Connected&lt;br&gt;
This is where it clicks: joins and relationships solve the same problem at different stages of your pipeline.&lt;/p&gt;

&lt;p&gt;A join in Power Query physically combines two tables into one before loading. A relationship in the model keeps tables separate but links them logically. The choice between them comes down to this:&lt;/p&gt;

&lt;p&gt;Join (Power Query)  Relationship (Model)&lt;br&gt;
When it runs    At data refresh / load time At query / visual render time&lt;br&gt;
Result  One merged table    Two separate linked tables&lt;br&gt;
Best for    Lookup columns you need in the fact table   Filtering and aggregating across tables&lt;br&gt;
Performance Can increase table size Keeps model lean&lt;br&gt;
A common pattern: use a join to bring a single lookup column (like County Region) into your fact table, while using relationships to connect your full dimension tables (Date, Product, Customer) for filtering.&lt;/p&gt;
&lt;h1&gt;
  
  
  Schemas
&lt;/h1&gt;

&lt;p&gt;How You Arrange Your Tables&lt;br&gt;
A schema is the overall blueprint of your model  how many tables you have and how they're arranged. There are two schemas you'll encounter most in Power BI.&lt;/p&gt;
&lt;h1&gt;
  
  
  Star Schema
&lt;/h1&gt;

&lt;p&gt;The star schema has one central fact table surrounded by several dimension tables. The fact table holds your numbers (revenue, yield, quantity). The dimension tables hold your descriptive context (who, what, when, where).&lt;/p&gt;

&lt;p&gt;dim_Date        ─┐&lt;br&gt;
dim_County      ─┤&lt;br&gt;
dim_CropType    ─┼──── fact_FarmerProduction&lt;br&gt;
dim_Season      ─┤&lt;br&gt;
dim_SoilType    ─┘&lt;/p&gt;

&lt;p&gt;Every dimension connects directly to the fact table in a one-to-many relationship. This is the recommended structure for Power BI it's simple, fast, and the DAX engine is optimised for it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Snowflake Schema
&lt;/h1&gt;

&lt;p&gt;The snowflake schema normalises the star further by splitting dimension tables into sub-tables. Instead of one flat dim_Product table, you might have dim_Product → dim_Category → dim_SubCategory.&lt;/p&gt;

&lt;p&gt;It reduces data duplication but adds complexity. In Power BI, the extra join hops can slow down queries and make DAX harder to write. Unless you have a strong reason (very large dimension tables with many repeated values), stick with the star schema.&lt;/p&gt;
&lt;h1&gt;
  
  
  Flat Table
&lt;/h1&gt;

&lt;p&gt;A single flat table with no relationships is fine for small, simple datasets. If you're working with under 10,000 rows and don't need to join to anything else, a flat table keeps things uncomplicated. The moment you need to combine data sources, or your dimension data is repeated hundreds of times, move to a star schema.&lt;/p&gt;

&lt;p&gt;Putting It All Together&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load your raw tables into Power Query
Use joins to pull in any lookup values you need directly in the fact table (e.g., a region name from a county lookup)
Load separate dimension tables  Date, County, Crop Type, Season — without merging them
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Define relationships in Model view between your fact table and each dimension (one-to-many, single-direction filter)
Arrange your model as a star schema — fact table in the centre, dimensions around it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Concept    : What it does, Where in Power BI
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inner join: Keep only matched rows  Power Query → Merge
Left join:  Keep all left rows + matches    Power Query → Merge
One-to-many relationship: Link dimension to fact table  Model view
Many-to-many relationship:Complex links, use bridge table   Model view
Star schema:    Fact + flat dimensions  Model view layout
Snowflake schema:Fact + normalised sub-dimensions   Model view layout

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Data modelling in Power BI isn't about complexity, it's about clarity. A well-structured star schema with clean one-to-many relationships will outperform a messy flat table every time, both in query speed and in how easy your DAX becomes to write and maintain.&lt;/p&gt;

&lt;p&gt;Start with your fact table, build your dimensions, connect them with single-direction relationships, and keep it flat. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>From confusion to clarity;How Excel-Data analysis has Transformed my skills</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Fri, 27 Mar 2026 09:54:01 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/from-confusion-to-clarityhow-excel-data-analysis-has-transformed-my-skills-4ji8</link>
      <guid>https://dev.to/ephantus_macharia_/from-confusion-to-clarityhow-excel-data-analysis-has-transformed-my-skills-4ji8</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;The Dataset That Changed Everything&lt;/strong&gt;&lt;br&gt;
I will be honest. When I first opened the Jumia Kenya product dataset, I had no idea where to begin. There were 115 rows of product data, but the prices were buried inside text strings like "KSh 1,525", the ratings were written as "4.5 out of 5", the review counts were all negative numbers, and a full 50 per cent of the rows had no rating information at all. It looked less like a dataset and more like a problem waiting to punish me.&lt;br&gt;
That experience, the confusion, the slow process of fixing each issue one by one, and the moment when the data finally came alive is exactly what this article is about. Learning Excel data analysis did not just teach me a set of formulas. It changed the way I think, the way I approach problems, and the way I trust my own conclusions. This is my story of how that happened, told through the real data I cleaned, interpreted, and turned into a working dashboard.&lt;/p&gt;

&lt;p&gt;Step One:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working on Messy Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What Real-World Data Taught Me First&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-world data is rarely clean  I learned that the hard way!&lt;/li&gt;
&lt;li&gt;Data analysis means looking at clean and beautiful tables and extracting insights  or so I thought!&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Well, I was wrong  most of the work happens before even a single chart is drawn!&lt;/p&gt;
&lt;h1&gt;
  
  
  The 6 Problems I Found in the Jumia Dataset
&lt;/h1&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prices were stored as text  every price had a "KSh" prefix attached to it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ratings were written as sentences  "4.5 out of 5" instead of just "4.5".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Review counts were negative  every review count was entered as a minus value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;One product had a price range  "KSh 1,620 – KSh 1,980" instead of a single value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Percentage Discounts were stored as text  "38%" instead of a real number.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;58 products had no rating at all  half of the products were blank for ratings!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What This Taught Me
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Every single problem needed a deliberate solution in Excel.&lt;/li&gt;
&lt;li&gt;Not only did I learn how to solve these problems, but I also learned how to solve them permanently!&lt;/li&gt;
&lt;li&gt;The order in which I solved these problems also taught me how to diagnose the problem before even touching the data!.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=VALUE(SUBSTITUTE(SUBSTITUTE(A2,"KSh",""),",",""))   // Strip KSh and commas from price text, then convert to a true number
=VALUE(LEFT(A2,3))   // Extract just the numeric rating from "4.5 out of 5."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=ABS(A2)   // Convert negative review counts to positive values
=IF(ISBLANK(A2),"No Rating",IF(A2&amp;lt;3,"Poor",IF(A2&amp;lt;4.5,"Average","Excellent")))   // 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What Actually Surprised Me About Data Cleaning
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The difficulty was not the surprising part. Each fix is straightforward once you know the function&lt;/li&gt;
&lt;li&gt;What surprised me is the amount of change the data underwent after the fixes
The column of seemingly random characters, the wall of meaningless text, suddenly looked like:
✅ Sortable
✅ Calculable
✅ Chart-ready&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Real Meaning of Data Cleaning
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Data cleaning is not about correcting errors. It is about transforming noise into a signal&lt;/li&gt;
&lt;li&gt;The moment the data underwent its transformation is the moment I understood the true meaning of cleaning&lt;/li&gt;
&lt;li&gt;No explanation or book can do justice to the experience of going through the transformation yourself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Skill Gained: &lt;strong&gt;What I Do Differently Now&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; I instinctively inspect every new dataset for the following six things before I do anything else&lt;/li&gt;
&lt;li&gt;It takes five minutes to inspect&lt;/li&gt;
&lt;li&gt; It saves hours of confusion&lt;/li&gt;
&lt;li&gt;This is not something I learned by reading about it. This is something I learned by going through the experience 
once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step Two:&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating Meaning with Formulas Data Enrichment
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Clean data tells you what exists. Enriched data tells you what it means&lt;/li&gt;
&lt;li&gt;This is where Excel formulas started feeling genuinely powerful&lt;/li&gt;
&lt;li&gt;I was no longer just correcting errors; I was creating knowledge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;New Columns I Added to the Dataset&lt;br&gt;
 &lt;strong&gt;Discount Amount&lt;/strong&gt; (KES)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Formula: Current Price subtracted from Old Price&lt;/li&gt;
&lt;li&gt;Reveals the real savings in shillings, not just a percentage&lt;/li&gt;
&lt;li&gt;Why it matters:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;64% off a KES 199 item = only KES 354 saved&lt;br&gt;
39% off a KES 3,750 drill = KES 2,393 saved&lt;br&gt;
 Percentage figures alone were hiding this distinction entirely&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rating Category&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used an IFS formula to classify every product into a clear tier:&lt;/p&gt;

&lt;p&gt;Poor — rating below 3&lt;br&gt;
 Average — rating between 3 and 4.4&lt;br&gt;
 Excellent — rating of 4.5 and above&lt;/p&gt;

&lt;p&gt;Turns a raw number into a label anyone can read instantly&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Discount Category&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Grouped every product into one of three discount tiers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Low Discount — below 20%&lt;/li&gt;
&lt;li&gt; Medium Discount — between 20% and 40%&lt;/li&gt;
&lt;li&gt; High Discount — above 40%&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why These Columns Mattered&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rating Category and Discount Category became the foundation of almost every comparison in the final analysis&lt;/li&gt;
&lt;li&gt;Without them, grouping and comparing products would have required manual sorting every single time&lt;/li&gt;
&lt;li&gt;With them, a single AVERAGEIF or COUNTIF formula answers any group-level question instantly
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=IFS(D2&amp;lt;3,"Poor",D2&amp;lt;4.5,"Average",D2&amp;gt;=4.5,"Excellent
=IFS(C2&amp;lt;20,"Low Discount",C2&amp;lt;=40,"Medium Discount",C2&amp;gt;40,"High Discount")   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step taught me something important about data analysis: the raw data rarely tells the whole story. The enriched data does. A number like 3.7 says very little on its own. The label "Average" is placed alongside it, in context with 114 other products.&lt;/p&gt;

&lt;p&gt;Step Three: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Charts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After cleaning and enriching the data, I ran a full descriptive analysis using AVERAGE, COUNTIF, AVERAGEIF, and CORREL functions. But the moment the analysis truly came alive was when I built the visualizations. The charts below were produced directly from the cleaned Jumia dataset, and each one taught me something that the tables had kept hidden.&lt;/p&gt;

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

&lt;p&gt;This chart showed me immediately that 65 out of 115 products more than half carry a discount above 40%. At first, I assumed this meant they were the best-performing products. &lt;/p&gt;

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

&lt;p&gt;The rating category chart was the most visually striking finding of the entire analysis. The grey "No Rating" segment representing 50% of all products dominates the chart. This is not just a design choice; it is a data quality alarm. Half the products in this dataset have never been reviewed. Any conclusion I draw about ratings applies only to the other half, and I must clearly state this every time I present findings. Learning to read that caveat into a chart and to communicate it honestly felt like a genuine step forward as an analyst.&lt;/p&gt;

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

&lt;p&gt;The top 10 discount chart delivered a surprise. The highest-discounted products are not expensive electronics or premium appliances. They are small everyday items: a bottle opener, a keychain, crochet needles, and a pillowcase. The product with the single highest discount in the entire dataset (64% off) costs just KES 199. That is a powerful reminder that percentage discounts and absolute value are entirely different things, a lesson I learned from the data, not from a textbook&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Average rating and average reviews by discount category&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finding that medium-discount products outperform high-discount ones on both measures&lt;br&gt;
This final chart is the one I am most proud of, because it contradicts the most natural assumption in the entire dataset. I expected high-discount products to have the most reviews and the highest ratings. More discounts should mean more buyers, and more buyers should mean more reviews. The data said the opposite. Medium-discount products (20–40% off) had an average rating of 4.28 and 15.3 reviews. High-discount products rated only 3.61 and averaged 11.1 reviews. The correlation between discount percentage and reviews was just −0.14, essentially zero. Higher discounts do not drive customer engagement. Product quality does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Products Analysed-115 &lt;/li&gt;
&lt;li&gt;Avg Current Price-1174
&lt;/li&gt;
&lt;li&gt;Avg Discount-36.96%
&lt;/li&gt;
&lt;li&gt;Avg Rating-3.89/5 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How This Has Made Me a Better Analyst  and a Better Thinker&lt;br&gt;
Working through this project from raw CSV to finished dashboard gave me five concrete skills that I did not have before, and that I now use every time I open a spreadsheet.&lt;br&gt;
1) Skills This Project Built&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Diagnose the first  scan for broken data before touching anything&lt;/li&gt;
&lt;li&gt; Write self-explanatory formulas SUBSTITUTE, VALUE, ABS, IFS, AVERAGEIF&lt;/li&gt;
&lt;li&gt;Trust the data, not your expectations  correlation was −0.14, not what I hoped&lt;/li&gt;
&lt;li&gt; Charts are not decoration  every chart reveals what the table could not&lt;/li&gt;
&lt;li&gt; Declare data gaps honestly  50% missing ratings must be stated. 
not hidden&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Spreadsheet That Taught Me to Think&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here I Started vs Where I finished&lt;/li&gt;
&lt;li&gt;Started, finished 115 rows of messy data. A fully formatted Excel dashboard. Did not know VLOOKUP. Can clean, enrich, analyse, and visualise. Assumed data was neat. Know how to diagnose and fix real problems. Trusted percentages. Know how to check the numbers 
behind them&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What This Project Actually Taught Me
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Not just which function to use, but when and why&lt;/li&gt;
&lt;li&gt;Not just how to build charts, but how to read and explain them&lt;/li&gt;
&lt;li&gt;Not just Excel skills, but thinking skills&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Things That Made It Real
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Excel gave me the tools&lt;/li&gt;
&lt;li&gt; The Jumia dataset gave me the practice&lt;/li&gt;
&lt;li&gt; This course gave me the framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will carry these skills into every dataset, every report, and every decision I face from here on.&lt;/p&gt;

</description>
      <category>data</category>
      <category>excell</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Data Analysis Setup: Tools, Installation, and Best Practices</title>
      <dc:creator>Ephantus Macharia</dc:creator>
      <pubDate>Mon, 23 Mar 2026 06:38:00 +0000</pubDate>
      <link>https://dev.to/ephantus_macharia_/data-analysis-setup-tools-installation-and-best-practices-3cd4</link>
      <guid>https://dev.to/ephantus_macharia_/data-analysis-setup-tools-installation-and-best-practices-3cd4</guid>
      <description>&lt;p&gt;In previous centuries, we used to decide as we always used to: based on a gut feeling, a coin toss or a prayer. However, we can not afford to guess in a world that is changing this rapidly. However, it is feared that the analysis of data removes humanity in the decision-making process- that it reduces individuals to statistics. I want to argue the opposite. Proper data analysis is the most understanding thing that you can do. It prevents the projection of personal prejudices on the world and makes one see people as they are, not as you think they are. It is not a de-humanizing of the process, it is simply a human element that has gotten right at last.&lt;/p&gt;

&lt;p&gt;Below are various tools that will help you kickstart your journey  of Data Analysis:&lt;/p&gt;

&lt;p&gt;EXCEL INSTALLATION GUIDE&lt;/p&gt;

&lt;p&gt;Step 1&lt;/p&gt;

&lt;p&gt;Steps into Installaling Excel on windows&lt;/p&gt;

&lt;p&gt;1.Go to the official page of MICROSOFT OFFICE Website:&lt;a href="https://www.office.com/" rel="noopener noreferrer"&gt;https://www.office.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2 . Sign in with your Microsoft Account.&lt;/p&gt;

&lt;p&gt;Click  install Office&lt;/p&gt;

&lt;p&gt;4 Open the downloaded OfficeSetup.exe file.&lt;/p&gt;

&lt;p&gt;5   Wait for the installation to complete.&lt;/p&gt;

&lt;p&gt;6.After installation:&lt;/p&gt;

&lt;p&gt;7 Open Microsoft Excel from the Start Menu.&lt;/p&gt;

&lt;p&gt;8  Sign in to activate the software.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ANACONDA INSTALLATION GUIDE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Steps into Installing  Anaconda  On windows&lt;/p&gt;

&lt;p&gt;Anaconda-platforms is a tool developed to design to securely build,  and deploy artificial intelligence and machine learning models, primarily using Python and open-source software.&lt;/p&gt;

&lt;p&gt;Step 2&lt;/p&gt;

&lt;p&gt;1 .Go to the official Anaconda website: &lt;a href="https://www.anaconda.com" rel="noopener noreferrer"&gt;https://www.anaconda.com&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;2 .Download the Anaconda Distribution for Windows.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F395kd17cbb2exx4zrsox.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F395kd17cbb2exx4zrsox.jpg" alt=" " width="502" height="390"&gt;&lt;/a&gt;&lt;br&gt;
3 .Open the downloaded .exe installer.&lt;/p&gt;

&lt;p&gt;4 .Click Next → Agree to License.&lt;/p&gt;

&lt;p&gt;5 .Choose Just Me installation.&lt;/p&gt;

&lt;p&gt;6 .Select the installation location (default recommended).&lt;/p&gt;

&lt;p&gt;7 .Click Install.&lt;/p&gt;

&lt;p&gt;8 .After installation, click Finish.&lt;/p&gt;

&lt;p&gt;9 .To verify installation:&lt;/p&gt;

&lt;p&gt;10 .Open Anaconda Navigator from the Start Menu&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
  </channel>
</rss>
