0Pricing
Data Science Academy · Lezione

Usare train_test_split correttamente

Stratify, random_state e proporzioni.

Usare train_test_split correttamente è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 2 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.

One Helper, Two Sets

The function train_test_split takes your features and target and hands back train and test pieces in one tidy call.

from sklearn.model_selection import train_test_split

Four Things Come Back

It returns four objects in a fixed order: train features, test features, train target, test target. Unpack them carefully so each lands in the right name.

X_train, X_test, y_train, y_test = train_test_split(X, y)

Set the Test Size

Use test_size to choose how much data to reserve. A value of 0.2 means 20% goes to the test set.

train_test_split(X, y, test_size=0.2)

Shuffling by Default

By default the rows are shuffled before splitting. That mixing prevents any hidden order in your file from biasing either set.

Make It Reproducible

Pass random_state to lock the shuffle. The same number gives the same split every run, so your results are repeatable. 🔁

train_test_split(X, y, test_size=0.2, random_state=42)

Why Reproducibility Matters

Without a fixed seed, every run reshuffles and your scores wobble. A locked random_state lets teammates reproduce your exact numbers.

The Imbalance Problem

If a class is rare, a plain random split might dump most of it into one set. Now train and test no longer reflect the same class balance.

Stratify to the Rescue

Pass stratify equal to your target so both sets keep the same class proportions. Essential for classification with uneven classes.

train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)

Keep X and y Aligned

The split keeps each row's features and label together. Row 7's features always travel with row 7's label, never scrambled apart.

Choosing the Ratio

Common splits are 80/20 or 70/30. With lots of data you can spare a smaller test slice; with little data, give the test set a bit more.

Split Before You Touch

Run the split first, before scaling or filling values. Doing prep on the full set leaks test information back into training.

Quick Check

Your target classes are very imbalanced. Which argument helps?

Recap

Unpack four sets, set test_size, fix random_state for repeatability, and use stratify when classes are uneven. Split first, prep later. 🔁

Domande Frequenti

La lezione «Usare train_test_split correttamente» è gratuita?

Sì — il testo completo di «Usare train_test_split correttamente» è 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 «Usare train_test_split correttamente»?

Stratify, random_state e proporzioni. 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 2 di 4.

Quanto tempo richiede la lezione «Usare train_test_split correttamente»?

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é tenere da parte un test set
  2. Usare train_test_split correttamente
  3. Cross-validation K-Fold
  4. Prevenire il data leakage fin dall'inizio
← Torna a Data Science Academy