<?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: Adolph Odhiambo</title>
    <description>The latest articles on DEV Community by Adolph Odhiambo (@adolphtech).</description>
    <link>https://dev.to/adolphtech</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F886679%2F386a9497-7630-43df-8d98-59fd7a3ba48e.jpeg</url>
      <title>DEV Community: Adolph Odhiambo</title>
      <link>https://dev.to/adolphtech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adolphtech"/>
    <language>en</language>
    <item>
      <title>From Pythagorean Theorem to K-Means: How Grade School Math Powers Machine Learning</title>
      <dc:creator>Adolph Odhiambo</dc:creator>
      <pubDate>Tue, 21 Jan 2025 07:37:21 +0000</pubDate>
      <link>https://dev.to/adolphtech/from-pythagorean-theorem-to-k-means-how-grade-school-math-powers-machine-learning-1kcn</link>
      <guid>https://dev.to/adolphtech/from-pythagorean-theorem-to-k-means-how-grade-school-math-powers-machine-learning-1kcn</guid>
      <description>&lt;h3&gt;
  
  
  A Fourth-Grade Discovery That Shaped My Career
&lt;/h3&gt;

&lt;p&gt;Back in 2010, I was a curious fourth-grader staring at the colorful posters on my math classroom walls. Among them was one that etched itself into my memory: the &lt;strong&gt;Pythagorean Theorem&lt;/strong&gt;. Memorizing the iconic triples like &lt;strong&gt;(3-4-5)&lt;/strong&gt; and &lt;strong&gt;(5-12-13)&lt;/strong&gt; felt like solving magical puzzles. I didn’t know it then, but that simple formula  

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;a2+b2=c2a^2 + b^2 = c^2&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;b&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;c&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
  would one day power some of the most critical algorithms I use in my career. &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%2Fsaegl24b1a8miqn1apga.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%2Fsaegl24b1a8miqn1apga.png" alt="Pythagorean" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fast forward 15 years, and I’m a data scientist clustering customers based on their transaction patterns and account growth. My go-to algorithm? &lt;strong&gt;K-Means clustering&lt;/strong&gt;, a machine learning technique that owes its elegance and efficiency to none other than the Pythagorean theorem. &lt;/p&gt;
&lt;h3&gt;
  
  
  The Pythagorean Theorem’s Hidden Superpower
&lt;/h3&gt;

&lt;p&gt;The Pythagorean theorem states:&lt;br&gt;&lt;br&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;c=a2+b2
c = \sqrt{a^2 + b^2}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;c&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;a&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;b&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;br&gt;&lt;br&gt;
where ( c ) is the hypotenuse of a right triangle, and ( a ), ( b ) are the other two sides.  

&lt;p&gt;But here’s the twist: &lt;strong&gt;this formula is the secret sauce behind measuring distances in machine learning&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
By simply reinterpreting the sides of the triangle, we can measure distances in higher-dimensional spaces a technique that underpins many algorithms.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Secret Superpower: Euclidean Distance
&lt;/h3&gt;

&lt;p&gt;Let’s start with the &lt;strong&gt;Euclidean distance&lt;/strong&gt;—the straight-line distance between two points. Imagine two points on a 2D plane, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(x1,y1)(x_1, y_1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
  and  
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(x2,y2)(x_2, y_2)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. The distance between them is:&lt;br&gt;&lt;br&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;d=(x2−x1)2+(y2−y1)2
d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
  

&lt;p&gt;This formula is essentially the Pythagorean theorem in disguise! Instead of triangle sides, the differences in 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;xx&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
- and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;yy&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-coordinates form the “legs,” while the hypotenuse becomes the distance between points.Instead of triangle sides, we’re measuring the “straight-line” distance between points.  &lt;/p&gt;
&lt;h4&gt;
  
  
  Why Distance Matters in Machine Learning ?
&lt;/h4&gt;

&lt;p&gt;In machine learning, &lt;strong&gt;distance = similarity&lt;/strong&gt;. The closer two data points are in a feature space, the more alike they are.  &lt;/p&gt;

&lt;p&gt;For example, consider two customers:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Customer A&lt;/strong&gt;: 25 years old, earning $50K. Represented as:
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;a⃗=[2550]
\vec{\mathbf{a}} = \begin{bmatrix} 25 &amp;amp; 50 \end{bmatrix}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord mathbf"&gt;a&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="accent-body"&gt;&lt;span class="overlay"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;25&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;50&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Customer B&lt;/strong&gt;: 40 years old, earning $80K. Represented as:
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;b⃗=[4080]
\vec{\mathbf{b}} = \begin{bmatrix} 40 &amp;amp; 80 \end{bmatrix}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord mathbf"&gt;b&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="accent-body"&gt;&lt;span class="overlay"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;40&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;80&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;To measure their similarity, calculate the Euclidean distance:&lt;br&gt;&lt;br&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;d=(40−25)2+(80−50)2=225+900=33.54
d = \sqrt{(40 - 25)^2 + (80 - 50)^2} = \sqrt{225 + 900} = 33.54
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;40&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;25&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;80&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;50&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;225&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;900&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;33.54&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
  

&lt;p&gt;The smaller the distance, the more similar the customers. This simple concept becomes the backbone of clustering algorithms like K-Means.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Scaling to Higher Dimensions (and Real-World Problems)
&lt;/h3&gt;

&lt;p&gt;What if we add more features, like &lt;em&gt;number of purchases&lt;/em&gt; or &lt;em&gt;average transaction amount&lt;/em&gt;? The Euclidean distance formula adapts effortlessly:&lt;br&gt;&lt;br&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;d=(x2−x1)2+(y2−y1)2+(z2−z1)2+…
d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2 + \dots}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;z&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;z&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="minner"&gt;…&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
  

&lt;p&gt;Even in 100-dimensional space, the principle remains the same simplified as:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;d=∑i=1n(xi2−xi1)2
d = \sqrt{\sum_{i=1}^n (x_{i2} - x_{i1})^2}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;d&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord sqrt"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span class="svg-align"&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="hide-tail"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
  

&lt;h3&gt;
  
  
  K-Means Clustering: Geometry in Action
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffnyfazhjvpinddczqkjf.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%2Ffnyfazhjvpinddczqkjf.png" alt="KMEANS" width="561" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;K-Means is one of the most popular clustering algorithms in machine learning. Here’s how it works:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt;: Start by guessing initial cluster centers (centroids).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assignment&lt;/strong&gt;: Assign each data point to the nearest centroid, using Euclidean distance.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update&lt;/strong&gt;: Recalculate the centroids as the average of all points assigned to them.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeat&lt;/strong&gt;: Continue until the centroids stabilize.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Euclidean distance is the heart of this process, ensuring that clusters group together similar points.&lt;/p&gt;

&lt;h3&gt;
  
  
  Full Circle: A Fourth-Grade Formula in Action
&lt;/h3&gt;

&lt;p&gt;In my customer segmentation project, every customer’s transaction history became a vector in multi-dimensional space. By calculating Euclidean distances, I grouped customers with similar behavior patterns into clusters. This allowed my team to design targeted marketing strategies and predict account growth effectively.  &lt;/p&gt;

&lt;p&gt;Looking back, it’s incredible to see how a formula I first encountered in elementary school has grown with me, becoming a tool I use every day.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Math isn’t just a subject , it’s a lens to understand the world. The &lt;strong&gt;Pythagorean theorem&lt;/strong&gt;, once a tool to solve triangles, now powers machine learning models that drive real-world decisions.  Whether it’s triangles on a chalkboard or billion-dollar ML models, &lt;strong&gt;the fundamentals remain timeless&lt;/strong&gt;. Next time you see a right triangle, remember: you’re staring at the foundation of modern AI.  &lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>datascience</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Data Driven Dreams: Building My Data Science Career</title>
      <dc:creator>Adolph Odhiambo</dc:creator>
      <pubDate>Mon, 05 Aug 2024 11:58:26 +0000</pubDate>
      <link>https://dev.to/adolphtech/data-driven-dreams-building-my-data-science-career-4239</link>
      <guid>https://dev.to/adolphtech/data-driven-dreams-building-my-data-science-career-4239</guid>
      <description>&lt;p&gt;I wake up every day &lt;strong&gt;dreaming&lt;/strong&gt; of crunching numbers, decoding patterns, and deriving insights that shape the future—right after hitting the snooze button five times and contemplating my life choices. Welcome to the exhilarating, unpredictable, and occasionally maddening world of data science! Here, I will guide you through the labyrinthine journey of building a career in one of today's most sought-after fields, where data scientists are the new rock stars (minus the groupies, of course).&lt;/p&gt;

&lt;h3&gt;
  
  
  A Fork in the Road: Choosing My Path
&lt;/h3&gt;

&lt;p&gt;After graduating from university, I found myself standing at a crossroads, much like a protagonist in a choose-your-own-adventure book. Should I pursue a master's degree, further entrenching myself in the world of academia, or should I leap into the world of practical skills and possibly insane work hours? The two contenders: data science or software engineering. &lt;/p&gt;

&lt;p&gt;Software engineering seemed like the easier choice. After all, I'd been blogging about JavaScript and Node.js for ages, and I could code "Hello World" in my sleep. Never in a thousand lifetimes did I imagine I'd be diving into data science—a field filled with statistical wizardry, machine learning magic, and the occasional existential crisis when your model doesn't work.&lt;/p&gt;

&lt;p&gt;Yet, here I am, after a rather depressing job hunt that involved more rejections than I'd like to admit, I found myself with a data science internship. It was a leap of faith, a challenge that I decided to tackle head-on, and now it's become my daily routine. Just picture it: me, sitting in an interview, knowing zilch about data science beyond the introductory courses from college. The only thing I had was the faint memory of my machine learning unit from computer science, which felt about as relevant as a philosophy degree at a tech convention.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Unexpected Interview Success
&lt;/h3&gt;

&lt;p&gt;I prepared for my interview at one of Kenya's leading financial institutions with the fervor of a detective on a juicy case. And thank the digital gods for &lt;a href="https://www.glassdoor.com/" rel="noopener noreferrer"&gt;GLASSDOOR&lt;/a&gt;, which was my saving grace. It felt like having the answers to a test before actually taking it—apart from those pesky technical data science questions. Still, I navigated the interview with the grace of a stumbling giraffe, and somehow, it worked!&lt;/p&gt;

&lt;p&gt;Upon landing the internship, I quickly discovered something that would haunt me: impostor syndrome. Surrounded by geniuses, masters of the art, and big names in the Kenyan data industry, I felt like a toddler in a room full of seasoned marathon runners. All I wanted was to become like them—or better—so I had to devise a learning path that would transform me from a data dud to a data dynamo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Crafting My Learning Path
&lt;/h3&gt;

&lt;p&gt;Creating my learning path was akin to planning a cross-country road trip with only a half-charged GPS and a bag of snacks. I researched online for the best ways to learn data science, acknowledging that my Python programming background would be a helpful, albeit humble, beginning.&lt;/p&gt;

&lt;p&gt;I divided my learning path into four critical areas:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mathematics&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Programming&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Libraries&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Science&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are the resources I've used on this enlightening (and sometimes frustrating) journey.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Mathematics: The Language of Data Science
&lt;/h4&gt;

&lt;p&gt;Learning the theoretical background for data science or machine learning can be a daunting experience, as it involves multiple fields of mathematics and a long list of online resources. But fear not! I have broken down these topics into bite-sized pieces that are as digestible as a slice of pizza (or so I tell myself).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linear Algebra&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For linear algebra, Khan Academy came in handy. It's like having a personal math tutor who doesn't judge your lack of algebraic prowess. It will really build your mathematical confidence because you can start from the level you understand, whether it's high school algebra or the more advanced stuff you only pretend to understand.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.khanacademy.org/math/linear-algebra" rel="noopener noreferrer"&gt;Khan Academy Linear Algebra&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ocw.mit.edu/courses/mathematics/18-06-linear-algebra-spring-2010/index.htm" rel="noopener noreferrer"&gt;MIT Linear Algebra Course&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Calculus&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's face it, calculus is a bit like cilantro—some people love it, and some people can't stand it. But if you're in data science, you need to acquire a taste for it. Luckily, Khan Academy offers a course that makes it as palatable as possible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.khanacademy.org/math/multivariable-calculus" rel="noopener noreferrer"&gt;Khan Academy Calculus&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Probability and Statistics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Statistics are to data science what salt is to cooking; you can't do without them. And no, making things up and calling it "intuitive" doesn't count.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.khanacademy.org/math/statistics-probability" rel="noopener noreferrer"&gt;Khan Academy Probability and Statistics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Programming: The Backbone of Data Science
&lt;/h4&gt;

&lt;p&gt;For programming, I turned to Colt Steele, whose teaching style is as engaging as a Netflix series you binge-watch on a lazy Sunday. His course helped me grasp Python concepts with ease and confidence.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/the-modern-python3-bootcamp" rel="noopener noreferrer"&gt;Modern Python 3 Bootcamp by Colt Steele on Udemy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Libraries: Tools of the Trade
&lt;/h4&gt;

&lt;p&gt;Once you've wrapped your head around the mathematics and programming, it's time to roll up your sleeves and get down to business with data science libraries. These are the Swiss Army knives of data science, and they're indispensable for anyone serious about the field.&lt;/p&gt;

&lt;p&gt;I found a fantastic course by Zero to Mastery that offers a hands-on approach to learning these essential tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/complete-machine-learning-and-data-science-zero-to-mastery" rel="noopener noreferrer"&gt;Machine Learning Course on Udemy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Data Science: The Grand Finale
&lt;/h3&gt;

&lt;p&gt;This is where it all comes together. After sweating through math problems, debugging Python code, and making friends with libraries, you're ready to take on data science projects like a boss.&lt;/p&gt;

&lt;p&gt;So, there you have it! My carefully curated data science learning path, shared with the world for the common good. Whether you're a fellow beginner or a seasoned pro looking for some new resources, I hope this helps you expand your machine learning knowledge and overcome the fear of discovering what's happening behind the scenes of your &lt;code&gt;sklearn&lt;/code&gt;, &lt;code&gt;Keras&lt;/code&gt;, and &lt;code&gt;pandas&lt;/code&gt; import statements.&lt;/p&gt;

&lt;p&gt;Your contributions and feedback are more than welcome! Feel free to review one of the listed resources or add new, remarkable ones in the comments. Let's learn, laugh, and conquer the data science world together—one import statement at a time!&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>data</category>
      <category>career</category>
      <category>bigdata</category>
    </item>
    <item>
      <title>Unlocking the Power of JSON Web Tokens: Demystifying Authorization and Authentication in Node.js</title>
      <dc:creator>Adolph Odhiambo</dc:creator>
      <pubDate>Mon, 05 Aug 2024 10:57:52 +0000</pubDate>
      <link>https://dev.to/adolphtech/unlocking-the-power-of-json-web-tokens-demystifying-authorization-and-authentication-in-nodejs-2lpe</link>
      <guid>https://dev.to/adolphtech/unlocking-the-power-of-json-web-tokens-demystifying-authorization-and-authentication-in-nodejs-2lpe</guid>
      <description>&lt;p&gt;In the vast digital landscape of today's interconnected applications, ensuring secure access and protecting user data are of paramount importance. As developers, we need reliable tools to handle authorization and authentication effectively. Enter JSON Web Tokens (JWTs), a compact and self-contained way to securely transmit information between parties as a JSON object. In the realm of Node.js, JWTs have emerged as a popular choice for implementing authorization and authentication mechanisms. In this blog post, we will dive into the world of JWTs, exploring their inner workings and uncovering how they can bolster the security of your Node.js applications. So fasten your seatbelts and get ready to embark on a journey where we unravel the secrets behind JWT-based authorization and authentication in Node.js.&lt;/p&gt;

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

&lt;p&gt;At its core, a JWT is a string comprised of three distinct parts: the header, the payload, and the signature. The header contains information about the type of token and the algorithm used to sign it. The payload carries the claims or assertions about the user, such as their identity and any additional metadata. Finally, the signature ensures the integrity and authenticity of the token, preventing tampering and unauthorized modifications.&lt;/p&gt;

&lt;p&gt;One of the significant advantages of JWTs is their self-contained nature, meaning all the necessary information is embedded within the token itself. This characteristic eliminates the need for server-side storage or database lookups, reducing the overall complexity and improving scalability. Additionally, the compact size of JWTs makes them ideal for transmitting across networks and storing within cookies or local storage.&lt;/p&gt;

&lt;p&gt;When it comes to authentication, JWTs offer a reliable way to verify the identity of a user. Once a user provides their credentials, such as a username and password, the server can generate a JWT and send it back to the client. This token can then be included in subsequent requests, allowing the server to validate the user's identity without needing to store session data or perform database queries.&lt;/p&gt;

&lt;p&gt;Authorization, on the other hand, deals with determining what actions a user is allowed to perform within an application. JWTs can carry additional claims in the payload, such as user roles or permissions, which can be utilized by the server to grant or deny access to certain resources or functionalities. By decoding the JWT and examining its payload, the server can make informed decisions about what the user is authorized to do.&lt;/p&gt;

&lt;h3&gt;
  
  
  NUFF said let's get our hands dirt !!!!
&lt;/h3&gt;

&lt;p&gt;For this tutorial I am going to use &lt;a href="https://nodejs.org/en/docs" rel="noopener noreferrer"&gt;Node js&lt;/a&gt; and following the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/MVC" rel="noopener noreferrer"&gt;MVC&lt;/a&gt; pattern a  popular software architectural pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Installed Nodejs&lt;/li&gt;
&lt;li&gt;Installed MongoDb&lt;/li&gt;
&lt;li&gt;Code Editor &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is my tree structure&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%2Fd8onxj4nl6k5oh9pzk7m.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%2Fd8onxj4nl6k5oh9pzk7m.png" alt="Folders tree structure" width="181" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.Initializing the npm, run the following in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the required dependencies:
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;npm install express axios mongoose bcryptjs cookie-parser cors dotenv hbs jsonwebtoken&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the root folder create a folder with the name &lt;code&gt;src&lt;/code&gt; inside the &lt;code&gt;src&lt;/code&gt; folder create another folder called &lt;code&gt;db&lt;/code&gt; we will use the &lt;code&gt;db&lt;/code&gt; folder to store the database configurations . I will be using mongoDb&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Create a file inside &lt;code&gt;db&lt;/code&gt; and name it &lt;code&gt;mongoose.js&lt;/code&gt; which looks like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");
mongoose.set('strictQuery', false);

function connection() {
    mongoose.connect("mongodb://127.0.0.1:27017/authSec");
}

module.exports = { connection };

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

&lt;/div&gt;



&lt;p&gt;The code above begins by importing the mongoose library, which is a popular Object-Document Mapping (ODM) library for MongoDB in Node.js. This library simplifies the interaction with MongoDB by providing a higher-level API and data modeling capabilities.&lt;/p&gt;

&lt;p&gt;The line &lt;code&gt;mongoose.set('strictQuery', false);&lt;/code&gt; sets the strictQuery option to false. This option allows more flexibility when querying the database, as it disables the strict mode for queries. In strict mode, Mongoose throws an error if you try to query with undefined fields. Setting it to false allows querying with undefined fields without throwing an error.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;connection&lt;/code&gt; function is defined, which is exported as a module. It handles establishing a connection to the MongoDB database. The &lt;code&gt;mongoose.connect&lt;/code&gt; method is used to connect to the database specified by the MongoDB connection string &lt;code&gt;mongodb://127.0.0.1:27017/authSec&lt;/code&gt;. Modify the connection string as per your MongoDB setup.&lt;/p&gt;

&lt;p&gt;Finally, the connection function is exported using &lt;code&gt;module.exports&lt;/code&gt;, allowing other parts of the application to import and utilize this function to establish a connection with the MongoDB database.&lt;/p&gt;

&lt;p&gt;To use this code, you would typically import the connection function from this module and call it whenever you need to establish a connection with your MongoDB database.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After initializing  our database let's move to the server .Inside the &lt;code&gt;src&lt;/code&gt; folder create two files &lt;code&gt;server.js&lt;/code&gt; and &lt;code&gt;app.js&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We will have the following code inside the &lt;code&gt;server.js&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require("http");

const app = require("./app");

const PORT = process.env.PORT || 3000;

const server = http.createServer(app);
const { connection } = require("./db/mongoose");

server.listen(PORT, async() =&amp;gt; {
    // console.log(`server has started on ${PORT}`)

    await connection();
    console.log(`server is listening on port ${PORT}`
    );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The code begins by importing the &lt;code&gt;http&lt;/code&gt; module, which is a built-in Node.js module that provides functionality for creating HTTP servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;app&lt;/code&gt; module is imported using &lt;code&gt;require("./app")&lt;/code&gt;. This  refers to a separate file that defines an Express application, which handles the routing and middleware logic for the server. We will create this file later&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;PORT&lt;/code&gt; constant is defined, which specifies the port number on which the server will listen. It is set to the value of the &lt;code&gt;process.env.PORT&lt;/code&gt; environment variable, which allows the server to use the port specified by the hosting environment. If the environment variable is not set, it defaults to port 3000.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;http.createServer(app)&lt;/code&gt; method is used to create an HTTP server instance, passing the app module as the request handler. This associates the Express application with the server, allowing it to handle incoming HTTP requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;connection&lt;/code&gt; function is imported from the &lt;code&gt;"./db/mongoose"&lt;/code&gt; module. This likely refers to a separate file that establishes a connection to a MongoDB database using Mongoose.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>jsonwebtokens</category>
      <category>jwt</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating Weather App using Weather API and Node Js : Part Two</title>
      <dc:creator>Adolph Odhiambo</dc:creator>
      <pubDate>Wed, 13 Jul 2022 22:34:48 +0000</pubDate>
      <link>https://dev.to/adolphtech/creating-weather-app-using-weather-api-and-node-js-part-two-edo</link>
      <guid>https://dev.to/adolphtech/creating-weather-app-using-weather-api-and-node-js-part-two-edo</guid>
      <description>&lt;p&gt;In part one 👉👉 (&lt;a href="https://dev.to/adolphtech/creating-weather-app-using-weather-api-and-node-js-part-one-1n49"&gt;Part One&lt;/a&gt;) we made our API get the longitudes of the location we want. In this part we are really going to get the weather data of the location using another API.&lt;/p&gt;

&lt;p&gt;We are going to use  &lt;a href="https://weatherstack.com" rel="noopener noreferrer"&gt;Weather Stack&lt;/a&gt;.The first step to using the API is to authenticate with your weatherstack account's unique API access key, which can be found in your account dashboard after registration. To authenticate with the API, simply use the base URL below and pass your API access key to the API's &lt;code&gt;access_key&lt;/code&gt; parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://api.weatherstack.com/current
    ? access_key = YOUR_ACCESS_KEY
    &amp;amp; query = {coordinates}

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

&lt;/div&gt;



&lt;p&gt;Weather Stack is easy to integrate and there is an option of passing the place name you want to get the weather here is an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://api.weatherstack.com/current
    ? access_key = YOUR_ACCESS_KEY
    &amp;amp; query = New York
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But today we are going to use the geoCoding where we will pass the coordinates returned from the geoCode function we created in part One.In part one the function returns an object so to access the latitude will be &lt;code&gt;cordinates.latitude&lt;/code&gt; and longitude we use &lt;code&gt;cordinates.latitude&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;geoCode("Nairobi").then(async function(cordinates){
   response = await axios.get(`http://api.weatherstack.com/current?access_key=**your Accesskey&amp;amp;query=${cordinates.latitude},${cordinates.longitude}&amp;amp;units=m`)
   const description=response.data.current.weather_descriptions[0];
   const temperature = response.data.current.temperature;
   const timeTaken = response.data.current.observation_time;


})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will get us the weather data  but we are going to only use the &lt;code&gt;description ,temperature and time the data was recorded&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next part we will be structuring our files and start working with express&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Creating Weather App using Weather API and Node Js : Part One</title>
      <dc:creator>Adolph Odhiambo</dc:creator>
      <pubDate>Sat, 09 Jul 2022 19:20:50 +0000</pubDate>
      <link>https://dev.to/adolphtech/creating-weather-app-using-weather-api-and-node-js-part-one-1n49</link>
      <guid>https://dev.to/adolphtech/creating-weather-app-using-weather-api-and-node-js-part-one-1n49</guid>
      <description>&lt;p&gt;We are going to create a webapp using Nodejs ,express and an API,&lt;a href="https://ady-weather-app.herokuapp.com" rel="noopener noreferrer"&gt; Here is the link to what we are going to build &lt;/a&gt;.You only need basic understanding of routes ,async function and promises .The front end may be trash but the focus here is &lt;strong&gt;node Js and express&lt;/strong&gt; 😂. So let's get our hands dirty.&lt;/p&gt;

&lt;p&gt;So the first thing you do on the website is inserting the location, I know there are other easier APIs to convert location to API easily but here we are going to use two API's just for practice .The first API we are going to use is &lt;strong&gt;mapbox&lt;/strong&gt; ,head over to &lt;a href="https://account.mapbox.com/auth/signup/" rel="noopener noreferrer"&gt;https://account.mapbox.com/auth/signup/&lt;/a&gt; and create an account. After creating your account head over to &lt;a href="https://account.mapbox.com/" rel="noopener noreferrer"&gt;https://account.mapbox.com/&lt;/a&gt; here you  will have to copy your &lt;strong&gt;"default public token"&lt;/strong&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%2Fe33gormsgupmiyikuykr.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%2Fe33gormsgupmiyikuykr.png" alt="Mapbox dashboard with default public access token" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I&lt;br&gt;
So  head over to your favorite code editor , i am going to use  vsCode.&lt;br&gt;
In mapbox we are going to pass the address so that it returns the longitude and latitude&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a file called &lt;code&gt;app.js&lt;/code&gt;
&lt;em&gt;1.In the file we create an async function with the name &lt;strong&gt;geoCode&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function geoCode(){


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

&lt;/div&gt;


&lt;p&gt;From the map box documentation we have to make a request to the API ,the api we are using here is the geocoding api &lt;a href="https://docs.mapbox.com/api/search/geocoding/" rel="noopener noreferrer"&gt;https://docs.mapbox.com/api/search/geocoding/&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;We are making a request to this api &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
https://api.mapbox.com/geocoding/v5/mapbox.places/**the address you want to get the longitudes**.json?access_token=**Your default public access token**&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the example below I am going to use  &lt;strong&gt;New York&lt;/strong&gt; as my address,but sorry because i will hide my access token use the one you copied from mapbox&lt;br&gt;
&lt;code&gt;https://api.mapbox.com/geocoding/v5/mapbox.places/New York.json?access_token=place your access token  here&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To test if our request works let us post the link above to a new web browser app i am going to use Chrome.&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%2F7ebj11kbrj5tx1xu1w5q.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%2F7ebj11kbrj5tx1xu1w5q.png" alt="Request from url to mapbox" width="800" height="405"&gt;&lt;/a&gt; &lt;br&gt;
If your request worked you are going to see something similar to the above.&lt;/p&gt;

&lt;p&gt;The data returned is a JSON so let us use the &lt;a href="https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh" rel="noopener noreferrer"&gt;JSON viewer chrome extension&lt;/a&gt; to format the response.&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%2Fr1y7togb3irqcuw108pb.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%2Fr1y7togb3irqcuw108pb.png" alt="json viewer" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The data is now readable and you can see the response is a result of all places with the name "New York" but we want the New York which is the capital of USA so we can set the &lt;strong&gt;limit to 1&lt;/strong&gt; read more about it here &lt;a href="https://docs.mapbox.com/api/search/geocoding/#geocoding-response-object" rel="noopener noreferrer"&gt;https://docs.mapbox.com/api/search/geocoding/#geocoding-response-object&lt;/a&gt;.&lt;br&gt;
To add the limit we &lt;code&gt;&amp;amp;limit=1&lt;/code&gt; at the end of the URL&lt;/p&gt;

&lt;p&gt;So if we set the limit our code will look like this &lt;code&gt;https://api.mapbox.com/geocoding/v5/mapbox.places/New York.json?access_token=place your access token  here&amp;amp;limit=1&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkqiu3t6tmhzma7au4no.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%2Fzkqiu3t6tmhzma7au4no.png" alt="one result" width="800" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now have one result and that is nice ,now let us go back to our code editor and create a constant &lt;code&gt;const url&lt;/code&gt; which will be equal to the url we request data with.The code will look something 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;async function geoCode(){
 const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/newyork.json?access_token=place your access token here&amp;amp;limit=1";

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

&lt;/div&gt;



&lt;p&gt;Before we continue i'm assuming you have already installed node in your computer without errors.&lt;/p&gt;

&lt;p&gt;Let us now open the VS code terminal and install node packages &lt;br&gt;
so in my terminal and in the root folder i am going to  run the command &lt;code&gt;npm init -y&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanptj8aapq94fod7owio.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%2Fanptj8aapq94fod7owio.png" alt="npm init" width="693" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create a new folder called &lt;code&gt;package.json&lt;/code&gt; .&lt;br&gt;
We are going to install &lt;code&gt;axios&lt;/code&gt; which we will be using to fetch data from the API. To install AXIOS we use &lt;code&gt;npm install axios&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To use axios we need to require it &lt;code&gt;const axios = require("axios"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how our code looks like now, we will also console.log the response&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require("axios")

async function geoCode(){

 const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/newyork.json?access_token=place your access token here&amp;amp;limit=1";
response = await axios.get(url);
console.log(response.data)
}

geocode()   //calling the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when we run our &lt;code&gt;app.js&lt;/code&gt; file we are able to see the results on the terminal.Remember we were only using mapbox to get the coordinates which are stored in &lt;code&gt;response.data.features[0].center&lt;/code&gt;. So we let's implement that on our code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require("axios")

async function geoCode(){

 const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/newyork.json?access_token=place your access token here&amp;amp;limit=1";
response = await axios.get(url);
console.log(response.data.features[0].center)
}

geocode()   //calling the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the &lt;code&gt;center&lt;/code&gt; array contains the longitude and latitude, with the longitude having an index of &lt;code&gt;0&lt;/code&gt; and the latitode have an index of &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our code we are going to &lt;code&gt;console.log&lt;/code&gt; an object containing an object.&lt;br&gt;
&lt;code&gt;const cordinates= {&lt;br&gt;
   longitude: response.data.features[0].center[1],&lt;br&gt;
   latitude: response.data.features[0].center[0]&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
 then we &lt;code&gt;return cordinates&lt;/code&gt; because we will use the cordinates later.&lt;/p&gt;

&lt;p&gt;Here is our code now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require("axios")

async function geoCode(){

 const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/newyork.json?access_token=place your access token here&amp;amp;limit=1";
response = await axios.get(url);
const cordinates= {
   longitude: response.data.features[0].center[1],
   latitude: response.data.features[0].center[0]
}

return cordinates;
}
}

geocode()   //calling the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to load the address dynamically so let us create a constant &lt;code&gt;const address = Nairobi&lt;/code&gt; .(Nairobi is the capital city of Kenya). So we concatenated the address in the url , our &lt;code&gt;url&lt;/code&gt; variable will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const address = "Nairobi"
const url `https://api.mapbox.com/geocoding/v5/mapbox.places/${address}.json?access_token=place your access token here&amp;amp;limit=1`;

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

&lt;/div&gt;



&lt;p&gt;We are now done for our part One let us continue making our weather app in part two.Here is our our final code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require("axios")

async function geoCode(){

const address = "Nairobi" 
 const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/${address}.json?access_token=place your access token here&amp;amp;limit=1";
response = await axios.get(url);
const cordinates= {
   longitude: response.data.features[0].center[1],
   latitude: response.data.features[0].center[0]
}

return cordinates;
}
}

geocode()   //calling the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>api</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
