One honest rule
Your test set is for the final check. The model must not learn from it in any way before evaluation.
If your score is only good because the model saw the answers, your repo will look weak in review.
Why split data
When you train a model, it can memorise patterns from the data. A train/test split checks if it can work on new rows.
Think of it like exam prep. Practice questions are training data. The exam paper is test data. You should not peek at the exam before studying.
Basic split flow
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
scikit-learn split
Use train_test_split for most starter projects. Keep random_state fixed so someone else can reproduce your result from the repo.
For classification, stratify keeps class balance similar in train and test. This helps when labels are uneven, like pass/fail, fraud/not fraud, or churn/stay.
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
X = df[["study_hours", "attendance", "past_score"]]
y = df["passed"]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
score = accuracy_score(y_test, predictions)
print("Test accuracy:", round(score, 3))Avoid data leakage
Leakage means information from the test set enters training. It makes scores look better than they really are.
Common leakage: scaling the full dataset before splitting, filling missing values using the full dataset average, or creating features using the label.
Safer preprocessing
If you scale or fill values, fit the preprocessing on training data only. The simplest way is a scikit-learn Pipeline.
The pipeline learns the scaler from X_train, then applies the same scaler to X_test during prediction.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
pipeline = Pipeline([
("scaler", StandardScaler()),
("model", LogisticRegression(max_iter=1000))
])
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
print("Test accuracy:", round(accuracy_score(y_test, predictions), 3))What to report
In your README, write the split, metric, and result. Do not only show the best number from many tries.
Example: Dataset split: 80 percent train, 20 percent test, random_state=42. Metric: accuracy. Test accuracy: 0.83.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done