0Pricing
Data Science Academy · Lezione

Pesi delle classi e soglie

Orientare il modello verso la classe minoritaria.

Pesi delle classi e soglie è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. 🎯

Domande Frequenti

La lezione «Pesi delle classi e soglie» è gratuita?

Sì — il testo completo di «Pesi delle classi e soglie» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.

Cosa imparerò in «Pesi delle classi e soglie»?

Orientare il modello verso la classe minoritaria. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Data Science Academy?

Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Pesi delle classi e soglie»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Data Science Academy?

Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Perché l'accuratezza inganna con dati sbilanciati
  2. Ricampionamento: SMOTE e undersampling
  3. Pesi delle classi e soglie
  4. Scegliere le metriche per gli eventi rari
← Torna a Data Science Academy