0Pricing
Data Science Academy · Lesson

Class Weights and Thresholds

Tuning the model toward the minority.

Class Weights and Thresholds is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Fix It Without Resampling

You can fight imbalance without touching the data at all. Two model-side levers help: class weights and the decision threshold.

What Class Weights Do

Class weights tell the model that mistakes on the rare class hurt more. It pays a bigger penalty for missing minority rows.

The Easy Default

Many scikit-learn models accept class_weight set to balanced. It auto-weights each class inversely to how often it appears.

model = LogisticRegression(class_weight='balanced')
model.fit(X_train, y_train)

Weights vs Resampling

Class weights reshape the loss instead of the dataset. No rows are added or dropped, so you keep all your original data.

The Hidden 0.5 Cutoff

Most classifiers predict a probability, then label it positive if it tops 0.5. That default cutoff is a choice, not a law.

Move the Threshold

Lower the threshold and the model flags positives more eagerly. That catches more rare cases, at the price of more false alarms.

Predict Probabilities First

To tune a threshold, ask the model for probabilities, not hard labels. Then apply your own cutoff to those scores.

proba = model.predict_proba(X_test)[:, 1]
preds = (proba >= 0.30).astype(int)

The Core Trade-Off

A lower threshold lifts recall but drops precision. Raising it does the reverse. The right point depends on which mistake costs more.

Let Cost Drive the Cutoff

If a missed fraud is far worse than a false alarm, lean toward a lower threshold so the rare class is rarely missed.

Combine the Levers

Class weights and threshold tuning stack nicely. Weight the rare class during training, then pick a cutoff that matches your real costs.

Tune, Then Lock It In

Choose the threshold using validation data, never the test set. Then apply that same fixed cutoff for an honest final score.

Quick Check

What happens when you lower the decision threshold?

Recap

Without resampling, class weights penalize rare-class errors and a tuned threshold trades recall against precision to fit your costs. 🎯

Frequently asked questions

Is the “Class Weights and Thresholds” lesson free?

Yes — the full text of “Class Weights and Thresholds” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Class Weights and Thresholds”?

Tuning the model toward the minority. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Class Weights and Thresholds” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Accuracy Lies on Imbalance
  2. Resampling: SMOTE and Undersampling
  3. Class Weights and Thresholds
  4. Pick Metrics for Rare Events
← Back to Data Science Academy