Imagine you train a model on some data and then check how well it works on the same data. Why is it important to use a different set of data (test set) to evaluate the model?
Think about how practicing a skill on the same questions might not prepare you for new questions.
Using a separate test set shows how the model will perform on data it has never seen before. This helps detect if the model is just memorizing the training data (overfitting) rather than learning general patterns.
Given the following code that splits data and evaluates a model, what will be printed?
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score X, y = load_iris(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) model = DecisionTreeClassifier(random_state=42) model.fit(X_train, y_train) predictions_train = model.predict(X_train) predictions_test = model.predict(X_test) print(f"Train accuracy: {accuracy_score(y_train, predictions_train):.2f}") print(f"Test accuracy: {accuracy_score(y_test, predictions_test):.2f}")
Decision trees can perfectly fit training data but may perform slightly worse on test data.
The decision tree perfectly fits the training data (accuracy 1.00) but has slightly lower accuracy on test data (around 0.98), showing some generalization.
You train a classifier and get very high accuracy on training data but much lower accuracy on test data. Which metric helps you best understand this overfitting problem?
Think about comparing performance on training vs test data.
The difference between training and test accuracy shows if the model performs much better on training data than on new data, indicating overfitting.
You want to prevent overfitting on a small dataset. Which model choice is best?
Simpler models tend to generalize better on small data.
Simpler models are less likely to memorize training data and more likely to find general patterns, reducing overfitting.
Look at this code snippet. It trains and evaluates a model but the evaluation results are misleading. Why?
from sklearn.datasets import load_digits from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score data = load_digits() X, y = data.data, data.target model = DecisionTreeClassifier() model.fit(X, y) predictions = model.predict(X) print(f"Accuracy: {accuracy_score(y, predictions):.2f}")
Think about what happens if you test on the same data you trained on.
Evaluating on training data shows overly optimistic accuracy because the model memorized the data. Proper evaluation requires separate test data.
