<?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: Venkata Geetarth</title>
    <description>The latest articles on DEV Community by Venkata Geetarth (@venkata_geetarth_e395c9c6).</description>
    <link>https://dev.to/venkata_geetarth_e395c9c6</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%2F4158743%2Fa914bdab-0fd5-4106-9945-f4a82e32dcbf.png</url>
      <title>DEV Community: Venkata Geetarth</title>
      <link>https://dev.to/venkata_geetarth_e395c9c6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/venkata_geetarth_e395c9c6"/>
    <language>en</language>
    <item>
      <title>Tuning a Churn-Prediction Random Forest with GASearchCV</title>
      <dc:creator>Venkata Geetarth</dc:creator>
      <pubDate>Sat, 03 Oct 2026 01:49:26 +0000</pubDate>
      <link>https://dev.to/venkata_geetarth_e395c9c6/tuning-a-churn-prediction-random-forest-with-gasearchcv-1c6k</link>
      <guid>https://dev.to/venkata_geetarth_e395c9c6/tuning-a-churn-prediction-random-forest-with-gasearchcv-1c6k</guid>
      <description>&lt;p&gt;&lt;em&gt;What happened when I let a genetic algorithm pick my model's settings instead of guessing them myself.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem I was trying to solve
&lt;/h2&gt;

&lt;p&gt;I built a model that predicts whether a phone company customer is going to cancel their plan ("churn") or stick around. I used real data from IBM — about 7,043 customers, and only about 1 in 4 of them actually churned.&lt;/p&gt;

&lt;p&gt;Here's the thing about this kind of problem: the model can mess up in two different ways, and they are NOT equally bad.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt; Imagine the model says "this customer is totally fine, they're staying" — but they actually cancel next month. That's a real customer, and real money, walking out the door. Ouch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt; Now imagine the model says "uh oh, this customer might leave!" — but they were actually going to stay the whole time. What happens? The company probably just sends them a coupon or a "we miss you" email they didn't need. A little wasteful, but not a big deal.&lt;/p&gt;

&lt;p&gt;So missing a real churner (Example 1) is way more expensive than falsely worrying about a loyal customer (Example 2). That means I want my model to lean toward catching as many real churners as possible — even if it means a few false alarms along the way. In machine learning terms, that means I care more about &lt;strong&gt;recall&lt;/strong&gt; than &lt;strong&gt;precision&lt;/strong&gt;. (Quick refresher: recall = "out of everyone who really did churn, how many did I catch?" Precision = "out of everyone I flagged as a churn risk, how many actually churned?")&lt;/p&gt;

&lt;p&gt;My first version of the model, using pretty normal, hand-picked settings, looked like this:&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;My original model&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Recall (Churn)&lt;/td&gt;
&lt;td&gt;0.481 (48%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Precision (Churn)&lt;/td&gt;
&lt;td&gt;0.623 (62%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F1 (Churn)&lt;/td&gt;
&lt;td&gt;0.543&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;48% recall means my model was basically a coin flip on catching real churners — it missed more than half of them! Not great. I later nudged this up to 68% by manually adjusting a setting called the "decision threshold" (basically, lowering how confident the model needs to be before it raises its hand and says "churn risk"). That helped, but I had to fiddle with it by trial and error to find that number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter GASearchCV: let evolution do the guessing
&lt;/h2&gt;

&lt;p&gt;Here's an analogy. Imagine you're trying to bake the perfect chocolate chip cookie, and you have 6 things you can change: how much sugar, how much flour, oven temperature, baking time, how much butter, and whether you chill the dough first. Trying every single combination would take forever.&lt;/p&gt;

&lt;p&gt;A genetic algorithm does something smarter, kind of like how evolution works in nature:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bake 20 random batches of cookies (the "population").&lt;/li&gt;
&lt;li&gt;Taste-test all of them and rank which ones are best.&lt;/li&gt;
&lt;li&gt;Take the best ones, mix and combine their recipes a little (like "breeding" them), and throw in a few random tweaks (mutations).&lt;/li&gt;
&lt;li&gt;Bake a new batch of 20 with these improved recipes.&lt;/li&gt;
&lt;li&gt;Repeat this a bunch of times (called "generations").&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each generation, the recipes tend to get a little better, because you're always building on your best results so far instead of starting from scratch. That's exactly what &lt;code&gt;GASearchCV&lt;/code&gt;, from the Python library &lt;code&gt;sklearn-genetic-opt&lt;/code&gt;, does — except instead of cookie recipes, it's tweaking your model's settings (like how many trees to use, how deep each tree can grow, etc.), and instead of a taste test, it scores each "recipe" using cross-validation.&lt;/p&gt;

&lt;p&gt;Here's the actual code — don't worry, I'll explain each part with an 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_genetic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;GASearchCV&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn_genetic.space&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Categorical&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Continuous&lt;/span&gt;

&lt;span class="n"&gt;param_grid&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;n_estimators&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Integer&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;300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;       &lt;span class="c1"&gt;# e.g. "try between 50 and 300 trees"
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;max_depth&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;            &lt;span class="c1"&gt;# e.g. "try tree depths from 3 to 20"
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;min_samples_split&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Integer&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;20&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;min_samples_leaf&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Integer&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;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;max_features&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Continuous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;class_weight&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Categorical&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;balanced&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;  &lt;span class="c1"&gt;# e.g. "try both options"
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;evolved_rf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GASearchCV&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;estimator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;RandomForestClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random_state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;param_grid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;param_grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cv&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="c1"&gt;# taste-test each recipe 5 different ways
&lt;/span&gt;    &lt;span class="n"&gt;scoring&lt;/span&gt;&lt;span class="o"&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="c1"&gt;# judge recipes by recall, since that's what I care about
&lt;/span&gt;    &lt;span class="n"&gt;population_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;# 20 recipes per generation
&lt;/span&gt;    &lt;span class="n"&gt;generations&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;# repeat the process 15 times
&lt;/span&gt;    &lt;span class="n"&gt;n_jobs&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;evolved_rf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of &lt;code&gt;Integer(50, 300)&lt;/code&gt; like telling the algorithm "you're allowed to try anywhere from 50 to 300 trees in the forest — go find the sweet spot yourself," instead of me just guessing "let's do 100 trees" and hoping for the best.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually happened
&lt;/h2&gt;

&lt;p&gt;After the algorithm ran through its 15 generations of "baking and taste-testing," here's what it landed on:&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;My original model&lt;/th&gt;
&lt;th&gt;GA-tuned model&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Recall (Churn)&lt;/td&gt;
&lt;td&gt;0.481 (48%)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.826 (83%)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Precision (Churn)&lt;/td&gt;
&lt;td&gt;0.623 (62%)&lt;/td&gt;
&lt;td&gt;0.475 (48%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F1 (Churn)&lt;/td&gt;
&lt;td&gt;0.543&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.604&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Recall basically doubled — from 48% to 83%! That's a huge jump, and it's even better than the 68% I got earlier by manually fiddling with the threshold. And here's the part that surprised me most: even though precision dropped, the F1 score (which balances both) still went up. So this isn't just "trading one thing for another" — it's an actual, real improvement.&lt;/p&gt;

&lt;p&gt;The winning "recipe" the algorithm found was:&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;n_estimators&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;299&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;max_depth&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;min_samples_split&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;min_samples_leaf&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;max_features&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.112&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;class_weight&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;balanced&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 part I found genuinely surprising: &lt;code&gt;max_depth=3&lt;/code&gt;. That means each tree in the forest is really shallow — like, barely a tree at all, more like a bush. I never would have guessed that on my own; my instinct would've been "deeper trees = smarter model." But apparently, for this dataset, a bunch of simple, shallow trees working together (plus &lt;code&gt;class_weight='balanced'&lt;/code&gt;, which tells the model "pay extra attention to the rare churn cases") worked way better than one big complicated tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I took away from this
&lt;/h2&gt;

&lt;p&gt;If you already know which mistake is worse for your specific problem (like how missing a churner is worse than a false alarm), you can literally tell &lt;code&gt;GASearchCV&lt;/code&gt; "go optimize for that" using the &lt;code&gt;scoring&lt;/code&gt; parameter, and it'll search way more of the possibility space than you'd ever try by hand — kind of like having a tireless assistant testing hundreds of cookie recipes overnight while you sleep. It doesn't replace understanding &lt;em&gt;why&lt;/em&gt; the trade-off matters in the first place — you still need to know your problem. But it definitely saves you from a lot of manual, trial-and-error guessing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Full code: tune_churn_rf_with_gasearchcv.py&lt;/em&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
