Practice
1. What is the main purpose of training data in machine learning?
easy
Solution
Step 1: Understand the role of training data
Training data is used to show the model examples so it can learn patterns.Step 2: Differentiate training from testing
Testing data checks model accuracy, but training data teaches the model.Final Answer:
To teach the model patterns and relationships in data -> Option AQuick Check:
Training data = Teach model [OK]
Hint: Training data is what the model learns from [OK]
Common Mistakes:
- Confusing training data with testing data
- Thinking training data stores predictions
- Assuming training data is for cleaning only
2. Which of the following is the correct way to split data into training and testing sets in Python using scikit-learn?
easy
Solution
Step 1: Recall the scikit-learn function name
The correct function to split data istrain_test_split.Step 2: Check the parameter syntax
The parametertest_size=0.2correctly sets 20% data for testing.Final Answer:
train_test_split(data, test_size=0.2) -> Option BQuick Check:
Correct function and parameter = train_test_split(data, test_size=0.2) [OK]
Hint: Remember scikit-learn's function is train_test_split [OK]
Common Mistakes:
- Using wrong function names
- Mixing parameter names
- Incorrect order of arguments
3. Given the following Python code, what will be the output?
from sklearn.linear_model import LinearRegression import numpy as np X = np.array([[1], [2], [3], [4]]) y = np.array([2, 4, 6, 8]) model = LinearRegression() model.fit(X, y) pred = model.predict([[5]]) print(round(pred[0], 1))
medium
Solution
Step 1: Understand the training data relationship
X values are 1 to 4, y values are double X (2,4,6,8), so model learns y = 2 * X.Step 2: Predict for X=5 using the model
Prediction = 2 * 5 = 10.0, rounding to 1 decimal is 10.0.Final Answer:
10.0 -> Option DQuick Check:
Prediction for 5 = 10.0 [OK]
Hint: Model learns y=2x, predict for 5 gives 10 [OK]
Common Mistakes:
- Ignoring rounding in output
- Misunderstanding model relationship
- Using wrong input shape for predict
4. Identify the error in this code snippet for training a model:
from sklearn.linear_model import LogisticRegression X_train = [[0, 1], [1, 0]] y_train = [0, 1] model = LogisticRegression() model.fit(y_train, X_train)
medium
Solution
Step 1: Check the model.fit() argument order
fit() expects features first (X_train), then labels (y_train).Step 2: Identify the mistake in the code
Code passes y_train first, then X_train, which is incorrect and causes error.Final Answer:
The order of arguments in model.fit() is reversed -> Option AQuick Check:
fit(X_train, y_train) correct order [OK]
Hint: fit() needs features first, labels second [OK]
Common Mistakes:
- Swapping X and y in fit()
- Assuming data type causes error here
- Thinking LogisticRegression needs string labels
5. You have a dataset with 1000 samples and 10 features. You want to train a model but avoid overfitting. Which approach helps most during training?
hard
Solution
Step 1: Understand overfitting and validation
Overfitting happens when model learns training data too well and fails on new data.Step 2: Use validation set to check model generalization
Splitting data into training and validation helps detect and reduce overfitting.Final Answer:
Split data into training and validation sets -> Option CQuick Check:
Validation split prevents overfitting [OK]
Hint: Validation set helps check model generalization [OK]
Common Mistakes:
- Training on all data hides overfitting
- Using too few samples reduces learning
- Ignoring feature scaling can hurt model
