<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Nyawade</title>
    <description>The latest articles on DEV Community by Michael Nyawade (@mnyawade).</description>
    <link>https://dev.to/mnyawade</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3839529%2Fb336d897-0a7d-4a18-84b5-124af989e837.png</url>
      <title>DEV Community: Michael Nyawade</title>
      <link>https://dev.to/mnyawade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mnyawade"/>
    <language>en</language>
    <item>
      <title>6 Common Pandas Errors and How to Fix Them (With Practical Examples)</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Thu, 12 Nov 2026 13:48:38 +0000</pubDate>
      <link>https://dev.to/mnyawade/6-common-pandas-errors-and-how-to-fix-them-with-practical-examples-53ja</link>
      <guid>https://dev.to/mnyawade/6-common-pandas-errors-and-how-to-fix-them-with-practical-examples-53ja</guid>
      <description>&lt;p&gt;If you've spent any time working with Python for data analysis, chances are you've encountered a few frustrating pandas errors.&lt;/p&gt;

&lt;p&gt;Sometimes, a simple operation like merging two DataFrames throws a &lt;code&gt;KeyError&lt;/code&gt;. Other times, calculating an average results in a &lt;code&gt;TypeError&lt;/code&gt;, or modifying a column triggers a &lt;code&gt;SettingWithCopyWarning&lt;/code&gt; that leaves you wondering what went wrong.&lt;/p&gt;

&lt;p&gt;The frustrating part is that these issues can interrupt your workflow, especially when you're working with large datasets or dealing with data from multiple sources.&lt;/p&gt;

&lt;p&gt;The good news? Most pandas errors are easier to fix once you understand what's causing them.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore six common pandas problems, why they happen, and how to fix them with practical Python examples.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. KeyError When Merging DataFrames
&lt;/h2&gt;

&lt;p&gt;One of the most common issues when working with pandas is encountering a &lt;code&gt;KeyError&lt;/code&gt; while merging two DataFrames.&lt;/p&gt;

&lt;p&gt;This usually happens when the column you're trying to use as a merge key doesn't exist in one or both DataFrames.&lt;/p&gt;

&lt;p&gt;For example, imagine you're working with customer information stored in two separate DataFrames:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Michael"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"CustomerID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, suppose you try to merge them using &lt;code&gt;customer_id&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get a &lt;code&gt;KeyError&lt;/code&gt; because the second DataFrame uses &lt;code&gt;CustomerID&lt;/code&gt;, not &lt;code&gt;customer_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Python and pandas are case-sensitive, so these two column names are treated differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;There are two straightforward approaches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1: Use &lt;code&gt;left_on&lt;/code&gt; and &lt;code&gt;right_on&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the column names are different but contain the same information, you can specify them separately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;left_on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;right_on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"CustomerID"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2: Rename columns for consistency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can rename the column before merging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;orders&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="s2"&gt;"CustomerID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&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="kc"&gt;True&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;merged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;Before merging DataFrames, always inspect their column names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, remember that matching column names alone doesn't guarantee matching data. Check that the keys have compatible data types and represent the same identifiers.&lt;/p&gt;

&lt;p&gt;A few seconds spent checking your merge keys can save you a lot of debugging time.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. TypeError When Performing Calculations
&lt;/h2&gt;

&lt;p&gt;Another common problem occurs when you try to perform mathematical operations on columns containing unexpected data types.&lt;/p&gt;

&lt;p&gt;For example, imagine you have a DataFrame containing customer transaction amounts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"100"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"200"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"300"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"unknown"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, the &lt;code&gt;amount&lt;/code&gt; column looks like it contains numbers.&lt;/p&gt;

&lt;p&gt;However, pandas treats these values as strings.&lt;/p&gt;

&lt;p&gt;Trying to calculate the mean directly can produce a &lt;code&gt;TypeError&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is that mathematical operations expect compatible numeric values.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;pd.to_numeric()&lt;/code&gt; to convert the column into a numeric data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"amount"&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="nf"&gt;to_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coerce"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;errors="coerce"&lt;/code&gt; parameter converts values that cannot be interpreted as numbers into &lt;code&gt;NaN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you can calculate the mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Pandas automatically skips missing values when calculating the mean by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;Get into the habit of checking your DataFrame's data types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dtypes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also inspect the overall structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is particularly useful when importing CSV or Excel files, where numeric columns may accidentally be interpreted as strings.&lt;/p&gt;

&lt;p&gt;One important detail: &lt;code&gt;errors="coerce"&lt;/code&gt; can hide problematic values by converting them into missing values. When data accuracy matters, inspect the original values that failed conversion before proceeding.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. SettingWithCopyWarning When Modifying DataFrames
&lt;/h2&gt;

&lt;p&gt;Have you ever tried modifying a DataFrame column and received a warning like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SettingWithCopyWarning:
A value is trying to be set on a copy of a slice
from a DataFrame.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This warning can be confusing, especially when your code appears to work.&lt;/p&gt;

&lt;p&gt;It commonly occurs when you select a portion of a DataFrame and then attempt to modify it through chained indexing.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Michael"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Adult"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is that &lt;code&gt;adults&lt;/code&gt; was created by filtering another DataFrame.&lt;/p&gt;

&lt;p&gt;Depending on the operation and pandas version, you may be working with a copy or a view. Pandas cannot always determine whether your modification should affect the original DataFrame.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Approach 1: Create an explicit copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want an independent DataFrame, use &lt;code&gt;.copy()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;adults&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Adult"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes your intention clear: you're working with a separate DataFrame.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2: Use &lt;code&gt;.loc&lt;/code&gt; to modify the original DataFrame&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your intention is to update the original DataFrame, use &lt;code&gt;.loc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Adult"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is generally the clearest way to perform conditional assignments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;Avoid chained assignments when modifying DataFrames.&lt;/p&gt;

&lt;p&gt;Instead, decide whether you want to modify the original DataFrame or create an independent copy.&lt;/p&gt;

&lt;p&gt;Being explicit about this makes your code easier to understand and helps prevent unexpected modifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Unexpected Results Caused by NaN Values
&lt;/h2&gt;

&lt;p&gt;Missing values are almost unavoidable when working with real-world datasets.&lt;/p&gt;

&lt;p&gt;You might encounter missing transaction amounts, incomplete customer records, or unavailable measurements.&lt;/p&gt;

&lt;p&gt;In pandas, missing numeric values are commonly represented by &lt;code&gt;NaN&lt;/code&gt; (Not a Number).&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, imagine you're calculating the total transaction value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, pandas skips missing values during this calculation.&lt;/p&gt;

&lt;p&gt;That might be acceptable in some situations, but it could also produce misleading results if missing transactions represent incomplete financial records.&lt;/p&gt;

&lt;p&gt;The important thing is to understand how missing values affect your calculations.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;There are two common approaches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 1: Fill missing values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have a valid reason to treat missing values as zero, use &lt;code&gt;fillna()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&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="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approach 2: Remove rows containing missing values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If incomplete records should not be included in your analysis, use &lt;code&gt;dropna()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df_clean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df_clean&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"transaction"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we're using &lt;code&gt;subset&lt;/code&gt; to specify the column we want to check.&lt;/p&gt;

&lt;p&gt;This prevents pandas from dropping rows just because an unrelated column contains a missing value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;Don't automatically replace every missing value with zero.&lt;/p&gt;

&lt;p&gt;For example, a missing salary doesn't necessarily mean that an employee earns nothing. Similarly, a missing transaction amount doesn't mean that no transaction occurred.&lt;/p&gt;

&lt;p&gt;Understand why the data is missing before deciding how to handle it.&lt;/p&gt;

&lt;p&gt;For financial analysis, this distinction is especially important because incorrect assumptions about missing values can lead to misleading reports and business decisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Unexpected NaN Values When Working With Multiple DataFrames
&lt;/h2&gt;

&lt;p&gt;Here's another pandas behavior that can catch developers off guard: automatic index alignment.&lt;/p&gt;

&lt;p&gt;When performing operations between two pandas Series or DataFrames, pandas aligns their values using their index labels rather than simply matching their positions.&lt;/p&gt;

&lt;p&gt;Consider these two DataFrames:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's add them together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;df2&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   sales
0    NaN
1  250.0
2  400.0
3    NaN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why does this happen?&lt;/p&gt;

&lt;p&gt;Because pandas matches the index labels.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Index &lt;code&gt;1&lt;/code&gt; exists in both DataFrames, so &lt;code&gt;200 + 50 = 250&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;2&lt;/code&gt; exists in both DataFrames, so &lt;code&gt;300 + 100 = 400&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;0&lt;/code&gt; exists only in the first DataFrame.&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;3&lt;/code&gt; exists only in the second DataFrame.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the unmatched indices, pandas returns &lt;code&gt;NaN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is intentional behavior, but it can produce unexpected results if you're assuming that pandas matches rows by position.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Approach 1: Use &lt;code&gt;.add()&lt;/code&gt; with &lt;code&gt;fill_value&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want missing values to be treated as zero during the addition, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fill_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   sales
0  100.0
1  250.0
2  400.0
3  150.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful when you're combining datasets with partially overlapping indices and treating absent values as zero is appropriate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2: Reset the index when appropriate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the row positions represent the same entities and the index labels are not meaningful, you can reset the indices before performing the operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df1_reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;drop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;df2_reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reset_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;drop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df1_reset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;df2_reset&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, only do this when positional alignment is actually what you want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;Before performing arithmetic operations across multiple DataFrames, inspect their indices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, understand whether your operation should align records by index, by a shared identifier, or by row position.&lt;/p&gt;

&lt;p&gt;Never assume these approaches are interchangeable.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Memory Issues When Working With Large DataFrames
&lt;/h2&gt;

&lt;p&gt;Pandas is convenient, but working with large datasets can consume a significant amount of memory.&lt;/p&gt;

&lt;p&gt;This becomes particularly noticeable when you're dealing with millions of rows, repeatedly creating intermediate DataFrames, or loading more columns than your analysis actually needs.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df_copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df_filtered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df_copy&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df_copy&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;df_final&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df_filtered&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the size of the original DataFrame, these operations can increase memory consumption.&lt;/p&gt;

&lt;p&gt;If you're working with limited system resources, this can become a serious performance issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Approach 1: Select only the columns you need&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of loading or carrying around unnecessary columns, select the relevant ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"sales.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;usecols&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can significantly reduce memory usage when working with wide datasets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 2: Avoid unnecessary copies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't need an independent DataFrame, avoid creating additional copies without a good reason.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df_filtered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"customer_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;.copy()&lt;/code&gt; when you need an independent object, not automatically after every operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach 3: Monitor memory usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pandas provides a convenient method for inspecting how much memory a DataFrame consumes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;memory_used&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memory_usage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memory_used&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To calculate the total memory usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;total_memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memory_usage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Memory used: {total_memory / 1024**2:.2f} MB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;deep=True&lt;/code&gt; parameter provides a more complete estimate for object-dtype columns, including the memory used by their underlying Python objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practice
&lt;/h3&gt;

&lt;p&gt;When working with large datasets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load only the columns you need.&lt;/li&gt;
&lt;li&gt;Avoid unnecessary copies.&lt;/li&gt;
&lt;li&gt;Use appropriate data types.&lt;/li&gt;
&lt;li&gt;Consider categorical types for columns containing repeated string values.&lt;/li&gt;
&lt;li&gt;Process very large CSV files in chunks when necessary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, you can process a large CSV file in smaller portions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"large_sales.csv"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunksize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Process each chunk independently&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sales"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach can help when the entire dataset cannot comfortably fit into memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Working with pandas becomes much easier when you understand how it handles data types, missing values, indexing, and DataFrame operations.&lt;/p&gt;

&lt;p&gt;The six issues we've discussed are common, but they're also good reminders that debugging isn't just about fixing errors when they appear. It's about understanding your data and writing code that avoids unnecessary problems in the first place.&lt;/p&gt;

&lt;p&gt;Here are a few habits worth developing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Always inspect column names before merging DataFrames.&lt;/li&gt;
&lt;li&gt;Check data types before performing calculations.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;.loc&lt;/code&gt; and &lt;code&gt;.copy()&lt;/code&gt; appropriately when modifying DataFrames.&lt;/li&gt;
&lt;li&gt;Understand your missing values before filling or dropping them.&lt;/li&gt;
&lt;li&gt;Pay attention to index alignment when combining datasets.&lt;/li&gt;
&lt;li&gt;Monitor memory usage when working with large datasets.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most importantly, don't treat pandas errors as random obstacles. They're often telling you something useful about your data or the assumptions in your code.&lt;/p&gt;

&lt;p&gt;The more familiar you become with these behaviors, the less time you'll spend debugging and the more time you'll have to focus on what actually matters: extracting meaningful insights from your data.&lt;/p&gt;

</description>
      <category>data</category>
      <category>datascience</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Stop Building Data Science Notebooks. Start Building End-to-End Projects That Get You Hired.</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Sun, 08 Nov 2026 12:59:19 +0000</pubDate>
      <link>https://dev.to/mnyawade/stop-building-data-science-notebooks-start-building-end-to-end-projects-that-get-you-hired-435n</link>
      <guid>https://dev.to/mnyawade/stop-building-data-science-notebooks-start-building-end-to-end-projects-that-get-you-hired-435n</guid>
      <description>&lt;p&gt;If you're trying to break into data science, chances are you've already built a few machine learning projects.&lt;/p&gt;

&lt;p&gt;You downloaded a dataset, cleaned it, explored some columns, trained a model, evaluated its performance, and probably pushed the notebook to GitHub.&lt;/p&gt;

&lt;p&gt;And that's great. It's a start.&lt;/p&gt;

&lt;p&gt;But here's something worth thinking about: if a hiring manager opens your GitHub repository, what exactly are they supposed to learn about you?&lt;/p&gt;

&lt;p&gt;That you know how to call &lt;code&gt;.fit()&lt;/code&gt; on a scikit-learn model?&lt;/p&gt;

&lt;p&gt;Or that you can take an actual business problem, work with messy data, extract useful insights, build a predictive solution, and make that solution accessible to other people?&lt;/p&gt;

&lt;p&gt;There's a big difference between the two.&lt;/p&gt;

&lt;p&gt;A strong data science portfolio shouldn't just demonstrate that you understand machine learning algorithms. It should demonstrate that you understand the entire data science workflow.&lt;/p&gt;

&lt;p&gt;Let's look at what that actually means using a practical example: a &lt;strong&gt;DoorDash Delivery Duration Prediction project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwln11zsdowvtzjj87og4.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwln11zsdowvtzjj87og4.png" alt="Complete data science workflow from business problem to deployment" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Start With a Business Problem, Not a Dataset
&lt;/h2&gt;

&lt;p&gt;One mistake in many data science portfolios is that projects start with a dataset rather than a problem.&lt;/p&gt;

&lt;p&gt;Someone finds an interesting dataset on Kaggle, opens a Jupyter notebook, and immediately starts exploring the columns.&lt;/p&gt;

&lt;p&gt;There's nothing inherently wrong with that approach when you're learning. But if you're building a portfolio project for potential employers, you need to think differently.&lt;/p&gt;

&lt;p&gt;Start by asking:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What problem am I trying to solve, and why should anyone care about solving it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider the DoorDash delivery duration prediction project.&lt;/p&gt;

&lt;p&gt;Instead of presenting it as:&lt;/p&gt;

&lt;p&gt;"Predicting Delivery Time Using Machine Learning"&lt;/p&gt;

&lt;p&gt;Frame it around the actual business problem:&lt;/p&gt;

&lt;p&gt;"How Can We Predict Food Delivery Times to Improve Delivery Operations and Customer Experience?"&lt;/p&gt;

&lt;p&gt;See the difference?&lt;/p&gt;

&lt;p&gt;The second title communicates the business context. It tells the reader what you're trying to accomplish and why the project matters.&lt;/p&gt;

&lt;p&gt;Predicting delivery duration isn't just about achieving a low prediction error. More accurate estimates can help delivery platforms improve operational planning, set better customer expectations, and identify factors contributing to delivery delays.&lt;/p&gt;

&lt;p&gt;That's the kind of thinking that makes a portfolio project interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Think Beyond the Notebook: The Complete Data Science Workflow
&lt;/h2&gt;

&lt;p&gt;A production-oriented data science project involves much more than exploratory analysis and model training.&lt;/p&gt;

&lt;p&gt;For the DoorDash example, I'd break the workflow into nine stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Business problem definition&lt;/li&gt;
&lt;li&gt;Data retrieval using SQL&lt;/li&gt;
&lt;li&gt;Data cleaning and preparation using Python&lt;/li&gt;
&lt;li&gt;Exploratory Data Analysis (EDA)&lt;/li&gt;
&lt;li&gt;Feature engineering&lt;/li&gt;
&lt;li&gt;Model development and experimentation&lt;/li&gt;
&lt;li&gt;Model evaluation and selection&lt;/li&gt;
&lt;li&gt;Model deployment through an API&lt;/li&gt;
&lt;li&gt;Building an interactive dashboard&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each stage demonstrates a different skill.&lt;/p&gt;

&lt;p&gt;Let's go through them.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Define the Business Problem Clearly
&lt;/h2&gt;

&lt;p&gt;Before writing SQL or importing pandas, establish what you're trying to predict.&lt;/p&gt;

&lt;p&gt;In this case, the objective is to estimate how long an order will take to reach the customer.&lt;/p&gt;

&lt;p&gt;That sounds straightforward, but you need to define the problem more carefully.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;What information is available when an order is placed?&lt;/li&gt;
&lt;li&gt;Which variables could influence delivery duration?&lt;/li&gt;
&lt;li&gt;What exactly counts as delivery duration?&lt;/li&gt;
&lt;li&gt;How will prediction accuracy be measured?&lt;/li&gt;
&lt;li&gt;How could the business use these predictions?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions help establish the project's boundaries.&lt;/p&gt;

&lt;p&gt;They also prevent a common portfolio problem: building something technically interesting without explaining its practical purpose.&lt;/p&gt;

&lt;p&gt;A hiring manager should understand what your project does within the first few minutes of opening your repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Retrieve the Data Using SQL
&lt;/h2&gt;

&lt;p&gt;SQL is an important part of real-world data science workflows.&lt;/p&gt;

&lt;p&gt;Data rarely arrives in one perfectly structured CSV file. In many organizations, information is distributed across different database tables.&lt;/p&gt;

&lt;p&gt;For a delivery prediction problem, imagine having access to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Orders&lt;/li&gt;
&lt;li&gt;Restaurants&lt;/li&gt;
&lt;li&gt;Customers&lt;/li&gt;
&lt;li&gt;Delivery partners&lt;/li&gt;
&lt;li&gt;Delivery timestamps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may need to join these tables before creating your modeling dataset.&lt;/p&gt;

&lt;p&gt;SQL allows you to retrieve the information you need, filter irrelevant records, aggregate data, and perform preliminary transformations.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;restaurant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;order_timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;delivery_timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;delivery_timestamp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;order_timestamp&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;delivery_duration&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;delivery_timestamp&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simplified illustration, since timestamp subtraction and interval handling vary between database systems.&lt;/p&gt;

&lt;p&gt;The important point is that demonstrating SQL skills in your portfolio shows that you can work with structured data beyond the typical CSV-and-pandas workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Clean and Prepare the Data With Python
&lt;/h2&gt;

&lt;p&gt;Once the data has been retrieved, the next step is making sure it's suitable for analysis and modeling.&lt;/p&gt;

&lt;p&gt;Real-world datasets are rarely perfect.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Missing values&lt;/li&gt;
&lt;li&gt;Duplicate records&lt;/li&gt;
&lt;li&gt;Incorrect data types&lt;/li&gt;
&lt;li&gt;Invalid timestamps&lt;/li&gt;
&lt;li&gt;Outliers&lt;/li&gt;
&lt;li&gt;Inconsistent categorical values
Python libraries such as pandas and NumPy make this process much easier.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_data.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Remove duplicate records
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drop_duplicates&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Convert timestamp columns
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order_timestamp&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="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order_timestamp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_timestamp&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="nf"&gt;to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_timestamp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Calculate delivery duration in minutes
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_duration&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="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_timestamp&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;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order_timestamp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;total_seconds&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;

&lt;span class="c1"&gt;# Remove records with invalid target values
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_duration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;notna&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_duration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, cleaning decisions should depend on the actual dataset.&lt;/p&gt;

&lt;p&gt;For example, don't automatically remove every unusually long delivery. Some of those records might represent exactly the operational problems your model should learn to identify.&lt;/p&gt;

&lt;p&gt;Document your decisions and explain why you made them.&lt;/p&gt;

&lt;p&gt;That is more valuable than simply showing a notebook full of cleaning commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Explore the Data Before Building a Model
&lt;/h2&gt;

&lt;p&gt;Exploratory Data Analysis (EDA) is where you start understanding what's happening in your dataset.&lt;/p&gt;

&lt;p&gt;Before training any model, investigate the relationships between your variables and delivery duration.&lt;/p&gt;

&lt;p&gt;Some useful questions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do deliveries take longer during certain hours?&lt;/li&gt;
&lt;li&gt;Does distance influence delivery duration?&lt;/li&gt;
&lt;li&gt;Are some restaurants consistently associated with longer delivery times?&lt;/li&gt;
&lt;li&gt;Does weather affect delivery performance, if weather data is available?&lt;/li&gt;
&lt;li&gt;Are there noticeable differences between weekdays and weekends?
Visualizations can help answer these questions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, you could create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Histograms to understand delivery duration distributions.&lt;/li&gt;
&lt;li&gt;Box plots to identify potential outliers.&lt;/li&gt;
&lt;li&gt;Scatter plots to investigate relationships between distance and delivery duration.&lt;/li&gt;
&lt;li&gt;Heatmaps to examine numerical correlations.&lt;/li&gt;
&lt;li&gt;Time-based visualizations to identify delivery patterns.
One important thing: don't turn EDA into a collection of random charts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every visualization should help answer a question.&lt;/p&gt;

&lt;p&gt;A good portfolio project doesn't just show what the data looks like. It explains what you learned from looking at it.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Feature Engineering: Where Things Get Interesting
&lt;/h2&gt;

&lt;p&gt;Feature engineering is one of the most important parts of a predictive modeling project.&lt;/p&gt;

&lt;p&gt;Your raw dataset might contain useful information, but that doesn't necessarily mean the information is already in the best form for a machine learning algorithm.&lt;/p&gt;

&lt;p&gt;The goal is to transform existing data into features that help the model identify meaningful patterns.&lt;/p&gt;

&lt;p&gt;For a delivery duration prediction project, potential engineered features include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time-based features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hour of the day&lt;/li&gt;
&lt;li&gt;Day of the week&lt;/li&gt;
&lt;li&gt;Weekend indicator&lt;/li&gt;
&lt;li&gt;Peak-hour indicator
&lt;strong&gt;Distance-related features&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Delivery distance&lt;/li&gt;
&lt;li&gt;Distance categories&lt;/li&gt;
&lt;li&gt;Distance-to-estimated-duration ratios, where appropriate
&lt;strong&gt;Order-related features&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Number of items&lt;/li&gt;
&lt;li&gt;Total order value&lt;/li&gt;
&lt;li&gt;Number of distinct items
&lt;strong&gt;Operational features&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Restaurant preparation time, if available at prediction time&lt;/li&gt;
&lt;li&gt;Delivery partner availability, if available&lt;/li&gt;
&lt;li&gt;Historical restaurant delivery statistics
You can also investigate correlations between numerical features and the target variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, remember that correlation doesn't automatically imply causation.&lt;/p&gt;

&lt;p&gt;More importantly, make sure that every feature is actually available when the prediction is supposed to happen.&lt;/p&gt;

&lt;p&gt;For example, using the actual delivery timestamp to predict delivery duration would introduce target leakage.&lt;/p&gt;

&lt;p&gt;The model would effectively be using information that only becomes available after the delivery has already happened.&lt;/p&gt;

&lt;p&gt;That's not a useful prediction system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a Scikit-learn Pipeline&lt;/strong&gt;&lt;br&gt;
One way to make your workflow more reliable is to use a scikit-learn pipeline.&lt;/p&gt;

&lt;p&gt;Pipelines help combine preprocessing and model training into one consistent process.&lt;/p&gt;

&lt;p&gt;They also reduce the risk of data leakage when preprocessing steps are fitted correctly within the training workflow.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.compose&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ColumnTransformer&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.pipeline&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Pipeline&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OneHotEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.impute&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SimpleImputer&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.ensemble&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RandomForestRegressor&lt;/span&gt;

&lt;span class="n"&gt;numeric_features&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;distance&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;hour&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;number_of_items&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;categorical_features&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;day_of_week&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;restaurant_type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;numeric_pipeline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;imputer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SimpleImputer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;median&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scaler&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;categorical_pipeline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;imputer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SimpleImputer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;most_frequent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;encoder&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;OneHotEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handle_unknown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ignore&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;preprocessor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ColumnTransformer&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;numeric&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numeric_pipeline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numeric_features&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;categorical&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;categorical_pipeline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;categorical_features&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;model_pipeline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;preprocessor&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;preprocessor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;RandomForestRegressor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;n_estimators&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;
    &lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now preprocessing and model training can be handled together.&lt;/p&gt;

&lt;p&gt;This makes experimentation easier and helps ensure that the same transformations are applied during training and prediction.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Train Multiple Models Instead of Betting on One
&lt;/h2&gt;

&lt;p&gt;Once your features are ready, it's time to experiment with different models.&lt;/p&gt;

&lt;p&gt;Don't immediately jump to the most sophisticated algorithm you know.&lt;/p&gt;

&lt;p&gt;Start with a baseline.&lt;/p&gt;

&lt;p&gt;For a regression problem like delivery duration prediction, you could experiment with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dummy Regressor&lt;/li&gt;
&lt;li&gt;Linear Regression&lt;/li&gt;
&lt;li&gt;Decision Tree Regressor&lt;/li&gt;
&lt;li&gt;Random Forest Regressor&lt;/li&gt;
&lt;li&gt;Gradient Boosting&lt;/li&gt;
&lt;li&gt;XGBoost&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The baseline gives you a reference point.&lt;/p&gt;

&lt;p&gt;Without one, you might build a complicated model and celebrate its performance without knowing whether it actually improves on a simple prediction strategy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Evaluate Models Using RMSE&lt;/strong&gt;&lt;br&gt;
Since we're predicting a continuous value (delivery duration), regression metrics are appropriate.&lt;/p&gt;

&lt;p&gt;One useful metric is Root Mean Squared Error (RMSE).&lt;/p&gt;

&lt;p&gt;RMSE measures the typical magnitude of prediction errors while penalizing larger errors more heavily.&lt;/p&gt;

&lt;p&gt;Its unit is the same as the target variable.&lt;/p&gt;

&lt;p&gt;If delivery duration is measured in minutes, RMSE is also measured in minutes.&lt;/p&gt;

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

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

&lt;span class="n"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model_pipeline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;rmse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;root_mean_squared_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;predictions&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RMSE: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;rmse&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; minutes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can compare the performance of different models using a table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;RMSE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dummy Regressor&lt;/td&gt;
&lt;td&gt;To be measured&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linear Regression&lt;/td&gt;
&lt;td&gt;To be measured&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decision Tree&lt;/td&gt;
&lt;td&gt;To be measured&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Random Forest&lt;/td&gt;
&lt;td&gt;To be measured&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;XGBoost&lt;/td&gt;
&lt;td&gt;To be measured&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The values should come from your actual experiments.&lt;/p&gt;

&lt;p&gt;Don't invent performance numbers just to make the project look impressive.&lt;/p&gt;

&lt;p&gt;Also consider other metrics, such as MAE, because they can provide additional insight into prediction errors.&lt;/p&gt;

&lt;p&gt;The best model isn't necessarily the one with the lowest RMSE alone. Consider its complexity, inference speed, maintainability, and suitability for the intended use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Deploy the Model as an API
&lt;/h2&gt;

&lt;p&gt;Here's where your project starts becoming more than a machine learning experiment.&lt;/p&gt;

&lt;p&gt;A trained model sitting inside a Jupyter notebook isn't particularly useful to someone who wants to interact with it.&lt;/p&gt;

&lt;p&gt;You need a way for other applications to send input data and receive predictions.&lt;/p&gt;

&lt;p&gt;That's where an API comes in.&lt;/p&gt;

&lt;p&gt;You could use FastAPI to expose your trained model.&lt;/p&gt;

&lt;p&gt;A simplified example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseModel&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;joblib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delivery_model.pkl&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeliveryRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;number_of_items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;day_of_week&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;restaurant_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;

&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/predict&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict_delivery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DeliveryRequest&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;input_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;number_of_items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day_of_week&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;restaurant_type&lt;/span&gt;
    &lt;span class="p"&gt;]]&lt;/span&gt;

    &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_data&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;predicted_delivery_duration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an illustrative starting point. In a complete implementation, the API should use the exact feature schema and preprocessing pipeline expected by the trained model.&lt;/p&gt;

&lt;p&gt;You should also validate inputs, handle errors, and document the endpoint.&lt;/p&gt;

&lt;p&gt;Now your model can be accessed by an external application rather than being restricted to a notebook.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Build an Interactive Dashboard
&lt;/h2&gt;

&lt;p&gt;![Interactive food delivery duration prediction dashboard(&lt;a href="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/p5rgpfwt8ozvglocs0gl.png" rel="noopener noreferrer"&gt;https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/p5rgpfwt8ozvglocs0gl.png&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;The final piece is making the solution accessible to people who aren't necessarily developers.&lt;/p&gt;

&lt;p&gt;Imagine a simple dashboard where someone can enter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delivery distance&lt;/li&gt;
&lt;li&gt;Order time&lt;/li&gt;
&lt;li&gt;Number of items&lt;/li&gt;
&lt;li&gt;Restaurant category&lt;/li&gt;
&lt;li&gt;Day of the week
After submitting the information, the dashboard sends a request to your API and displays the estimated delivery duration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You could build this interface using Streamlit.&lt;/p&gt;

&lt;p&gt;The dashboard should make it easy to understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What information is required.&lt;/li&gt;
&lt;li&gt;What the predicted delivery duration is.&lt;/li&gt;
&lt;li&gt;How the prediction might be useful operationally.
This is a small but important distinction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You're no longer just showing someone that you trained a model.&lt;/p&gt;

&lt;p&gt;You're showing them how that model could fit into an actual business workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. What This Means for Your Data Science Portfolio
&lt;/h2&gt;

&lt;p&gt;Let's step back for a moment.&lt;/p&gt;

&lt;p&gt;Look at everything this single project demonstrates:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project Stage&lt;/th&gt;
&lt;th&gt;Skills Demonstrated&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Business problem definition&lt;/td&gt;
&lt;td&gt;Business understanding&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data retrieval&lt;/td&gt;
&lt;td&gt;SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data cleaning&lt;/td&gt;
&lt;td&gt;Python, pandas&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exploratory analysis&lt;/td&gt;
&lt;td&gt;Statistics, visualization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Feature engineering&lt;/td&gt;
&lt;td&gt;Domain knowledge, analytical thinking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Model development&lt;/td&gt;
&lt;td&gt;Machine learning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Model evaluation&lt;/td&gt;
&lt;td&gt;Statistical reasoning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API deployment&lt;/td&gt;
&lt;td&gt;Backend development&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interactive dashboard&lt;/td&gt;
&lt;td&gt;Data communication and product thinking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's a considerably stronger demonstration of your abilities than a notebook containing five different algorithms and a few evaluation metrics.&lt;/p&gt;

&lt;p&gt;It also gives you more interesting things to discuss during interviews.&lt;/p&gt;

&lt;p&gt;You can explain why you selected certain features, how you handled missing values, why one model performed better than another, and how you made the final solution accessible.&lt;/p&gt;

&lt;p&gt;Those conversations demonstrate problem-solving ability, not just familiarity with Python libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;If you're building a data science portfolio, don't focus exclusively on how many machine learning algorithms you can implement.&lt;/p&gt;

&lt;p&gt;Focus on how effectively you can solve problems.&lt;/p&gt;

&lt;p&gt;A good project should tell a complete story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What was the problem?&lt;/li&gt;
&lt;li&gt;Why did it matter?&lt;/li&gt;
&lt;li&gt;Where did the data come from?&lt;/li&gt;
&lt;li&gt;How did you prepare and understand it?&lt;/li&gt;
&lt;li&gt;What features did you engineer?&lt;/li&gt;
&lt;li&gt;Which models did you experiment with?&lt;/li&gt;
&lt;li&gt;How did you evaluate the results?&lt;/li&gt;
&lt;li&gt;How can someone actually use the solution?
You don't need ten unfinished notebooks in your GitHub repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few well-executed, end-to-end projects can communicate far more about your abilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build projects that demonstrate how you think, how you solve problems, and how you turn data into something useful.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's what makes a data science portfolio stand out.&lt;/p&gt;

</description>
      <category>career</category>
      <category>datascience</category>
      <category>portfolio</category>
    </item>
    <item>
      <title>How to Evaluate a Binary Logistic Regression Model: Beyond Accuracy</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Sun, 08 Nov 2026 11:58:07 +0000</pubDate>
      <link>https://dev.to/mnyawade/how-to-evaluate-a-binary-logistic-regression-model-beyond-accuracy-32bp</link>
      <guid>https://dev.to/mnyawade/how-to-evaluate-a-binary-logistic-regression-model-beyond-accuracy-32bp</guid>
      <description>&lt;p&gt;Your logistic regression model reports &lt;strong&gt;99% accuracy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Your team celebrates.&lt;/p&gt;

&lt;p&gt;Then the fraud department tells you it caught almost none of the fraudulent transactions.&lt;/p&gt;

&lt;p&gt;What happened?&lt;/p&gt;

&lt;p&gt;Nothing is necessarily wrong with the logistic regression algorithm. The problem is probably &lt;strong&gt;how you're evaluating it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Binary classification models don't just need a model score. They need the - right evaluation strategy - one that reflects what happens when the model makes a wrong prediction.&lt;/p&gt;

&lt;p&gt;In this article, we'll walk through the core classification metrics you should know when evaluating a binary logistic regression model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accuracy&lt;/li&gt;
&lt;li&gt;Precision&lt;/li&gt;
&lt;li&gt;Recall&lt;/li&gt;
&lt;li&gt;False positives and false negatives&lt;/li&gt;
&lt;li&gt;Threshold selection&lt;/li&gt;
&lt;li&gt;ROC curves&lt;/li&gt;
&lt;li&gt;ROC-AUC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also use &lt;code&gt;scikit-learn&lt;/code&gt; to calculate everything with real Python code.&lt;/p&gt;




&lt;h2&gt;
  
  
  First: Logistic Regression Doesn't Actually Predict "Yes" or "No"
&lt;/h2&gt;

&lt;p&gt;Despite its name, logistic regression produces a &lt;strong&gt;probability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, imagine we're predicting whether a credit-card transaction is fraudulent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction A → 0.03
Transaction B → 0.17
Transaction C → 0.82
Transaction D → 0.94
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These numbers represent the model's estimated probability that each transaction belongs to the positive class - in this case, &lt;code&gt;fraud&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To turn those probabilities into binary predictions, we choose a &lt;strong&gt;classification threshold&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The commonly used default is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;probability &amp;gt;= 0.50 → positive
probability &amp;lt;  0.50 → negative
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.82 → Fraud
0.94 → Fraud
0.17 → Not Fraud
0.03 → Not Fraud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is where things get interesting.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Flaw of the 0.5 Threshold
&lt;/h3&gt;

&lt;p&gt;The 50% threshold feels reasonable.&lt;/p&gt;

&lt;p&gt;After all, if the model says there's a greater than 50% chance of fraud, shouldn't we call it fraud?&lt;/p&gt;

&lt;p&gt;Not necessarily.&lt;/p&gt;

&lt;p&gt;Consider a medical screening system where missing a genuinely sick patient could have serious consequences.&lt;/p&gt;

&lt;p&gt;Suppose the model outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Patient A → 0.42 probability of disease
Patient B → 0.73 probability of disease
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a threshold of &lt;code&gt;0.50&lt;/code&gt;, Patient A is classified as healthy.&lt;/p&gt;

&lt;p&gt;But what if the cost of missing a sick patient is much higher than the cost of sending a healthy patient for additional testing?&lt;/p&gt;

&lt;p&gt;You might deliberately choose a threshold of &lt;code&gt;0.30&lt;/code&gt; instead.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.42 → Disease
0.73 → Disease
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You've changed the model's behavior &lt;strong&gt;without retraining the model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's a crucial idea:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The model produces probabilities. The threshold determines how those probabilities become decisions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why this matters in production
&lt;/h3&gt;

&lt;p&gt;Different applications have different costs for mistakes.&lt;/p&gt;

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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Application&lt;/th&gt;
&lt;th&gt;Potentially costly mistake&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fraud detection&lt;/td&gt;
&lt;td&gt;Missing actual fraud&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cancer screening&lt;/td&gt;
&lt;td&gt;Missing a disease&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spam filtering&lt;/td&gt;
&lt;td&gt;Blocking a legitimate email&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Loan approval&lt;/td&gt;
&lt;td&gt;Incorrectly rejecting a customer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Intrusion detection&lt;/td&gt;
&lt;td&gt;Missing an actual attack&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is no universal rule saying that &lt;code&gt;0.5&lt;/code&gt; is the correct threshold.&lt;/p&gt;

&lt;p&gt;The appropriate threshold depends on the &lt;strong&gt;business or operational cost of false positives and false negatives&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fef7x8zuj511xtw1gjg9f.jpeg" alt="Probability scores" width="799" height="436"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Accuracy: Useful, But Easy to Fool
&lt;/h2&gt;

&lt;p&gt;Accuracy is probably the first metric most developers reach for.&lt;/p&gt;

&lt;p&gt;The formula is straightforward:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;In other words:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Accuracy = (TP + TN) / (TP + TN + FP + FN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TP&lt;/strong&gt; = True Positives&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TN&lt;/strong&gt; = True Negatives&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FP&lt;/strong&gt; = False Positives&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FN&lt;/strong&gt; = False Negatives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It sounds sensible.&lt;/p&gt;

&lt;p&gt;But there's a major problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 99% Accuracy Trap
&lt;/h2&gt;

&lt;p&gt;Imagine a fraud-detection system processing &lt;strong&gt;10,000 transactions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Only 1% are fraudulent.&lt;/p&gt;

&lt;p&gt;That means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9,900 legitimate transactions
100 fraudulent transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine our model is completely useless.&lt;/p&gt;

&lt;p&gt;It predicts:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Not fraud" for every transaction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How accurate is it?&lt;/p&gt;

&lt;p&gt;It correctly identifies all 9,900 legitimate transactions.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Accuracy = 9,900 / 10,000
         = 99%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;99% accuracy.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a fraud detector that catches &lt;strong&gt;zero fraud&lt;/strong&gt;, that's obviously not a useful model.&lt;/p&gt;

&lt;p&gt;This is the classic &lt;strong&gt;class imbalance&lt;/strong&gt; problem.&lt;/p&gt;

&lt;p&gt;When one class vastly outnumbers the other, accuracy can hide poor performance on the minority class.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb98qzx405zw9rexi87sf.jpeg" 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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb98qzx405zw9rexi87sf.jpeg" alt="10,000 transactions showing 9,900 legitimate vs. 100 fraudulent, with an &amp;quot;always predict legitimate&amp;quot; model achieving 99% accuracy" width="799" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When should you use accuracy?
&lt;/h3&gt;

&lt;p&gt;Accuracy can still be useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classes are reasonably balanced.&lt;/li&gt;
&lt;li&gt;False positives and false negatives have similar consequences.&lt;/li&gt;
&lt;li&gt;You care about overall correctness.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But when your positive class is rare, don't evaluate the model using accuracy alone.&lt;/p&gt;

&lt;p&gt;Fraud, disease detection, anomaly detection, and security problems are classic examples where you should look deeper.&lt;/p&gt;




&lt;h2&gt;
  
  
  Precision vs. Recall
&lt;/h2&gt;

&lt;p&gt;Now we get to two of the most important metrics in classification:&lt;/p&gt;

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

&lt;p&gt;The easiest way to understand them is with a concrete example.&lt;/p&gt;

&lt;p&gt;Let's build a spam filter.&lt;/p&gt;

&lt;p&gt;Our model predicts whether an email is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Spam
Not spam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are four possible outcomes.&lt;/p&gt;

&lt;h3&gt;
  
  
  True Positive
&lt;/h3&gt;

&lt;p&gt;The email is actually spam, and the model says spam.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actual:    Spam
Predicted: Spam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good catch.&lt;/p&gt;

&lt;h4&gt;
  
  
  True Negative
&lt;/h4&gt;

&lt;p&gt;The email isn't spam, and the model says it isn't spam.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actual:    Not spam
Predicted: Not spam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also good.&lt;/p&gt;

&lt;h4&gt;
  
  
  False Positive
&lt;/h4&gt;

&lt;p&gt;The email isn't spam, but the model says it is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actual:    Not spam
Predicted: Spam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is potentially painful.&lt;/p&gt;

&lt;p&gt;Your model just threw a legitimate email into the spam folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  False Negative
&lt;/h4&gt;

&lt;p&gt;The email is spam, but the model says it isn't.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actual:    Spam
Predicted: Not spam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The spam gets through.&lt;/p&gt;

&lt;p&gt;These four outcomes form the foundation of classification metrics.&lt;/p&gt;




&lt;h2&gt;
  
  
  Precision: "When I Predict Positive, How Often Am I Right?"
&lt;/h2&gt;

&lt;p&gt;Precision focuses on the predictions your model labeled as positive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Precision = TP / (TP + FP)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In plain English:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Of everything the model predicted as positive, how much was actually positive?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose our spam filter flagged 100 emails as spam.&lt;/p&gt;

&lt;p&gt;Of those:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;90 were actually spam
10 were legitimate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Precision = 90 / (90 + 10)
          = 90%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A precision of 90% means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When the model says "spam," it's correct about 90% of the time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Precision becomes especially important when &lt;strong&gt;false positives are expensive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, imagine a fraud system automatically blocks transactions.&lt;/p&gt;

&lt;p&gt;A false positive means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This legitimate transaction is fraud."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Too many false positives could frustrate legitimate customers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Recall: "How Much of the Positive Class Did I Find?"
&lt;/h2&gt;

&lt;p&gt;Recall asks a different question.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Recall = TP / (TP + FN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In plain English:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Of all the actual positive cases, how many did the model successfully find?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose there are 100 spam emails in our dataset.&lt;/p&gt;

&lt;p&gt;Our model catches 90 of them.&lt;/p&gt;

&lt;p&gt;It misses 10.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Recall = 90 / (90 + 10)
       = 90%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the model has a recall of 90%.&lt;/p&gt;

&lt;p&gt;Recall is particularly important when &lt;strong&gt;false negatives are expensive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider a disease-screening system.&lt;/p&gt;

&lt;p&gt;A false negative means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The patient has the disease, but the model says they don't."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That could be far more consequential than sending some healthy patients for additional testing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Precision and Recall: The Seesaw
&lt;/h2&gt;

&lt;p&gt;Here's where classification gets interesting.&lt;/p&gt;

&lt;p&gt;You generally can't maximize precision and recall simultaneously just by moving the threshold around.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because changing the threshold changes which observations are classified as positive.&lt;/p&gt;

&lt;p&gt;Imagine our model produces these probabilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.12
0.21
0.34
0.46
0.58
0.67
0.79
0.91
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a threshold of &lt;code&gt;0.50&lt;/code&gt;, we classify four observations as positive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.58
0.67
0.79
0.91
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lower the threshold to &lt;code&gt;0.30&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Suddenly we classify six observations as positive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.34
0.46
0.58
0.67
0.79
0.91
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've become more aggressive about predicting the positive class.&lt;/p&gt;

&lt;p&gt;That can help us &lt;strong&gt;find more actual positives&lt;/strong&gt;, increasing recall.&lt;/p&gt;

&lt;p&gt;But it can also cause us to incorrectly label more negative examples as positive, reducing precision.&lt;/p&gt;

&lt;p&gt;Conversely, raising the threshold generally makes the model more conservative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Higher threshold
      ↓
Fewer positive predictions
      ↓
Usually fewer false positives
      ↓
Precision can increase

But...

      ↓
More actual positives may be missed
      ↓
Recall can decrease
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the precision-recall trade-off.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9t1anoi9qvjl8g17s7jx.jpeg" 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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9t1anoi9qvjl8g17s7jx.jpeg" alt="Two-panel diagram showing threshold movement and its effect on precision and recall" width="799" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The important takeaway isn't that one metric is "better."&lt;/p&gt;

&lt;p&gt;It's that &lt;strong&gt;the right balance depends on the problem you're solving&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Calculating the Metrics with scikit-learn
&lt;/h2&gt;

&lt;p&gt;Let's assume we've already trained our logistic regression model.&lt;/p&gt;

&lt;p&gt;A clean evaluation workflow looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Binary predictions using the default 0.5 threshold
&lt;/span&gt;&lt;span class="n"&gt;y_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Probability of the positive class
&lt;/span&gt;&lt;span class="n"&gt;y_proba&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)[:,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;accuracy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;precision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;recall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;roc_auc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Accuracy:  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;accuracy&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Precision: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;precision&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Recall:    &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recall&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ROC-AUC:   &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;roc_auc&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's an important detail here:&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="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we're passing &lt;strong&gt;probabilities&lt;/strong&gt;, not &lt;code&gt;y_pred&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's intentional.&lt;/p&gt;

&lt;p&gt;ROC-AUC evaluates the model across possible classification thresholds, so we want the model's continuous probability scores rather than predictions produced using one particular threshold.&lt;/p&gt;




&lt;h2&gt;
  
  
  ROC Curve: Evaluating More Than One Threshold
&lt;/h2&gt;

&lt;p&gt;If threshold selection is so important, how can we evaluate a model without committing to one threshold?&lt;/p&gt;

&lt;p&gt;Enter the &lt;strong&gt;ROC curve&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;ROC stands for:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Receiver Operating Characteristic&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A ROC curve plots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True Positive Rate
        vs.
False Positive Rate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The true positive rate is another name for &lt;strong&gt;recall&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TPR = TP / (TP + FN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The false positive rate is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FPR = FP / (FP + TN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the ROC curve essentially asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"As I change the classification threshold, how does my ability to catch positives change relative to how many negatives I incorrectly flag?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine starting with a very high threshold:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Only extremely confident predictions become positive.&lt;/p&gt;

&lt;p&gt;You'll generally have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Few false positives&lt;/li&gt;
&lt;li&gt;Few true positives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lower the threshold:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;More observations become positive.&lt;/p&gt;

&lt;p&gt;You'll potentially catch more actual positives, but you'll also flag more negative cases.&lt;/p&gt;

&lt;p&gt;Continue lowering it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.80 → 0.70 → 0.60 → 0.50 → 0.40 → ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each threshold produces a different point on the ROC curve.&lt;/p&gt;

&lt;h2&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3b5fdgjm94h15l0hj5nv.jpeg" alt="ROC curve with True Positive Rate on the Y-axis and False Positive Rate on the X-axis" width="799" height="436"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Plotting a ROC Curve in Python
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;scikit-learn&lt;/code&gt;, plotting a ROC curve is straightforward.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="n"&gt;RocCurveDisplay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_predictions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ROC Curve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;False Positive Rate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;True Positive Rate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also calculate the underlying values yourself:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="n"&gt;fpr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thresholds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;roc_curve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fpr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tpr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logistic Regression&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Random classifier&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;False Positive Rate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;True Positive Rate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ROC Curve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dashed diagonal represents a classifier with &lt;strong&gt;no discriminatory ability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The closer a useful ROC curve gets toward the &lt;strong&gt;top-left corner&lt;/strong&gt;, the more effectively the model separates the two classes.&lt;/p&gt;




&lt;h2&gt;
  
  
  ROC-AUC: One Number for the ROC Curve
&lt;/h2&gt;

&lt;p&gt;Looking at an entire curve is useful, but sometimes you need a single summary statistic.&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;ROC-AUC&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;AUC means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Area Under the Curve&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The score ranges from 0 to 1.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AUC ≈ 0.50
→ No better than random ranking

AUC &amp;gt; 0.50
→ Some ability to distinguish classes

AUC → 1.00
→ Very strong class separation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROC-AUC = 0.50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means the model has essentially no ability to distinguish the classes based on its ranking.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROC-AUC = 0.90
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;has substantially better ability to rank positive examples above negative examples.&lt;/p&gt;

&lt;p&gt;But there's an important nuance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ROC-AUC is not the same thing as accuracy.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And it isn't the same thing as precision or recall.&lt;/p&gt;

&lt;p&gt;A model can have a strong ROC-AUC while still requiring careful threshold selection for your actual application.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Does AUC Actually Mean?
&lt;/h2&gt;

&lt;p&gt;Here's a useful mental model.&lt;/p&gt;

&lt;p&gt;Take one randomly selected positive example and one randomly selected negative example.&lt;/p&gt;

&lt;p&gt;A ROC-AUC value can be interpreted as the probability that the model assigns a &lt;strong&gt;higher score to the positive example than to the negative example&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROC-AUC = 0.80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can think of that as roughly an 80% probability that the model will rank a randomly chosen positive case above a randomly chosen negative case.&lt;/p&gt;

&lt;p&gt;This makes ROC-AUC fundamentally about &lt;strong&gt;ranking and discrimination&lt;/strong&gt;, rather than one particular classification threshold.&lt;/p&gt;




&lt;h2&gt;
  
  
  Accuracy vs. Precision vs. Recall vs. ROC-AUC
&lt;/h2&gt;

&lt;p&gt;At this point, it's useful to keep the metrics straight.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Main question&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;How often is the model correct overall?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Precision&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;When the model predicts positive, how often is it right?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Recall&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Of all actual positives, how many did it find?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ROC-AUC&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;How well does the model distinguish positives from negatives across thresholds?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each metric tells you something different.&lt;/p&gt;

&lt;p&gt;That's why evaluating a classifier with a single number can be misleading.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Practical Evaluation Workflow
&lt;/h2&gt;

&lt;p&gt;When evaluating a binary logistic regression model, I recommend thinking in layers.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Start with the confusion matrix
&lt;/h3&gt;

&lt;p&gt;Before interpreting metrics, understand the raw counts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Predicted
              Negative  Positive
Actual
Negative         TN         FP

Positive         FN         TP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This immediately tells you what kinds of mistakes the model is making.&lt;/p&gt;

&lt;h2&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftcvhz2iccqu6lt80t2l8.jpeg" alt="Confusion matrix heatmap showing TP, TN, FP, and FN" width="799" height="436"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2. Calculate accuracy—but don't stop there
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Accuracy is useful context.&lt;/p&gt;

&lt;p&gt;But if your classes are heavily imbalanced, treat it cautiously.&lt;/p&gt;

&lt;p&gt;A 99% accuracy score could mean:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Model predicts the majority class every time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The confusion matrix helps you distinguish between those situations.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Check precision and recall
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;Is a false positive more problematic?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pay closer attention to &lt;strong&gt;precision&lt;/strong&gt;.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Is a false negative more problematic?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pay closer attention to &lt;strong&gt;recall&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In many real-world systems, you'll care about both.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Evaluate ROC-AUC using probabilities
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells you how well the model separates the classes across thresholds.&lt;/p&gt;

&lt;p&gt;It also avoids tying your evaluation to the arbitrary assumption that:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;






&lt;h3&gt;
  
  
  5. Choose the threshold deliberately
&lt;/h3&gt;

&lt;p&gt;Once you've established that the model has useful predictive power, choose a threshold based on the application's requirements.&lt;/p&gt;

&lt;p&gt;For example, you might decide:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;because missing positive cases is particularly costly.&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;because false alarms are expensive.&lt;/p&gt;

&lt;p&gt;The threshold should be treated as a &lt;strong&gt;modeling decision&lt;/strong&gt;, not an unquestioned default.&lt;/p&gt;




&lt;h2&gt;
  
  
  One More Important Detail: Don't Tune on the Test Set
&lt;/h2&gt;

&lt;p&gt;There's a common evaluation mistake worth calling out.&lt;/p&gt;

&lt;p&gt;Suppose you try 20 different thresholds on your test set and pick the threshold that gives you the best recall/precision combination.&lt;/p&gt;

&lt;p&gt;You've now used the test set to make a modeling decision.&lt;/p&gt;

&lt;p&gt;That's data leakage into your evaluation process.&lt;/p&gt;

&lt;p&gt;A better workflow is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Training data
     ↓
Train logistic regression
     ↓
Validation data
     ↓
Choose threshold
     ↓
Final test data
     ↓
Report final performance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using cross-validation, threshold selection can be incorporated into the validation process as well.&lt;/p&gt;

&lt;p&gt;The test set should remain your &lt;strong&gt;final, untouched estimate of generalization performance&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Production-Friendly Evaluation Function
&lt;/h2&gt;

&lt;p&gt;For repeated model evaluation, it's useful to wrap the metrics into a small function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evaluate_binary_classifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Evaluate a binary classifier at a specified threshold.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="n"&gt;y_proba&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_proba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)[:,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;y_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_proba&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;threshold&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;accuracy&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;accuracy_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;precision&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;recall&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;y_pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;roc_auc&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;roc_auc_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_proba&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;metrics&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;evaluate_binary_classifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The nice thing about this pattern is that you can evaluate different thresholds without retraining the model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;metrics&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;evaluate_binary_classifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That makes the precision/recall trade-off much easier to investigate.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Big Picture
&lt;/h2&gt;

&lt;p&gt;Evaluating a binary logistic regression model isn't about finding one magical metric.&lt;/p&gt;

&lt;p&gt;It's about understanding &lt;strong&gt;what the model gets right, what it gets wrong, and how those errors behave as the decision threshold changes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The mental model I'd keep is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Logistic Regression
                         │
                         ▼
                Probability scores
                         │
                         ▼
                 Choose threshold
                         │
                         ▼
              Binary predictions
                         │
          ┌──────────────┼──────────────┐
          ▼              ▼              ▼
       Accuracy       Precision       Recall

                         │
                         ▼
               Evaluate all thresholds
                         │
                         ▼
                     ROC-AUC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And remember the key lessons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don't blindly trust the 0.5 threshold.&lt;/strong&gt; It's a default, not a law.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accuracy can be dangerously misleading with imbalanced classes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precision measures the quality of positive predictions.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recall measures how many actual positives you successfully find.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;False positives and false negatives have different real-world costs.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Changing the threshold shifts the precision/recall trade-off.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ROC-AUC evaluates class separation across thresholds rather than at just one threshold.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use probability scores when calculating ROC-AUC.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Choose your production threshold using validation data and the actual cost of errors.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most important shift in thinking is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't ask, "What's my model's accuracy?" Ask, "What kinds of mistakes does my model make, and are those mistakes acceptable for the problem I'm solving?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the difference between simply training a classifier and actually evaluating one.&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>My Machine Learning Project Taught Me More About Software Engineering Than Machine Learning</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Fri, 31 Jul 2026 06:05:31 +0000</pubDate>
      <link>https://dev.to/mnyawade/my-machine-learning-project-taught-me-more-about-software-engineering-than-machine-learning-4895</link>
      <guid>https://dev.to/mnyawade/my-machine-learning-project-taught-me-more-about-software-engineering-than-machine-learning-4895</guid>
      <description>&lt;p&gt;When I started building my ad click prediction project, I thought the hard part would be training a machine learning model.&lt;/p&gt;

&lt;p&gt;I was wrong.&lt;/p&gt;

&lt;p&gt;Before I even got to preprocessing the data, I found myself dealing with project architecture, testing, imports, packaging, and debugging environment issues. Looking back, those lessons were probably more valuable than the model itself.&lt;/p&gt;

&lt;p&gt;Here's what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  It Started With a Notebook
&lt;/h2&gt;

&lt;p&gt;Like many data science projects, everything began inside a Jupyter notebook.&lt;/p&gt;

&lt;p&gt;Loading the dataset was as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data/raw/Advertising.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It worked perfectly.&lt;/p&gt;

&lt;p&gt;At first glance, there wasn't much reason to change it. The dataset loaded correctly, there were no missing values, no duplicates, and I could immediately start exploring the data.&lt;/p&gt;

&lt;p&gt;But there was one problem.&lt;/p&gt;

&lt;p&gt;The notebook knew how to load the data.&lt;/p&gt;

&lt;p&gt;Nothing else did.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separating Exploration From Reusable Code
&lt;/h2&gt;

&lt;p&gt;Instead of leaving everything inside the notebook, I moved the loading logic into a dedicated module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
└── data/
    └── load_data.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The notebook changed from doing the work itself to simply calling a function:&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;src.data.load_data&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_raw_data&lt;/span&gt;

&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;load_raw_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data/raw/Advertising.csv&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It doesn't feel like a huge improvement until you think about the future.&lt;/p&gt;

&lt;p&gt;Now every notebook, script, or training pipeline can reuse the same code instead of copying and pasting it.&lt;/p&gt;

&lt;p&gt;Small change.&lt;/p&gt;

&lt;p&gt;Big difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defensive Programming Matters
&lt;/h2&gt;

&lt;p&gt;The next improvement wasn't about functionality.&lt;/p&gt;

&lt;p&gt;It was about failure.&lt;/p&gt;

&lt;p&gt;Instead of relying on &lt;code&gt;pandas.read_csv()&lt;/code&gt; to throw an error if the dataset disappeared, I added an explicit check that raises a meaningful &lt;code&gt;FileNotFoundError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That seemed unnecessary at first.&lt;/p&gt;

&lt;p&gt;But then I realized something.&lt;/p&gt;

&lt;p&gt;Good software doesn't just work when everything is correct.&lt;/p&gt;

&lt;p&gt;It also fails in ways that are easy to understand.&lt;/p&gt;

&lt;p&gt;A descriptive error today can save a lot of debugging tomorrow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing My First Test
&lt;/h2&gt;

&lt;p&gt;The next step was introducing automated testing.&lt;/p&gt;

&lt;p&gt;The first test was intentionally simple.&lt;/p&gt;

&lt;p&gt;It verified that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the dataset loads successfully,&lt;/li&gt;
&lt;li&gt;it has the expected number of rows and columns,&lt;/li&gt;
&lt;li&gt;the target column exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nothing fancy.&lt;/p&gt;

&lt;p&gt;Still, it was the first time the project had a safety net.&lt;/p&gt;

&lt;p&gt;Or so I thought.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Test Didn't Fail Because of My Code
&lt;/h2&gt;

&lt;p&gt;Instead, it failed with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ModuleNotFoundError: No module named 'src'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first I assumed I had made a mistake.&lt;/p&gt;

&lt;p&gt;I hadn't.&lt;/p&gt;

&lt;p&gt;The notebook worked because I had manually modified &lt;code&gt;sys.path&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pytest&lt;/code&gt;, however, starts a fresh Python process.&lt;/p&gt;

&lt;p&gt;It had no idea where &lt;code&gt;src&lt;/code&gt; lived.&lt;/p&gt;

&lt;p&gt;The interesting part wasn't the error.&lt;/p&gt;

&lt;p&gt;It was what the error revealed.&lt;/p&gt;

&lt;p&gt;My project structure wasn't as solid as I thought.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Best Practice" Didn't Work
&lt;/h2&gt;

&lt;p&gt;The first idea was to package the project properly using &lt;code&gt;pyproject.toml&lt;/code&gt; and install it in editable mode.&lt;/p&gt;

&lt;p&gt;That's the modern Python approach and, in many environments, it's absolutely the right solution.&lt;/p&gt;

&lt;p&gt;Except...&lt;/p&gt;

&lt;p&gt;My development environment had other ideas.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;didn't work because of the packaging tools available in the hosted Anaconda environment.&lt;/p&gt;

&lt;p&gt;For a moment, it felt like I'd gone down the wrong path.&lt;/p&gt;

&lt;p&gt;But that turned into another lesson.&lt;/p&gt;

&lt;p&gt;Sometimes the technically "best" solution isn't the practical one for your environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pragmatism Beats Perfection
&lt;/h2&gt;

&lt;p&gt;Instead of spending hours fighting the environment, I switched to a simpler approach.&lt;/p&gt;

&lt;p&gt;Adding a small &lt;code&gt;tests/conftest.py&lt;/code&gt; file allowed &lt;code&gt;pytest&lt;/code&gt; to locate the project correctly.&lt;/p&gt;

&lt;p&gt;One command later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Success.&lt;/p&gt;

&lt;p&gt;More importantly, I learned something that applies far beyond Python.&lt;/p&gt;

&lt;p&gt;Engineering isn't about blindly following best practices.&lt;/p&gt;

&lt;p&gt;It's about choosing the right level of complexity for the problem you're solving.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Milestone
&lt;/h2&gt;

&lt;p&gt;By the end of this stage, I hadn't trained a model.&lt;/p&gt;

&lt;p&gt;I hadn't engineered features.&lt;/p&gt;

&lt;p&gt;I hadn't even started preprocessing.&lt;/p&gt;

&lt;p&gt;Yet the project had already changed dramatically.&lt;/p&gt;

&lt;p&gt;Instead of a single notebook, I now had:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a reusable data loading module,&lt;/li&gt;
&lt;li&gt;a notebook that consumes reusable code instead of owning it,&lt;/li&gt;
&lt;li&gt;automated tests,&lt;/li&gt;
&lt;li&gt;a working pytest configuration,&lt;/li&gt;
&lt;li&gt;clearer error handling,&lt;/li&gt;
&lt;li&gt;a cleaner project structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That feels much closer to a production codebase than a collection of experiments.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'll Do Next
&lt;/h2&gt;

&lt;p&gt;The next stage is building a reusable preprocessing pipeline.&lt;/p&gt;

&lt;p&gt;Rather than cleaning data directly inside the notebook, I'll move that logic into &lt;code&gt;src/data/preprocess.py&lt;/code&gt;, where every transformation can be tested and reused.&lt;/p&gt;

&lt;p&gt;The notebook will become a client of the pipeline instead of the pipeline itself.&lt;/p&gt;

&lt;p&gt;I'm beginning to appreciate that building a machine learning project isn't just about choosing algorithms.&lt;/p&gt;

&lt;p&gt;It's about building software that happens to use machine learning.&lt;/p&gt;

&lt;p&gt;And honestly, that's turning out to be the most valuable lesson so far.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>I Thought My Machine Learning Project Was Finished. Then I Asked One Question.</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Mon, 13 Jul 2026 14:15:28 +0000</pubDate>
      <link>https://dev.to/mnyawade/i-thought-my-machine-learning-project-was-finished-then-i-asked-one-question-496o</link>
      <guid>https://dev.to/mnyawade/i-thought-my-machine-learning-project-was-finished-then-i-asked-one-question-496o</guid>
      <description>&lt;p&gt;When I completed my &lt;a href="https://github.com/Michael-Nyawade/customer-segmentation-rfm" rel="noopener noreferrer"&gt;customer segmentation project&lt;/a&gt;, I felt proud of what I had built.&lt;/p&gt;

&lt;p&gt;I had worked with nearly half a million retail transactions from the UCI Online Retail dataset. After cleaning the data, I engineered Recency, Frequency, and Monetary (RFM) features, applied K-Means clustering, experimented with different numbers of clusters using the Elbow and Silhouette methods, and visualized the results using PCA.&lt;/p&gt;

&lt;p&gt;The outcome made sense.&lt;/p&gt;

&lt;p&gt;I could identify VIP customers, loyal high-spenders, mid-value customers, and customers who were at risk of becoming inactive. Better yet, I could explain how each segment could help a business improve marketing, customer retention, and loyalty programs.&lt;/p&gt;

&lt;p&gt;I wrote a detailed README, pushed everything to GitHub, and considered the project complete.&lt;/p&gt;

&lt;p&gt;Or so I thought.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Question Changed How I Saw the Project
&lt;/h2&gt;

&lt;p&gt;A few months later, I looked at the repository again and asked myself a question I hadn't considered before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If I gave this project to a business today, could they actually use it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer was no.&lt;/p&gt;

&lt;p&gt;Not because the model was inaccurate.&lt;/p&gt;

&lt;p&gt;Not because the clustering was poor.&lt;/p&gt;

&lt;p&gt;Simply because everything lived inside a Jupyter notebook.&lt;/p&gt;

&lt;p&gt;The only person who could use the project was the person who built it.&lt;/p&gt;

&lt;p&gt;Me.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Notebook Was Never the Product
&lt;/h2&gt;

&lt;p&gt;I think this is a stage many aspiring data scientists go through.&lt;/p&gt;

&lt;p&gt;We spend hours cleaning messy datasets, engineering features, comparing models, tuning hyperparameters, and producing visualizations. Once the metrics look good and the notebook runs from top to bottom without errors, it feels like the project is finished.&lt;/p&gt;

&lt;p&gt;That's exactly how I approached my customer segmentation project.&lt;/p&gt;

&lt;p&gt;From a machine learning perspective, I had done what I set out to do.&lt;/p&gt;

&lt;p&gt;From a user's perspective, though, nothing had been built.&lt;/p&gt;

&lt;p&gt;Imagine you're the owner of a retail business.&lt;/p&gt;

&lt;p&gt;You don't want to open a notebook.&lt;/p&gt;

&lt;p&gt;You don't want to install Python or understand K-Means clustering.&lt;/p&gt;

&lt;p&gt;You probably don't even care how the customers are segmented.&lt;/p&gt;

&lt;p&gt;You simply want to upload your latest sales data and receive a list of VIP customers, loyal customers, and customers who might need a re-engagement campaign.&lt;/p&gt;

&lt;p&gt;My project couldn't do that.&lt;/p&gt;

&lt;p&gt;It produced useful insights, but only if someone opened the notebook and reran the analysis.&lt;/p&gt;

&lt;p&gt;That realization forced me to rethink what "finished" really means.&lt;/p&gt;

&lt;h2&gt;
  
  
  I Discovered a Gap in My Learning
&lt;/h2&gt;

&lt;p&gt;For a long time, I believed the next step in improving as a data scientist was learning another algorithm.&lt;/p&gt;

&lt;p&gt;Maybe I should study gradient boosting more deeply.&lt;/p&gt;

&lt;p&gt;Maybe I should explore deep learning.&lt;/p&gt;

&lt;p&gt;Maybe I should improve my feature engineering.&lt;/p&gt;

&lt;p&gt;Those are valuable skills, but they weren't the gap I had uncovered.&lt;/p&gt;

&lt;p&gt;The real gap was everything that happens after the notebook.&lt;/p&gt;

&lt;p&gt;How do you package a model?&lt;/p&gt;

&lt;p&gt;How do you let someone interact with it without touching your code?&lt;/p&gt;

&lt;p&gt;How do you turn analysis into something another person can actually use?&lt;/p&gt;

&lt;p&gt;Those weren't questions I had been asking.&lt;/p&gt;

&lt;p&gt;Now they seem impossible to ignore.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Project Isn't Finished Just Because the Analysis Is
&lt;/h2&gt;

&lt;p&gt;Looking back, I don't think my customer segmentation project was a failure.&lt;/p&gt;

&lt;p&gt;Far from it.&lt;/p&gt;

&lt;p&gt;It taught me how to work with real-world transactional data, engineer meaningful business features, apply unsupervised learning techniques, and communicate insights through visualizations.&lt;/p&gt;

&lt;p&gt;Those are valuable skills.&lt;/p&gt;

&lt;p&gt;But I've stopped thinking of the notebook as the final product.&lt;/p&gt;

&lt;p&gt;I now see it as a prototype.&lt;/p&gt;

&lt;p&gt;A place where ideas are tested before they become something useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Comes Next
&lt;/h2&gt;

&lt;p&gt;I'm not writing this as someone who has mastered deployment or MLOps.&lt;/p&gt;

&lt;p&gt;I haven't.&lt;/p&gt;

&lt;p&gt;In fact, that's exactly why I'm writing this.&lt;/p&gt;

&lt;p&gt;I've realized that every machine learning project I've completed has stopped at the notebook stage, and I don't want the next one to end there.&lt;/p&gt;

&lt;p&gt;My next challenge isn't building a model with a slightly better score.&lt;/p&gt;

&lt;p&gt;It's learning how to take one of the projects I've already built and make it accessible to someone who has never written a line of Python.&lt;/p&gt;

&lt;p&gt;Maybe that means exposing it through an API.&lt;/p&gt;

&lt;p&gt;Maybe it becomes a simple web application.&lt;/p&gt;

&lt;p&gt;Maybe it evolves into something I haven't even considered yet.&lt;/p&gt;

&lt;p&gt;I don't know exactly what that journey will look like.&lt;/p&gt;

&lt;p&gt;But I know where it starts.&lt;/p&gt;

&lt;p&gt;It starts by recognizing that a notebook is an excellent place to build a model.&lt;/p&gt;

&lt;p&gt;It just isn't always where the project should end.&lt;/p&gt;




&lt;p&gt;Have you ever looked back at one of your machine learning projects and realized it wasn't as "finished" as you first thought? I'd love to hear how you've approached moving beyond the notebook—or whether you're figuring it out too.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>I Kept Recreating My Django Superuser Every Time I Switched Computers — Until I Finally Understood What Was Happening</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Tue, 02 Jun 2026 12:24:36 +0000</pubDate>
      <link>https://dev.to/mnyawade/i-kept-recreating-my-django-superuser-every-time-i-switched-computers-until-i-finally-understood-2col</link>
      <guid>https://dev.to/mnyawade/i-kept-recreating-my-django-superuser-every-time-i-switched-computers-until-i-finally-understood-2col</guid>
      <description>&lt;p&gt;I’ve been building a Django project called &lt;strong&gt;SmartDuka&lt;/strong&gt;, a smart shop management system for small retailers and shop owners. It handles inventory tracking, low stock alerts, dead stock detection, sales tracking, credit sales, and daily business insights.&lt;/p&gt;

&lt;p&gt;This is also my first serious Django project.&lt;/p&gt;

&lt;p&gt;And for a while, every time I switched from my home laptop to my work laptop, I went through the exact same process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone
python &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
python manage.py migrate
python manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I would open the admin panel and start entering products, customers, and sample sales all over again.&lt;/p&gt;

&lt;p&gt;Every. Single. Time.&lt;/p&gt;

&lt;p&gt;At first, I thought this was just part of working with Django. But eventually I realized I wasn’t actually understanding the relationship between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;my code&lt;/li&gt;
&lt;li&gt;migrations&lt;/li&gt;
&lt;li&gt;and the database itself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article is basically the explanation I wish someone had given me earlier.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Mistake I Wasn’t Really Making
&lt;/h2&gt;

&lt;p&gt;To be fair, I &lt;em&gt;was&lt;/em&gt; doing one important thing correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.sqlite3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;was inside my &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For a good reason SQLite databases are local files and usually shouldn’t be committed to Git.&lt;/p&gt;

&lt;p&gt;So technically my workflow looked “correct.”&lt;/p&gt;

&lt;p&gt;The problem was this:&lt;/p&gt;

&lt;p&gt;I assumed migrations would somehow preserve my data across machines.&lt;/p&gt;

&lt;p&gt;They don’t.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Django Migrations Actually Do
&lt;/h2&gt;

&lt;p&gt;This part finally clicked for me after some frustration.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Django recreates the &lt;strong&gt;database structure&lt;/strong&gt;, not the actual records inside it.&lt;/p&gt;

&lt;p&gt;So migrations restore things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tables&lt;/li&gt;
&lt;li&gt;columns&lt;/li&gt;
&lt;li&gt;relationships&lt;/li&gt;
&lt;li&gt;constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But they do &lt;em&gt;not&lt;/em&gt; restore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;superusers&lt;/li&gt;
&lt;li&gt;products&lt;/li&gt;
&lt;li&gt;inventory&lt;/li&gt;
&lt;li&gt;sales records&lt;/li&gt;
&lt;li&gt;customers&lt;/li&gt;
&lt;li&gt;suppliers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That data lives inside the database itself.&lt;/p&gt;

&lt;p&gt;And in SQLite, that database is literally one file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.sqlite3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once I understood that, everything made sense.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why I Had to Keep Recreating My Superuser
&lt;/h2&gt;

&lt;p&gt;Because every new machine was getting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fresh empty database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After migrations, Django had the tables it needed, but there were no actual records inside them yet.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;no users existed&lt;/li&gt;
&lt;li&gt;no inventory existed&lt;/li&gt;
&lt;li&gt;no sales existed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I wasn’t losing my code.&lt;/p&gt;

&lt;p&gt;I was losing my database state.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Different Solutions I Considered
&lt;/h2&gt;

&lt;p&gt;Once I understood the problem properly, I realized there are several ways developers approach this.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Recreate Everything Every Time
&lt;/h3&gt;

&lt;p&gt;This was my original workflow.&lt;/p&gt;

&lt;p&gt;It technically works, but only for tiny projects.&lt;/p&gt;

&lt;p&gt;Once your app starts accumulating meaningful data, this becomes painful very quickly.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Commit &lt;code&gt;db.sqlite3&lt;/code&gt; to Git
&lt;/h3&gt;

&lt;p&gt;I considered this briefly.&lt;/p&gt;

&lt;p&gt;It solves the syncing issue, but introduces other problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;binary file conflicts&lt;/li&gt;
&lt;li&gt;messy Git history&lt;/li&gt;
&lt;li&gt;merge problems&lt;/li&gt;
&lt;li&gt;potential security risks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a real project, this didn’t feel right.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Use Fixtures
&lt;/h3&gt;

&lt;p&gt;Django supports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py dumpdata
python manage.py loaddata
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which export and import data as JSON fixtures.&lt;/p&gt;

&lt;p&gt;This is actually pretty useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;demo data&lt;/li&gt;
&lt;li&gt;seed data&lt;/li&gt;
&lt;li&gt;lookup tables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But for constantly changing business data like inventory and sales, it felt awkward as the main solution.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Copy &lt;code&gt;db.sqlite3&lt;/code&gt; Between Machines
&lt;/h3&gt;

&lt;p&gt;This ended up being the most practical short-term solution for me.&lt;/p&gt;

&lt;p&gt;Instead of recreating everything, I simply keep the latest SQLite database and transfer it between machines manually.&lt;/p&gt;

&lt;p&gt;That preserves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;my superuser&lt;/li&gt;
&lt;li&gt;inventory records&lt;/li&gt;
&lt;li&gt;customers&lt;/li&gt;
&lt;li&gt;sales history&lt;/li&gt;
&lt;li&gt;everything else&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The workflow now looks more like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull
pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;span class="c"&gt;# copy latest db.sqlite3&lt;/span&gt;
python manage.py migrate
python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much better.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Important Detail About Migrations
&lt;/h2&gt;

&lt;p&gt;One thing I learned here:&lt;/p&gt;

&lt;p&gt;The correct order is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;copy &lt;code&gt;db.sqlite3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;then run migrations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Not the other way around.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because migrations should update your &lt;em&gt;existing&lt;/em&gt; database schema if new model changes were added.&lt;/p&gt;

&lt;p&gt;For example, if yesterday my &lt;code&gt;Product&lt;/code&gt; model had:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and today I added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;updates the copied database safely.&lt;/p&gt;

&lt;p&gt;That’s exactly what migrations are designed for.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bigger Realization
&lt;/h2&gt;

&lt;p&gt;At some point, I realized SmartDuka started behaving like a real system.&lt;/p&gt;

&lt;p&gt;And once that happens, things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;persistence&lt;/li&gt;
&lt;li&gt;synchronization&lt;/li&gt;
&lt;li&gt;database strategy&lt;/li&gt;
&lt;li&gt;backups&lt;/li&gt;
&lt;li&gt;scalability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;suddenly become important.&lt;/p&gt;

&lt;p&gt;That transition caught me by surprise a little.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I’m Planning Next
&lt;/h2&gt;

&lt;p&gt;Right now I’m still using SQLite during development because it’s simple and fast.&lt;/p&gt;

&lt;p&gt;But I can already see that eventually I’ll need to move toward PostgreSQL, especially because SmartDuka deals with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;transactions&lt;/li&gt;
&lt;li&gt;inventory&lt;/li&gt;
&lt;li&gt;analytics&lt;/li&gt;
&lt;li&gt;credit management&lt;/li&gt;
&lt;li&gt;reporting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At that point, having a shared database instead of manually copying SQLite files will make much more sense.&lt;/p&gt;

&lt;p&gt;But honestly, understanding &lt;em&gt;why&lt;/em&gt; I was recreating my superuser every time was already a huge learning moment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;One thing I’m learning while building real projects is that software development isn’t only about writing features.&lt;/p&gt;

&lt;p&gt;Sometimes the biggest lessons come from workflow problems.&lt;/p&gt;

&lt;p&gt;This was one of them for me.&lt;/p&gt;

&lt;p&gt;If you’re new to Django and you’ve ever wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Why do I keep losing my data even after running migrations?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;hopefully this article saves you a few hours of confusion.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why Adding a Unique SKU Field Broke My Django Migration</title>
      <dc:creator>Michael Nyawade</dc:creator>
      <pubDate>Sun, 10 May 2026 08:05:18 +0000</pubDate>
      <link>https://dev.to/mnyawade/why-adding-a-unique-sku-field-broke-my-django-migration-3bk8</link>
      <guid>https://dev.to/mnyawade/why-adding-a-unique-sku-field-broke-my-django-migration-3bk8</guid>
      <description>&lt;p&gt;In building projects using Django, one thing that I have realized about database migrations is that things may appear simple until the moment one alters an existing model.&lt;/p&gt;

&lt;p&gt;For example, in developing an inventory management app for my project recently, I experienced a migration problem, which, although small, helped me understand how migrations work within Django and in particular, when adding unique constraints to existing databases.&lt;/p&gt;

&lt;p&gt;I would like to document this problem since someone else may have experienced the same thing.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Initial Inventory Model
&lt;/h2&gt;

&lt;p&gt;At the beginning, my &lt;code&gt;Product&lt;/code&gt; model was simple.&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;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;buying_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_digits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal_places&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;selling_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_digits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal_places&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stock_quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PositiveIntegerField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;low_stock_threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PositiveIntegerField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto_now_add&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the model, I generated and applied migrations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py makemigrations
python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also registered the model in &lt;code&gt;admin.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;


&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything worked perfectly.&lt;/p&gt;

&lt;p&gt;I created a superuser, logged into the Django admin panel, tested the model, and there were no issues.&lt;/p&gt;




&lt;h2&gt;
  
  
  Updating the Inventory App
&lt;/h2&gt;

&lt;p&gt;As the project evolved, I realized the inventory system needed product categories and SKU support.&lt;/p&gt;

&lt;p&gt;So I updated &lt;code&gt;models.py&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;verbose_name_plural&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Categories&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;on_delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;related_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;products&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&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;buying_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_digits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal_places&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;selling_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_digits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal_places&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;stock_quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PositiveIntegerField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;low_stock_threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PositiveIntegerField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto_now_add&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;profit_per_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selling_price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buying_price&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_low_stock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock_quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;low_stock_threshold&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also updated &lt;code&gt;admin.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;


&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I created new migrations.&lt;/p&gt;

&lt;p&gt;At this point, I expected everything to work normally.&lt;/p&gt;

&lt;p&gt;Instead, Django threw a migration error.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Error I Encountered
&lt;/h2&gt;

&lt;p&gt;When I ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I got this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django.db.utils.IntegrityError:
UNIQUE constraint failed: new__inventory_product.sku
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, I was confused.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;sku&lt;/code&gt; field looked correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So why was the migration failing?&lt;/p&gt;




&lt;h2&gt;
  
  
  What Was Actually Happening
&lt;/h2&gt;

&lt;p&gt;After looking into it more carefully, I realized the issue was related to existing database records.&lt;/p&gt;

&lt;p&gt;Before adding the &lt;code&gt;sku&lt;/code&gt; field, the &lt;code&gt;Product&lt;/code&gt; table already existed in the database.&lt;/p&gt;

&lt;p&gt;When Django attempted to apply the migration, SQLite internally tried to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new version of the table&lt;/li&gt;
&lt;li&gt;Copy the old data into the new table&lt;/li&gt;
&lt;li&gt;Apply the new constraints&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The problem was that the new &lt;code&gt;sku&lt;/code&gt; field had &lt;code&gt;unique=True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Existing records in the database didn’t have SKU values yet.&lt;/p&gt;

&lt;p&gt;As SQLite tried moving data into the new table structure, duplicate empty values conflicted with the unique constraint.&lt;/p&gt;

&lt;p&gt;That’s why the migration failed.&lt;/p&gt;




&lt;h2&gt;
  
  
  How I Fixed It
&lt;/h2&gt;

&lt;p&gt;Since I was still in the development stage of the project, I decided to reset the database completely.&lt;/p&gt;

&lt;p&gt;Here’s what I did:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Stopped the Development Server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;CTRL + C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Deleted the SQLite Database
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.sqlite3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Deleted Old Migration Files
&lt;/h3&gt;

&lt;p&gt;I deleted the migration files inside the app’s &lt;code&gt;migrations&lt;/code&gt; folder, except:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;__init__.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Recreated Migrations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py makemigrations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Applied Migrations Again
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Recreated the Superuser
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, everything worked correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;This experience taught me an important lesson:&lt;/p&gt;

&lt;p&gt;Adding unique fields to existing Django models can break migrations if the database already contains records.&lt;/p&gt;

&lt;p&gt;Even though the code itself may look correct, the migration process also depends on the current database state.&lt;/p&gt;

&lt;p&gt;I also learned that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resetting the database is acceptable during early development&lt;/li&gt;
&lt;li&gt;It is not a good solution for production systems&lt;/li&gt;
&lt;li&gt;Database schema changes should be planned carefully&lt;/li&gt;
&lt;li&gt;Unique constraints require special attention when existing data is involved&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  A Better Production Approach
&lt;/h2&gt;

&lt;p&gt;If this were a production project, deleting the database would obviously be a bad idea.&lt;/p&gt;

&lt;p&gt;A safer approach would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add the field without &lt;code&gt;unique=True&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Populate existing rows with unique values&lt;/li&gt;
&lt;li&gt;Then apply the unique constraint in a later migration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That prevents conflicts with existing records.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This wasn’t the biggest bug I’ve encountered in Django, but it was one of those moments that helped me understand migrations better.&lt;/p&gt;

&lt;p&gt;Sometimes small errors teach the most useful lessons.&lt;/p&gt;

&lt;p&gt;If you’re learning Django and run into migration issues after modifying models, don’t panic immediately. In many cases, the problem is related to how existing database records interact with the new schema changes.&lt;/p&gt;

&lt;p&gt;Hopefully this saves someone else a few hours of confusion.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>database</category>
      <category>django</category>
      <category>python</category>
    </item>
  </channel>
</rss>
