Wagi klas i progi
Dostrajanie modelu z naciskiem na klasę mniejszościową
Wagi klas i progi to bezpłatna lekcja Data Science Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Data Science Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Data Science Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎯
Często zadawane pytania
Czy lekcja „Wagi klas i progi” jest bezpłatna?
Tak — pełny tekst „Wagi klas i progi” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Data Science Academy, przejdź na CoddyKit PRO. Kurs Data Science Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Wagi klas i progi”?
Dostrajanie modelu z naciskiem na klasę mniejszościową Ćwiczysz Data Science Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Data Science Academy?
Nie wymagamy żadnego doświadczenia. Data Science Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Wagi klas i progi”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Data Science Academy?
Tak. Każda lekcja Data Science Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Dlaczego accuracy zawodzi przy niezbalansowanych danych
- Resampling: SMOTE i undersampling
- Wagi klas i progi
- Dobór metryk dla rzadkich zdarzeń