<?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>Forem: Salman Khan</title>
    <description>The latest articles on Forem by Salman Khan (@salmank).</description>
    <link>https://forem.com/salmank</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%2F1178279%2F09404a27-6f65-4b74-abd1-6327efe90d86.jpg</url>
      <title>Forem: Salman Khan</title>
      <link>https://forem.com/salmank</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/salmank"/>
    <language>en</language>
    <item>
      <title>A Practical Guide to Hypothesis Testing I : Association - From Fisher’s Exact Test to the Chi-Square</title>
      <dc:creator>Salman Khan</dc:creator>
      <pubDate>Sun, 07 Sep 2025 11:25:51 +0000</pubDate>
      <link>https://forem.com/salmank/a-practical-guide-to-testing-association-in-2x2-tables-from-fishers-exact-test-to-the-chi-square-5hmk</link>
      <guid>https://forem.com/salmank/a-practical-guide-to-testing-association-in-2x2-tables-from-fishers-exact-test-to-the-chi-square-5hmk</guid>
      <description>&lt;p&gt;In experimental research and A/B testing, analysts frequently compare two independent groups on a binary outcome such as success/failure, conversion/no conversion, or alive/dead. The situation has three ingredients:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two independent groups (for example treatment and control)
&lt;/li&gt;
&lt;li&gt;A binary outcome variable (for example death / survival)
&lt;/li&gt;
&lt;li&gt;The comparison of proportions between groups&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The data naturally form a 2×2 contingency table, and the central inferential question is whether any observed difference in proportions reflects a real effect or mere chance. &lt;/p&gt;

&lt;p&gt;Two of the most prominent methods for answering this question are &lt;em&gt;Fisher's Exact Test&lt;/em&gt; and the &lt;em&gt;Chi-Square Test of Independence&lt;/em&gt;. While both tests address the same core hypothesis, they are founded on different statistical philosophies and assumptions. This article provides a practical guide to navigating this choice. We will demystify the underlying principles of each test, clarify their assumptions, and address common misconceptions. &lt;/p&gt;

&lt;p&gt;To illustrate the practical application and differences between these tests, we will use a clinical trial example throughout this guide. Imagine a study comparing a new treatment to a control with a one-sided hypothesis 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;pT&amp;lt;pCp_T &amp;lt; p_C&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;p&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 mathnormal mtight"&gt;T&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="mrel"&gt;&amp;lt;&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;p&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
 (example adapted from &lt;a href="https://learning.edx.org/course/course-v1:MITx+6.419x+3T2025/home" rel="noopener noreferrer"&gt;MITx 6.419x&lt;/a&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Treatment&lt;/th&gt;
&lt;th&gt;Control&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Death&lt;/td&gt;
&lt;td&gt;39&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Survive&lt;/td&gt;
&lt;td&gt;30,961&lt;/td&gt;
&lt;td&gt;30,937&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total&lt;/td&gt;
&lt;td&gt;31,000&lt;/td&gt;
&lt;td&gt;31,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The observed mortality rates are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Treatment: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;p^T=39/31,000≈0.126%\hat p_T = 39/31{,}000 \approx 0.126\%&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 accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;T&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="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;39/31&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;000&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;0.126%&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;Control: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;p^C=63/31,000≈0.203%\hat p_C = 63/31{,}000 \approx 0.203\%&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 accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;C&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="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;63/31&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;000&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;0.203%&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;Risk difference: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;p^T−p^C≈−0.077%\hat p_T - \hat p_C \approx -0.077\%&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 accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;T&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&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;C&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="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&gt;&lt;span class="mord"&gt;0.077%&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although the treatment group shows a lower mortality rate, we must employ statistical testing to determine whether this difference is statistically significant or likely to have occurred by chance under the null hypothesis.&lt;/p&gt;
&lt;h2&gt;
  
  
  Fisher's Exact Test
&lt;/h2&gt;

&lt;p&gt;Fisher's exact test is a non-parametric method that calculates the exact probability of observing a table as extreme as, or more extreme than, the one observed, given the fixed marginal totals. It is ideal for small sample sizes or rare events, because it does not rely on large-sample approximations.&lt;/p&gt;

&lt;p&gt;The hypergeometric probability for observing exactly 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;aa&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;a&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 events in the treatment group is&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;P(A=a)=(nTa) (nCs−a)(ns)
P(A=a)=\frac{\binom{n_T}{a}\,\binom{n_C}{s-a}}{\binom{n}{s}}
&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;P&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&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 mathnormal"&gt;a&lt;/span&gt;&lt;span class="mclose"&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="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&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;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mfrac"&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;s&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="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&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 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 class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mfrac"&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;a&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="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&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-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;T&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&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="mclose delimcenter"&gt;&lt;span class="delimsizing size1"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size1"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mfrac"&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;s&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;a&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="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&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-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;C&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&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="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 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="mclose nulldelimiter"&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;where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nTn_T&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;n&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 mathnormal mtight"&gt;T&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&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;nCn_C&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;n&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
 are the treatment and control sample sizes, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;ss&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;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is the total number of events and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=nT+nCn=n_T+n_C&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;n&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;n&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 mathnormal mtight"&gt;T&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&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;n&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;For one-sided testing of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;pT&amp;lt;pCp_T &amp;lt; p_C&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;p&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 mathnormal mtight"&gt;T&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="mrel"&gt;&amp;lt;&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;p&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
 we sum hypergeometric probabilities for tables with treatment-group counts less than or equal to the observed count.&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;fisher_exact&lt;/span&gt;

&lt;span class="c1"&gt;# Table format: [[Deaths_T, Survived_T], [Deaths_C, Survived_C]]
&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30961&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
         &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;63&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30937&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="n"&gt;odds_ratio&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p_value_fisher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fisher_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&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;less&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;Fisher&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s exact test (one-sided, p_T &amp;lt; p_C):&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;  Odds ratio:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;odds_ratio&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="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;p_value_fisher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Assumption&lt;/em&gt;: Fisher's test assumes that both row and column margins are fixed. Under this model the total number of events and the group sizes are treated as fixed, and the probability of the observed cell counts follows a hypergeometric distribution. Fisher’s test gives exact p-values under this conditioning assumption.&lt;/p&gt;
&lt;h2&gt;
  
  
  Barnard's test
&lt;/h2&gt;

&lt;p&gt;Barnard's test treats each group as an independent binomial experiment and constructs an exact test without conditioning on the column margin. In many small-sample settings it is more powerful than Fisher's test because it uses the larger sample space of possible outcomes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Assumption&lt;/em&gt;: Barnard's test models each group as an independent binomial with probabilities 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;pTp_T&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;p&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 mathnormal mtight"&gt;T&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&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;pCp_C&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;p&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
. It does not condition on the column margin (total events). As a result Barnard's test is unconditional and often has higher power than Fisher in small samples because it does not restrict attention to fixed column totals&lt;/p&gt;
&lt;h2&gt;
  
  
  The Chi-Square Test of Independence and the pooled Z-Test
&lt;/h2&gt;

&lt;p&gt;The Pearson chi-square test and the pooled two-proportion z-test are asymptotic methods that test the hypothesis of equal proportions. For a 2×2 table, they are mathematically equivalent.&lt;/p&gt;

&lt;p&gt;Pooled proportion&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;p^=a+cnT+nC
\hat p = \frac{a + c}{n_T + n_C}
&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 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 mathnormal"&gt;p&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="mord"&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 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="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&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;&lt;span class="mord mathnormal"&gt;n&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 mathnormal mtight"&gt;T&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;n&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 mathnormal mtight"&gt;C&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&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;a&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 mathnormal"&gt;c&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="mclose nulldelimiter"&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;Pooled standard error&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;SEpooled=p^(1−p^)(1nT+1nC)
SE_{\text{pooled}} = \sqrt{\hat p(1-\hat p)\left(\frac{1}{n_T} + \frac{1}{n_C}\right)}
&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;S&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;E&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 text mtight"&gt;&lt;span class="mord mtight"&gt;pooled&lt;/span&gt;&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="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 accent"&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 mathnormal"&gt;p&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="mord"&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 class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&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 accent"&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 mathnormal"&gt;p&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="mord"&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 class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size3"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&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;&lt;span class="mord mathnormal"&gt;n&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 mathnormal mtight"&gt;T&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&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&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 class="mclose nulldelimiter"&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="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&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;&lt;span class="mord mathnormal"&gt;n&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 mathnormal mtight"&gt;C&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&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&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 class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter"&gt;&lt;span class="delimsizing size3"&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;Z-statistic for the difference in proportions (treatment minus control)&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;z=p^T−p^CSEpooled
z = \frac{\hat p_T - \hat p_C}{SE_{\text{pooled}}}
&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;z&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="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&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 mathnormal"&gt;S&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;E&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 text mtight"&gt;&lt;span class="mord mtight"&gt;pooled&lt;/span&gt;&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&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;T&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 accent"&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 mathnormal"&gt;p&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="mord"&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 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 mathnormal mtight"&gt;C&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&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="mclose nulldelimiter"&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;Note on one-sided versus two-sided p-values&lt;/p&gt;

&lt;p&gt;The pooled z-test can produce a one-sided p-value directly from the z-statistic.&lt;/p&gt;

&lt;p&gt;The chi-square routine typically returns a two-sided p-value based on the chi-square upper tail. If the z-statistic has the direction you expect (for example negative when testing 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;pT&amp;lt;pCp_T &amp;lt; p_C&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;p&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 mathnormal mtight"&gt;T&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="mrel"&gt;&amp;lt;&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;p&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 mathnormal mtight"&gt;C&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&gt;&lt;/span&gt;
&lt;/span&gt;
), the one-sided p-value in that direction equals half the chi-square two-sided p-value. In other words, when the direction of the effect matches the alternative, one-sided p ≈ chi-square p / 2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Treat counts as approximately normal via the central limit theorem when expected counts are sufficiently large. The pooled z-test uses a pooled variance estimate under the null. The chi-square compares observed to expected counts under independence.
&lt;/li&gt;
&lt;li&gt;These methods are fast and accurate for moderate to large samples, but they are approximations. The usual rule of thumb is expected counts at least 5, but this is a heuristic. For rare events or very skewed margins, check approximations against exact methods or simulation.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Enhancing Machine Learning Models: A Deep Dive into Feature Engineering</title>
      <dc:creator>Salman Khan</dc:creator>
      <pubDate>Sun, 31 Dec 2023 12:48:27 +0000</pubDate>
      <link>https://forem.com/salmank/enhancing-machine-learning-models-a-deep-dive-into-feature-engineering-2dh7</link>
      <guid>https://forem.com/salmank/enhancing-machine-learning-models-a-deep-dive-into-feature-engineering-2dh7</guid>
      <description>&lt;p&gt;The efficacy of machine learning models heavily depends on the quality of input data and features [1]. In traditional machine learning, transforming raw data into features is crucial for model accuracy. Feature engineering aims to transform existing data into informative, relevant, and discriminative features. Although deep learning and end-to-end learning have revolutionized and automated processing for images, text, and signals, feature engineering for relational and human behavioural data remains an iterative, slow and laborious task [2].  &lt;/p&gt;

&lt;p&gt;This article explores techniques for feature engineering to enhance the accuracy and reliability of a predictive model. Additionally, it presents solutions that can help streamline the feature engineering process.&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%2Flezpafemytdv0mziiuvr.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%2Flezpafemytdv0mziiuvr.PNG" alt="Data Science Workflow" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Data Science Workflow: An Iterative Three-Step Process [2]
&lt;/h6&gt;

&lt;h6&gt;
  
  
  In the initial phase, analysts define the predictive objectives. Data engineers then extract, load, transform variables, engineer features, and define target labels. Finally, machine learning engineers construct models tailored to the specified predictive goals, exploring various techniques iteratively for the most suitable solution.
&lt;/h6&gt;




&lt;h2&gt;
  
  
  What is Feature Engineering?
&lt;/h2&gt;

&lt;p&gt;Feature Engineering is an essential step in traditional machine learning models, where the experts manually design and extract relevant features from the processed data. The goal is to encode expert knowledge, intuitive judgement, and human preconceptions into the machine learning model. This enables easier learning, especially with smaller data sets and increased model accuracy and interpretability.&lt;/p&gt;

&lt;p&gt;However, feature engineering is not a one-size-fits-all solution. The choice of features and techniques depends on the nature of the data, the complexity of the problem, and the goals of the model. Moreover, feature engineering is an iterative process where the model performance is evaluated, and the features are refined and updated accordingly.&lt;/p&gt;

&lt;p&gt;The figure below illustrates how simply projecting existing covariates into higher dimensional space, i.e. creating polynomial variants of existing features, can make the data linearly separable and easier to learn by a simple machine learning model.&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%2Fvnbmxi4te60so5a1mvpu.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%2Fvnbmxi4te60so5a1mvpu.png" alt="SVM" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Example of how feature engineering improves model accuracy - Adding a polynomial variant of existing features can make classes linearly separable and easier for a simple ML model to learn.
&lt;/h6&gt;




&lt;h2&gt;
  
  
  Basic Feature Engineering Techniques on relational and temporal data
&lt;/h2&gt;

&lt;p&gt;The table below summarizes some basic feature engineering techniques relevant to the traditional machine learning models.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Technique&lt;/th&gt;
&lt;th&gt;Description and Use Cases&lt;/th&gt;
&lt;th&gt;Procedure&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Imputation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Filling or estimating missing values to complete the dataset. Critical for handling missing data before model training.&lt;/td&gt;
&lt;td&gt;Mean, median, or mode imputation for numerical variables.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scaling&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Normalizing numerical features to a similar scale to prevent bias. Essential for preventing the dominance of features with larger magnitudes.&lt;/td&gt;
&lt;td&gt;Min-Max scaling (values in [0, 1]). Z-score normalization (mean of 0, standard deviation of 1).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Outliers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Setting predefined upper and lower bounds for numerical values to limit extreme values (outliers). This helps prevent extreme values from disproportionately influencing the model.&lt;/td&gt;
&lt;td&gt;Define upper and lower bounds based on percentiles or specific thresholds. Cap values above the upper bound and collar values below the lower bound.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;One-Hot Encoding&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Representing categorical variables as binary vectors. Enables machine learning algorithms to work with categorical data.&lt;/td&gt;
&lt;td&gt;Create a binary column for each category. Assign 1 to the corresponding category, 0 otherwise.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Binning&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Transforming continuous numerical features into categorical ones. Useful for handling non-linear relationships in numerical data.&lt;/td&gt;
&lt;td&gt;Group values into discrete intervals or bins.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Log Transform&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Applying logarithmic transformation to skewed numerical features. Effective for variables like income with skewed distributions.&lt;/td&gt;
&lt;td&gt;Logarithm of values to handle right-skewed distributions.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Polynomial Features&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Creating new features via polynomial transformation. Captures non-linear relationships in data.&lt;/td&gt;
&lt;td&gt;If x is a feature, x^2 and x^3 become new features.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Feature Interactions&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Creating new features by combining existing features. Captures joint effects on the target variable.&lt;/td&gt;
&lt;td&gt;If x1 and x2 are features, create a new feature x1 * x2.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Feature Aggregation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Combining multiple related features into a single, more informative feature. Reduces dimensionality and captures consolidated information.&lt;/td&gt;
&lt;td&gt;Calculate averages or sums of related features.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Time-Based Features&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Extracting temporal information from timestamps or time-related data. Useful for understanding temporal patterns in the data.&lt;/td&gt;
&lt;td&gt;Examples: Day of the week, hour of the day, time lags for time series data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Regular Expressions Features&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Extracting patterns from text data using regular expressions. Useful for identifying specific structures or formats in text.&lt;/td&gt;
&lt;td&gt;Examples: Matching email addresses, extracting dates, identifying hashtags in social media text.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Frequency Encoding&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Assigning numerical values based on the frequency of categorical variables. Useful for encoding categorical variables with varying frequencies.&lt;/td&gt;
&lt;td&gt;Preserves information about the distribution of categories. Suitable for high-cardinality categorical variables.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Domain Specific Feature Engineering
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Natural Language Processing (NLP)&lt;/strong&gt;&lt;br&gt;
NLP tasks require extractions of features from text data, which include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tokenization: Splitting text into individual words or tokens.&lt;/li&gt;
&lt;li&gt;Stemming and lemmatization: Removing prefixes and suffixes from words and mapping them to their base or dictionary form.&lt;/li&gt;
&lt;li&gt;Part-of-speech (POS) tagging -  labelling each word with its corresponding POS, i.e. noun, verb, adjective, etc.&lt;/li&gt;
&lt;li&gt;Named-entity recognition: Locating and tagging named entities in text, such as persons, organizations, and locations.&lt;/li&gt;
&lt;li&gt;Bag of Words: Representing text as an integer vector of its word counts from a predefined vocabulary.&lt;/li&gt;
&lt;li&gt;Term Frequency-Inverse Document Frequency: Representing words by their numeric weights based on their frequency in a document relative to their frequency across all documents.&lt;/li&gt;
&lt;li&gt;Word Embeddings: Representing words as dense vectors in a continuous vector space based on semantic similarity.&lt;/li&gt;
&lt;li&gt;Sentiment analysis: Identifying the sentiment or tone of the text, whether it is positive, negative, or neutral.&lt;/li&gt;
&lt;li&gt;Topic modelling: Identifying the underlying topics in a document or set of documents.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Computer Vision (CV)&lt;/strong&gt;&lt;br&gt;
Feature engineering in computer vision involves techniques for extracting features from images, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image augmentation: Transforming the training set through geometric alterations like image rotation and filters to increase the training set for better model generalization.&lt;/li&gt;
&lt;li&gt;Edge detection filter: Utilizing Sobel, Prewitt, Laplacian, or Canny edge filters to highlight changes in intensity or edges in the image.&lt;/li&gt;
&lt;li&gt;Scale-invariant feature transform (SIFT): Identifying and describing local features in images that are invariant to scaling and rotation.&lt;/li&gt;
&lt;li&gt;Colour Histogram:  Representing an image by the distribution of its colours.&lt;/li&gt;
&lt;li&gt;Histogram of Oriented Gradients (HOG): Extracting features from an image based on the distribution of gradients in the image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Time-Series Analysis&lt;/strong&gt;&lt;br&gt;
Feature engineering in time-series analysis involves techniques for extracting features from time-series data, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Autocorrelation: Measuring the correlation between time series and its lagged values.&lt;/li&gt;
&lt;li&gt;Moving averages: Calculating the average of a subset of time-series data over a defined window.&lt;/li&gt;
&lt;li&gt;Trend analysis: Identifying trends and patterns in the time series data.&lt;/li&gt;
&lt;li&gt;Fourier transforms: Decomposing a time-series signal into its frequency components.&lt;/li&gt;
&lt;li&gt;Mel-frequency cepstral coefficients (MFCCs): Representing the audio signal by its power spectrum.&lt;/li&gt;
&lt;li&gt;Phonemes Representing words by phonemes, leveraging human preconceptions about how words are pronounced.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Automated Feature Engineering
&lt;/h2&gt;

&lt;p&gt;There are a myriad of tools and open-source packages that can help automate and streamline feature engineering. These packages utilize algorithms to generate and select features based on data characteristics. This reduces manual efforts and broadens the exploration of potential features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.featuretools.com/en/v0.16.0/index.html" rel="noopener noreferrer"&gt;Featuretools&lt;/a&gt;: designed for automated feature engineering on temporal and relational data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://tsfresh.readthedocs.io/en/latest/index.html" rel="noopener noreferrer"&gt;tsfresh&lt;/a&gt;: designed for feature engineering from time-series and other sequential data &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://pypi.org/project/autofeat/" rel="noopener noreferrer"&gt;AutoFeat&lt;/a&gt;: streamlines the generation of nonlinear features from data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://epistasislab.github.io/tpot/" rel="noopener noreferrer"&gt;TPOT (Tree-based Pipeline Optimization Tool)&lt;/a&gt;: Designed to automate all aspects of machine learning pipeline, i.e. feature engineering, feature selection and model optimization using genetic programming. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/AutoViML/featurewiz" rel="noopener noreferrer"&gt;featurewiz&lt;/a&gt;: automates feature engineering and selection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Featuretool, with its deep feature synthesis (DFS) [2]  algorithm, stands out for its versatility, particularly when working with relational datasets and incorporating temporal aggregation. &lt;/p&gt;

&lt;h4&gt;
  
  
  Tutorial
&lt;/h4&gt;

&lt;p&gt;In this tutorial, we will implement Featuretools on a dataset consisting of four tables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;clients - information about clients at a credit union&lt;/li&gt;
&lt;li&gt;loan - previous loans taken out by the clients&lt;/li&gt;
&lt;li&gt;payments due - payments due date and amount&lt;/li&gt;
&lt;li&gt;outcomes - loan payment and date&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Data Source: &lt;a href="https://www.kaggle.com/code/willkoehrsen/automated-feature-engineering-tutorial/input" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt; [3]&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%2Fl0bfulpjrzzg72xv773e.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%2Fl0bfulpjrzzg72xv773e.png" alt="Data Sample" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Data Sample: The objective of the machine learning model here is to classify if the customer will make or miss the next payment
&lt;/h6&gt;




&lt;p&gt;Featuretools offers three distinct advantages that make it a powerful tool for automated feature engineering:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. EntitySet Approach&lt;/strong&gt;&lt;br&gt;
Featuretools operates on EntitySet, i.e. data frames and the relationships between them. This simplifies the feature engineering process for relational datasets, enables users to define relationships between tables, and automatically generates features based on these relationships.&lt;/p&gt;

&lt;h6&gt;
  
  
  Code:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EntitySet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;clients&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;## Entities Dataframe
&lt;/span&gt;&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_dataframe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dataframe_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;clients&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;dataframe&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;clients&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;client_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;time_index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;joined&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_dataframe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dataframe_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;loans&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;dataframe&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;loans&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;loan_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;time_index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;loan_start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_dataframe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dataframe_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;payments_due&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;dataframe&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payments_due&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;payment_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;time_index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;due_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_dataframe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dataframe_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;outcome&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;dataframe&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;outcome&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;time_index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;outcome_time&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;## Adding Relationships in data frames
&lt;/span&gt;
&lt;span class="c1"&gt;# Relationship between clients and previous loans
&lt;/span&gt;&lt;span class="n"&gt;ed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_relationship&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;clients&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;client_id&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;loans&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;client_id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Relationship between previous loans and payments
&lt;/span&gt;&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_relationship&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;loans&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;loan_id&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;payments_due&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;loan_id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Relationship between payments and outcome
&lt;/span&gt;&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_relationship&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;payments_due&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;payment_id&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;outcome&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;payment_id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;es&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;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fbxkt75ht0qndvulstuoh.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%2Fbxkt75ht0qndvulstuoh.png" alt="Featuretools EntitySet" width="296" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Featuretools EntitySet - Tables and their Relationship.
&lt;/h6&gt;




&lt;p&gt;&lt;strong&gt;2. Feature Primitives, Deep Feature Synthesis (DFS)&lt;/strong&gt;&lt;br&gt;
"Feature primitives are the building blocks of Featuretools. They define computations that can be applied to raw datasets to create new features."[4]. &lt;/p&gt;

&lt;p&gt;Feature primitives fall into two categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Aggregation: Functions that group together child datapoints for each parent and compute statistics such as mean, variance etc. [3]&lt;/li&gt;
&lt;li&gt;Transformation: Operations applied to a subset of columns in a table, e.g. extracting the day from dates, difference between two columns. etc. [3]&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Code:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Index and the time to use as a cutoff time
&lt;/span&gt;&lt;span class="n"&gt;cutoff_times&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;payments_due&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;payment_id&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;due_date&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]].&lt;/span&gt;&lt;span class="nf"&gt;sort_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;by&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;due_date&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Rename columns to avoid confusion
&lt;/span&gt;&lt;span class="n"&gt;cutoff_times&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;due_date&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;time&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
                    &lt;span class="n"&gt;inplace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cutoff_times&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="c1"&gt;# subtract 1 day from the time
&lt;/span&gt;&lt;span class="n"&gt;cutoff_times&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;time&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;cutoff_times&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;time&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;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Timedelta&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;days&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;payments_due&lt;/span&gt;

&lt;span class="c1"&gt;# Feature Primitives
&lt;/span&gt;&lt;span class="n"&gt;agg_primitives&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;sum&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;min&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;trans_primitives&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;time_since_previous&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Deep feature synthesis 
&lt;/span&gt;&lt;span class="n"&gt;agg_primitives&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;sum&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;count&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;trans_primitives&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;time_since_previous&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Deep feature synthesis 
&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;feature_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entityset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target_dataframe_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;payments_due&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;agg_primitives&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agg_primitives&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;trans_primitives&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trans_primitives&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;n_jobs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verbose&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="n"&gt;cutoff_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cutoff_times&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                    
                       &lt;span class="n"&gt;cutoff_time_in_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="n"&gt;max_depth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Moreover, Featuretools provides a visual representation to inspect, interpret, and validate generated features, enhancing understanding and interpretability of the subsequently built machine learning model.&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%2Ftcjbjwnbmyaw1n50n1w2.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%2Ftcjbjwnbmyaw1n50n1w2.png" alt="Feature Details" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Visual representation from featuretool of how a particular feature is generated for the given dataset.
&lt;/h6&gt;




&lt;p&gt;&lt;strong&gt;3. Data Leakage and Handling Time&lt;/strong&gt;&lt;br&gt;
Data leakage in its many forms remains a challenge for machine learning models and systems [5]. Featuretools provide an intrinsic solution to prevent 'temporal leakage' in feature generation by specifying cutoff times for each record and filtering out all data preceding that time stamp before calculating features, effectively preventing the introduction of temporal leakage [6].&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;A detailed tutorial on how to use featuretool with consideration for handling time can be found here: &lt;a href="https://github.com/SalK91/dfs_tutorial" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;In conclusion, feature engineering is a critical step in machine learning models that can significantly enhance the model's performance and accuracy. Automated tools and packages, such as Featuretools, can streamline the feature engineering process and contribute to the success of a machine learning system.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;[1] Domingos, P., 2012. A few useful things to know about machine learning. Communications of the ACM, 55(10), pp.78-87.&lt;/p&gt;

&lt;p&gt;[2] Kanter, J.M. and Veeramachaneni, K., 2015, October. Deep feature synthesis: Towards automating data science endeavors. In 2015 IEEE international conference on data science and advanced analytics (DSAA) (pp. 1-10). IEEE.&lt;/p&gt;

&lt;p&gt;[3] Automated Feature Engineering Tutorial - &lt;a href="https://www.kaggle.com/code/willkoehrsen/automated-feature-engineering-tutorial/input" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] Feature primitives - &lt;a href="https://featuretools.alteryx.com/en/stable/getting_started/primitives.html" rel="noopener noreferrer"&gt;Featuretools Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[5] Kapoor, S. and Narayanan, A., 2023. Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9).&lt;/p&gt;

&lt;p&gt;[6] Handling Time - &lt;a href="https://docs.featuretools.com/en/v0.16.0/automated_feature_engineering/handling_time.html" rel="noopener noreferrer"&gt;Featuretools Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>productivity</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Beyond Model Deployment: Catching Data Drift</title>
      <dc:creator>Salman Khan</dc:creator>
      <pubDate>Sun, 17 Dec 2023 12:20:18 +0000</pubDate>
      <link>https://forem.com/salmank/beyond-model-deployment-catching-data-drift-45ca</link>
      <guid>https://forem.com/salmank/beyond-model-deployment-catching-data-drift-45ca</guid>
      <description>&lt;p&gt;Machine learning and deep learning techniques 'learn' by recognizing and generalizing patterns and statistical properties within the training data. The efficacy of these models in real-world scenarios is contingent on the assumption that the training data is an accurate representation of the production data. However, this assumption often breaks in the real world. Consumer behaviours and market trends may undergo gradual or even drastic shifts. Sensors responsible for data collection can experience a decline in sensitivity over time. Additionally, disruptions such as broken data pipelines, alterations in upstream systems, and changes in external APIs can introduce gradual or abrupt changes to the data used for predictions in production. In essence, the dynamic nature of real-world conditions poses challenges to the sustained accuracy and reliability of any ML system. Therefore, it is crucial to understand how the model behaves in production and promptly identify and resolve any issues that may arise. One of the critical aspects of ML Monitoring is identifying data drift. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is data drift?
&lt;/h2&gt;

&lt;p&gt;"Data drift is a change in the statistical properties and characteristics of the input data. It occurs when a machine learning model production encounters data that deviates from the data the model was initially trained on or earlier production data"[1]. Simply put, data drift is a change in the distribution of the input features, as illustrated in the figure below.&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%2Fxqpd3f4dmew5blwfb3og.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%2Fxqpd3f4dmew5blwfb3og.png" alt="Data-drift example" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How to identify data drift?
&lt;/h2&gt;

&lt;p&gt;Data drift monitoring necessitates a well-defined reference dataset. This dataset serves as a benchmark against which production data can be systematically compared and analyzed. Only by establishing the baseline via this reference data set it is possible to discern any variations in the distribution of features, enabling the timely identification of potential drift and ensuring the ongoing reliability and performance of the model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Methods to identify data drift&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule Based:&lt;/strong&gt;&lt;br&gt;
Heuristic-based alerts can be set up to indirectly monitor data drifts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Percentage of missing values.&lt;/li&gt;
&lt;li&gt;Percentage of numeric values outside a predefined min-max threshold.&lt;/li&gt;
&lt;li&gt;Percentage of new values in a categorical feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Statistical Tests:&lt;/strong&gt;&lt;br&gt;
Parametric and non-parametric tests can be utilized to compare the production data against reference datasets, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html" rel="noopener noreferrer"&gt;Two sample t-test&lt;/a&gt; - to compare means for numeric features.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ks_2samp.html" rel="noopener noreferrer"&gt;Kolmogorov Smirnov test (KS)&lt;/a&gt; - to test for equality of distribution of numerical features.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chisquare.html" rel="noopener noreferrer"&gt;Chi-squared test&lt;/a&gt; - to test for equality of distribution of categorical features.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.anderson_ksamp.html" rel="noopener noreferrer"&gt;K-Sample Anderson-Darling (AK)&lt;/a&gt; - tests the null hypothesis that k-samples are drawn from the same population without having to specify the distribution function of that population.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Distance Metrics:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.kl_div.html" rel="noopener noreferrer"&gt;Kullback–Leibler Divergence (KL Divergence)&lt;/a&gt; -  it is a non-symmetric metric that measures the relative entropy or difference in information represented by two distributions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.jensenshannon.html" rel="noopener noreferrer"&gt;Jensen–Shannon distance (JS Distance) &lt;/a&gt; -  measures the similarity between two probability distributions. It is based on KL Divergence, and one main difference between JS divergence and KL divergence is that JS is symmetric and it always has a finite value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.aporia.com/learn/data-science/practical-introduction-to-population-stability-index-psi/" rel="noopener noreferrer"&gt;Population Stability Index (PSI)&lt;/a&gt; - measures the distance between the distribution of numeric and categorical features.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Selecting the right metric
&lt;/h2&gt;

&lt;p&gt;Each metric discussed above possesses distinct properties and inherent assumptions. Therefore, it is crucial to identify the metric that aligns most effectively with the problem. This selection should consider both the dataset volume and the magnitude of drift, which is significant for the particular model. &lt;/p&gt;

&lt;p&gt;To gain a deeper understanding, we will analyze and compare these metrics across two distinct variations of a numerical feature in relation to reference data across various sample sizes. The figure below depicts the distribution of the two variants compared to the reference dataset.&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%2Fu3i176o72pjg80h8zcxw.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%2Fu3i176o72pjg80h8zcxw.png" alt="Experiment Distribution" width="800" height="266"&gt;&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%2F5udh27dxdjvosn04ufun.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%2F5udh27dxdjvosn04ufun.PNG" alt="Table Metrics" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Based on the experiments above, the following observations can be made:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Statistical Test Sensitivity&lt;/em&gt;: It is evident that statistical tests often demonstrate heightened sensitivity when applied to large datasets. Even minute near zero differences can attain statistical significance with a sufficiently high volume of data.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Distance Metric Challenges&lt;/em&gt;: Distance metrics lack standardized cutoffs for alarms, and their interpretation depends on the specific context of application and analysis goals. Establishing suitable thresholds for these metrics necessitates empirical evaluation based on the data's characteristics and the ML model's objectives.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;The code employed for the aforementioned experiments is available on &lt;a href="https://github.com/SalK91/data_drift_measures" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;In conclusion, the dynamic nature of real-world conditions poses significant challenges to the accuracy and reliability of machine learning systems. Changes in consumer behaviours, market trends, and potential disruptions in data collection mechanisms can introduce gradual or abrupt changes to the data used for predictions in production. In this context, monitoring and identifying data drift becomes paramount. As demonstrated through various statistical tests, distance metrics, and the analysis of experiment results, it is clear that selecting the right metric for monitoring data drift is a nuanced task. The sensitivity of statistical tests and the lack of standardized cutoffs for distance metrics highlight the need for a context-specific and empirical approach to establishing thresholds for effective monitoring. Ultimately, understanding how machine learning models behave in production and promptly addressing any identified issues are critical for ensuring these models' ongoing success and reliability in real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;[1] &lt;a href="https://www.evidentlyai.com/ml-in-production/data-drift" rel="noopener noreferrer"&gt;https://www.evidentlyai.com/ml-in-production/data-drift&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://docs.scipy.org/doc/" rel="noopener noreferrer"&gt;https://docs.scipy.org/doc/&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://www.aporia.com/learn/data-science/practical-introduction-to-population-stability-index-psi/" rel="noopener noreferrer"&gt;https://www.aporia.com/learn/data-science/practical-introduction-to-population-stability-index-psi/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mlops</category>
      <category>monitoring</category>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Decoding Deep Learning: An Introductory Guide to Neural Networks</title>
      <dc:creator>Salman Khan</dc:creator>
      <pubDate>Sat, 09 Dec 2023 13:07:36 +0000</pubDate>
      <link>https://forem.com/salmank/decoding-deep-learning-an-introductory-guide-to-neural-networks-3j9h</link>
      <guid>https://forem.com/salmank/decoding-deep-learning-an-introductory-guide-to-neural-networks-3j9h</guid>
      <description>&lt;p&gt;Deep Learning has become a pivotal driving force in reshaping the technology landscape, empowering advancements in image analysis, text interpretation, and the development of Generative AI. &lt;/p&gt;

&lt;p&gt;The global Deep Learning market size is projected to grow from $17.60 billion in 2023 to $188.58 billion by 2030, at a CAGR of 40.3% during the forecast period [1]. While it might initially appear complex, Deep Learning basics are pretty straightforward. &lt;/p&gt;

&lt;p&gt;This article aims to cover the basics of Neural Networks and Deep Learning for beginners.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Deep Learning?
&lt;/h2&gt;

&lt;p&gt;Deep Learning is a subset of artificial intelligence (AI) that employs advanced algorithms to discern intricate patterns in data, primarily through multi-layered neural networks. Deep Learning offers the inherent ability to simultaneously process &lt;strong&gt;multi-modal&lt;/strong&gt; data, such as text, images, and audio, through specialised architectures that extract patterns from different modalities. It further enables &lt;strong&gt;end-to-end learning&lt;/strong&gt;. End-to-end learning involves training neural networks to directly transform raw input data into desired output, eliminating the need for manual feature engineering or intermediate processing steps. For example, in natural language processing, end-to-end learning can involve training a neural network to directly translate one language into another without requiring explicit linguistic feature extraction or rule-based translation systems. In computer vision, end-to-end learning can enable a neural network to take raw image data and output information about objects or scenes, bypassing manual image preprocessing steps.  This streamlined approach allows deep learning models to learn the most relevant features and representations from the data without requiring feature engineering. It is particularly well-suited for tasks where traditional, multi-stage processing pipelines may be cumbersome or less effective.&lt;/p&gt;

&lt;p&gt;The applications of Deep Learning are diverse and far-reaching - from enabling autonomous vehicles to medical diagnostics. Deep Learning has enabled exponential advancements in the realm of Generative AI, which can generate new data - from realistic images to chatbots that generate human-like responses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Neural Networks  - Building Blocks of Deep Learning
&lt;/h2&gt;

&lt;p&gt;Neural networks draw inspiration from how the human brain operates, especially how biological neurons manage data. In the brain, neurons receive, process, and transmit information, forming the fundamental basis for thinking and learning.&lt;/p&gt;

&lt;p&gt;Neural Networks are composed of layers that act as distinct stages of information processing. As data progresses from one layer to the next, it transforms, with each layer contributing to the final decision or prediction made by the network. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common layer types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input layer:&lt;/strong&gt; This initial layer interfaces with the provided data. It takes in the raw information, be it pixels from an image, values from a dataset, or any other form of data, and forwards it to subsequent layers for processing. The number of neurons in this layer corresponds to the number of features or inputs in the dataset. This layer does not perform any computation on the input data&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hidden layer:&lt;/strong&gt;  Hidden layers are positioned between the input and output layers. Hidden layers are where the majority of computation occurs. They transform the data from the input layer and pass it on to another hidden layer or the output layer. The depth of a Neural Network often refers to the number of these hidden layers. Commonly used hidden layers include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dense Layer: Dense or Fully Connected Layers are the most common layers in feedforward neural networks. Neurons in a dense layer are connected to &lt;strong&gt;all&lt;/strong&gt; neurons in the previous layer. They are responsible for learning and representing complex patterns in the data.&lt;/li&gt;
&lt;li&gt;Convolutional layers: Mainly used in image processing tasks, they are specialised for spatial hierarchies in data. Using a mathematical operation called convolution, they can detect local patterns like edges, textures, and shapes in images. They are fundamental to Convolutional Neural Networks (CNNs), often used in image recognition and classification tasks.&lt;/li&gt;
&lt;li&gt;Pooling Layer: They are generally used in conjunction with convolution layers and reduce the spatial dimensions of the feature maps, which helps reduce computation and control for overfitting.&lt;/li&gt;
&lt;li&gt;Recurrent layer: These layers are tailored for sequential data, like time series or natural language. Unlike traditional layers, recurrent layers maintain a form of memory from their previous outputs, which allows them to make decisions influenced by a sequence of data rather than individual data points. Recurrent Neural Networks (RNNs), which use these layers, are beneficial for tasks like language modelling, speech recognition, and time series forecasting.
Advanced NN architectures include the Dropout Layer, Long short-term memory (LSTM), and Gated recurrent unit (GRU) layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Output layer:&lt;/strong&gt; The final layer in the sequence delivers the result. Depending on the network's design and function, this result can be a single value, a set of values, a category label, or even a complex data structure.&lt;/p&gt;

&lt;p&gt;So, layers are the building blocks of Neural Networks. Each serves a unique purpose, ensuring the network can process various data types and perform different tasks.&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%2F2b5yo4u05vgihu2u5zee.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%2F2b5yo4u05vgihu2u5zee.PNG" alt="Fully Connected NN" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Neurons - Building Blocks of Neural Networks
&lt;/h2&gt;

&lt;p&gt;Each layer comprises a group of &lt;strong&gt;neurons&lt;/strong&gt; that generally utilise non-linear functions to transform the data. Three critical components define the behaviour of each neuron -  &lt;strong&gt;weights, biases and the activation function&lt;/strong&gt;. Weights determine the significance of input data as a neuron processes it. In simple terms, weights amplify or dampen the input. Biases, on the other hand, are additional parameters that ensure neurons have a non-zero value when activated, even if all their input data is zero. The activation function, which is generally non-linear, enables the network to approximate and represent a wide range of functions and, hence, learn complex patterns in data.&lt;/p&gt;

&lt;p&gt;Overall, understanding a Neural Network's anatomy involves recognising its layers' purpose and function, the neurons within those layers, and the critical role weights and biases play in processing and refining information.&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%2F2v3s5ss2w12w5p1huuel.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%2F2v3s5ss2w12w5p1huuel.PNG" alt="Neuron" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Training a Neural Network
&lt;/h2&gt;

&lt;p&gt;Neural networks learn the underlying parameters of the model via an iterative process in which the calculations are carried out forward and backwards through the network until the desired level of accuracy is attained.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Forward Propagation&lt;/strong&gt;&lt;br&gt;
This is the first phase of training, where the input data is passed through the Neural Network. This result is then compared to the actual desired output to compute the error (loss) between the actual and model outputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backward Propagation&lt;/strong&gt; &lt;br&gt;
Backward propagation involves calculating the gradient of the loss with respect to each model parameter (weight and bias). This gradient represents the sensitivity of the loss to changes in each parameter. With this gradient, the model parameters (weights and biases) are adjusted in reverse order, starting from the output layer and working back to the input layer&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%2Fa4qninhqyr7tml05l41o.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%2Fa4qninhqyr7tml05l41o.PNG" alt="Forward and Backward Propogation" width="800" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loss Functions&lt;/strong&gt;&lt;br&gt;
These assess the performance of a Neural Network by measuring the disparity between its predictions and the actual values. Commonly used loss functions include Mean Squared Error for regression tasks and Cross-Entropy for classification tasks. The main goal of training is to minimise this loss value, which means that the model's predictions are getting closer to the actual values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimisation&lt;/strong&gt; &lt;br&gt;
Optimisation algorithms adjust the weights and biases in the network to minimise the loss. One of the most popular optimisation techniques is Gradient Descent, where the model iteratively updates its parameters in the direction that reduces the loss. Variants of Gradient Descent, like Stochastic Gradient Descent and Adam, offer more nuanced approaches to this adjustment process.&lt;/p&gt;

&lt;p&gt;Training a Neural Network is an iterative process of prediction, evaluation, and refinement. By adjusting its internal parameters in response to training data, the network tries to produce outputs that closely match the actual results.&lt;/p&gt;
&lt;h2&gt;
  
  
  How does this all look in practice?
&lt;/h2&gt;

&lt;p&gt;Let us examine a basic deep-learning task: classifying handwritten numbers using the well-known MNIST dataset. This collection contains grayscale images of handwritten digits from 0 to 9 and is a standard reference for testing Neural Network models.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Objective:&lt;/em&gt; To build a Neural Network model that can accurately identify and classify images of handwritten digits.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Data Preparation:&lt;/em&gt; The MNIST dataset is split into training and testing sets. Each image is 28x28 pixels and represents a flattened array of 784 values (28 multiplied by 28). These values, ranging from 0 to 255, represent pixel intensities. To make computations more stable, the pixel values are normalised to range between 0 and 1.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Network Architecture:&lt;/em&gt; Our primary Neural Network consists of the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An input layer with 784 nodes (corresponding to the 784-pixel values).&lt;/li&gt;
&lt;li&gt;A hidden layer with 128 nodes and a ReLU (Rectified Linear Unit) activation function.&lt;/li&gt;
&lt;li&gt;An output layer with ten nodes (representing digits 0–9) and a softmax activation function to provide classification probabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Forward and Backward Propagation:&lt;/em&gt; As previously discussed, the model initialises with forward propagation to produce outputs, then backward propagation to adjust weights and biases.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Loss Function:&lt;/em&gt; Given this classification problem, we use the Cross-Entropy loss function.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Optimisation:&lt;/em&gt; For this example, we will use the Adam optimiser, known for its efficiency.&lt;/p&gt;

&lt;p&gt;Training iterations: The model is trained over multiple iterations (often called epochs). After each epoch, the model's performance on a validation set can be assessed to avoid overfitting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech solutions and tools:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TensorFlow: This open-source framework developed by Google offers several tools for building and training Machine Learning models. For our handwritten digit classification, TensorFlow provides predefined functions and structures that simplify the model-building process.&lt;/p&gt;

&lt;p&gt;PyTorch: This tool, developed by Facebook's AI Research lab, is another popular framework for Deep Learning. PyTorch offers an adaptable and straightforward approach, making it an excellent alternative to TensorFlow for Neural Network tasks.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Final thoughts and suggestions
&lt;/h2&gt;

&lt;p&gt;Neural Networks are central to current technological breakthroughs, including Generative AI. We have covered their core components and the detailed training process. For those just starting, this is only the beginning. A vast field of application and innovation remains to be explored.&lt;/p&gt;

&lt;p&gt;For further learning, you can find information in multiple sources:&lt;/p&gt;

&lt;p&gt;Books:  "Deep Learning" by Ian Goodfellow, Yoshua Bengio, and Aaron Courville as your first step.&lt;/p&gt;

&lt;p&gt;Online courses: Platforms like Coursera or Udemy offer detailed courses on the subject.&lt;/p&gt;

&lt;p&gt;Communities: Use platforms like Stack Overflow or dedicated subreddits to discuss and learn.&lt;/p&gt;

&lt;p&gt;Deep Learning is expansive and constantly evolving. Embrace the challenge and get the results.&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://www.fortunebusinessinsights.com/deep-learning-market-107801" rel="noopener noreferrer"&gt;https://www.fortunebusinessinsights.com/deep-learning-market-107801&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>deeplearning</category>
      <category>tutorial</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
